| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use crate::i18n::tr; |
| 8 | use crate::settings_section::SettingsSection; |
| 9 | use crate::theme; |
| 10 | use crate::views::widgets::icon; |
| 11 | |
| 12 | pub const TEAMS_GROUP: [SettingsSection; 8] = [ |
| 13 | SettingsSection::General, |
| 14 | SettingsSection::Notifications, |
| 15 | SettingsSection::Appearance, |
| 16 | SettingsSection::Chats, |
| 17 | SettingsSection::Calls, |
| 18 | SettingsSection::Calendar, |
| 19 | SettingsSection::CalendarAccounts, |
| 20 | SettingsSection::Devices, |
| 21 | ]; |
| 22 | pub const RUSTY_GROUP: [SettingsSection; 2] = [SettingsSection::Agents, SettingsSection::Mcp]; |
| 23 | const ITEM_MIN_HEIGHT: f32 = 32.; |
| 24 | const ITEM_PADDING_Y: f32 = 6.; |
| 25 | const ITEM_LINE_HEIGHT: f32 = 20.; |
| 26 | const ITEM_GAP: f32 = 2.; |
| 27 | const ITEM_RADIUS: f32 = 6.; |
| 28 | const ITEM_ICON: f32 = 16.; |
| 29 | const NAV_PADDING_X: f32 = 12.; |
| 30 | |
| 31 | const CALENDAR_VIEW_TERMS: [&str; 5] = [ |
| 32 | "settings.calendar.default_view", |
| 33 | "settings.calendar.work_days", |
| 34 | "settings.calendar.work_hours", |
| 35 | "settings.calendar.time_scale", |
| 36 | "settings.calendar.weekend", |
| 37 | ]; |
| 38 | |
| 39 | const CALENDAR_ACCOUNTS_TERMS: [&str; 12] = [ |
| 40 | "settings.calendar.google_title", |
| 41 | "settings.calendar.google_client_id", |
| 42 | "settings.calendar.google_client_secret", |
| 43 | "settings.calendar.google_client_guide_1", |
| 44 | "settings.calendar.connect_button", |
| 45 | "settings.calendar.caldav_title", |
| 46 | "settings.calendar.caldav_connect", |
| 47 | "settings.calendar.ics_title", |
| 48 | "settings.calendar.ics_connect", |
| 49 | "settings.calendar.section_home", |
| 50 | "settings.calendar.section_write", |
| 51 | "settings.calendar.outlook_mailbox", |
| 52 | ]; |
| 53 | |
| 54 | fn search_terms(section: SettingsSection) -> Vec<String> { |
| 55 | match section { |
| 56 | SettingsSection::General => vec![ |
| 57 | tr("settings.general.auto_start"), |
| 58 | tr("settings.general.open_in_background"), |
| 59 | tr("settings.general.show_in_tray"), |
| 60 | tr("settings.general.close_to_tray"), |
| 61 | tr("settings.general.external_browser"), |
| 62 | tr("settings.general.up_arrow"), |
| 63 | tr("settings.general.translate_into"), |
| 64 | tr("settings.general.behavior_title"), |
| 65 | tr("settings.general.never_translate"), |
| 66 | ], |
| 67 | SettingsSection::Notifications => vec![ |
| 68 | tr("settings.notifications.chat_messages"), |
| 69 | tr("settings.notifications.meeting_chats"), |
| 70 | tr("settings.notifications.likes_reactions"), |
| 71 | tr("settings.notifications.teams_channels"), |
| 72 | tr("settings.notifications.show_preview"), |
| 73 | tr("settings.notifications.notification_sound"), |
| 74 | tr("settings.notifications.urgent_sound"), |
| 75 | tr("settings.notifications.position"), |
| 76 | tr("settings.notifications.flash_taskbar"), |
| 77 | tr("settings.notifications.hide_in_calls"), |
| 78 | tr("settings.notifications.send_test"), |
| 79 | ], |
| 80 | SettingsSection::Appearance => vec![ |
| 81 | tr("settings.appearance.region.title"), |
| 82 | tr("settings.appearance.clock.title"), |
| 83 | tr("settings.appearance.first_day.title"), |
| 84 | tr("settings.appearance.theme"), |
| 85 | tr("settings.appearance.color_scheme"), |
| 86 | tr("settings.appearance.app_icon"), |
| 87 | ], |
| 88 | SettingsSection::Chats => vec![ |
| 89 | tr("settings.chats.density"), |
| 90 | tr("settings.chats.read_receipts"), |
| 91 | tr("settings.chats.message_actions"), |
| 92 | tr("settings.chats.muted_section"), |
| 93 | tr("settings.chats.enter_to"), |
| 94 | tr("settings.chats.markdown"), |
| 95 | ], |
| 96 | SettingsSection::Calls => vec![tr("settings.calls.ringtones")], |
| 97 | SettingsSection::Calendar => CALENDAR_VIEW_TERMS.iter().map(|key| tr(key)).collect(), |
| 98 | SettingsSection::CalendarAccounts => { |
| 99 | CALENDAR_ACCOUNTS_TERMS.iter().map(|key| tr(key)).collect() |
| 100 | } |
| 101 | SettingsSection::Devices => vec![ |
| 102 | "Speaker".to_owned(), |
| 103 | "Microphone".to_owned(), |
| 104 | "Noise suppression".to_owned(), |
| 105 | "Music mode".to_owned(), |
| 106 | "Camera".to_owned(), |
| 107 | "Background".to_owned(), |
| 108 | "Soft focus".to_owned(), |
| 109 | "Adjust brightness".to_owned(), |
| 110 | "Mirror my video".to_owned(), |
| 111 | "Make a test call".to_owned(), |
| 112 | "Save a test report after each call".to_owned(), |
| 113 | ], |
| 114 | SettingsSection::Agents => vec!["Claude".to_owned(), "Agent runs".to_owned()], |
| 115 | SettingsSection::Mcp => vec![ |
| 116 | "Model Context Protocol".to_owned(), |
| 117 | "Conversation access".to_owned(), |
| 118 | ], |
| 119 | SettingsSection::Help => vec![ |
| 120 | tr("settings.help.create_bundle"), |
| 121 | tr("settings.help.open_issue"), |
| 122 | "GitHub".to_owned(), |
| 123 | "diagnostics".to_owned(), |
| 124 | "support".to_owned(), |
| 125 | ], |
| 126 | SettingsSection::About => vec!["Version".to_owned(), "Build".to_owned()], |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub fn matches(section: SettingsSection, query: &str) -> bool { |
| 131 | let query = query.trim().to_lowercase(); |
| 132 | query.is_empty() |
| 133 | || std::iter::once(label(section)) |
| 134 | .chain(search_terms(section)) |
| 135 | .any(|text| text.to_lowercase().contains(&query)) |
| 136 | } |
| 137 | |
| 138 | pub fn first_match(query: &str, agents_available: bool) -> Option<SettingsSection> { |
| 139 | TEAMS_GROUP |
| 140 | .into_iter() |
| 141 | .chain(RUSTY_GROUP) |
| 142 | .chain([SettingsSection::Help, SettingsSection::About]) |
| 143 | .filter(|section| agents_available || *section != SettingsSection::Agents) |
| 144 | .find(|section| matches(*section, query)) |
| 145 | } |
| 146 | |
| 147 | pub fn label(section: SettingsSection) -> String { |
| 148 | match section { |
| 149 | SettingsSection::General => tr("settings.nav.general"), |
| 150 | SettingsSection::Notifications => tr("settings.nav.notifications"), |
| 151 | SettingsSection::Appearance => tr("settings.nav.appearance"), |
| 152 | SettingsSection::Chats => tr("settings.nav.chats"), |
| 153 | SettingsSection::Calls => tr("settings.nav.calls"), |
| 154 | SettingsSection::Calendar => tr("settings.nav.calendar"), |
| 155 | SettingsSection::CalendarAccounts => tr("settings.nav.calendar_accounts"), |
| 156 | SettingsSection::Devices => tr("settings.nav.devices"), |
| 157 | SettingsSection::Agents => tr("settings.nav.agents"), |
| 158 | SettingsSection::Mcp => tr("settings.nav.mcp"), |
| 159 | SettingsSection::Help => tr("settings.nav.help"), |
| 160 | SettingsSection::About => tr("settings.nav.about"), |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn icon_name(section: SettingsSection) -> IconName { |
| 165 | match section { |
| 166 | SettingsSection::General => IconName::SlidersHorizontal, |
| 167 | SettingsSection::Notifications => IconName::Bell, |
| 168 | SettingsSection::Appearance => IconName::Palette, |
| 169 | SettingsSection::Chats => IconName::MessageSquare, |
| 170 | SettingsSection::Calls => IconName::Phone, |
| 171 | SettingsSection::Calendar => IconName::Calendar, |
| 172 | SettingsSection::CalendarAccounts => IconName::Calendar, |
| 173 | SettingsSection::Devices => IconName::Headphones, |
| 174 | SettingsSection::Agents => IconName::Bot, |
| 175 | SettingsSection::Mcp => IconName::Plug, |
| 176 | SettingsSection::Help => IconName::BookOpen, |
| 177 | SettingsSection::About => IconName::Info, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | pub fn item_selector(section: SettingsSection) -> String { |
| 182 | format!("settings/nav/item/{}", element_id(section)) |
| 183 | } |
| 184 | |
| 185 | pub fn label_selector(section: SettingsSection) -> String { |
| 186 | format!("{}/label", item_selector(section)) |
| 187 | } |
| 188 | |
| 189 | fn element_id(section: SettingsSection) -> &'static str { |
| 190 | match section { |
| 191 | SettingsSection::General => "settings-nav-general", |
| 192 | SettingsSection::Notifications => "settings-nav-notifications", |
| 193 | SettingsSection::Appearance => "settings-nav-appearance", |
| 194 | SettingsSection::Chats => "settings-nav-chats", |
| 195 | SettingsSection::Calls => "settings-nav-calls", |
| 196 | SettingsSection::Calendar => "settings-nav-calendar", |
| 197 | SettingsSection::CalendarAccounts => "settings-nav-calendar-accounts", |
| 198 | SettingsSection::Devices => "settings-nav-devices", |
| 199 | SettingsSection::Agents => "settings-nav-agents", |
| 200 | SettingsSection::Mcp => "settings-nav-mcp", |
| 201 | SettingsSection::Help => "settings-nav-help", |
| 202 | SettingsSection::About => "settings-nav-about", |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | pub struct NavLayout { |
| 207 | pub width: f32, |
| 208 | pub padding_y: f32, |
| 209 | } |
| 210 | |
| 211 | pub struct NavSearch<'a> { |
| 212 | pub input: AnyElement, |
| 213 | pub query: &'a str, |
| 214 | } |
| 215 | |
| 216 | pub fn render_nav( |
| 217 | active: SettingsSection, |
| 218 | agents_available: bool, |
| 219 | layout: NavLayout, |
| 220 | search: NavSearch<'_>, |
| 221 | on_select: impl Fn(SettingsSection, &mut Window, &mut App) + Clone + 'static, |
| 222 | ) -> Div { |
| 223 | let item = |section: SettingsSection| nav_item(section, section == active, on_select.clone()); |
| 224 | let query = search.query; |
| 225 | let teams: Vec<Stateful<Div>> = TEAMS_GROUP |
| 226 | .into_iter() |
| 227 | .filter(|section| matches(*section, query)) |
| 228 | .map(item) |
| 229 | .collect(); |
| 230 | let rusty: Vec<Stateful<Div>> = RUSTY_GROUP |
| 231 | .into_iter() |
| 232 | .filter(|section| agents_available || *section != SettingsSection::Agents) |
| 233 | .filter(|section| matches(*section, query)) |
| 234 | .map(item) |
| 235 | .collect(); |
| 236 | let help_shown = matches(SettingsSection::Help, query); |
| 237 | let about_shown = matches(SettingsSection::About, query); |
| 238 | let nothing_found = teams.is_empty() && rusty.is_empty() && !help_shown && !about_shown; |
| 239 | let group = |items: Vec<Stateful<Div>>| { |
| 240 | v_flex() |
| 241 | .w_full() |
| 242 | .px(px(NAV_PADDING_X)) |
| 243 | .gap(px(ITEM_GAP)) |
| 244 | .children(items) |
| 245 | }; |
| 246 | v_flex() |
| 247 | .debug_selector(|| "settings/nav".into()) |
| 248 | .w(px(layout.width)) |
| 249 | .h_full() |
| 250 | .flex_none() |
| 251 | .pt(px(layout.padding_y)) |
| 252 | .pb(px(NAV_PADDING_X)) |
| 253 | .gap(px(ITEM_GAP)) |
| 254 | .rounded_l(px(super::dialog::PANEL_RADIUS)) |
| 255 | .bg(theme::surface_raised()) |
| 256 | .child( |
| 257 | div() |
| 258 | .mx(px(2. * NAV_PADDING_X)) |
| 259 | .mb(px(18.)) |
| 260 | .text_size(px(20.)) |
| 261 | .line_height(px(26.)) |
| 262 | .font_weight(FontWeight::SEMIBOLD) |
| 263 | .text_color(theme::text()) |
| 264 | .child(tr("common.settings")), |
| 265 | ) |
| 266 | .child( |
| 267 | div() |
| 268 | .debug_selector(|| "settings/nav/search".into()) |
| 269 | .w_full() |
| 270 | .px(px(NAV_PADDING_X)) |
| 271 | .pb(px(10.)) |
| 272 | .child(search.input), |
| 273 | ) |
| 274 | .child( |
| 275 | v_flex() |
| 276 | .id("settings-nav-groups") |
| 277 | .w_full() |
| 278 | .flex_1() |
| 279 | .min_h(px(0.)) |
| 280 | .overflow_y_scrollbar() |
| 281 | .gap(px(ITEM_GAP)) |
| 282 | .child(group(teams)) |
| 283 | .when(nothing_found, |groups| { |
| 284 | groups.child( |
| 285 | div() |
| 286 | .px(px(2. * NAV_PADDING_X)) |
| 287 | .py(px(ITEM_PADDING_Y)) |
| 288 | .text_size(px(13.)) |
| 289 | .text_color(theme::text_muted()) |
| 290 | .child(tr("settings.search.no_results")), |
| 291 | ) |
| 292 | }) |
| 293 | .when(!rusty.is_empty(), |groups| { |
| 294 | groups |
| 295 | .child( |
| 296 | // No debug_selector: scroll content bounds are unclipped, so a |
| 297 | // tagged divider under settings/nav false-overlaps sticky About. |
| 298 | div() |
| 299 | .w_full() |
| 300 | .h(px(1.)) |
| 301 | .flex_none() |
| 302 | .mt(px(10.)) |
| 303 | .bg(theme::border_strong()), |
| 304 | ) |
| 305 | .child( |
| 306 | div() |
| 307 | .px(px(2. * NAV_PADDING_X)) |
| 308 | .pt(px(12.)) |
| 309 | .pb(px(6.)) |
| 310 | .text_size(px(12.)) |
| 311 | .font_weight(FontWeight::SEMIBOLD) |
| 312 | .text_color(theme::text_muted()) |
| 313 | .child("RUSTY TEAMS"), |
| 314 | ) |
| 315 | .child(group(rusty)) |
| 316 | }), |
| 317 | ) |
| 318 | .when(help_shown || about_shown, |nav| { |
| 319 | nav.child( |
| 320 | v_flex() |
| 321 | .w_full() |
| 322 | .flex_none() |
| 323 | .px(px(NAV_PADDING_X)) |
| 324 | .pt(px(6.)) |
| 325 | .gap(px(ITEM_GAP)) |
| 326 | .when(help_shown, |footer| { |
| 327 | footer.child( |
| 328 | item(SettingsSection::Help) |
| 329 | .debug_selector(|| "settings/nav/help".into()), |
| 330 | ) |
| 331 | }) |
| 332 | .when(about_shown, |footer| { |
| 333 | footer.child( |
| 334 | item(SettingsSection::About) |
| 335 | .debug_selector(|| "settings/nav/about".into()), |
| 336 | ) |
| 337 | }), |
| 338 | ) |
| 339 | }) |
| 340 | } |
| 341 | |
| 342 | fn nav_item( |
| 343 | section: SettingsSection, |
| 344 | active: bool, |
| 345 | on_select: impl Fn(SettingsSection, &mut Window, &mut App) + 'static, |
| 346 | ) -> Stateful<Div> { |
| 347 | let label_text = label(section); |
| 348 | h_flex() |
| 349 | .id(element_id(section)) |
| 350 | .debug_selector(move || item_selector(section)) |
| 351 | .w_full() |
| 352 | .min_h(px(ITEM_MIN_HEIGHT)) |
| 353 | .flex_none() |
| 354 | .px(px(12.)) |
| 355 | .py(px(ITEM_PADDING_Y)) |
| 356 | .gap(px(12.)) |
| 357 | .items_center() |
| 358 | .rounded(px(ITEM_RADIUS)) |
| 359 | .cursor_pointer() |
| 360 | .text_size(px(14.)) |
| 361 | .line_height(px(ITEM_LINE_HEIGHT)) |
| 362 | .when(active, |item| { |
| 363 | item.bg(theme::popup_hover()).text_color(theme::text()) |
| 364 | }) |
| 365 | .when(!active, |item| { |
| 366 | item.text_color(theme::text_strong()) |
| 367 | .hover(|item| item.bg(theme::popup())) |
| 368 | }) |
| 369 | .child(icon( |
| 370 | icon_name(section), |
| 371 | ITEM_ICON, |
| 372 | if active { |
| 373 | theme::accent_text() |
| 374 | } else { |
| 375 | theme::text_muted() |
| 376 | }, |
| 377 | )) |
| 378 | .child( |
| 379 | div() |
| 380 | .debug_selector(move || label_selector(section)) |
| 381 | .flex_1() |
| 382 | .min_w_0() |
| 383 | .child(label_text), |
| 384 | ) |
| 385 | .on_click(move |_, window, cx| on_select(section, window, cx)) |
| 386 | } |
| 387 | |
| 388 | #[cfg(test)] |
| 389 | mod tests { |
| 390 | use app_base::ui_locale::apply_locale; |
| 391 | |
| 392 | use super::{SettingsSection, first_match, label, matches, search_terms}; |
| 393 | |
| 394 | #[test] |
| 395 | fn search_finds_sections_by_name_and_by_setting() { |
| 396 | let _guard = crate::i18n::locale_test_lock(); |
| 397 | apply_locale("en"); |
| 398 | assert!(matches(SettingsSection::Chats, "read receipts")); |
| 399 | assert!(matches(SettingsSection::Devices, "MICRO")); |
| 400 | assert!(matches(SettingsSection::Calls, "calls")); |
| 401 | assert!(!matches(SettingsSection::Appearance, "receipts")); |
| 402 | assert!(matches(SettingsSection::Appearance, " ")); |
| 403 | assert_eq!(first_match("noise", true), Some(SettingsSection::Devices)); |
| 404 | assert_eq!(first_match("claude", false), None); |
| 405 | assert_eq!(first_match("xyz", true), None); |
| 406 | } |
| 407 | |
| 408 | fn search_term_keys(section: SettingsSection) -> &'static [&'static str] { |
| 409 | match section { |
| 410 | SettingsSection::General => &[ |
| 411 | "settings.general.auto_start", |
| 412 | "settings.general.open_in_background", |
| 413 | "settings.general.show_in_tray", |
| 414 | "settings.general.close_to_tray", |
| 415 | "settings.general.up_arrow", |
| 416 | "settings.general.translate_into", |
| 417 | "settings.general.behavior_title", |
| 418 | "settings.general.never_translate", |
| 419 | ], |
| 420 | SettingsSection::Notifications => &[ |
| 421 | "settings.notifications.chat_messages", |
| 422 | "settings.notifications.meeting_chats", |
| 423 | "settings.notifications.likes_reactions", |
| 424 | "settings.notifications.teams_channels", |
| 425 | "settings.notifications.show_preview", |
| 426 | "settings.notifications.notification_sound", |
| 427 | "settings.notifications.urgent_sound", |
| 428 | "settings.notifications.position", |
| 429 | "settings.notifications.flash_taskbar", |
| 430 | "settings.notifications.hide_in_calls", |
| 431 | "settings.notifications.send_test", |
| 432 | ], |
| 433 | SettingsSection::Appearance => &[ |
| 434 | "settings.appearance.theme", |
| 435 | "settings.appearance.color_scheme", |
| 436 | "settings.appearance.app_icon", |
| 437 | ], |
| 438 | SettingsSection::Chats => &[ |
| 439 | "settings.chats.density", |
| 440 | "settings.chats.read_receipts", |
| 441 | "settings.chats.message_actions", |
| 442 | "settings.chats.muted_section", |
| 443 | "settings.chats.enter_to", |
| 444 | "settings.chats.markdown", |
| 445 | ], |
| 446 | SettingsSection::Calendar => &super::CALENDAR_VIEW_TERMS, |
| 447 | SettingsSection::CalendarAccounts => &super::CALENDAR_ACCOUNTS_TERMS, |
| 448 | _ => &[], |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn every_search_term_names_a_setting_on_its_page() { |
| 454 | let _guard = crate::i18n::locale_test_lock(); |
| 455 | apply_locale("en"); |
| 456 | let calendar_accounts_source = concat!( |
| 457 | include_str!("calendar.rs"), |
| 458 | "\n", |
| 459 | include_str!("caldav_form.rs"), |
| 460 | "\n", |
| 461 | include_str!("google_client_form.rs"), |
| 462 | ); |
| 463 | let pages = [ |
| 464 | (SettingsSection::General, include_str!("general.rs")), |
| 465 | ( |
| 466 | SettingsSection::Notifications, |
| 467 | include_str!("notifications.rs"), |
| 468 | ), |
| 469 | (SettingsSection::Appearance, include_str!("appearance.rs")), |
| 470 | (SettingsSection::Devices, include_str!("devices.rs")), |
| 471 | (SettingsSection::Calendar, include_str!("calendar.rs")), |
| 472 | (SettingsSection::CalendarAccounts, calendar_accounts_source), |
| 473 | ]; |
| 474 | for (section, source) in pages { |
| 475 | for key in search_term_keys(section) { |
| 476 | assert!( |
| 477 | source.contains(key), |
| 478 | "{key} is not wired on {}", |
| 479 | label(section) |
| 480 | ); |
| 481 | } |
| 482 | if search_term_keys(section).is_empty() { |
| 483 | for term in search_terms(section) { |
| 484 | assert!( |
| 485 | source.contains(&term), |
| 486 | "{term} is not on {}", |
| 487 | label(section) |
| 488 | ); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |