1mod caption;
4mod platform;
5#[cfg(test)]
6mod tests;
7
8use gpui::{
9 AnyElement, App, ElementId, Font, Hsla, InteractiveElement, IntoElement, MouseButton,
10 MouseDownEvent, ParentElement, Pixels, RenderOnce, SharedString, Styled, Window,
11 WindowControlArea, div, prelude::FluentBuilder, px,
12};
13use gpui_base::TestSupportExt as _;
14use gpui_material_foundation::{color, current_material_theme, font_for_style};
15use gpui_material_theme::{ColorRole, Direction, TypeRole, TypographyEmphasis};
16
17pub use caption::{CaptionButtonColors, CaptionButtonLabels, CaptionButtons};
18pub use platform::{
19 ChromePlatform, DEFAULT_LINUX_CAPTION_BUTTON_WIDTH, DEFAULT_MACOS_TRAFFIC_LIGHT_PADDING,
20 DEFAULT_TITLE_BAR_HEIGHT, DEFAULT_WINDOWS_CAPTION_BUTTON_WIDTH, title_bar_window_options,
21 title_bar_window_options_with_height,
22};
23
24use platform::{
25 CloseHandler, read_window_title, sanitize_nonnegative, sanitize_positive, should_show_title_bar,
26};
27
28#[derive(Clone, Copy, Debug, Default, PartialEq)]
31pub struct TitleBarColors {
32 pub background: Option<Hsla>,
34 pub active_foreground: Option<Hsla>,
36 pub inactive_foreground: Option<Hsla>,
38 pub border: Option<Hsla>,
40}
41
42#[derive(IntoElement)]
46pub struct TitleBar {
47 id: ElementId,
48 title: Option<SharedString>,
49 height: Pixels,
50 title_role: TypeRole,
51 title_font: Option<Font>,
52 title_font_family: Option<SharedString>,
53 title_font_size: Option<Pixels>,
54 leading: Option<AnyElement>,
55 content: Option<AnyElement>,
56 trailing: Option<AnyElement>,
57 traffic_light_padding: Pixels,
58 colors: TitleBarColors,
59 caption_colors: CaptionButtonColors,
60 caption_labels: CaptionButtonLabels,
61 caption_button_width: Option<Pixels>,
62 show_caption_buttons: Option<bool>,
63 on_close: Option<CloseHandler>,
64 platform: ChromePlatform,
65}
66
67impl TitleBar {
68 pub fn new(id: impl Into<ElementId>) -> Self {
70 Self {
71 id: id.into(),
72 title: None,
73 height: DEFAULT_TITLE_BAR_HEIGHT,
74 title_role: TypeRole::TitleSmall,
75 title_font: None,
76 title_font_family: None,
77 title_font_size: None,
78 leading: None,
79 content: None,
80 trailing: None,
81 traffic_light_padding: DEFAULT_MACOS_TRAFFIC_LIGHT_PADDING,
82 colors: TitleBarColors::default(),
83 caption_colors: CaptionButtonColors::default(),
84 caption_labels: CaptionButtonLabels::default(),
85 caption_button_width: None,
86 show_caption_buttons: None,
87 on_close: None,
88 platform: ChromePlatform::current(),
89 }
90 }
91
92 pub fn title(mut self, title: impl Into<SharedString>) -> Self {
94 self.title = Some(title.into());
95 self
96 }
97
98 pub fn height(mut self, height: Pixels) -> Self {
100 self.height = sanitize_positive(height, DEFAULT_TITLE_BAR_HEIGHT);
101 self
102 }
103
104 pub fn title_role(mut self, role: TypeRole) -> Self {
106 self.title_role = role;
107 self
108 }
109
110 pub fn title_font(mut self, font: Font) -> Self {
112 self.title_font = Some(font);
113 self
114 }
115
116 pub fn title_font_family(mut self, family: impl Into<SharedString>) -> Self {
118 self.title_font_family = Some(family.into());
119 self
120 }
121
122 pub fn title_font_size(mut self, size: Pixels) -> Self {
124 self.title_font_size = Some(size);
125 self
126 }
127
128 pub fn leading(mut self, content: impl IntoElement) -> Self {
131 self.leading = Some(content.into_any_element());
132 self
133 }
134
135 pub fn content(mut self, content: impl IntoElement) -> Self {
137 self.content = Some(content.into_any_element());
138 self
139 }
140
141 pub fn trailing(mut self, content: impl IntoElement) -> Self {
143 self.trailing = Some(content.into_any_element());
144 self
145 }
146
147 pub fn traffic_light_padding(mut self, padding: Pixels) -> Self {
150 self.traffic_light_padding =
151 sanitize_nonnegative(padding, DEFAULT_MACOS_TRAFFIC_LIGHT_PADDING);
152 self
153 }
154
155 pub fn colors(mut self, colors: TitleBarColors) -> Self {
157 self.colors = colors;
158 self
159 }
160
161 pub fn caption_colors(mut self, colors: CaptionButtonColors) -> Self {
163 self.caption_colors = colors;
164 self
165 }
166
167 pub fn caption_labels(mut self, labels: CaptionButtonLabels) -> Self {
169 self.caption_labels = labels;
170 self
171 }
172
173 pub fn caption_button_width(mut self, width: Pixels) -> Self {
175 self.caption_button_width = Some(width);
176 self
177 }
178
179 pub fn show_caption_buttons(mut self, show: bool) -> Self {
181 self.show_caption_buttons = Some(show);
182 self
183 }
184
185 pub fn on_close(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
187 self.on_close = Some(std::rc::Rc::new(handler));
188 self
189 }
190
191 pub fn platform(mut self, platform: ChromePlatform) -> Self {
193 self.platform = platform;
194 self
195 }
196}
197
198impl RenderOnce for TitleBar {
199 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
200 let platform = self.platform;
201 if !should_show_title_bar(window, platform, self.show_caption_buttons) {
202 return div().id(self.id).test_support();
203 }
204
205 let theme = current_material_theme(window, cx);
206 let rtl = theme.layout.direction == Direction::Rtl;
207 let tone = |role| color(theme.colors.color(role));
208
209 let background = self
210 .colors
211 .background
212 .unwrap_or_else(|| tone(ColorRole::Surface));
213 let foreground = if window.is_window_active() {
214 self.colors
215 .active_foreground
216 .unwrap_or_else(|| tone(ColorRole::OnSurface))
217 } else {
218 self.colors
219 .inactive_foreground
220 .unwrap_or_else(|| tone(ColorRole::OnSurfaceVariant))
221 };
222
223 let type_style = theme
224 .typography
225 .style(self.title_role, TypographyEmphasis::Baseline);
226 let mut resolved_font = self
227 .title_font
228 .unwrap_or_else(|| font_for_style(&theme.typography, type_style, cx));
229 if let Some(family) = self.title_font_family {
230 resolved_font.family = family;
231 }
232 let font_size = self.title_font_size.map_or(px(type_style.size), |size| {
233 sanitize_positive(size, px(type_style.size))
234 });
235
236 let title_text = self.title.unwrap_or_else(|| read_window_title(window));
237 let fullscreen = window.is_fullscreen();
238 let show_traffic_light_inset = platform.uses_traffic_lights() && !fullscreen;
239 let has_leading = self.leading.is_some();
240 let start_has_inset = (!rtl && show_traffic_light_inset) || has_leading;
241 let title_start_pad = if start_has_inset { px(8.) } else { px(16.) };
242
243 let drag_region = div()
244 .flex()
245 .flex_1()
246 .min_w(px(32.))
247 .items_center()
248 .when(rtl, |title| title.flex_row_reverse().pr(title_start_pad))
249 .when(!rtl, |title| title.pl(title_start_pad))
250 .when(!fullscreen, |title| {
251 let title = title.window_control_area(WindowControlArea::Drag);
252 match platform {
253 ChromePlatform::MacOs => title.on_mouse_down(
254 MouseButton::Left,
255 |event: &MouseDownEvent, window, _| {
256 if event.click_count == 2 {
257 window.titlebar_double_click();
258 }
259 },
260 ),
261 ChromePlatform::Linux => title
262 .on_mouse_down(MouseButton::Left, |event: &MouseDownEvent, window, _| {
263 if event.click_count == 2 {
264 if window.is_resizable() && window.window_controls().maximize {
265 window.zoom_window();
266 }
267 } else {
268 window.start_window_move();
269 }
270 })
271 .on_mouse_down(MouseButton::Right, |event: &MouseDownEvent, window, _| {
272 if window.window_controls().window_menu {
273 window.show_window_menu(event.position);
274 }
275 }),
276 ChromePlatform::Windows => title,
277 }
278 })
279 .child(
280 div()
281 .min_w_0()
282 .truncate()
283 .font(resolved_font)
284 .text_size(font_size)
285 .child(title_text),
286 );
287
288 let caption_buttons = CaptionButtons::new((self.id.clone(), "caption-buttons"))
289 .colors(self.caption_colors)
290 .labels(self.caption_labels)
291 .with_button_width_override(self.caption_button_width)
292 .platform(platform)
293 .with_show_override(self.show_caption_buttons)
294 .with_close_handler(self.on_close);
295
296 let content_row = div()
297 .flex()
298 .flex_1()
299 .min_w_0()
300 .items_stretch()
301 .when(rtl, |row| row.flex_row_reverse())
302 .when_some(self.leading, |row, leading| {
303 row.child(
304 div()
305 .flex()
306 .flex_shrink_0()
307 .items_center()
308 .when(!show_traffic_light_inset && !rtl, |slot| slot.pl(px(8.)))
309 .when(rtl, |slot| slot.pr(px(8.)))
310 .child(leading),
311 )
312 })
313 .child(drag_region)
314 .when_some(self.content, |row, content| {
315 row.child(div().flex().min_w_0().items_center().child(content))
316 })
317 .when_some(self.trailing, |row, trailing| {
318 row.child(div().flex().min_w_0().items_center().child(trailing))
319 })
320 .child(caption_buttons);
321
322 div()
325 .id(self.id.clone())
326 .flex()
327 .w_full()
328 .h(self.height)
329 .min_h(self.height)
330 .flex_shrink_0()
331 .items_stretch()
332 .bg(background)
333 .text_color(foreground)
334 .when_some(self.colors.border, |bar, border| {
335 bar.border_b_1().border_color(border)
336 })
337 .when(show_traffic_light_inset, |bar| {
338 bar.child(
339 div()
340 .id((self.id.clone(), "traffic-light-inset"))
341 .w(self.traffic_light_padding)
342 .h_full()
343 .flex_shrink_0()
344 .test_support(),
345 )
346 })
347 .child(content_row)
348 .test_support()
349 }
350}