| 1 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use crate::settings_section::SettingsSection; |
| 8 | use crate::theme; |
| 9 | use crate::views::widgets::icon; |
| 10 | |
| 11 | pub const TEAMS_GROUP: [SettingsSection; 6] = [ |
| 12 | SettingsSection::General, |
| 13 | SettingsSection::Notifications, |
| 14 | SettingsSection::Appearance, |
| 15 | SettingsSection::Chats, |
| 16 | SettingsSection::Calls, |
| 17 | SettingsSection::Devices, |
| 18 | ]; |
| 19 | pub const RUSTY_GROUP: [SettingsSection; 2] = [SettingsSection::Agents, SettingsSection::Mcp]; |
| 20 | const ITEM_MIN_HEIGHT: f32 = 32.; |
| 21 | const ITEM_PADDING_Y: f32 = 6.; |
| 22 | const ITEM_LINE_HEIGHT: f32 = 20.; |
| 23 | const ITEM_GAP: f32 = 2.; |
| 24 | const ITEM_RADIUS: f32 = 6.; |
| 25 | const ITEM_ICON: f32 = 16.; |
| 26 | const NAV_PADDING_X: f32 = 12.; |
| 27 | const NO_RESULTS: &str = "No settings found"; |
| 28 | |
| 29 | fn search_terms(section: SettingsSection) -> &'static [&'static str] { |
| 30 | match section { |
| 31 | SettingsSection::General => &[ |
| 32 | "Auto-start Teams", |
| 33 | "Open application in background", |
| 34 | "On close, keep the application running", |
| 35 | "Up arrow key behaviour in compose box", |
| 36 | "Translate messages into this language", |
| 37 | "How to handle messages in other languages", |
| 38 | "Never translate messages in these languages", |
| 39 | ], |
| 40 | SettingsSection::Notifications => &[ |
| 41 | "Chat message notifications", |
| 42 | "Meeting chat notifications", |
| 43 | "Likes and reactions", |
| 44 | "Teams and channels", |
| 45 | "Show message preview", |
| 46 | "Notification sound", |
| 47 | "Urgent sound", |
| 48 | "Position on the screen", |
| 49 | "Flash taskbar when notifications are received", |
| 50 | "Hide notifications during calls and meetings", |
| 51 | "Send test notification", |
| 52 | ], |
| 53 | SettingsSection::Appearance => &["Theme", "Color scheme", "App icon"], |
| 54 | SettingsSection::Chats => &[ |
| 55 | "Message density", |
| 56 | "Read receipts", |
| 57 | "Show message actions on right click", |
| 58 | "Show muted items in their own section", |
| 59 | "When writing a message, press Enter to", |
| 60 | "Format messages using mark-down", |
| 61 | ], |
| 62 | SettingsSection::Calls => &["Ringtones"], |
| 63 | SettingsSection::Devices => &[ |
| 64 | "Speaker", |
| 65 | "Microphone", |
| 66 | "Noise suppression", |
| 67 | "Music mode", |
| 68 | "Camera", |
| 69 | "Background", |
| 70 | "Soft focus", |
| 71 | "Adjust brightness", |
| 72 | "Mirror my video", |
| 73 | "Make a test call", |
| 74 | "Save a test report after each call", |
| 75 | ], |
| 76 | SettingsSection::Agents => &["Claude", "Agent runs"], |
| 77 | SettingsSection::Mcp => &["Model Context Protocol", "Conversation access"], |
| 78 | SettingsSection::About => &["Version", "Build"], |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | pub fn matches(section: SettingsSection, query: &str) -> bool { |
| 83 | let query = query.trim().to_lowercase(); |
| 84 | query.is_empty() |
| 85 | || std::iter::once(label(section)) |
| 86 | .chain(search_terms(section).iter().copied()) |
| 87 | .any(|text| text.to_lowercase().contains(&query)) |
| 88 | } |
| 89 | |
| 90 | pub fn first_match(query: &str, agents_available: bool) -> Option<SettingsSection> { |
| 91 | TEAMS_GROUP |
| 92 | .into_iter() |
| 93 | .chain(RUSTY_GROUP) |
| 94 | .chain([SettingsSection::About]) |
| 95 | .filter(|section| agents_available || *section != SettingsSection::Agents) |
| 96 | .find(|section| matches(*section, query)) |
| 97 | } |
| 98 | |
| 99 | pub fn label(section: SettingsSection) -> &'static str { |
| 100 | match section { |
| 101 | SettingsSection::General => "General", |
| 102 | SettingsSection::Notifications => "Notifications and activity", |
| 103 | SettingsSection::Appearance => "Appearance and accessibility", |
| 104 | SettingsSection::Chats => "Chats and channels", |
| 105 | SettingsSection::Calls => "Calls", |
| 106 | SettingsSection::Devices => "Devices", |
| 107 | SettingsSection::Agents => "Agents", |
| 108 | SettingsSection::Mcp => "MCP", |
| 109 | SettingsSection::About => "About", |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn icon_name(section: SettingsSection) -> IconName { |
| 114 | match section { |
| 115 | SettingsSection::General => IconName::SlidersHorizontal, |
| 116 | SettingsSection::Notifications => IconName::Bell, |
| 117 | SettingsSection::Appearance => IconName::Palette, |
| 118 | SettingsSection::Chats => IconName::MessageSquare, |
| 119 | SettingsSection::Calls => IconName::Phone, |
| 120 | SettingsSection::Devices => IconName::Headphones, |
| 121 | SettingsSection::Agents => IconName::Bot, |
| 122 | SettingsSection::Mcp => IconName::Plug, |
| 123 | SettingsSection::About => IconName::Info, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn item_selector(section: SettingsSection) -> String { |
| 128 | format!("settings/nav/item/{}", element_id(section)) |
| 129 | } |
| 130 | |
| 131 | pub fn label_selector(section: SettingsSection) -> String { |
| 132 | format!("{}/label", item_selector(section)) |
| 133 | } |
| 134 | |
| 135 | fn element_id(section: SettingsSection) -> &'static str { |
| 136 | match section { |
| 137 | SettingsSection::General => "settings-nav-general", |
| 138 | SettingsSection::Notifications => "settings-nav-notifications", |
| 139 | SettingsSection::Appearance => "settings-nav-appearance", |
| 140 | SettingsSection::Chats => "settings-nav-chats", |
| 141 | SettingsSection::Calls => "settings-nav-calls", |
| 142 | SettingsSection::Devices => "settings-nav-devices", |
| 143 | SettingsSection::Agents => "settings-nav-agents", |
| 144 | SettingsSection::Mcp => "settings-nav-mcp", |
| 145 | SettingsSection::About => "settings-nav-about", |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | pub struct NavLayout { |
| 150 | pub width: f32, |
| 151 | pub padding_y: f32, |
| 152 | } |
| 153 | |
| 154 | pub struct NavSearch<'a> { |
| 155 | pub input: AnyElement, |
| 156 | pub query: &'a str, |
| 157 | } |
| 158 | |
| 159 | pub fn render_nav( |
| 160 | active: SettingsSection, |
| 161 | agents_available: bool, |
| 162 | layout: NavLayout, |
| 163 | search: NavSearch<'_>, |
| 164 | on_select: impl Fn(SettingsSection, &mut Window, &mut App) + Clone + 'static, |
| 165 | ) -> Div { |
| 166 | let item = |section: SettingsSection| nav_item(section, section == active, on_select.clone()); |
| 167 | let query = search.query; |
| 168 | let teams: Vec<Stateful<Div>> = TEAMS_GROUP |
| 169 | .into_iter() |
| 170 | .filter(|section| matches(*section, query)) |
| 171 | .map(item) |
| 172 | .collect(); |
| 173 | let rusty: Vec<Stateful<Div>> = RUSTY_GROUP |
| 174 | .into_iter() |
| 175 | .filter(|section| agents_available || *section != SettingsSection::Agents) |
| 176 | .filter(|section| matches(*section, query)) |
| 177 | .map(item) |
| 178 | .collect(); |
| 179 | let about_shown = matches(SettingsSection::About, query); |
| 180 | let nothing_found = teams.is_empty() && rusty.is_empty() && !about_shown; |
| 181 | let group = |items: Vec<Stateful<Div>>| { |
| 182 | v_flex() |
| 183 | .w_full() |
| 184 | .px(px(NAV_PADDING_X)) |
| 185 | .gap(px(ITEM_GAP)) |
| 186 | .children(items) |
| 187 | }; |
| 188 | v_flex() |
| 189 | .debug_selector(|| "settings/nav".into()) |
| 190 | .w(px(layout.width)) |
| 191 | .h_full() |
| 192 | .flex_none() |
| 193 | .pt(px(layout.padding_y)) |
| 194 | .pb(px(NAV_PADDING_X)) |
| 195 | .gap(px(ITEM_GAP)) |
| 196 | .rounded_l(px(super::dialog::PANEL_RADIUS)) |
| 197 | .bg(theme::surface_raised()) |
| 198 | .child( |
| 199 | div() |
| 200 | .mx(px(2. * NAV_PADDING_X)) |
| 201 | .mb(px(18.)) |
| 202 | .text_size(px(20.)) |
| 203 | .line_height(px(26.)) |
| 204 | .font_weight(FontWeight::SEMIBOLD) |
| 205 | .text_color(theme::text()) |
| 206 | .child("Settings"), |
| 207 | ) |
| 208 | .child( |
| 209 | div() |
| 210 | .debug_selector(|| "settings/nav/search".into()) |
| 211 | .w_full() |
| 212 | .px(px(NAV_PADDING_X)) |
| 213 | .pb(px(10.)) |
| 214 | .child(search.input), |
| 215 | ) |
| 216 | .child( |
| 217 | v_flex() |
| 218 | .id("settings-nav-groups") |
| 219 | .w_full() |
| 220 | .flex_1() |
| 221 | .min_h(px(0.)) |
| 222 | .overflow_y_scrollbar() |
| 223 | .gap(px(ITEM_GAP)) |
| 224 | .child(group(teams)) |
| 225 | .when(nothing_found, |groups| { |
| 226 | groups.child( |
| 227 | div() |
| 228 | .px(px(2. * NAV_PADDING_X)) |
| 229 | .py(px(ITEM_PADDING_Y)) |
| 230 | .text_size(px(13.)) |
| 231 | .text_color(theme::text_muted()) |
| 232 | .child(NO_RESULTS), |
| 233 | ) |
| 234 | }) |
| 235 | .when(!rusty.is_empty(), |groups| groups.child( |
| 236 | div() |
| 237 | .debug_selector(|| "settings/nav/divider".into()) |
| 238 | .w_full() |
| 239 | .h(px(1.)) |
| 240 | .flex_none() |
| 241 | .mt(px(10.)) |
| 242 | .bg(theme::border_strong()), |
| 243 | ) |
| 244 | .child( |
| 245 | div() |
| 246 | .px(px(2. * NAV_PADDING_X)) |
| 247 | .pt(px(12.)) |
| 248 | .pb(px(6.)) |
| 249 | .text_size(px(12.)) |
| 250 | .font_weight(FontWeight::SEMIBOLD) |
| 251 | .text_color(theme::text_muted()) |
| 252 | .child("RUSTY TEAMS"), |
| 253 | ).child(group(rusty))), |
| 254 | ) |
| 255 | .when(about_shown, |nav| { |
| 256 | nav.child( |
| 257 | div().w_full().px(px(NAV_PADDING_X)).child( |
| 258 | item(SettingsSection::About).debug_selector(|| "settings/nav/about".into()), |
| 259 | ), |
| 260 | ) |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | fn nav_item( |
| 265 | section: SettingsSection, |
| 266 | active: bool, |
| 267 | on_select: impl Fn(SettingsSection, &mut Window, &mut App) + 'static, |
| 268 | ) -> Stateful<Div> { |
| 269 | h_flex() |
| 270 | .id(element_id(section)) |
| 271 | .debug_selector(move || item_selector(section)) |
| 272 | .w_full() |
| 273 | .min_h(px(ITEM_MIN_HEIGHT)) |
| 274 | .flex_none() |
| 275 | .px(px(12.)) |
| 276 | .py(px(ITEM_PADDING_Y)) |
| 277 | .gap(px(12.)) |
| 278 | .items_center() |
| 279 | .rounded(px(ITEM_RADIUS)) |
| 280 | .cursor_pointer() |
| 281 | .text_size(px(14.)) |
| 282 | .line_height(px(ITEM_LINE_HEIGHT)) |
| 283 | .when(active, |item| { |
| 284 | item.bg(theme::popup_hover()).text_color(theme::text()) |
| 285 | }) |
| 286 | .when(!active, |item| { |
| 287 | item.text_color(theme::text_strong()) |
| 288 | .hover(|item| item.bg(theme::popup())) |
| 289 | }) |
| 290 | .child(icon( |
| 291 | icon_name(section), |
| 292 | ITEM_ICON, |
| 293 | if active { |
| 294 | theme::accent_text() |
| 295 | } else { |
| 296 | theme::text_muted() |
| 297 | }, |
| 298 | )) |
| 299 | .child( |
| 300 | div() |
| 301 | .debug_selector(move || label_selector(section)) |
| 302 | .flex_1() |
| 303 | .min_w_0() |
| 304 | .child(label(section)), |
| 305 | ) |
| 306 | .on_click(move |_, window, cx| on_select(section, window, cx)) |
| 307 | } |
| 308 | |
| 309 | #[cfg(test)] |
| 310 | mod tests { |
| 311 | use super::{SettingsSection, first_match, label, matches, search_terms}; |
| 312 | |
| 313 | #[test] |
| 314 | fn search_finds_sections_by_name_and_by_setting() { |
| 315 | assert!(matches(SettingsSection::Chats, "read receipts")); |
| 316 | assert!(matches(SettingsSection::Devices, "MICRO")); |
| 317 | assert!(matches(SettingsSection::Calls, "calls")); |
| 318 | assert!(!matches(SettingsSection::Appearance, "receipts")); |
| 319 | assert!(matches(SettingsSection::Appearance, " ")); |
| 320 | assert_eq!(first_match("noise", true), Some(SettingsSection::Devices)); |
| 321 | assert_eq!(first_match("claude", false), None); |
| 322 | assert_eq!(first_match("xyz", true), None); |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn every_search_term_names_a_setting_on_its_page() { |
| 327 | let pages = [ |
| 328 | (SettingsSection::General, include_str!("general.rs")), |
| 329 | (SettingsSection::Notifications, include_str!("notifications.rs")), |
| 330 | (SettingsSection::Appearance, include_str!("appearance.rs")), |
| 331 | (SettingsSection::Chats, include_str!("chats.rs")), |
| 332 | (SettingsSection::Devices, include_str!("devices.rs")), |
| 333 | ]; |
| 334 | for (section, source) in pages { |
| 335 | for term in search_terms(section) { |
| 336 | assert!(source.contains(term), "{term} is not on {}", label(section)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |