1use 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
26pub 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 .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
120pub 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 .overflow_hidden(),
149 false => div().bg(theme.window_bg()),
150 }
151}
152
153pub 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
162pub 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
184fn 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
208pub 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
223fn 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}