BlueGit

ict rusty-teams

Files

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

nav.rs

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