1use gpui::{
2 AccessibleAction, App, Bounds, ClickEvent, ElementId, Hsla, InteractiveElement, IntoElement,
3 ParentElement, PathBuilder, Pixels, RenderOnce, Role, SharedString, StatefulInteractiveElement,
4 Styled, Window, WindowControlArea, canvas, div, point, prelude::FluentBuilder, px,
5 transparent_black,
6};
7use gpui_base::TestSupportExt as _;
8use gpui_material_foundation::{color, current_material_theme};
9use gpui_material_theme::{ColorRole, Direction};
10
11use super::platform::{
12 ChromePlatform, CloseHandler, activate_close, activate_maximize_or_restore, activate_minimize,
13 sanitize_positive, should_show_caption_buttons,
14};
15
16#[derive(Clone, Copy, Debug, Default, PartialEq)]
19pub struct CaptionButtonColors {
20 pub active_foreground: Option<Hsla>,
22 pub inactive_foreground: Option<Hsla>,
24 pub hover_background: Option<Hsla>,
26 pub hover_foreground: Option<Hsla>,
28 pub pressed_background: Option<Hsla>,
30 pub pressed_foreground: Option<Hsla>,
32 pub close_hover_background: Option<Hsla>,
34 pub close_hover_foreground: Option<Hsla>,
36 pub close_pressed_background: Option<Hsla>,
38 pub close_pressed_foreground: Option<Hsla>,
40}
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44pub struct CaptionButtonLabels {
45 pub minimize: SharedString,
47 pub maximize: SharedString,
49 pub restore: SharedString,
51 pub close: SharedString,
53}
54
55impl Default for CaptionButtonLabels {
56 fn default() -> Self {
57 Self {
58 minimize: SharedString::new_static("Minimize"),
59 maximize: SharedString::new_static("Maximize"),
60 restore: SharedString::new_static("Restore"),
61 close: SharedString::new_static("Close"),
62 }
63 }
64}
65
66impl CaptionButtonLabels {
67 fn for_control(&self, control: CaptionButton) -> SharedString {
68 match control {
69 CaptionButton::Minimize => self.minimize.clone(),
70 CaptionButton::Maximize => self.maximize.clone(),
71 CaptionButton::Restore => self.restore.clone(),
72 CaptionButton::Close => self.close.clone(),
73 }
74 }
75}
76
77#[derive(IntoElement)]
83pub struct CaptionButtons {
84 id: ElementId,
85 colors: CaptionButtonColors,
86 labels: CaptionButtonLabels,
87 button_width: Option<Pixels>,
88 platform: ChromePlatform,
89 show: Option<bool>,
90 on_close: Option<CloseHandler>,
91}
92
93impl CaptionButtons {
94 pub fn new(id: impl Into<ElementId>) -> Self {
96 Self {
97 id: id.into(),
98 colors: CaptionButtonColors::default(),
99 labels: CaptionButtonLabels::default(),
100 button_width: None,
101 platform: ChromePlatform::current(),
102 show: None,
103 on_close: None,
104 }
105 }
106
107 pub fn colors(mut self, colors: CaptionButtonColors) -> Self {
109 self.colors = colors;
110 self
111 }
112
113 pub fn labels(mut self, labels: CaptionButtonLabels) -> Self {
115 self.labels = labels;
116 self
117 }
118
119 pub fn button_width(mut self, width: Pixels) -> Self {
122 self.button_width = Some(width);
123 self
124 }
125
126 pub fn platform(mut self, platform: ChromePlatform) -> Self {
128 self.platform = platform;
129 self
130 }
131
132 pub fn show(mut self, show: bool) -> Self {
136 self.show = Some(show);
137 self
138 }
139
140 pub fn on_close(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
142 self.on_close = Some(std::rc::Rc::new(handler));
143 self
144 }
145
146 pub(super) fn with_close_handler(mut self, handler: Option<CloseHandler>) -> Self {
147 self.on_close = handler;
148 self
149 }
150
151 pub(super) fn with_show_override(mut self, show: Option<bool>) -> Self {
152 self.show = show;
153 self
154 }
155
156 pub(super) fn with_button_width_override(mut self, width: Option<Pixels>) -> Self {
157 self.button_width = width;
158 self
159 }
160}
161
162impl RenderOnce for CaptionButtons {
163 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
164 let buttons = div().id(self.id);
165 if !should_show_caption_buttons(window, self.platform, self.show) {
166 return buttons;
167 }
168
169 let controls = window.window_controls();
170 let show_minimize = window.is_minimizable() && controls.minimize;
171 let show_maximize = window.is_resizable() && controls.maximize;
172 let button_width = self.button_width.map_or_else(
173 || self.platform.default_caption_button_width(),
174 |width| sanitize_positive(width, self.platform.default_caption_button_width()),
175 );
176 let rtl = current_material_theme(window, cx).layout.direction == Direction::Rtl;
177
178 buttons
179 .flex()
180 .items_stretch()
181 .flex_shrink_0()
182 .when(rtl, |buttons| buttons.flex_row_reverse())
183 .when(show_minimize, |buttons| {
184 buttons.child(title_bar_control(
185 CaptionButton::Minimize,
186 self.platform,
187 button_width,
188 self.colors,
189 self.labels.for_control(CaptionButton::Minimize),
190 None,
191 window,
192 cx,
193 ))
194 })
195 .when(show_maximize, |buttons| {
196 let control = if window.is_maximized() {
197 CaptionButton::Restore
198 } else {
199 CaptionButton::Maximize
200 };
201 buttons.child(title_bar_control(
202 control,
203 self.platform,
204 button_width,
205 self.colors,
206 self.labels.for_control(control),
207 None,
208 window,
209 cx,
210 ))
211 })
212 .child(title_bar_control(
213 CaptionButton::Close,
214 self.platform,
215 button_width,
216 self.colors,
217 self.labels.for_control(CaptionButton::Close),
218 self.on_close,
219 window,
220 cx,
221 ))
222 }
223}
224
225#[derive(Clone, Copy)]
226enum CaptionButton {
227 Minimize,
228 Maximize,
229 Restore,
230 Close,
231}
232
233impl CaptionButton {
234 fn id(self) -> &'static str {
235 match self {
236 Self::Minimize => "minimize",
237 Self::Maximize | Self::Restore => "maximize-restore",
238 Self::Close => "close",
239 }
240 }
241
242 fn area(self) -> WindowControlArea {
243 match self {
244 Self::Minimize => WindowControlArea::Min,
245 Self::Maximize | Self::Restore => WindowControlArea::Max,
246 Self::Close => WindowControlArea::Close,
247 }
248 }
249
250 fn activate(self, window: &mut Window, cx: &mut App, on_close: Option<&CloseHandler>) {
251 match self {
252 Self::Minimize => activate_minimize(window),
253 Self::Maximize | Self::Restore => activate_maximize_or_restore(window),
254 Self::Close => activate_close(window, cx, on_close),
255 }
256 }
257}
258
259#[allow(clippy::too_many_arguments)]
260fn title_bar_control(
261 control: CaptionButton,
262 platform: ChromePlatform,
263 button_width: Pixels,
264 colors: CaptionButtonColors,
265 label: SharedString,
266 on_close: Option<CloseHandler>,
267 window: &Window,
268 cx: &App,
269) -> impl IntoElement {
270 let theme = current_material_theme(window, cx);
271 let tone = |role| color(theme.colors.color(role));
272 let foreground = if window.is_window_active() {
273 colors
274 .active_foreground
275 .unwrap_or_else(|| tone(ColorRole::OnSurface))
276 } else {
277 colors
278 .inactive_foreground
279 .unwrap_or_else(|| tone(ColorRole::OnSurfaceVariant))
280 };
281 let (hover_fill, hover_text, pressed_fill, pressed_text) = match control {
282 CaptionButton::Close => (
283 colors
284 .close_hover_background
285 .unwrap_or_else(|| tone(ColorRole::Error)),
286 colors
287 .close_hover_foreground
288 .unwrap_or_else(|| tone(ColorRole::OnError)),
289 colors
290 .close_pressed_background
291 .unwrap_or_else(|| tone(ColorRole::ErrorContainer)),
292 colors
293 .close_pressed_foreground
294 .unwrap_or_else(|| tone(ColorRole::OnErrorContainer)),
295 ),
296 _ => (
297 colors
298 .hover_background
299 .unwrap_or_else(|| tone(ColorRole::SurfaceContainerHigh)),
300 colors
301 .hover_foreground
302 .unwrap_or_else(|| tone(ColorRole::OnSurface)),
303 colors
304 .pressed_background
305 .unwrap_or_else(|| tone(ColorRole::SurfaceContainerHighest)),
306 colors
307 .pressed_foreground
308 .unwrap_or_else(|| tone(ColorRole::OnSurface)),
309 ),
310 };
311 let ring = tone(ColorRole::Secondary);
312
313 let use_native_win32_area = platform == ChromePlatform::Windows
316 && !(matches!(control, CaptionButton::Close) && on_close.is_some());
317
318 let click_close = on_close.clone();
319 let a11y_close = on_close;
320
321 div()
322 .id(control.id())
323 .flex()
324 .items_center()
325 .justify_center()
326 .w(button_width)
327 .min_h(px(32.))
328 .flex_shrink_0()
329 .border_1()
330 .border_color(transparent_black())
331 .text_color(foreground)
332 .when(use_native_win32_area, |button| {
333 button.window_control_area(control.area())
334 })
335 .role(Role::Button)
336 .aria_label(label)
337 .tab_index(0)
338 .focus_visible(move |style| style.border_color(ring))
339 .hover(move |style| style.bg(hover_fill).text_color(hover_text))
340 .active(move |style| style.bg(pressed_fill).text_color(pressed_text))
341 .on_click(move |event, window, cx| {
342 if !use_native_win32_area || matches!(event, ClickEvent::Keyboard(_)) {
343 control.activate(window, cx, click_close.as_ref());
344 }
345 })
346 .on_a11y_action(AccessibleAction::Click, move |_, window, cx| {
347 control.activate(window, cx, a11y_close.as_ref());
348 })
349 .child(caption_glyph(control))
350 .test_support()
351}
352
353fn caption_glyph(control: CaptionButton) -> impl IntoElement {
354 canvas(
355 |_, _, _| {},
356 move |bounds: Bounds<Pixels>, _, window: &mut Window, _| {
357 let stroke = window.text_style().color;
358 let x = bounds.origin.x;
359 let y = bounds.origin.y;
360 let w = bounds.size.width;
361 let h = bounds.size.height;
362 let mut builder = PathBuilder::stroke(px(1.));
363 match control {
364 CaptionButton::Minimize => {
365 let mid_y = y + h * 0.5;
366 builder.move_to(point(x, mid_y));
367 builder.line_to(point(x + w, mid_y));
368 }
369 CaptionButton::Maximize => {
370 let inset = px(0.5);
371 let x0 = x + inset;
372 let y0 = y + inset;
373 let x1 = x + w - inset;
374 let y1 = y + h - inset;
375 builder.add_polygon(
376 &[point(x0, y0), point(x1, y0), point(x1, y1), point(x0, y1)],
377 true,
378 );
379 }
380 CaptionButton::Restore => {
381 let inset = px(0.5);
382 let offset = px(2.);
383 let fx0 = x + inset;
385 let fy0 = y + offset + inset;
386 let fx1 = x + w - offset - inset;
387 let fy1 = y + h - inset;
388 builder.add_polygon(
389 &[
390 point(fx0, fy0),
391 point(fx1, fy0),
392 point(fx1, fy1),
393 point(fx0, fy1),
394 ],
395 true,
396 );
397 let bx0 = x + offset + inset;
399 let by0 = y + inset;
400 let bx1 = x + w - inset;
401 let by1 = y + h - offset - inset;
402 builder.move_to(point(bx0, fy0));
403 builder.line_to(point(bx0, by0));
404 builder.line_to(point(bx1, by0));
405 builder.line_to(point(bx1, by1));
406 builder.line_to(point(fx1, by1));
407 }
408 CaptionButton::Close => {
409 let inset = px(0.5);
410 let x0 = x + inset;
411 let y0 = y + inset;
412 let x1 = x + w - inset;
413 let y1 = y + h - inset;
414 builder.move_to(point(x0, y0));
415 builder.line_to(point(x1, y1));
416 builder.move_to(point(x1, y0));
417 builder.line_to(point(x0, y1));
418 }
419 }
420 if let Ok(path) = builder.build() {
421 window.paint_path(path, stroke);
422 }
423 },
424 )
425 .size(px(10.))
426 .flex_shrink_0()
427}