Skip to content

Commit a196642

Browse files
zahariashclaude
authored andcommitted
Client-side window decorations on Linux (GNOME Wayland had no frame)
GNOME/Mutter on Wayland does not implement xdg-decoration, so the window had no title bar: it could not be moved, resized or closed with the mouse. GPUI's default request for server-side decorations also makes `window_decorations()` report `Server` there, so nothing drew a frame. On Linux the window now requests client-side decorations. When GPUI reports `Decorations::Client`: - an invisible 8px border resizes the window (edges and corners, with resize cursors), plus a 1px border and shadow while not tiled; - the header's title/status area drags the window, double-click maximizes/restores, right-click opens the compositor window menu; - minimize / maximize / close buttons in the GNOME header-bar style (round, path-drawn icons) are appended to the header. Where the platform draws the frame (macOS, Windows, X11 without a compositor) all of this is a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5d1b505 commit a196642

4 files changed

Lines changed: 321 additions & 9 deletions

File tree

‎src/decorations.rs‎

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
//! Client-side window decorations.
2+
//!
3+
//! On Linux the window requests client-side decorations (see `main.rs`): with
4+
//! GPUI's default server-side request, compositors without xdg-decoration
5+
//! (GNOME/Mutter on Wayland) leave the window with no frame at all while GPUI
6+
//! still reports `Decorations::Server`. When `window.window_decorations()` is
7+
//! `Decorations::Client`, the root view is wrapped in an invisible resize
8+
//! border, the header becomes the drag area and gains minimize / maximize /
9+
//! close buttons. Where the platform draws the frame (macOS, Windows, X11
10+
//! without a compositor) every helper here is a no-op.
11+
12+
use crate::theme::Theme;
13+
use gpui::prelude::FluentBuilder;
14+
use gpui::*;
15+
16+
/// Width of the invisible border that starts a resize drag.
17+
pub const RESIZE_INSET: Pixels = px(8.0);
18+
19+
/// `Some(tiling)` when the app has to draw its own window frame.
20+
pub fn client_tiling(window: &Window) -> Option<Tiling> {
21+
match window.window_decorations() {
22+
Decorations::Server => None,
23+
Decorations::Client { tiling } => Some(tiling),
24+
}
25+
}
26+
27+
/// Which window edge or corner a point in window coordinates is on. Tiled
28+
/// edges (maximized, snapped) have no border and never resize.
29+
pub fn resize_edge(
30+
pos: Point<Pixels>,
31+
size: Size<Pixels>,
32+
inset: Pixels,
33+
tiling: Tiling,
34+
) -> Option<ResizeEdge> {
35+
let top = !tiling.top && pos.y < inset;
36+
let bottom = !tiling.bottom && pos.y > size.height - inset;
37+
let left = !tiling.left && pos.x < inset;
38+
let right = !tiling.right && pos.x > size.width - inset;
39+
match (top, bottom, left, right) {
40+
(true, _, true, _) => Some(ResizeEdge::TopLeft),
41+
(true, _, _, true) => Some(ResizeEdge::TopRight),
42+
(_, true, true, _) => Some(ResizeEdge::BottomLeft),
43+
(_, true, _, true) => Some(ResizeEdge::BottomRight),
44+
(true, _, _, _) => Some(ResizeEdge::Top),
45+
(_, true, _, _) => Some(ResizeEdge::Bottom),
46+
(_, _, true, _) => Some(ResizeEdge::Left),
47+
(_, _, _, true) => Some(ResizeEdge::Right),
48+
_ => None,
49+
}
50+
}
51+
52+
fn resize_cursor(edge: ResizeEdge) -> CursorStyle {
53+
match edge {
54+
ResizeEdge::Top | ResizeEdge::Bottom => CursorStyle::ResizeUpDown,
55+
ResizeEdge::Left | ResizeEdge::Right => CursorStyle::ResizeLeftRight,
56+
ResizeEdge::TopLeft | ResizeEdge::BottomRight => CursorStyle::ResizeUpLeftDownRight,
57+
ResizeEdge::TopRight | ResizeEdge::BottomLeft => CursorStyle::ResizeUpRightDownLeft,
58+
}
59+
}
60+
61+
/// Wrap the root view in a resizable, bordered frame when decorations are
62+
/// client-side; returns `content` unchanged otherwise.
63+
pub fn client_frame(content: impl IntoElement, theme: Theme, window: &mut Window) -> AnyElement {
64+
let Some(tiling) = client_tiling(window) else {
65+
return content.into_any_element();
66+
};
67+
window.set_client_inset(RESIZE_INSET);
68+
div()
69+
.id("client-frame")
70+
.size_full()
71+
.bg(transparent_black())
72+
.child(
73+
// Resize cursors over the border. Painted per frame from the
74+
// current mouse position; `on_mouse_move` below keeps it fresh.
75+
canvas(
76+
|_bounds, window, _cx| {
77+
window.insert_hitbox(
78+
Bounds::new(Point::default(), window.window_bounds().get_bounds().size),
79+
HitboxBehavior::Normal,
80+
)
81+
},
82+
move |_bounds, hitbox, window, _cx| {
83+
let size = window.window_bounds().get_bounds().size;
84+
if let Some(edge) =
85+
resize_edge(window.mouse_position(), size, RESIZE_INSET, tiling)
86+
{
87+
window.set_cursor_style(resize_cursor(edge), &hitbox);
88+
}
89+
},
90+
)
91+
.size_full()
92+
.absolute(),
93+
)
94+
.when(!tiling.top, |d| d.pt(RESIZE_INSET))
95+
.when(!tiling.bottom, |d| d.pb(RESIZE_INSET))
96+
.when(!tiling.left, |d| d.pl(RESIZE_INSET))
97+
.when(!tiling.right, |d| d.pr(RESIZE_INSET))
98+
.on_mouse_move(|_, window, _| window.refresh())
99+
.on_mouse_down(MouseButton::Left, move |e, window, _| {
100+
let size = window.window_bounds().get_bounds().size;
101+
if let Some(edge) = resize_edge(e.position, size, RESIZE_INSET, tiling) {
102+
window.start_window_resize(edge);
103+
}
104+
})
105+
.child(
106+
div()
107+
.size_full()
108+
.overflow_hidden()
109+
.cursor(CursorStyle::Arrow)
110+
.border_color(theme.border)
111+
.when(!tiling.top, |d| d.border_t_1())
112+
.when(!tiling.bottom, |d| d.border_b_1())
113+
.when(!tiling.left, |d| d.border_l_1())
114+
.when(!tiling.right, |d| d.border_r_1())
115+
.when(!tiling.is_tiled(), |d| {
116+
d.shadow(vec![BoxShadow {
117+
color: hsla(0., 0., 0., 0.4),
118+
blur_radius: RESIZE_INSET / 2.,
119+
spread_radius: px(0.),
120+
offset: point(px(0.), px(0.)),
121+
}])
122+
})
123+
.on_mouse_move(|_, _, cx| cx.stop_propagation())
124+
.child(content),
125+
)
126+
.into_any_element()
127+
}
128+
129+
/// Make `area` move the window on drag, maximize/restore on double-click and
130+
/// open the compositor's window menu on right-click.
131+
pub fn drag_area(area: Div, window: &Window) -> Div {
132+
if client_tiling(window).is_none() {
133+
return area;
134+
}
135+
area.on_mouse_down(MouseButton::Left, |e, window, _| {
136+
if e.click_count == 2 {
137+
window.zoom_window();
138+
} else {
139+
window.start_window_move();
140+
}
141+
})
142+
.on_mouse_down(MouseButton::Right, |e, window, _| {
143+
window.show_window_menu(e.position)
144+
})
145+
}
146+
147+
#[derive(Clone, Copy)]
148+
enum Icon {
149+
Minimize,
150+
Maximize,
151+
Restore,
152+
Close,
153+
}
154+
155+
/// Line icons drawn as paths (font glyphs for these differ in size and weight
156+
/// from font to font), sized like GNOME's symbolic window-control icons.
157+
fn paint_icon(icon: Icon, bounds: Bounds<Pixels>, color: Hsla, window: &mut Window) {
158+
let c = bounds.center();
159+
let p = |dx: f32, dy: f32| point(c.x + px(dx), c.y + px(dy));
160+
let mut path = PathBuilder::stroke(px(1.5));
161+
match icon {
162+
Icon::Minimize => {
163+
path.move_to(p(-4., 3.5));
164+
path.line_to(p(4., 3.5));
165+
}
166+
Icon::Maximize => {
167+
path.move_to(p(-4., -4.));
168+
path.line_to(p(4., -4.));
169+
path.line_to(p(4., 4.));
170+
path.line_to(p(-4., 4.));
171+
path.close();
172+
}
173+
Icon::Restore => {
174+
path.move_to(p(-4., -1.5));
175+
path.line_to(p(1.5, -1.5));
176+
path.line_to(p(1.5, 4.));
177+
path.line_to(p(-4., 4.));
178+
path.close();
179+
path.move_to(p(-1.5, -1.5));
180+
path.line_to(p(-1.5, -4.));
181+
path.line_to(p(4., -4.));
182+
path.line_to(p(4., 1.5));
183+
path.line_to(p(1.5, 1.5));
184+
}
185+
Icon::Close => {
186+
path.move_to(p(-4., -4.));
187+
path.line_to(p(4., 4.));
188+
path.move_to(p(4., -4.));
189+
path.line_to(p(-4., 4.));
190+
}
191+
}
192+
if let Ok(path) = path.build() {
193+
window.paint_path(path, color);
194+
}
195+
}
196+
197+
/// Minimize / maximize / close buttons in the GNOME header-bar style: small
198+
/// round buttons with a subtle fill and line icons.
199+
pub fn window_buttons(theme: Theme, window: &Window) -> Option<Div> {
200+
client_tiling(window)?;
201+
let controls = window.window_controls();
202+
let button = |icon: Icon| {
203+
div()
204+
.size(px(24.))
205+
.rounded_full()
206+
.bg(theme.selection)
207+
.hover(|s| s.bg(theme.border))
208+
.cursor_pointer()
209+
.child(
210+
canvas(
211+
|_, _, _| {},
212+
move |bounds, _, window, _| paint_icon(icon, bounds, theme.foreground, window),
213+
)
214+
.size_full(),
215+
)
216+
};
217+
let maximize = if window.is_maximized() {
218+
Icon::Restore
219+
} else {
220+
Icon::Maximize
221+
};
222+
Some(
223+
div()
224+
.ml_2()
225+
.flex()
226+
.flex_row()
227+
.items_center()
228+
.gap_3()
229+
.when(controls.minimize, |d| {
230+
d.child(
231+
button(Icon::Minimize)
232+
.on_mouse_down(MouseButton::Left, |_, window, _| window.minimize_window()),
233+
)
234+
})
235+
.when(controls.maximize, |d| {
236+
d.child(
237+
button(maximize)
238+
.on_mouse_down(MouseButton::Left, |_, window, _| window.zoom_window()),
239+
)
240+
})
241+
.child(
242+
button(Icon::Close)
243+
.on_mouse_down(MouseButton::Left, |_, window, _| window.remove_window()),
244+
),
245+
)
246+
}
247+
248+
#[cfg(test)]
249+
mod tests {
250+
use super::{resize_edge, RESIZE_INSET};
251+
use gpui::{point, px, Pixels, ResizeEdge, Size, Tiling};
252+
253+
const SIZE: Size<Pixels> = Size {
254+
width: px(800.),
255+
height: px(600.),
256+
};
257+
const FLOATING: Tiling = Tiling {
258+
top: false,
259+
left: false,
260+
right: false,
261+
bottom: false,
262+
};
263+
264+
fn edge_at(x: f32, y: f32, tiling: Tiling) -> Option<ResizeEdge> {
265+
resize_edge(point(px(x), px(y)), SIZE, RESIZE_INSET, tiling)
266+
}
267+
268+
#[test]
269+
fn interior_is_not_an_edge() {
270+
assert_eq!(edge_at(400., 300., FLOATING), None);
271+
assert_eq!(edge_at(8., 8., FLOATING), None);
272+
}
273+
274+
#[test]
275+
fn sides_and_corners() {
276+
assert_eq!(edge_at(400., 2., FLOATING), Some(ResizeEdge::Top));
277+
assert_eq!(edge_at(400., 598., FLOATING), Some(ResizeEdge::Bottom));
278+
assert_eq!(edge_at(2., 300., FLOATING), Some(ResizeEdge::Left));
279+
assert_eq!(edge_at(798., 300., FLOATING), Some(ResizeEdge::Right));
280+
assert_eq!(edge_at(2., 2., FLOATING), Some(ResizeEdge::TopLeft));
281+
assert_eq!(edge_at(798., 2., FLOATING), Some(ResizeEdge::TopRight));
282+
assert_eq!(edge_at(2., 598., FLOATING), Some(ResizeEdge::BottomLeft));
283+
assert_eq!(edge_at(798., 598., FLOATING), Some(ResizeEdge::BottomRight));
284+
}
285+
286+
#[test]
287+
fn tiled_edges_do_not_resize() {
288+
assert_eq!(edge_at(2., 2., Tiling::tiled()), None);
289+
assert_eq!(edge_at(798., 598., Tiling::tiled()), None);
290+
let left_snapped = Tiling {
291+
top: true,
292+
left: true,
293+
bottom: true,
294+
right: false,
295+
};
296+
assert_eq!(edge_at(2., 300., left_snapped), None);
297+
assert_eq!(edge_at(798., 2., left_snapped), Some(ResizeEdge::Right));
298+
}
299+
}

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
pub mod backend;
1313
pub mod cli;
1414
pub mod clusters;
15+
pub mod decorations;
1516
pub mod model;
1617
pub mod theme;
1718
pub mod ui;

‎src/main.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ fn main() {
123123
title: Some("Bugscope".into()),
124124
..Default::default()
125125
}),
126+
// See `decorations`: GNOME on Wayland draws no frame for us.
127+
window_decorations: cfg!(target_os = "linux").then_some(WindowDecorations::Client),
126128
..Default::default()
127129
},
128130
{

‎src/ui.rs‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::backend::{self, DatabaseInfo, GraphData, GraphNode};
55
use crate::cli::CliOptions;
66
use crate::clusters;
7+
use crate::decorations;
78
use crate::model::{node_size, Camera, GraphModel, Vec2};
89
use crate::theme::{edge_color, highlight, node_color, Theme};
910
use gpui::*;
@@ -1371,7 +1372,7 @@ impl RootView {
13711372
}
13721373
}
13731374

1374-
fn render_header(&mut self, theme: Theme, cx: &mut Context<Self>) -> Div {
1375+
fn render_header(&mut self, theme: Theme, window: &Window, cx: &mut Context<Self>) -> Div {
13751376
let db_name = self
13761377
.selected_db
13771378
.and_then(|id| self.databases.get(id))
@@ -1452,14 +1453,21 @@ impl RootView {
14521453
.py_2()
14531454
.bg(theme.surface)
14541455
.text_color(theme.foreground)
1455-
.child(div().font_weight(FontWeight::BOLD).child("Bugscope"))
1456-
.child(div().text_sm().child(format!("{db_name} · {status}")))
1457-
.child(
1456+
.child(decorations::drag_area(
14581457
div()
14591458
.flex_1()
14601459
.flex()
14611460
.flex_row()
1462-
.justify_end()
1461+
.items_center()
1462+
.gap_3()
1463+
.child(div().font_weight(FontWeight::BOLD).child("Bugscope"))
1464+
.child(div().text_sm().child(format!("{db_name} · {status}"))),
1465+
window,
1466+
))
1467+
.child(
1468+
div()
1469+
.flex()
1470+
.flex_row()
14631471
.items_center()
14641472
.gap_2()
14651473
.child(schema_toggle)
@@ -1482,7 +1490,8 @@ impl RootView {
14821490
}),
14831491
)
14841492
.child(theme_button),
1485-
),
1493+
)
1494+
.children(decorations::window_buttons(theme, window)),
14861495
)
14871496
}
14881497

@@ -2452,14 +2461,14 @@ impl Render for RootView {
24522461
} else {
24532462
None
24542463
};
2455-
div()
2464+
let root = div()
24562465
.flex()
24572466
.flex_col()
24582467
.size_full()
24592468
.relative()
24602469
.bg(theme.background)
24612470
.text_color(theme.foreground)
2462-
.child(self.render_header(theme, cx))
2471+
.child(self.render_header(theme, window, cx))
24632472
.child(self.render_breadcrumbs(theme, cx))
24642473
.child(
24652474
div()
@@ -2585,7 +2594,8 @@ impl Render for RootView {
25852594
.child(query_content),
25862595
),
25872596
)
2588-
.children(self.render_preferences_modal(theme, cx))
2597+
.children(self.render_preferences_modal(theme, cx));
2598+
decorations::client_frame(root, theme, window)
25892599
}
25902600
}
25912601

0 commit comments

Comments
 (0)