| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use crate::{ |
| 4 | ActiveTheme, Icon, IconName, InteractiveElementExt as _, StyledExt, h_flex, |
| 5 | }; |
| 6 | use gpui::{ |
| 7 | AnyElement, App, Background, ClickEvent, Context, Decorations, Hsla, InteractiveElement, |
| 8 | IntoElement, MouseButton, ParentElement, Pixels, Render, RenderOnce, Rgba, |
| 9 | StatefulInteractiveElement as _, StyleRefinement, Styled, TitlebarOptions, Window, |
| 10 | WindowControlArea, WindowOptions, div, linear_color_stop, linear_gradient, |
| 11 | prelude::FluentBuilder as _, px, |
| 12 | }; |
| 13 | use smallvec::SmallVec; |
| 14 | |
| 15 | pub const TITLE_BAR_HEIGHT: Pixels = px(34.); |
| 16 | #[cfg(target_os = "macos")] |
| 17 | const TITLE_BAR_LEFT_PADDING: Pixels = px(80.); |
| 18 | #[cfg(not(target_os = "macos"))] |
| 19 | const TITLE_BAR_LEFT_PADDING: Pixels = px(12.); |
| 20 | /// Windows 11 caption button width at 100% scale. |
| 21 | const CONTROL_BUTTON_WIDTH: Pixels = px(46.); |
| 22 | /// Box that renders the 16/24 window glyphs 10 px wide, like Segoe Fluent Icons at 10 px. |
| 23 | const CONTROL_ICON_SIZE: Pixels = px(15.); |
| 24 | |
| 25 | fn default_title_bar_background(title_bar: Hsla, background: Hsla) -> Background { |
| 26 | let title_bar_rgb = title_bar.to_rgb(); |
| 27 | let background_rgb = background.to_rgb(); |
| 28 | let mixed = Hsla::from(Rgba { |
| 29 | r: title_bar_rgb.r * 0.55 + background_rgb.r * 0.45, |
| 30 | g: title_bar_rgb.g * 0.55 + background_rgb.g * 0.45, |
| 31 | b: title_bar_rgb.b * 0.55 + background_rgb.b * 0.45, |
| 32 | a: title_bar_rgb.a * 0.55 + background_rgb.a * 0.45, |
| 33 | }); |
| 34 | |
| 35 | linear_gradient( |
| 36 | 180., |
| 37 | linear_color_stop(mixed, 0.), |
| 38 | linear_color_stop(title_bar, 1.), |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | /// TitleBar used to customize the appearance of the title bar. |
| 43 | /// |
| 44 | /// We can put some elements inside the title bar. |
| 45 | #[derive(IntoElement)] |
| 46 | pub struct TitleBar { |
| 47 | style: StyleRefinement, |
| 48 | children: SmallVec<[AnyElement; 1]>, |
| 49 | on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>, |
| 50 | } |
| 51 | |
| 52 | impl TitleBar { |
| 53 | /// Create a new TitleBar. |
| 54 | pub fn new() -> Self { |
| 55 | Self { |
| 56 | style: StyleRefinement::default(), |
| 57 | children: SmallVec::new(), |
| 58 | on_close_window: None, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Returns the default title bar options for compatible with the [`crate::TitleBar`]. |
| 63 | pub fn title_bar_options() -> TitlebarOptions { |
| 64 | TitlebarOptions { |
| 65 | title: None, |
| 66 | appears_transparent: true, |
| 67 | traffic_light_position: Some(gpui::point(px(9.0), px(9.0))), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Returns the default window options for compatible with the [`crate::TitleBar`]. |
| 72 | /// |
| 73 | /// Use this as the base of the [`WindowOptions`] of any window that renders a |
| 74 | /// [`crate::TitleBar`], so the title bar owns dragging and double clicking itself: |
| 75 | /// |
| 76 | /// ```no_run |
| 77 | /// # mod gpui_kit { pub use gpui::*; pub extern crate gpui_component as component; } |
| 78 | /// # use gpui_kit::WindowOptions; |
| 79 | /// # use gpui_kit::component::TitleBar; |
| 80 | /// let options = WindowOptions { |
| 81 | /// window_min_size: None, |
| 82 | /// ..TitleBar::window_options() |
| 83 | /// }; |
| 84 | /// ``` |
| 85 | pub fn window_options() -> WindowOptions { |
| 86 | WindowOptions { |
| 87 | titlebar: Some(Self::title_bar_options()), |
| 88 | // The title bar draws itself and moves the window via `start_window_move`, |
| 89 | // so AppKit must not treat it as a system window-move region. Otherwise macOS |
| 90 | // handles title bar double clicks on its own (in addition to `on_double_click` |
| 91 | // below) and delays title bar clicks while disambiguating double clicks. |
| 92 | app_owns_titlebar_drag: true, |
| 93 | ..Default::default() |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// Add custom for close window event, default is None, then click X button will call `window.remove_window()`. |
| 98 | /// Linux only, this will do nothing on other platforms. |
| 99 | pub fn on_close_window( |
| 100 | mut self, |
| 101 | f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 102 | ) -> Self { |
| 103 | if cfg!(target_os = "linux") { |
| 104 | self.on_close_window = Some(Rc::new(Box::new(f))); |
| 105 | } |
| 106 | self |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // We don't need implementation the click event for the control buttons. |
| 111 | // If user clicked in the bounds, the window event will be triggered. |
| 112 | #[derive(IntoElement, Clone)] |
| 113 | enum ControlIcon { |
| 114 | Minimize, |
| 115 | Restore, |
| 116 | Maximize, |
| 117 | Close { |
| 118 | on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>, |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | impl ControlIcon { |
| 123 | fn minimize() -> Self { |
| 124 | Self::Minimize |
| 125 | } |
| 126 | |
| 127 | fn restore() -> Self { |
| 128 | Self::Restore |
| 129 | } |
| 130 | |
| 131 | fn maximize() -> Self { |
| 132 | Self::Maximize |
| 133 | } |
| 134 | |
| 135 | fn close(on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>) -> Self { |
| 136 | Self::Close { on_close_window } |
| 137 | } |
| 138 | |
| 139 | fn id(&self) -> &'static str { |
| 140 | match self { |
| 141 | Self::Minimize => "minimize", |
| 142 | Self::Restore => "restore", |
| 143 | Self::Maximize => "maximize", |
| 144 | Self::Close { .. } => "close", |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn icon(&self) -> IconName { |
| 149 | match self { |
| 150 | Self::Minimize => IconName::WindowMinimize, |
| 151 | Self::Restore => IconName::WindowRestore, |
| 152 | Self::Maximize => IconName::WindowMaximize, |
| 153 | Self::Close { .. } => IconName::WindowClose, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn window_control_area(&self) -> WindowControlArea { |
| 158 | match self { |
| 159 | Self::Minimize => WindowControlArea::Min, |
| 160 | Self::Restore | Self::Maximize => WindowControlArea::Max, |
| 161 | Self::Close { .. } => WindowControlArea::Close, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | fn is_close(&self) -> bool { |
| 166 | matches!(self, Self::Close { .. }) |
| 167 | } |
| 168 | |
| 169 | #[inline] |
| 170 | fn hover_fg(&self, cx: &App) -> Hsla { |
| 171 | if self.is_close() { |
| 172 | cx.theme().danger_foreground |
| 173 | } else { |
| 174 | cx.theme().secondary_foreground |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[inline] |
| 179 | fn hover_bg(&self, cx: &App) -> Hsla { |
| 180 | if self.is_close() { |
| 181 | cx.theme().danger |
| 182 | } else { |
| 183 | cx.theme().secondary_hover |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[inline] |
| 188 | fn active_bg(&self, cx: &mut App) -> Hsla { |
| 189 | if self.is_close() { |
| 190 | cx.theme().danger_active |
| 191 | } else { |
| 192 | cx.theme().secondary_active |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl RenderOnce for ControlIcon { |
| 198 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 199 | let is_linux = cfg!(target_os = "linux"); |
| 200 | let is_windows = cfg!(target_os = "windows"); |
| 201 | let hover_fg = self.hover_fg(cx); |
| 202 | let hover_bg = self.hover_bg(cx); |
| 203 | let active_bg = self.active_bg(cx); |
| 204 | let icon = self.clone(); |
| 205 | let on_close_window = match &self { |
| 206 | ControlIcon::Close { on_close_window } => on_close_window.clone(), |
| 207 | _ => None, |
| 208 | }; |
| 209 | |
| 210 | div() |
| 211 | .id(self.id()) |
| 212 | .flex() |
| 213 | .w(CONTROL_BUTTON_WIDTH) |
| 214 | .h_full() |
| 215 | .flex_shrink_0() |
| 216 | .justify_center() |
| 217 | .content_center() |
| 218 | .items_center() |
| 219 | .text_color(cx.theme().foreground) |
| 220 | .hover(|style| style.bg(hover_bg).text_color(hover_fg)) |
| 221 | .active(|style| style.bg(active_bg).text_color(hover_fg)) |
| 222 | .when(is_windows, |this| { |
| 223 | this.window_control_area(self.window_control_area()) |
| 224 | }) |
| 225 | .when(is_linux, |this| { |
| 226 | this.on_mouse_down(MouseButton::Left, move |_, window, cx| { |
| 227 | window.prevent_default(); |
| 228 | cx.stop_propagation(); |
| 229 | }) |
| 230 | .on_click(move |_, window, cx| { |
| 231 | cx.stop_propagation(); |
| 232 | match icon { |
| 233 | Self::Minimize => window.minimize_window(), |
| 234 | Self::Restore | Self::Maximize => window.zoom_window(), |
| 235 | Self::Close { .. } => { |
| 236 | if let Some(f) = on_close_window.clone() { |
| 237 | f(&ClickEvent::default(), window, cx); |
| 238 | } else { |
| 239 | window.remove_window(); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | }) |
| 244 | }) |
| 245 | .child(Icon::new(self.icon()).size(CONTROL_ICON_SIZE)) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | #[derive(IntoElement)] |
| 250 | struct WindowControls { |
| 251 | on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>, |
| 252 | } |
| 253 | |
| 254 | impl RenderOnce for WindowControls { |
| 255 | fn render(self, window: &mut Window, _: &mut App) -> impl IntoElement { |
| 256 | if cfg!(target_os = "macos") || cfg!(target_family = "wasm") { |
| 257 | return div().id("window-controls"); |
| 258 | } |
| 259 | |
| 260 | // Under server-side decorations the window manager already renders |
| 261 | // its own title bar, complete with min/max/close; drawing ours as |
| 262 | // well stacks a duplicate set of controls on top of it (most |
| 263 | // visibly two close buttons). gpui falls back to server-side |
| 264 | // decorations on X11 sessions without a compositor, and a Wayland |
| 265 | // compositor may grant server mode even when client mode was |
| 266 | // requested. Skip ours unless this window is actually client-side |
| 267 | // decorated, mirroring the `is_client_decorated` gating of the |
| 268 | // title bar's window-menu overlay. |
| 269 | #[cfg(target_os = "linux")] |
| 270 | if !matches!(window.window_decorations(), Decorations::Client { .. }) { |
| 271 | return div().id("window-controls"); |
| 272 | } |
| 273 | |
| 274 | // The window manager declares which controls it can honor; a tiling |
| 275 | // compositor may support neither minimize nor maximize. Close is |
| 276 | // always ours to offer. |
| 277 | let supported = window.window_controls(); |
| 278 | |
| 279 | h_flex() |
| 280 | .id("window-controls") |
| 281 | .items_center() |
| 282 | .flex_shrink_0() |
| 283 | .h_full() |
| 284 | .when(supported.minimize, |this| { |
| 285 | this.child(ControlIcon::minimize()) |
| 286 | }) |
| 287 | .when(supported.maximize, |this| { |
| 288 | this.child(if window.is_maximized() { |
| 289 | ControlIcon::restore() |
| 290 | } else { |
| 291 | ControlIcon::maximize() |
| 292 | }) |
| 293 | }) |
| 294 | .child(ControlIcon::close(self.on_close_window)) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | impl Styled for TitleBar { |
| 299 | fn style(&mut self) -> &mut gpui::StyleRefinement { |
| 300 | &mut self.style |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | impl ParentElement for TitleBar { |
| 305 | fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { |
| 306 | self.children.extend(elements); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | struct TitleBarState { |
| 311 | should_move: bool, |
| 312 | } |
| 313 | |
| 314 | // TODO: Remove this when GPUI has released v0.2.3 |
| 315 | impl Render for TitleBarState { |
| 316 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 317 | div() |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl RenderOnce for TitleBar { |
| 322 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 323 | let is_client_decorated = matches!(window.window_decorations(), Decorations::Client { .. }); |
| 324 | let is_web = cfg!(target_family = "wasm"); |
| 325 | let is_linux = cfg!(target_os = "linux"); |
| 326 | let is_macos = cfg!(target_os = "macos"); |
| 327 | |
| 328 | let state = window.use_state(cx, |_, _| TitleBarState { should_move: false }); |
| 329 | |
| 330 | div().flex_shrink_0().child( |
| 331 | div() |
| 332 | .id("title-bar") |
| 333 | .flex() |
| 334 | .flex_row() |
| 335 | .items_center() |
| 336 | .justify_between() |
| 337 | .h(TITLE_BAR_HEIGHT) |
| 338 | // The macOS traffic lights are hidden in fullscreen, so their inset goes too. |
| 339 | .when(!(cfg!(target_os = "macos") && window.is_fullscreen()), |this| { |
| 340 | this.pl(TITLE_BAR_LEFT_PADDING) |
| 341 | }) |
| 342 | .border_b_1() |
| 343 | .border_color(cx.theme().title_bar_border) |
| 344 | .bg(default_title_bar_background( |
| 345 | cx.theme().title_bar, |
| 346 | cx.theme().background, |
| 347 | )) |
| 348 | .refine_style(&self.style) |
| 349 | .when(is_linux, |this| { |
| 350 | this.on_double_click(|_, window, _| window.zoom_window()) |
| 351 | }) |
| 352 | .when(is_macos, |this| { |
| 353 | this.on_double_click(|_, window, _| window.titlebar_double_click()) |
| 354 | }) |
| 355 | .on_mouse_down_out(window.listener_for(&state, |state, _, _, _| { |
| 356 | state.should_move = false; |
| 357 | })) |
| 358 | .on_mouse_down( |
| 359 | MouseButton::Left, |
| 360 | window.listener_for(&state, |state, _, _, _| { |
| 361 | state.should_move = true; |
| 362 | }), |
| 363 | ) |
| 364 | .on_mouse_up( |
| 365 | MouseButton::Left, |
| 366 | window.listener_for(&state, |state, _, _, _| { |
| 367 | state.should_move = false; |
| 368 | }), |
| 369 | ) |
| 370 | .on_mouse_move(window.listener_for(&state, |state, _, window, _| { |
| 371 | if state.should_move { |
| 372 | state.should_move = false; |
| 373 | window.start_window_move(); |
| 374 | } |
| 375 | })) |
| 376 | .child( |
| 377 | h_flex() |
| 378 | .id("bar") |
| 379 | .h_full() |
| 380 | .justify_between() |
| 381 | .flex_shrink_0() |
| 382 | .flex_1() |
| 383 | .when(!is_web, |this| { |
| 384 | this.window_control_area(WindowControlArea::Drag) |
| 385 | .when(window.is_fullscreen(), |this| this.pl_3()) |
| 386 | .when(is_linux && is_client_decorated, |this| { |
| 387 | this.child( |
| 388 | div() |
| 389 | .top_0() |
| 390 | .left_0() |
| 391 | .absolute() |
| 392 | .size_full() |
| 393 | .h_full() |
| 394 | .on_mouse_down( |
| 395 | MouseButton::Right, |
| 396 | move |ev, window, _| { |
| 397 | window.show_window_menu(ev.position) |
| 398 | }, |
| 399 | ), |
| 400 | ) |
| 401 | }) |
| 402 | }) |
| 403 | .children(self.children), |
| 404 | ) |
| 405 | .child(WindowControls { |
| 406 | on_close_window: self.on_close_window, |
| 407 | }), |
| 408 | ) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | #[cfg(test)] |
| 413 | mod tests { |
| 414 | use super::*; |
| 415 | use gpui::{Rgba, linear_color_stop, linear_gradient}; |
| 416 | |
| 417 | #[test] |
| 418 | fn test_default_title_bar_background() { |
| 419 | let title_bar = Hsla::black(); |
| 420 | let background = Hsla::white(); |
| 421 | |
| 422 | assert_eq!( |
| 423 | default_title_bar_background(title_bar, background), |
| 424 | linear_gradient( |
| 425 | 180., |
| 426 | linear_color_stop( |
| 427 | Hsla::from(Rgba { |
| 428 | r: 0.45, |
| 429 | g: 0.45, |
| 430 | b: 0.45, |
| 431 | a: 1., |
| 432 | }), |
| 433 | 0., |
| 434 | ), |
| 435 | linear_color_stop(title_bar, 1.), |
| 436 | ) |
| 437 | ); |
| 438 | } |
| 439 | } |