Skip to main content

ui/
window.rs

1//! [`frame`] — the border, corners, shadow and resize edges of a window the
2//! system does not decorate. [`content`] — the card an app's main view sits
3//! on, inside that window.
4//!
5//! `Window::window_decorations` answers which of the two is in force, and it
6//! is not a target: GNOME has never implemented `xdg-decoration`, so a Wayland
7//! window there is told `Client` whatever it asked for, while the same binary
8//! under KDE or on X11 is told `Server` and this draws nothing. macOS and
9//! Windows always answer `Server`.
10//!
11//! The window opens transparent or the band shows as a filled rect —
12//! `WindowBackgroundAppearance::Opaque` has the compositor paint the whole
13//! surface, corners included.
14//!
15//! ```ignore
16//! window::frame(self.content(window, cx), window, cx)
17//! ```
18
19use gpui::{
20    App, Bounds, CursorStyle, Decorations, Div, HitboxBehavior, Hsla, IntoElement, MouseButton,
21    Pixels, Point, ResizeEdge, Size, Tiling, Window, canvas, div, point, prelude::*, px, size,
22};
23
24use theme::Theme;
25
26/// Wrap a window's root in the frame it owes.
27///
28/// Under `Decorations::Server` the child is handed back in a full-size div and
29/// nothing else is painted. Under `Decorations::Client` it takes a border,
30/// [`Theme::surface_radius`] corners and [`theme::frame_shadows`], inside a
31/// [`Theme::CLIENT_INSET`] band that resizes the window.
32///
33/// Must be the window's root element: the resize bands are hit-tested against
34/// `Window::window_bounds`, so an origin of anything but the window's own is a
35/// band in the wrong place.
36///
37/// An edge `tiling` reports flush — against a screen edge, or another window
38/// in a tile — keeps its square corner and gives up its band. A maximized
39/// window counts as flush on every edge: X11 window managers do not always
40/// report tiling for one.
41pub fn frame(child: impl IntoElement, window: &mut Window, cx: &App) -> Div {
42    let Decorations::Client { tiling } = window.window_decorations() else {
43        return div().size_full().child(child);
44    };
45    let tiling = match window.is_maximized() {
46        true => Tiling::tiled(),
47        false => tiling,
48    };
49
50    let inset = px(Theme::CLIENT_INSET);
51    let radius = px(Theme::surface_radius());
52    let border = Theme::of(cx).border;
53    window.set_client_inset(inset);
54
55    div()
56        .size_full()
57        .child(
58            canvas(
59                move |_, window, _| {
60                    let size = window.window_bounds().get_bounds().size;
61                    bands(size, inset)
62                        .into_iter()
63                        .filter(|(edge, _)| live(*edge, tiling))
64                        .map(|(edge, band)| {
65                            (edge, window.insert_hitbox(band, HitboxBehavior::Normal))
66                        })
67                        .collect::<Vec<_>>()
68                },
69                |_, bands, window, _| {
70                    for (edge, band) in bands {
71                        window.set_cursor_style(cursor(edge), &band);
72                    }
73                },
74            )
75            .absolute()
76            .size_full(),
77        )
78        .when(!tiling.top, |frame| frame.pt(inset))
79        .when(!tiling.bottom, |frame| frame.pb(inset))
80        .when(!tiling.left, |frame| frame.pl(inset))
81        .when(!tiling.right, |frame| frame.pr(inset))
82        .on_mouse_down(MouseButton::Left, move |event, window, _| {
83            let size = window.window_bounds().get_bounds().size;
84            if let Some(edge) =
85                resize_edge(event.position, inset, size).filter(|edge| live(*edge, tiling))
86            {
87                window.start_window_resize(edge);
88            }
89        })
90        .child(
91            div()
92                .size_full()
93                // Without this the child's own background paints over the
94                // corners the border rounds.
95                .overflow_hidden()
96                .border_color(border)
97                .when(!tiling.top, |surface| surface.border_t(px(1.0)))
98                .when(!tiling.bottom, |surface| surface.border_b(px(1.0)))
99                .when(!tiling.left, |surface| surface.border_l(px(1.0)))
100                .when(!tiling.right, |surface| surface.border_r(px(1.0)))
101                .when(!(tiling.top || tiling.left), |surface| {
102                    surface.rounded_tl(radius)
103                })
104                .when(!(tiling.top || tiling.right), |surface| {
105                    surface.rounded_tr(radius)
106                })
107                .when(!(tiling.bottom || tiling.left), |surface| {
108                    surface.rounded_bl(radius)
109                })
110                .when(!(tiling.bottom || tiling.right), |surface| {
111                    surface.rounded_br(radius)
112                })
113                .when(!tiling.is_tiled(), |surface| {
114                    surface.shadow(theme::frame_shadows())
115                })
116                .child(child),
117        )
118}
119
120/// The element an app's main view is drawn in.
121///
122/// Floating, it is a card on the window's chrome, [`Theme::sidebar_bg`],
123/// which its parent paints: half [`Theme::CONTENT_INSET`] of margin on every
124/// side, [`Theme::panel_radius`] corners, a hairline border and
125/// [`Theme::content_bg`] over the chrome. An opaque card casts
126/// [`theme::content_shadows`]; under [`Theme::vibrancy`] it casts none, since a
127/// shadow shows through a translucent fill. Flat, it is a plain
128/// [`Theme::window_bg`] fill with none of those.
129///
130/// The caller sizes it: it is handed back unsized, as a flex row or column
131/// item would want it. The parent of floating cards pads itself by the other
132/// half of the inset, so a card stands the whole inset off the window's edge
133/// and two cards side by side meet the whole inset apart.
134pub fn content(floating: bool, cx: &App) -> Div {
135    let theme = Theme::of(cx);
136    match floating {
137        true => div()
138            .bg(theme.content_bg())
139            .m(px(Theme::CONTENT_INSET / 2.0))
140            .rounded(px(Theme::panel_radius()))
141            .border_1()
142            .border_color(theme.border)
143            .when(!theme.vibrancy, |card| {
144                card.shadow(theme::content_shadows())
145            })
146            // Without this the child's own background paints over the
147            // corners the border rounds.
148            .overflow_hidden(),
149        false => div().bg(theme.window_bg()),
150    }
151}
152
153/// The one colour [`content`] shows, chrome included, for an element inside
154/// it that has to cover what is under it in the same colour.
155pub fn content_fill(floating: bool, theme: &Theme) -> Hsla {
156    match floating {
157        true => theme::over(theme.content_bg(), theme.sidebar_bg()),
158        false => theme.window_bg(),
159    }
160}
161
162/// Which edge a press at `pos` resizes, or `None` for a press on the content.
163///
164/// `inset` is the band's width, and `size` the whole window's — the band runs
165/// inside both, so the corners are `inset` squares and each edge is what is
166/// left of that side between them.
167pub fn resize_edge(pos: Point<Pixels>, inset: Pixels, size: Size<Pixels>) -> Option<ResizeEdge> {
168    let (top, bottom) = (pos.y < inset, pos.y > size.height - inset);
169    let (left, right) = (pos.x < inset, pos.x > size.width - inset);
170
171    Some(match (top, bottom, left, right) {
172        (true, _, true, _) => ResizeEdge::TopLeft,
173        (true, _, _, true) => ResizeEdge::TopRight,
174        (_, true, true, _) => ResizeEdge::BottomLeft,
175        (_, true, _, true) => ResizeEdge::BottomRight,
176        (true, ..) => ResizeEdge::Top,
177        (_, true, ..) => ResizeEdge::Bottom,
178        (_, _, true, _) => ResizeEdge::Left,
179        (_, _, _, true) => ResizeEdge::Right,
180        _ => return None,
181    })
182}
183
184/// The eight bands, as the rects [`resize_edge`] classifies to. Disjoint: the
185/// corners take their `inset` square and the edges take what is left, so no
186/// press lands in two and the hitboxes need no order between them.
187fn bands(window: Size<Pixels>, inset: Pixels) -> [(ResizeEdge, Bounds<Pixels>); 8] {
188    let band = |x: Pixels, y: Pixels, width: Pixels, height: Pixels| Bounds {
189        origin: point(x, y),
190        size: size(width, height),
191    };
192    let (far_x, far_y) = (window.width - inset, window.height - inset);
193    let (span_x, span_y) = (window.width - inset - inset, window.height - inset - inset);
194    let zero = px(0.0);
195
196    [
197        (ResizeEdge::TopLeft, band(zero, zero, inset, inset)),
198        (ResizeEdge::TopRight, band(far_x, zero, inset, inset)),
199        (ResizeEdge::BottomLeft, band(zero, far_y, inset, inset)),
200        (ResizeEdge::BottomRight, band(far_x, far_y, inset, inset)),
201        (ResizeEdge::Top, band(inset, zero, span_x, inset)),
202        (ResizeEdge::Bottom, band(inset, far_y, span_x, inset)),
203        (ResizeEdge::Left, band(zero, inset, inset, span_y)),
204        (ResizeEdge::Right, band(far_x, inset, inset, span_y)),
205    ]
206}
207
208/// Whether `edge` resizes: none of the sides it touches is flush.
209pub fn live(edge: ResizeEdge, tiling: Tiling) -> bool {
210    let (top, bottom, left, right) = match edge {
211        ResizeEdge::Top => (true, false, false, false),
212        ResizeEdge::Bottom => (false, true, false, false),
213        ResizeEdge::Left => (false, false, true, false),
214        ResizeEdge::Right => (false, false, false, true),
215        ResizeEdge::TopLeft => (true, false, true, false),
216        ResizeEdge::TopRight => (true, false, false, true),
217        ResizeEdge::BottomLeft => (false, true, true, false),
218        ResizeEdge::BottomRight => (false, true, false, true),
219    };
220    !(top && tiling.top || bottom && tiling.bottom || left && tiling.left || right && tiling.right)
221}
222
223/// The pointer an edge shows.
224fn cursor(edge: ResizeEdge) -> CursorStyle {
225    match edge {
226        ResizeEdge::Top | ResizeEdge::Bottom => CursorStyle::ResizeUpDown,
227        ResizeEdge::Left | ResizeEdge::Right => CursorStyle::ResizeLeftRight,
228        ResizeEdge::TopLeft | ResizeEdge::BottomRight => CursorStyle::ResizeUpLeftDownRight,
229        ResizeEdge::TopRight | ResizeEdge::BottomLeft => CursorStyle::ResizeUpRightDownLeft,
230    }
231}