| 1 | use std::sync::{Arc, OnceLock}; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::component::{TitleBar, h_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use super::widgets::icon; |
| 9 | use crate::app_state::Mode; |
| 10 | use crate::appearance::AppIcon; |
| 11 | use crate::theme; |
| 12 | use crate::updater::UpdateStatus; |
| 13 | |
| 14 | pub const TITLE_BAR_HEIGHT: f32 = 40.; |
| 15 | const SEARCH_WIDTH: f32 = 380.; |
| 16 | const SEARCH_MIN_WIDTH: f32 = 240.; |
| 17 | const SEARCH_MIN_VISIBLE_WIDTH: f32 = 36.; |
| 18 | const SEARCH_HEIGHT: f32 = 28.; |
| 19 | const SEARCH_GAP: f32 = 8.; |
| 20 | const SHORTCUT_MIN_WIDTH: f32 = 200.; |
| 21 | const SEARCH_HINT: &str = "Search chats, people and channels"; |
| 22 | const SHORTCUT_HINT: &str = "Ctrl K"; |
| 23 | const APP_ICON_SIZE: f32 = 18.; |
| 24 | /// Frame of one macOS traffic light button (14 x 14 since macOS 26). |
| 25 | const TRAFFIC_LIGHT_SIZE: f32 = 14.; |
| 26 | const TRAFFIC_LIGHT_LEFT: f32 = 9.; |
| 27 | |
| 28 | /// Base options for a window that renders a [`TitleBar`] of [`TITLE_BAR_HEIGHT`], with the macOS traffic lights centered in it. |
| 29 | pub fn window_options() -> WindowOptions { |
| 30 | let mut options = TitleBar::window_options(); |
| 31 | if let Some(titlebar) = options.titlebar.as_mut() { |
| 32 | titlebar.traffic_light_position = Some(point( |
| 33 | px(TRAFFIC_LIGHT_LEFT), |
| 34 | px((TITLE_BAR_HEIGHT - TRAFFIC_LIGHT_SIZE) / 2.), |
| 35 | )); |
| 36 | } |
| 37 | options |
| 38 | } |
| 39 | |
| 40 | pub fn app_icon_image(app_icon: AppIcon) -> Arc<Image> { |
| 41 | static IMAGES: [OnceLock<Arc<Image>>; AppIcon::ALL.len()] = |
| 42 | [const { OnceLock::new() }; AppIcon::ALL.len()]; |
| 43 | let index = AppIcon::ALL |
| 44 | .iter() |
| 45 | .position(|candidate| *candidate == app_icon) |
| 46 | .unwrap_or(0); |
| 47 | IMAGES[index] |
| 48 | .get_or_init(|| Arc::new(Image::from_bytes(ImageFormat::Png, app_icon.png().to_vec()))) |
| 49 | .clone() |
| 50 | } |
| 51 | |
| 52 | fn mode_pill(label: &'static str) -> Div { |
| 53 | div() |
| 54 | .debug_selector(|| "shell/title-bar/mode-pill".into()) |
| 55 | .flex_none() |
| 56 | .px(px(8.)) |
| 57 | .h(px(18.)) |
| 58 | .flex() |
| 59 | .items_center() |
| 60 | .rounded_full() |
| 61 | .bg(theme::amber().opacity(0.14)) |
| 62 | .text_size(px(11.)) |
| 63 | .font_weight(FontWeight::SEMIBOLD) |
| 64 | .text_color(theme::amber()) |
| 65 | .child(label) |
| 66 | } |
| 67 | |
| 68 | pub fn mode_label(mode: Mode) -> Option<&'static str> { |
| 69 | if mode.demo { |
| 70 | Some("Demo") |
| 71 | } else if mode.read_only { |
| 72 | Some("Read-only") |
| 73 | } else { |
| 74 | None |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn update_button( |
| 79 | update: &UpdateStatus, |
| 80 | on_restart: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 81 | ) -> Option<AnyElement> { |
| 82 | let installing = match update { |
| 83 | UpdateStatus::Ready => false, |
| 84 | UpdateStatus::Installing => true, |
| 85 | UpdateStatus::UpToDate | UpdateStatus::Failed(_) => return None, |
| 86 | }; |
| 87 | Some( |
| 88 | div() |
| 89 | .id("title-update-restart") |
| 90 | .debug_selector(|| "shell/title-bar/update".into()) |
| 91 | .flex_none() |
| 92 | .h(px(24.)) |
| 93 | .px(px(10.)) |
| 94 | .flex() |
| 95 | .items_center() |
| 96 | .rounded(px(6.)) |
| 97 | .bg(theme::accent()) |
| 98 | .text_color(theme::on_accent()) |
| 99 | .text_size(px(12.)) |
| 100 | .font_weight(FontWeight::SEMIBOLD) |
| 101 | .when(!installing, |button| { |
| 102 | button |
| 103 | .cursor_pointer() |
| 104 | .hover(|button| button.bg(theme::accent_text())) |
| 105 | .on_click(on_restart) |
| 106 | }) |
| 107 | .when(installing, |button| button.opacity(0.7)) |
| 108 | .child(if installing { |
| 109 | "Restarting..." |
| 110 | } else { |
| 111 | "Restart to update" |
| 112 | }) |
| 113 | .into_any_element(), |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 118 | struct SearchSpan { |
| 119 | left: f32, |
| 120 | width: f32, |
| 121 | } |
| 122 | |
| 123 | fn search_span(window_width: f32, leading_end: f32, trailing_start: f32) -> Option<SearchSpan> { |
| 124 | let start = leading_end + SEARCH_GAP; |
| 125 | let end = trailing_start - SEARCH_GAP; |
| 126 | let room = end - start; |
| 127 | if room < SEARCH_MIN_VISIBLE_WIDTH { |
| 128 | return None; |
| 129 | } |
| 130 | let center = window_width / 2.; |
| 131 | let centered_width = (2. * (center - start).min(end - center)).min(SEARCH_WIDTH); |
| 132 | let width = centered_width.max(SEARCH_MIN_WIDTH).min(room); |
| 133 | let left = (center - width / 2.).clamp(start, end - width); |
| 134 | Some(SearchSpan { left, width }) |
| 135 | } |
| 136 | |
| 137 | #[derive(Clone, Copy, PartialEq)] |
| 138 | struct Reserved { |
| 139 | leading_end: f32, |
| 140 | trailing_from_right: f32, |
| 141 | } |
| 142 | |
| 143 | #[derive(IntoElement)] |
| 144 | struct TitleBarRow { |
| 145 | app_icon: AppIcon, |
| 146 | mode_label: Option<&'static str>, |
| 147 | controls: Vec<AnyElement>, |
| 148 | on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>, |
| 149 | } |
| 150 | |
| 151 | pub fn render_title_bar( |
| 152 | app_icon: AppIcon, |
| 153 | mode_label: Option<&'static str>, |
| 154 | controls: impl IntoIterator<Item = AnyElement>, |
| 155 | on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 156 | ) -> impl IntoElement { |
| 157 | TitleBarRow { |
| 158 | app_icon, |
| 159 | mode_label, |
| 160 | controls: controls.into_iter().collect(), |
| 161 | on_search: Box::new(on_search), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // The Windows caption hit-test takes every point the Drag area reaches, so a control without occlusion never gets its click. |
| 166 | fn outside_caption(control: AnyElement) -> Div { |
| 167 | div().flex_none().occlude().child(control) |
| 168 | } |
| 169 | |
| 170 | fn search_box( |
| 171 | span: SearchSpan, |
| 172 | on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>, |
| 173 | ) -> impl IntoElement { |
| 174 | h_flex() |
| 175 | .id("title-search") |
| 176 | .debug_selector(|| "shell/title-bar/search".into()) |
| 177 | .occlude() |
| 178 | .absolute() |
| 179 | .top(px((TITLE_BAR_HEIGHT - SEARCH_HEIGHT) / 2.)) |
| 180 | .left(px(span.left)) |
| 181 | .w(px(span.width)) |
| 182 | .h(px(SEARCH_HEIGHT)) |
| 183 | .px(px(10.)) |
| 184 | .gap(px(8.)) |
| 185 | .items_center() |
| 186 | .overflow_hidden() |
| 187 | .rounded(px(6.)) |
| 188 | .bg(theme::surface()) |
| 189 | .cursor_pointer() |
| 190 | .text_size(px(12.)) |
| 191 | .text_color(theme::text_muted()) |
| 192 | .child( |
| 193 | div() |
| 194 | .flex_none() |
| 195 | .child(icon(IconName::Search, 14., theme::text_muted())), |
| 196 | ) |
| 197 | .child(div().flex_1().min_w_0().truncate().child(SEARCH_HINT)) |
| 198 | .when(span.width >= SHORTCUT_MIN_WIDTH, |search| { |
| 199 | search.child( |
| 200 | div() |
| 201 | .flex_none() |
| 202 | .px(px(6.)) |
| 203 | .py(px(1.)) |
| 204 | .rounded(px(4.)) |
| 205 | .bg(theme::surface_raised()) |
| 206 | .text_size(px(11.)) |
| 207 | .child(SHORTCUT_HINT), |
| 208 | ) |
| 209 | }) |
| 210 | .on_click(move |event, window, cx| on_search(event, window, cx)) |
| 211 | } |
| 212 | |
| 213 | impl RenderOnce for TitleBarRow { |
| 214 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 215 | let reserved = window.use_keyed_state("title-bar-reserved", cx, |_, _| None::<Reserved>); |
| 216 | let window_width = f32::from(window.viewport_size().width); |
| 217 | let span = (*reserved.read(cx)).and_then(|reserved| { |
| 218 | search_span( |
| 219 | window_width, |
| 220 | reserved.leading_end, |
| 221 | window_width - reserved.trailing_from_right, |
| 222 | ) |
| 223 | }); |
| 224 | let leading = h_flex() |
| 225 | .debug_selector(|| "shell/title-bar/leading".into()) |
| 226 | .flex_none() |
| 227 | .h_full() |
| 228 | .gap(px(12.)) |
| 229 | .items_center() |
| 230 | .child( |
| 231 | img(app_icon_image(self.app_icon)) |
| 232 | .size(px(APP_ICON_SIZE)) |
| 233 | .flex_none(), |
| 234 | ) |
| 235 | .child( |
| 236 | div() |
| 237 | .text_size(px(13.)) |
| 238 | .font_weight(FontWeight::SEMIBOLD) |
| 239 | .text_color(theme::text()) |
| 240 | .child(app_base::APP_NAME), |
| 241 | ) |
| 242 | .children(self.mode_label.map(mode_pill)); |
| 243 | let trailing = h_flex() |
| 244 | .debug_selector(|| "shell/title-bar/trailing".into()) |
| 245 | .flex_none() |
| 246 | .h_full() |
| 247 | .gap(px(12.)) |
| 248 | .items_center() |
| 249 | .children(self.controls.into_iter().map(outside_caption)) |
| 250 | .child(div().w(px(4.))); |
| 251 | let row = h_flex() |
| 252 | .flex_1() |
| 253 | .h_full() |
| 254 | .gap(px(12.)) |
| 255 | .items_center() |
| 256 | .child(leading) |
| 257 | .child(div().flex_1()) |
| 258 | .child(trailing) |
| 259 | .on_children_prepainted(move |children, window, cx| { |
| 260 | let [leading, _, trailing] = children[..] else { |
| 261 | return; |
| 262 | }; |
| 263 | let measured = Reserved { |
| 264 | leading_end: f32::from(leading.right()), |
| 265 | trailing_from_right: f32::from(window.viewport_size().width - trailing.left()), |
| 266 | }; |
| 267 | reserved.update(cx, |current, cx| { |
| 268 | let unchanged = current.is_some_and(|current| { |
| 269 | (current.leading_end - measured.leading_end).abs() < 0.5 |
| 270 | && (current.trailing_from_right - measured.trailing_from_right).abs() |
| 271 | < 0.5 |
| 272 | }); |
| 273 | if !unchanged { |
| 274 | *current = Some(measured); |
| 275 | cx.notify(); |
| 276 | } |
| 277 | }); |
| 278 | }); |
| 279 | let bar = TitleBar::new() |
| 280 | .h(px(TITLE_BAR_HEIGHT)) |
| 281 | .border_b_0() |
| 282 | .bg(theme::background()) |
| 283 | .child(row); |
| 284 | let on_search = self.on_search; |
| 285 | div() |
| 286 | .debug_selector(|| "shell/title-bar".into()) |
| 287 | .relative() |
| 288 | .flex_none() |
| 289 | .w_full() |
| 290 | .child(bar) |
| 291 | .children(span.map(|span| search_box(span, on_search))) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #[cfg(test)] |
| 296 | mod tests { |
| 297 | use std::cell::{Cell, RefCell}; |
| 298 | use std::rc::Rc; |
| 299 | |
| 300 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 301 | use gpui_kit::{ |
| 302 | Context, Hitbox, HitboxBehavior, InteractiveElement as _, IntoElement, Modifiers, |
| 303 | ParentElement, Render, Styled, TestAppContext, Window, canvas, div, px, size, |
| 304 | }; |
| 305 | |
| 306 | use super::{ |
| 307 | AppIcon, SEARCH_MIN_WIDTH, SEARCH_WIDTH, UpdateStatus, render_title_bar, search_span, |
| 308 | update_button, |
| 309 | }; |
| 310 | |
| 311 | struct CaptionHarness { |
| 312 | caption: Rc<RefCell<Option<Hitbox>>>, |
| 313 | restarts: Rc<Cell<usize>>, |
| 314 | } |
| 315 | |
| 316 | impl Render for CaptionHarness { |
| 317 | fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement { |
| 318 | let caption = self.caption.clone(); |
| 319 | let restarts = self.restarts.clone(); |
| 320 | let own = div() |
| 321 | .debug_selector(|| "shell/title-bar/own-status".into()) |
| 322 | .child(Button::new("own").ghost().size(px(32.)).child("O")) |
| 323 | .into_any_element(); |
| 324 | div() |
| 325 | .relative() |
| 326 | .size_full() |
| 327 | .child( |
| 328 | canvas( |
| 329 | |bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal), |
| 330 | move |_, hitbox, _, _| *caption.borrow_mut() = Some(hitbox), |
| 331 | ) |
| 332 | .absolute() |
| 333 | .size_full(), |
| 334 | ) |
| 335 | .child(render_title_bar( |
| 336 | AppIcon::ALL[0], |
| 337 | Some("Demo"), |
| 338 | [ |
| 339 | update_button(&UpdateStatus::Ready, move |_, _, _| { |
| 340 | restarts.set(restarts.get() + 1) |
| 341 | }), |
| 342 | Some(own), |
| 343 | ] |
| 344 | .into_iter() |
| 345 | .flatten(), |
| 346 | |_, _, _| {}, |
| 347 | )) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | #[gpui_kit::test] |
| 352 | fn title_bar_controls_stay_out_of_the_caption(cx: &mut TestAppContext) { |
| 353 | cx.update(gpui_kit::init); |
| 354 | cx.update(crate::theme::apply); |
| 355 | let caption = Rc::new(RefCell::new(None)); |
| 356 | let restarts = Rc::new(Cell::new(0)); |
| 357 | let (_, window_cx) = cx.add_window_view({ |
| 358 | let caption = caption.clone(); |
| 359 | let restarts = restarts.clone(); |
| 360 | move |_, _| CaptionHarness { caption, restarts } |
| 361 | }); |
| 362 | window_cx.simulate_resize(size(px(1280.), px(800.))); |
| 363 | for _ in 0..2 { |
| 364 | window_cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 365 | } |
| 366 | let mut found = Vec::new(); |
| 367 | for (selector, caption_expected) in [ |
| 368 | ("shell/title-bar/update", false), |
| 369 | ("shell/title-bar/own-status", false), |
| 370 | ("shell/title-bar/search", false), |
| 371 | ("shell/title-bar/leading", true), |
| 372 | ] { |
| 373 | let Some(bounds) = window_cx.debug_bounds(selector) else { |
| 374 | found.push(format!("{selector} was not rendered")); |
| 375 | continue; |
| 376 | }; |
| 377 | let center = bounds.center(); |
| 378 | let in_caption = window_cx.update(|window, _| { |
| 379 | caption |
| 380 | .borrow() |
| 381 | .as_ref() |
| 382 | .is_some_and(|hitbox| hitbox.is_hovered_at(center, window)) |
| 383 | }); |
| 384 | if in_caption != caption_expected { |
| 385 | found.push(format!("{selector} in caption: {in_caption}")); |
| 386 | } |
| 387 | } |
| 388 | assert!(found.is_empty(), "{}", found.join("\n")); |
| 389 | let update = window_cx |
| 390 | .debug_bounds("shell/title-bar/update") |
| 391 | .expect("update button rendered"); |
| 392 | window_cx.simulate_click(update.center(), Modifiers::default()); |
| 393 | assert_eq!(restarts.get(), 1); |
| 394 | } |
| 395 | |
| 396 | #[test] |
| 397 | fn search_stays_centered_while_it_fits() { |
| 398 | let span = search_span(1280., 200., 1080.).expect("visible"); |
| 399 | assert_eq!(span.width, SEARCH_WIDTH); |
| 400 | assert_eq!(span.left + span.width / 2., 640.); |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn search_shrinks_centered_before_it_shifts() { |
| 405 | let span = search_span(800., 200., 580.).expect("visible"); |
| 406 | assert_eq!(span.left + span.width / 2., 400.); |
| 407 | assert_eq!(span.width, 344.); |
| 408 | let shifted = search_span(800., 200., 500.).expect("visible"); |
| 409 | assert_eq!(shifted.width, SEARCH_MIN_WIDTH); |
| 410 | assert_eq!(shifted.left + shifted.width, 492.); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn search_takes_the_room_left_and_hides_without_any() { |
| 415 | let span = search_span(640., 200., 400.).expect("visible"); |
| 416 | assert_eq!((span.left, span.width), (208., 184.)); |
| 417 | assert_eq!(search_span(640., 300., 340.), None); |
| 418 | } |
| 419 | } |