BlueGit

ict rusty-teams

Files

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

nav.rs

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