BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / nav.rs

nav.rs

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