BlueGit

ict rusty-teams

Files

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

nav.rs

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