|
| 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 | +} |
0 commit comments