BlueGit

ict rusty-teams

Files

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

nav.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 3 weeks ago
a6592765 History
nav.rs 12.0 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::settings_section::SettingsSection;
8use crate::theme;
9use crate::views::widgets::icon;
10
11pub const TEAMS_GROUP: [SettingsSection; 6] = [
12 SettingsSection::General,
13 SettingsSection::Notifications,
14 SettingsSection::Appearance,
15 SettingsSection::Chats,
16 SettingsSection::Calls,
17 SettingsSection::Devices,
18];
19pub const RUSTY_GROUP: [SettingsSection; 2] = [SettingsSection::Agents, SettingsSection::Mcp];
20const ITEM_MIN_HEIGHT: f32 = 32.;
21const ITEM_PADDING_Y: f32 = 6.;
22const ITEM_LINE_HEIGHT: f32 = 20.;
23const ITEM_GAP: f32 = 2.;
24const ITEM_RADIUS: f32 = 6.;
25const ITEM_ICON: f32 = 16.;
26const NAV_PADDING_X: f32 = 12.;
27const NO_RESULTS: &str = "No settings found";
28
29fn search_terms(section: SettingsSection) -> &'static [&'static str] {
30 match section {
31 SettingsSection::General => &[
32 "Auto-start Teams",
33 "Open application in background",
34 "On close, keep the application running",
35 "Up arrow key behaviour in compose box",
36 "Translate messages into this language",
37 "How to handle messages in other languages",
38 "Never translate messages in these languages",
39 ],
40 SettingsSection::Notifications => &[
41 "Chat message notifications",
42 "Meeting chat notifications",
43 "Likes and reactions",
44 "Teams and channels",
45 "Show message preview",
46 "Notification sound",
47 "Urgent sound",
48 "Position on the screen",
49 "Flash taskbar when notifications are received",
50 "Hide notifications during calls and meetings",
51 "Send test notification",
52 ],
53 SettingsSection::Appearance => &["Theme", "Color scheme", "App icon"],
54 SettingsSection::Chats => &[
55 "Message density",
56 "Read receipts",
57 "Show message actions on right click",
58 "Show muted items in their own section",
59 "When writing a message, press Enter to",
60 "Format messages using mark-down",
61 ],
62 SettingsSection::Calls => &["Ringtones"],
63 SettingsSection::Devices => &[
64 "Speaker",
65 "Microphone",
66 "Noise suppression",
67 "Music mode",
68 "Camera",
69 "Background",
70 "Soft focus",
71 "Adjust brightness",
72 "Mirror my video",
73 "Make a test call",
74 "Save a test report after each call",
75 ],
76 SettingsSection::Agents => &["Claude", "Agent runs"],
77 SettingsSection::Mcp => &["Model Context Protocol", "Conversation access"],
78 SettingsSection::About => &["Version", "Build"],
79 }
80}
81
82pub fn matches(section: SettingsSection, query: &str) -> bool {
83 let query = query.trim().to_lowercase();
84 query.is_empty()
85 || std::iter::once(label(section))
86 .chain(search_terms(section).iter().copied())
87 .any(|text| text.to_lowercase().contains(&query))
88}
89
90pub fn first_match(query: &str, agents_available: bool) -> Option<SettingsSection> {
91 TEAMS_GROUP
92 .into_iter()
93 .chain(RUSTY_GROUP)
94 .chain([SettingsSection::About])
95 .filter(|section| agents_available || *section != SettingsSection::Agents)
96 .find(|section| matches(*section, query))
97}
98
99pub fn label(section: SettingsSection) -> &'static str {
100 match section {
101 SettingsSection::General => "General",
102 SettingsSection::Notifications => "Notifications and activity",
103 SettingsSection::Appearance => "Appearance and accessibility",
104 SettingsSection::Chats => "Chats and channels",
105 SettingsSection::Calls => "Calls",
106 SettingsSection::Devices => "Devices",
107 SettingsSection::Agents => "Agents",
108 SettingsSection::Mcp => "MCP",
109 SettingsSection::About => "About",
110 }
111}
112
113fn icon_name(section: SettingsSection) -> IconName {
114 match section {
115 SettingsSection::General => IconName::SlidersHorizontal,
116 SettingsSection::Notifications => IconName::Bell,
117 SettingsSection::Appearance => IconName::Palette,
118 SettingsSection::Chats => IconName::MessageSquare,
119 SettingsSection::Calls => IconName::Phone,
120 SettingsSection::Devices => IconName::Headphones,
121 SettingsSection::Agents => IconName::Bot,
122 SettingsSection::Mcp => IconName::Plug,
123 SettingsSection::About => IconName::Info,
124 }
125}
126
127pub fn item_selector(section: SettingsSection) -> String {
128 format!("settings/nav/item/{}", element_id(section))
129}
130
131pub fn label_selector(section: SettingsSection) -> String {
132 format!("{}/label", item_selector(section))
133}
134
135fn element_id(section: SettingsSection) -> &'static str {
136 match section {
137 SettingsSection::General => "settings-nav-general",
138 SettingsSection::Notifications => "settings-nav-notifications",
139 SettingsSection::Appearance => "settings-nav-appearance",
140 SettingsSection::Chats => "settings-nav-chats",
141 SettingsSection::Calls => "settings-nav-calls",
142 SettingsSection::Devices => "settings-nav-devices",
143 SettingsSection::Agents => "settings-nav-agents",
144 SettingsSection::Mcp => "settings-nav-mcp",
145 SettingsSection::About => "settings-nav-about",
146 }
147}
148
149pub struct NavLayout {
150 pub width: f32,
151 pub padding_y: f32,
152}
153
154pub struct NavSearch<'a> {
155 pub input: AnyElement,
156 pub query: &'a str,
157}
158
159pub fn render_nav(
160 active: SettingsSection,
161 agents_available: bool,
162 layout: NavLayout,
163 search: NavSearch<'_>,
164 on_select: impl Fn(SettingsSection, &mut Window, &mut App) + Clone + 'static,
165) -> Div {
166 let item = |section: SettingsSection| nav_item(section, section == active, on_select.clone());
167 let query = search.query;
168 let teams: Vec<Stateful<Div>> = TEAMS_GROUP
169 .into_iter()
170 .filter(|section| matches(*section, query))
171 .map(item)
172 .collect();
173 let rusty: Vec<Stateful<Div>> = RUSTY_GROUP
174 .into_iter()
175 .filter(|section| agents_available || *section != SettingsSection::Agents)
176 .filter(|section| matches(*section, query))
177 .map(item)
178 .collect();
179 let about_shown = matches(SettingsSection::About, query);
180 let nothing_found = teams.is_empty() && rusty.is_empty() && !about_shown;
181 let group = |items: Vec<Stateful<Div>>| {
182 v_flex()
183 .w_full()
184 .px(px(NAV_PADDING_X))
185 .gap(px(ITEM_GAP))
186 .children(items)
187 };
188 v_flex()
189 .debug_selector(|| "settings/nav".into())
190 .w(px(layout.width))
191 .h_full()
192 .flex_none()
193 .pt(px(layout.padding_y))
194 .pb(px(NAV_PADDING_X))
195 .gap(px(ITEM_GAP))
196 .rounded_l(px(super::dialog::PANEL_RADIUS))
197 .bg(theme::surface_raised())
198 .child(
199 div()
200 .mx(px(2. * NAV_PADDING_X))
201 .mb(px(18.))
202 .text_size(px(20.))
203 .line_height(px(26.))
204 .font_weight(FontWeight::SEMIBOLD)
205 .text_color(theme::text())
206 .child("Settings"),
207 )
208 .child(
209 div()
210 .debug_selector(|| "settings/nav/search".into())
211 .w_full()
212 .px(px(NAV_PADDING_X))
213 .pb(px(10.))
214 .child(search.input),
215 )
216 .child(
217 v_flex()
218 .id("settings-nav-groups")
219 .w_full()
220 .flex_1()
221 .min_h(px(0.))
222 .overflow_y_scrollbar()
223 .gap(px(ITEM_GAP))
224 .child(group(teams))
225 .when(nothing_found, |groups| {
226 groups.child(
227 div()
228 .px(px(2. * NAV_PADDING_X))
229 .py(px(ITEM_PADDING_Y))
230 .text_size(px(13.))
231 .text_color(theme::text_muted())
232 .child(NO_RESULTS),
233 )
234 })
235 .when(!rusty.is_empty(), |groups| {
236 groups
237 .child(
238 div()
239 .debug_selector(|| "settings/nav/divider".into())
240 .w_full()
241 .h(px(1.))
242 .flex_none()
243 .mt(px(10.))
244 .bg(theme::border_strong()),
245 )
246 .child(
247 div()
248 .px(px(2. * NAV_PADDING_X))
249 .pt(px(12.))
250 .pb(px(6.))
251 .text_size(px(12.))
252 .font_weight(FontWeight::SEMIBOLD)
253 .text_color(theme::text_muted())
254 .child("RUSTY TEAMS"),
255 )
256 .child(group(rusty))
257 }),
258 )
259 .when(about_shown, |nav| {
260 nav.child(
261 div().w_full().px(px(NAV_PADDING_X)).child(
262 item(SettingsSection::About).debug_selector(|| "settings/nav/about".into()),
263 ),
264 )
265 })
266}
267
268fn nav_item(
269 section: SettingsSection,
270 active: bool,
271 on_select: impl Fn(SettingsSection, &mut Window, &mut App) + 'static,
272) -> Stateful<Div> {
273 h_flex()
274 .id(element_id(section))
275 .debug_selector(move || item_selector(section))
276 .w_full()
277 .min_h(px(ITEM_MIN_HEIGHT))
278 .flex_none()
279 .px(px(12.))
280 .py(px(ITEM_PADDING_Y))
281 .gap(px(12.))
282 .items_center()
283 .rounded(px(ITEM_RADIUS))
284 .cursor_pointer()
285 .text_size(px(14.))
286 .line_height(px(ITEM_LINE_HEIGHT))
287 .when(active, |item| {
288 item.bg(theme::popup_hover()).text_color(theme::text())
289 })
290 .when(!active, |item| {
291 item.text_color(theme::text_strong())
292 .hover(|item| item.bg(theme::popup()))
293 })
294 .child(icon(
295 icon_name(section),
296 ITEM_ICON,
297 if active {
298 theme::accent_text()
299 } else {
300 theme::text_muted()
301 },
302 ))
303 .child(
304 div()
305 .debug_selector(move || label_selector(section))
306 .flex_1()
307 .min_w_0()
308 .child(label(section)),
309 )
310 .on_click(move |_, window, cx| on_select(section, window, cx))
311}
312
313#[cfg(test)]
314mod tests {
315 use super::{SettingsSection, first_match, label, matches, search_terms};
316
317 #[test]
318 fn search_finds_sections_by_name_and_by_setting() {
319 assert!(matches(SettingsSection::Chats, "read receipts"));
320 assert!(matches(SettingsSection::Devices, "MICRO"));
321 assert!(matches(SettingsSection::Calls, "calls"));
322 assert!(!matches(SettingsSection::Appearance, "receipts"));
323 assert!(matches(SettingsSection::Appearance, " "));
324 assert_eq!(first_match("noise", true), Some(SettingsSection::Devices));
325 assert_eq!(first_match("claude", false), None);
326 assert_eq!(first_match("xyz", true), None);
327 }
328
329 #[test]
330 fn every_search_term_names_a_setting_on_its_page() {
331 let pages = [
332 (SettingsSection::General, include_str!("general.rs")),
333 (
334 SettingsSection::Notifications,
335 include_str!("notifications.rs"),
336 ),
337 (SettingsSection::Appearance, include_str!("appearance.rs")),
338 (SettingsSection::Chats, include_str!("chats.rs")),
339 (SettingsSection::Devices, include_str!("devices.rs")),
340 ];
341 for (section, source) in pages {
342 for term in search_terms(section) {
343 assert!(source.contains(term), "{term} is not on {}", label(section));
344 }
345 }
346 }
347}