BlueGit

ict rusty-teams

Files

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

nav.rs

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