1//! # settings_ui
2mod components;
3mod page_data;
4
5use anyhow::Result;
6use editor::{Editor, EditorEvent};
7use feature_flags::FeatureFlag;
8use fuzzy::StringMatchCandidate;
9use gpui::{
10 Action, App, Div, Entity, FocusHandle, Focusable, FontWeight, Global, ReadGlobal as _,
11 ScrollHandle, Stateful, Subscription, Task, TitlebarOptions, UniformListScrollHandle, Window,
12 WindowBounds, WindowHandle, WindowOptions, actions, div, point, prelude::*, px, size,
13 uniform_list,
14};
15use heck::ToTitleCase as _;
16use project::WorktreeId;
17use schemars::JsonSchema;
18use serde::Deserialize;
19use settings::{
20 BottomDockLayout, CloseWindowWhenNoItems, CodeFade, CursorShape, OnLastWindowClosed,
21 RestoreOnStartupBehavior, SaturatingBool, SettingsContent, SettingsStore,
22};
23use std::{
24 any::{Any, TypeId, type_name},
25 cell::RefCell,
26 collections::HashMap,
27 num::{NonZero, NonZeroU32},
28 ops::Range,
29 rc::Rc,
30 sync::{Arc, LazyLock, RwLock},
31};
32use title_bar::platform_title_bar::PlatformTitleBar;
33use ui::{
34 ContextMenu, Divider, DividerColor, DropdownMenu, DropdownStyle, IconButtonShape, KeyBinding,
35 KeybindingHint, PopoverMenu, Switch, SwitchColor, Tooltip, TreeViewItem, WithScrollbar,
36 prelude::*,
37};
38use ui_input::{NumberField, NumberFieldType};
39use util::{ResultExt as _, paths::PathStyle, rel_path::RelPath};
40use workspace::{OpenOptions, OpenVisible, Workspace, client_side_decorations};
41use zed_actions::OpenSettings;
42
43use crate::components::SettingsEditor;
44
45const NAVBAR_CONTAINER_TAB_INDEX: isize = 0;
46const NAVBAR_GROUP_TAB_INDEX: isize = 1;
47
48const HEADER_CONTAINER_TAB_INDEX: isize = 2;
49const HEADER_GROUP_TAB_INDEX: isize = 3;
50
51const CONTENT_CONTAINER_TAB_INDEX: isize = 4;
52const CONTENT_GROUP_TAB_INDEX: isize = 5;
53
54actions!(
55 settings_editor,
56 [
57 /// Minimizes the settings UI window.
58 Minimize,
59 /// Toggles focus between the navbar and the main content.
60 ToggleFocusNav,
61 /// Expands the navigation entry.
62 ExpandNavEntry,
63 /// Collapses the navigation entry.
64 CollapseNavEntry,
65 /// Focuses the next file in the file list.
66 FocusNextFile,
67 /// Focuses the previous file in the file list.
68 FocusPreviousFile,
69 /// Opens an editor for the current file
70 OpenCurrentFile,
71 /// Focuses the previous root navigation entry.
72 FocusPreviousRootNavEntry,
73 /// Focuses the next root navigation entry.
74 FocusNextRootNavEntry,
75 /// Focuses the first navigation entry.
76 FocusFirstNavEntry,
77 /// Focuses the last navigation entry.
78 FocusLastNavEntry
79 ]
80);
81
82#[derive(Action, PartialEq, Eq, Clone, Copy, Debug, JsonSchema, Deserialize)]
83#[action(namespace = settings_editor)]
84struct FocusFile(pub u32);
85
86struct SettingField<T: 'static> {
87 pick: fn(&SettingsContent) -> &Option<T>,
88 pick_mut: fn(&mut SettingsContent) -> &mut Option<T>,
89}
90
91impl<T: 'static> Clone for SettingField<T> {
92 fn clone(&self) -> Self {
93 *self
94 }
95}
96
97// manual impl because derive puts a Copy bound on T, which is inaccurate in our case
98impl<T: 'static> Copy for SettingField<T> {}
99
100/// Helper for unimplemented settings, used in combination with `SettingField::unimplemented`
101/// to keep the setting around in the UI with valid pick and pick_mut implementations, but don't actually try to render it.
102/// TODO(settings_ui): In non-dev builds (`#[cfg(not(debug_assertions))]`) make this render as edit-in-json
103struct UnimplementedSettingField;
104
105impl<T: 'static> SettingField<T> {
106 /// Helper for settings with types that are not yet implemented.
107 #[allow(unused)]
108 fn unimplemented(self) -> SettingField<UnimplementedSettingField> {
109 SettingField {
110 pick: |_| &Some(UnimplementedSettingField),
111 pick_mut: |_| unreachable!(),
112 }
113 }
114}
115
116trait AnySettingField {
117 fn as_any(&self) -> &dyn Any;
118 fn type_name(&self) -> &'static str;
119 fn type_id(&self) -> TypeId;
120 // Returns the file this value was set in and true, or File::Default and false to indicate it was not found in any file (missing default)
121 fn file_set_in(&self, file: SettingsUiFile, cx: &App) -> (settings::SettingsFile, bool);
122}
123
124impl<T> AnySettingField for SettingField<T> {
125 fn as_any(&self) -> &dyn Any {
126 self
127 }
128
129 fn type_name(&self) -> &'static str {
130 type_name::<T>()
131 }
132
133 fn type_id(&self) -> TypeId {
134 TypeId::of::<T>()
135 }
136
137 fn file_set_in(&self, file: SettingsUiFile, cx: &App) -> (settings::SettingsFile, bool) {
138 let (file, value) = cx
139 .global::<SettingsStore>()
140 .get_value_from_file(file.to_settings(), self.pick);
141 return (file, value.is_some());
142 }
143}
144
145#[derive(Default, Clone)]
146struct SettingFieldRenderer {
147 renderers: Rc<
148 RefCell<
149 HashMap<
150 TypeId,
151 Box<
152 dyn Fn(
153 &SettingsWindow,
154 &SettingItem,
155 SettingsUiFile,
156 Option<&SettingsFieldMetadata>,
157 &mut Window,
158 &mut Context<SettingsWindow>,
159 ) -> Stateful<Div>,
160 >,
161 >,
162 >,
163 >,
164}
165
166impl Global for SettingFieldRenderer {}
167
168impl SettingFieldRenderer {
169 fn add_basic_renderer<T: 'static>(
170 &mut self,
171 render_control: impl Fn(
172 SettingField<T>,
173 SettingsUiFile,
174 Option<&SettingsFieldMetadata>,
175 &mut Window,
176 &mut App,
177 ) -> AnyElement
178 + 'static,
179 ) -> &mut Self {
180 self.add_renderer(
181 move |settings_window: &SettingsWindow,
182 item: &SettingItem,
183 field: SettingField<T>,
184 settings_file: SettingsUiFile,
185 metadata: Option<&SettingsFieldMetadata>,
186 window: &mut Window,
187 cx: &mut Context<SettingsWindow>| {
188 render_settings_item(
189 settings_window,
190 item,
191 settings_file.clone(),
192 render_control(field, settings_file, metadata, window, cx),
193 window,
194 cx,
195 )
196 },
197 )
198 }
199
200 fn add_renderer<T: 'static>(
201 &mut self,
202 renderer: impl Fn(
203 &SettingsWindow,
204 &SettingItem,
205 SettingField<T>,
206 SettingsUiFile,
207 Option<&SettingsFieldMetadata>,
208 &mut Window,
209 &mut Context<SettingsWindow>,
210 ) -> Stateful<Div>
211 + 'static,
212 ) -> &mut Self {
213 let key = TypeId::of::<T>();
214 let renderer = Box::new(
215 move |settings_window: &SettingsWindow,
216 item: &SettingItem,
217 settings_file: SettingsUiFile,
218 metadata: Option<&SettingsFieldMetadata>,
219 window: &mut Window,
220 cx: &mut Context<SettingsWindow>| {
221 let field = *item
222 .field
223 .as_ref()
224 .as_any()
225 .downcast_ref::<SettingField<T>>()
226 .unwrap();
227 renderer(
228 settings_window,
229 item,
230 field,
231 settings_file,
232 metadata,
233 window,
234 cx,
235 )
236 },
237 );
238 self.renderers.borrow_mut().insert(key, renderer);
239 self
240 }
241}
242
243struct NonFocusableHandle {
244 handle: FocusHandle,
245 _subscription: Subscription,
246}
247
248impl NonFocusableHandle {
249 fn new(tab_index: isize, tab_stop: bool, window: &mut Window, cx: &mut App) -> Entity<Self> {
250 let handle = cx.focus_handle().tab_index(tab_index).tab_stop(tab_stop);
251 Self::from_handle(handle, window, cx)
252 }
253
254 fn from_handle(handle: FocusHandle, window: &mut Window, cx: &mut App) -> Entity<Self> {
255 cx.new(|cx| {
256 let _subscription = cx.on_focus(&handle, window, {
257 move |_, window, _| {
258 window.focus_next();
259 }
260 });
261 Self {
262 handle,
263 _subscription,
264 }
265 })
266 }
267}
268
269impl Focusable for NonFocusableHandle {
270 fn focus_handle(&self, _: &App) -> FocusHandle {
271 self.handle.clone()
272 }
273}
274
275struct SettingsFieldMetadata {
276 placeholder: Option<&'static str>,
277}
278
279pub struct SettingsUiFeatureFlag;
280
281impl FeatureFlag for SettingsUiFeatureFlag {
282 const NAME: &'static str = "settings-ui";
283}
284
285pub fn init(cx: &mut App) {
286 init_renderers(cx);
287
288 cx.observe_new(|workspace: &mut workspace::Workspace, _, _| {
289 workspace.register_action(|workspace, _: &OpenSettings, window, cx| {
290 let window_handle = window
291 .window_handle()
292 .downcast::<Workspace>()
293 .expect("Workspaces are root Windows");
294 open_settings_editor(workspace, window_handle, cx);
295 });
296 })
297 .detach();
298}
299
300fn init_renderers(cx: &mut App) {
301 cx.default_global::<SettingFieldRenderer>()
302 .add_basic_renderer::<UnimplementedSettingField>(|_, _, _, _, _| {
303 Button::new("open-in-settings-file", "Edit in settings.json")
304 .style(ButtonStyle::Outlined)
305 .size(ButtonSize::Medium)
306 .tab_index(0_isize)
307 .on_click(|_, window, cx| {
308 window.dispatch_action(Box::new(OpenCurrentFile), cx);
309 })
310 .into_any_element()
311 })
312 .add_basic_renderer::<bool>(render_toggle_button)
313 .add_basic_renderer::<String>(render_text_field)
314 .add_basic_renderer::<SaturatingBool>(render_toggle_button)
315 .add_basic_renderer::<CursorShape>(render_dropdown)
316 .add_basic_renderer::<RestoreOnStartupBehavior>(render_dropdown)
317 .add_basic_renderer::<BottomDockLayout>(render_dropdown)
318 .add_basic_renderer::<OnLastWindowClosed>(render_dropdown)
319 .add_basic_renderer::<CloseWindowWhenNoItems>(render_dropdown)
320 .add_basic_renderer::<settings::FontFamilyName>(render_font_picker)
321 // todo(settings_ui): This needs custom ui
322 // .add_renderer::<settings::BufferLineHeight>(|settings_field, file, _, window, cx| {
323 // // todo(settings_ui): Do we want to expose the custom variant of buffer line height?
324 // // right now there's a manual impl of strum::VariantArray
325 // render_dropdown(*settings_field, file, window, cx)
326 // })
327 .add_basic_renderer::<settings::BaseKeymapContent>(render_dropdown)
328 .add_basic_renderer::<settings::MultiCursorModifier>(render_dropdown)
329 .add_basic_renderer::<settings::HideMouseMode>(render_dropdown)
330 .add_basic_renderer::<settings::CurrentLineHighlight>(render_dropdown)
331 .add_basic_renderer::<settings::ShowWhitespaceSetting>(render_dropdown)
332 .add_basic_renderer::<settings::SoftWrap>(render_dropdown)
333 .add_basic_renderer::<settings::ScrollBeyondLastLine>(render_dropdown)
334 .add_basic_renderer::<settings::SnippetSortOrder>(render_dropdown)
335 .add_basic_renderer::<settings::ClosePosition>(render_dropdown)
336 .add_basic_renderer::<settings::DockSide>(render_dropdown)
337 .add_basic_renderer::<settings::TerminalDockPosition>(render_dropdown)
338 .add_basic_renderer::<settings::DockPosition>(render_dropdown)
339 .add_basic_renderer::<settings::GitGutterSetting>(render_dropdown)
340 .add_basic_renderer::<settings::GitHunkStyleSetting>(render_dropdown)
341 .add_basic_renderer::<settings::DiagnosticSeverityContent>(render_dropdown)
342 .add_basic_renderer::<settings::SeedQuerySetting>(render_dropdown)
343 .add_basic_renderer::<settings::DoubleClickInMultibuffer>(render_dropdown)
344 .add_basic_renderer::<settings::GoToDefinitionFallback>(render_dropdown)
345 .add_basic_renderer::<settings::ActivateOnClose>(render_dropdown)
346 .add_basic_renderer::<settings::ShowDiagnostics>(render_dropdown)
347 .add_basic_renderer::<settings::ShowCloseButton>(render_dropdown)
348 .add_basic_renderer::<settings::ProjectPanelEntrySpacing>(render_dropdown)
349 .add_basic_renderer::<settings::RewrapBehavior>(render_dropdown)
350 .add_basic_renderer::<settings::FormatOnSave>(render_dropdown)
351 .add_basic_renderer::<settings::IndentGuideColoring>(render_dropdown)
352 .add_basic_renderer::<settings::IndentGuideBackgroundColoring>(render_dropdown)
353 .add_basic_renderer::<settings::FileFinderWidthContent>(render_dropdown)
354 .add_basic_renderer::<settings::ShowDiagnostics>(render_dropdown)
355 .add_basic_renderer::<settings::WordsCompletionMode>(render_dropdown)
356 .add_basic_renderer::<settings::LspInsertMode>(render_dropdown)
357 .add_basic_renderer::<settings::AlternateScroll>(render_dropdown)
358 .add_basic_renderer::<settings::TerminalBlink>(render_dropdown)
359 .add_basic_renderer::<settings::CursorShapeContent>(render_dropdown)
360 .add_basic_renderer::<f32>(render_number_field)
361 .add_basic_renderer::<u32>(render_number_field)
362 .add_basic_renderer::<u64>(render_number_field)
363 .add_basic_renderer::<usize>(render_number_field)
364 .add_basic_renderer::<NonZero<usize>>(render_number_field)
365 .add_basic_renderer::<NonZeroU32>(render_number_field)
366 .add_basic_renderer::<CodeFade>(render_number_field)
367 .add_basic_renderer::<FontWeight>(render_number_field)
368 .add_basic_renderer::<settings::MinimumContrast>(render_number_field)
369 .add_basic_renderer::<settings::ShowScrollbar>(render_dropdown)
370 .add_basic_renderer::<settings::ScrollbarDiagnostics>(render_dropdown)
371 .add_basic_renderer::<settings::ShowMinimap>(render_dropdown)
372 .add_basic_renderer::<settings::DisplayIn>(render_dropdown)
373 .add_basic_renderer::<settings::MinimapThumb>(render_dropdown)
374 .add_basic_renderer::<settings::MinimapThumbBorder>(render_dropdown)
375 .add_basic_renderer::<settings::SteppingGranularity>(render_dropdown);
376 // .add_renderer::<ThemeSelection>(|settings_field, file, _, window, cx| {
377 // render_dropdown(*settings_field, file, window, cx)
378 // });
379}
380
381pub fn open_settings_editor(
382 _workspace: &mut Workspace,
383 workspace_handle: WindowHandle<Workspace>,
384 cx: &mut App,
385) {
386 let existing_window = cx
387 .windows()
388 .into_iter()
389 .find_map(|window| window.downcast::<SettingsWindow>());
390
391 if let Some(existing_window) = existing_window {
392 existing_window
393 .update(cx, |settings_window, window, _| {
394 settings_window.original_window = Some(workspace_handle);
395 window.activate_window();
396 })
397 .ok();
398 return;
399 }
400
401 // We have to defer this to get the workspace off the stack.
402
403 cx.defer(move |cx| {
404 cx.open_window(
405 WindowOptions {
406 titlebar: Some(TitlebarOptions {
407 title: Some("Settings Window".into()),
408 appears_transparent: true,
409 traffic_light_position: Some(point(px(12.0), px(12.0))),
410 }),
411 focus: true,
412 show: true,
413 is_movable: true,
414 kind: gpui::WindowKind::Floating,
415 window_background: cx.theme().window_background_appearance(),
416 window_min_size: Some(size(px(900.), px(750.))), // 4:3 Aspect Ratio
417 window_bounds: Some(WindowBounds::centered(size(px(900.), px(750.)), cx)),
418 ..Default::default()
419 },
420 |window, cx| cx.new(|cx| SettingsWindow::new(Some(workspace_handle), window, cx)),
421 )
422 .log_err();
423 });
424}
425
426/// The current sub page path that is selected.
427/// If this is empty the selected page is rendered,
428/// otherwise the last sub page gets rendered.
429///
430/// Global so that `pick` and `pick_mut` callbacks can access it
431/// and use it to dynamically render sub pages (e.g. for language settings)
432static SUB_PAGE_STACK: LazyLock<RwLock<Vec<SubPage>>> = LazyLock::new(|| RwLock::new(Vec::new()));
433
434fn sub_page_stack() -> std::sync::RwLockReadGuard<'static, Vec<SubPage>> {
435 SUB_PAGE_STACK
436 .read()
437 .expect("SUB_PAGE_STACK is never poisoned")
438}
439
440fn sub_page_stack_mut() -> std::sync::RwLockWriteGuard<'static, Vec<SubPage>> {
441 SUB_PAGE_STACK
442 .write()
443 .expect("SUB_PAGE_STACK is never poisoned")
444}
445
446pub struct SettingsWindow {
447 title_bar: Option<Entity<PlatformTitleBar>>,
448 original_window: Option<WindowHandle<Workspace>>,
449 files: Vec<(SettingsUiFile, FocusHandle)>,
450 worktree_root_dirs: HashMap<WorktreeId, String>,
451 current_file: SettingsUiFile,
452 pages: Vec<SettingsPage>,
453 search_bar: Entity<Editor>,
454 search_task: Option<Task<()>>,
455 /// Index into navbar_entries
456 navbar_entry: usize,
457 navbar_entries: Vec<NavBarEntry>,
458 navbar_scroll_handle: UniformListScrollHandle,
459 search_matches: Vec<Vec<bool>>,
460 content_handles: Vec<Vec<Entity<NonFocusableHandle>>>,
461 page_scroll_handle: ScrollHandle,
462 focus_handle: FocusHandle,
463 navbar_focus_handle: Entity<NonFocusableHandle>,
464 content_focus_handle: Entity<NonFocusableHandle>,
465 files_focus_handle: FocusHandle,
466 search_index: Option<Arc<SearchIndex>>,
467}
468
469struct SearchIndex {
470 bm25_engine: bm25::SearchEngine<usize>,
471 fuzzy_match_candidates: Vec<StringMatchCandidate>,
472 key_lut: Vec<SearchItemKey>,
473}
474
475struct SearchItemKey {
476 page_index: usize,
477 header_index: usize,
478 item_index: usize,
479}
480
481struct SubPage {
482 link: SubPageLink,
483 section_header: &'static str,
484}
485
486#[derive(Debug)]
487struct NavBarEntry {
488 title: &'static str,
489 is_root: bool,
490 expanded: bool,
491 page_index: usize,
492 item_index: Option<usize>,
493 focus_handle: FocusHandle,
494}
495
496struct SettingsPage {
497 title: &'static str,
498 items: Vec<SettingsPageItem>,
499}
500
501#[derive(PartialEq)]
502enum SettingsPageItem {
503 SectionHeader(&'static str),
504 SettingItem(SettingItem),
505 SubPageLink(SubPageLink),
506}
507
508impl std::fmt::Debug for SettingsPageItem {
509 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510 match self {
511 SettingsPageItem::SectionHeader(header) => write!(f, "SectionHeader({})", header),
512 SettingsPageItem::SettingItem(setting_item) => {
513 write!(f, "SettingItem({})", setting_item.title)
514 }
515 SettingsPageItem::SubPageLink(sub_page_link) => {
516 write!(f, "SubPageLink({})", sub_page_link.title)
517 }
518 }
519 }
520}
521
522impl SettingsPageItem {
523 fn render(
524 &self,
525 settings_window: &SettingsWindow,
526 section_header: &'static str,
527 is_last: bool,
528 window: &mut Window,
529 cx: &mut Context<SettingsWindow>,
530 ) -> AnyElement {
531 let file = settings_window.current_file.clone();
532 match self {
533 SettingsPageItem::SectionHeader(header) => v_flex()
534 .w_full()
535 .gap_1p5()
536 .child(
537 Label::new(SharedString::new_static(header))
538 .size(LabelSize::Small)
539 .color(Color::Muted)
540 .buffer_font(cx),
541 )
542 .child(Divider::horizontal().color(DividerColor::BorderFaded))
543 .into_any_element(),
544 SettingsPageItem::SettingItem(setting_item) => {
545 let renderer = cx.default_global::<SettingFieldRenderer>().clone();
546 let (_, found) = setting_item.field.file_set_in(file.clone(), cx);
547
548 let renderers = renderer.renderers.borrow();
549 let field_renderer =
550 renderers.get(&AnySettingField::type_id(setting_item.field.as_ref()));
551 let field_renderer_or_warning =
552 field_renderer.ok_or("NO RENDERER").and_then(|renderer| {
553 if cfg!(debug_assertions) && !found {
554 Err("NO DEFAULT")
555 } else {
556 Ok(renderer)
557 }
558 });
559
560 let field = match field_renderer_or_warning {
561 Ok(field_renderer) => field_renderer(
562 settings_window,
563 setting_item,
564 file,
565 setting_item.metadata.as_deref(),
566 window,
567 cx,
568 ),
569 Err(warning) => render_settings_item(
570 settings_window,
571 setting_item,
572 file,
573 Button::new("error-warning", warning)
574 .style(ButtonStyle::Outlined)
575 .size(ButtonSize::Medium)
576 .icon(Some(IconName::Debug))
577 .icon_position(IconPosition::Start)
578 .icon_color(Color::Error)
579 .tab_index(0_isize)
580 .tooltip(Tooltip::text(setting_item.field.type_name()))
581 .into_any_element(),
582 window,
583 cx,
584 ),
585 };
586
587 field
588 .pt_4()
589 .map(|this| {
590 if is_last {
591 this.pb_10()
592 } else {
593 this.pb_4()
594 .border_b_1()
595 .border_color(cx.theme().colors().border_variant)
596 }
597 })
598 .into_any_element()
599 }
600 SettingsPageItem::SubPageLink(sub_page_link) => h_flex()
601 .id(sub_page_link.title)
602 .w_full()
603 .min_w_0()
604 .gap_2()
605 .justify_between()
606 .pt_4()
607 .when(!is_last, |this| {
608 this.pb_4()
609 .border_b_1()
610 .border_color(cx.theme().colors().border_variant)
611 })
612 .child(
613 v_flex()
614 .w_full()
615 .max_w_1_2()
616 .child(Label::new(SharedString::new_static(sub_page_link.title))),
617 )
618 .child(
619 Button::new(("sub-page".into(), sub_page_link.title), "Configure")
620 .icon(IconName::ChevronRight)
621 .tab_index(0_isize)
622 .icon_position(IconPosition::End)
623 .icon_color(Color::Muted)
624 .icon_size(IconSize::Small)
625 .style(ButtonStyle::Outlined)
626 .size(ButtonSize::Medium),
627 )
628 .on_click({
629 let sub_page_link = sub_page_link.clone();
630 cx.listener(move |this, _, _, cx| {
631 this.push_sub_page(sub_page_link.clone(), section_header, cx)
632 })
633 })
634 .into_any_element(),
635 }
636 }
637}
638
639fn render_settings_item(
640 settings_window: &SettingsWindow,
641 setting_item: &SettingItem,
642 file: SettingsUiFile,
643 control: AnyElement,
644 _window: &mut Window,
645 cx: &mut Context<'_, SettingsWindow>,
646) -> Stateful<Div> {
647 let (found_in_file, _) = setting_item.field.file_set_in(file.clone(), cx);
648 let file_set_in = SettingsUiFile::from_settings(found_in_file);
649
650 h_flex()
651 .id(setting_item.title)
652 .min_w_0()
653 .gap_2()
654 .justify_between()
655 .child(
656 v_flex()
657 .w_1_2()
658 .child(
659 h_flex()
660 .w_full()
661 .gap_1()
662 .child(Label::new(SharedString::new_static(setting_item.title)))
663 .when_some(
664 file_set_in.filter(|file_set_in| file_set_in != &file),
665 |this, file_set_in| {
666 this.child(
667 Label::new(format!(
668 "— set in {}",
669 settings_window
670 .display_name(&file_set_in)
671 .expect("File name should exist")
672 ))
673 .color(Color::Muted)
674 .size(LabelSize::Small),
675 )
676 },
677 ),
678 )
679 .child(
680 Label::new(SharedString::new_static(setting_item.description))
681 .size(LabelSize::Small)
682 .color(Color::Muted),
683 ),
684 )
685 .child(control)
686}
687
688struct SettingItem {
689 title: &'static str,
690 description: &'static str,
691 field: Box<dyn AnySettingField>,
692 metadata: Option<Box<SettingsFieldMetadata>>,
693 files: FileMask,
694}
695
696#[derive(PartialEq, Eq, Clone, Copy)]
697struct FileMask(u8);
698
699impl std::fmt::Debug for FileMask {
700 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
701 write!(f, "FileMask(")?;
702 let mut items = vec![];
703
704 if self.contains(USER) {
705 items.push("USER");
706 }
707 if self.contains(LOCAL) {
708 items.push("LOCAL");
709 }
710 if self.contains(SERVER) {
711 items.push("SERVER");
712 }
713
714 write!(f, "{})", items.join(" | "))
715 }
716}
717
718const USER: FileMask = FileMask(1 << 0);
719const LOCAL: FileMask = FileMask(1 << 2);
720const SERVER: FileMask = FileMask(1 << 3);
721
722impl std::ops::BitAnd for FileMask {
723 type Output = Self;
724
725 fn bitand(self, other: Self) -> Self {
726 Self(self.0 & other.0)
727 }
728}
729
730impl std::ops::BitOr for FileMask {
731 type Output = Self;
732
733 fn bitor(self, other: Self) -> Self {
734 Self(self.0 | other.0)
735 }
736}
737
738impl FileMask {
739 fn contains(&self, other: FileMask) -> bool {
740 self.0 & other.0 != 0
741 }
742}
743
744impl PartialEq for SettingItem {
745 fn eq(&self, other: &Self) -> bool {
746 self.title == other.title
747 && self.description == other.description
748 && (match (&self.metadata, &other.metadata) {
749 (None, None) => true,
750 (Some(m1), Some(m2)) => m1.placeholder == m2.placeholder,
751 _ => false,
752 })
753 }
754}
755
756#[derive(Clone)]
757struct SubPageLink {
758 title: &'static str,
759 files: FileMask,
760 render: Arc<
761 dyn Fn(&mut SettingsWindow, &mut Window, &mut Context<SettingsWindow>) -> AnyElement
762 + 'static
763 + Send
764 + Sync,
765 >,
766}
767
768impl PartialEq for SubPageLink {
769 fn eq(&self, other: &Self) -> bool {
770 self.title == other.title
771 }
772}
773
774#[allow(unused)]
775#[derive(Clone, PartialEq)]
776enum SettingsUiFile {
777 User, // Uses all settings.
778 Project((WorktreeId, Arc<RelPath>)), // Has a special name, and special set of settings
779 Server(&'static str), // Uses a special name, and the user settings
780}
781
782impl SettingsUiFile {
783 fn is_server(&self) -> bool {
784 matches!(self, SettingsUiFile::Server(_))
785 }
786
787 fn worktree_id(&self) -> Option<WorktreeId> {
788 match self {
789 SettingsUiFile::User => None,
790 SettingsUiFile::Project((worktree_id, _)) => Some(*worktree_id),
791 SettingsUiFile::Server(_) => None,
792 }
793 }
794
795 fn from_settings(file: settings::SettingsFile) -> Option<Self> {
796 Some(match file {
797 settings::SettingsFile::User => SettingsUiFile::User,
798 settings::SettingsFile::Project(location) => SettingsUiFile::Project(location),
799 settings::SettingsFile::Server => SettingsUiFile::Server("todo: server name"),
800 settings::SettingsFile::Default => return None,
801 })
802 }
803
804 fn to_settings(&self) -> settings::SettingsFile {
805 match self {
806 SettingsUiFile::User => settings::SettingsFile::User,
807 SettingsUiFile::Project(location) => settings::SettingsFile::Project(location.clone()),
808 SettingsUiFile::Server(_) => settings::SettingsFile::Server,
809 }
810 }
811
812 fn mask(&self) -> FileMask {
813 match self {
814 SettingsUiFile::User => USER,
815 SettingsUiFile::Project(_) => LOCAL,
816 SettingsUiFile::Server(_) => SERVER,
817 }
818 }
819}
820
821impl SettingsWindow {
822 pub fn new(
823 original_window: Option<WindowHandle<Workspace>>,
824 window: &mut Window,
825 cx: &mut Context<Self>,
826 ) -> Self {
827 let font_family_cache = theme::FontFamilyCache::global(cx);
828
829 cx.spawn(async move |this, cx| {
830 font_family_cache.prefetch(cx).await;
831 this.update(cx, |_, cx| {
832 cx.notify();
833 })
834 })
835 .detach();
836
837 let current_file = SettingsUiFile::User;
838 let search_bar = cx.new(|cx| {
839 let mut editor = Editor::single_line(window, cx);
840 editor.set_placeholder_text("Search settings…", window, cx);
841 editor
842 });
843
844 cx.subscribe(&search_bar, |this, _, event: &EditorEvent, cx| {
845 let EditorEvent::Edited { transaction_id: _ } = event else {
846 return;
847 };
848
849 this.update_matches(cx);
850 })
851 .detach();
852
853 cx.observe_global_in::<SettingsStore>(window, move |this, window, cx| {
854 this.fetch_files(window, cx);
855 cx.notify();
856 })
857 .detach();
858
859 let title_bar = if !cfg!(target_os = "macos") {
860 Some(cx.new(|cx| PlatformTitleBar::new("settings-title-bar", cx)))
861 } else {
862 None
863 };
864
865 let mut this = Self {
866 title_bar,
867 original_window,
868 worktree_root_dirs: HashMap::default(),
869 files: vec![],
870 current_file: current_file,
871 pages: vec![],
872 navbar_entries: vec![],
873 navbar_entry: 0,
874 navbar_scroll_handle: UniformListScrollHandle::default(),
875 search_bar,
876 search_task: None,
877 search_matches: vec![],
878 content_handles: vec![],
879 page_scroll_handle: ScrollHandle::new(),
880 focus_handle: cx.focus_handle(),
881 navbar_focus_handle: NonFocusableHandle::new(
882 NAVBAR_CONTAINER_TAB_INDEX,
883 false,
884 window,
885 cx,
886 ),
887 content_focus_handle: NonFocusableHandle::new(
888 CONTENT_CONTAINER_TAB_INDEX,
889 false,
890 window,
891 cx,
892 ),
893 files_focus_handle: cx
894 .focus_handle()
895 .tab_index(HEADER_CONTAINER_TAB_INDEX)
896 .tab_stop(false),
897 search_index: None,
898 };
899
900 this.fetch_files(window, cx);
901 this.build_ui(window, cx);
902 this.build_search_index();
903
904 this.search_bar.update(cx, |editor, cx| {
905 editor.focus_handle(cx).focus(window);
906 });
907
908 this
909 }
910
911 fn toggle_navbar_entry(&mut self, nav_entry_index: usize) {
912 // We can only toggle root entries
913 if !self.navbar_entries[nav_entry_index].is_root {
914 return;
915 }
916
917 let expanded = &mut self.navbar_entries[nav_entry_index].expanded;
918 *expanded = !*expanded;
919 let expanded = *expanded;
920
921 let toggle_page_index = self.page_index_from_navbar_index(nav_entry_index);
922 let selected_page_index = self.page_index_from_navbar_index(self.navbar_entry);
923 // if currently selected page is a child of the parent page we are folding,
924 // set the current page to the parent page
925 if !expanded && selected_page_index == toggle_page_index {
926 self.navbar_entry = nav_entry_index;
927 // note: not opening page. Toggling does not change content just selected page
928 }
929 }
930
931 fn build_navbar(&mut self, cx: &App) {
932 let mut navbar_entries = Vec::with_capacity(self.navbar_entries.len());
933 for (page_index, page) in self.pages.iter().enumerate() {
934 navbar_entries.push(NavBarEntry {
935 title: page.title,
936 is_root: true,
937 expanded: false,
938 page_index,
939 item_index: None,
940 focus_handle: cx.focus_handle().tab_index(0).tab_stop(true),
941 });
942
943 for (item_index, item) in page.items.iter().enumerate() {
944 let SettingsPageItem::SectionHeader(title) = item else {
945 continue;
946 };
947 navbar_entries.push(NavBarEntry {
948 title,
949 is_root: false,
950 expanded: false,
951 page_index,
952 item_index: Some(item_index),
953 focus_handle: cx.focus_handle().tab_index(0).tab_stop(true),
954 });
955 }
956 }
957
958 self.navbar_entries = navbar_entries;
959 }
960
961 fn visible_navbar_entries(&self) -> impl Iterator<Item = (usize, &NavBarEntry)> {
962 let mut index = 0;
963 let entries = &self.navbar_entries;
964 let search_matches = &self.search_matches;
965 std::iter::from_fn(move || {
966 while index < entries.len() {
967 let entry = &entries[index];
968 let included_in_search = if let Some(item_index) = entry.item_index {
969 search_matches[entry.page_index][item_index]
970 } else {
971 search_matches[entry.page_index].iter().any(|b| *b)
972 || search_matches[entry.page_index].is_empty()
973 };
974 if included_in_search {
975 break;
976 }
977 index += 1;
978 }
979 if index >= self.navbar_entries.len() {
980 return None;
981 }
982 let entry = &entries[index];
983 let entry_index = index;
984
985 index += 1;
986 if entry.is_root && !entry.expanded {
987 while index < entries.len() {
988 if entries[index].is_root {
989 break;
990 }
991 index += 1;
992 }
993 }
994
995 return Some((entry_index, entry));
996 })
997 }
998
999 fn filter_matches_to_file(&mut self) {
1000 let current_file = self.current_file.mask();
1001 for (page, page_filter) in std::iter::zip(&self.pages, &mut self.search_matches) {
1002 let mut header_index = 0;
1003 let mut any_found_since_last_header = true;
1004
1005 for (index, item) in page.items.iter().enumerate() {
1006 match item {
1007 SettingsPageItem::SectionHeader(_) => {
1008 if !any_found_since_last_header {
1009 page_filter[header_index] = false;
1010 }
1011 header_index = index;
1012 any_found_since_last_header = false;
1013 }
1014 SettingsPageItem::SettingItem(setting_item) => {
1015 if !setting_item.files.contains(current_file) {
1016 page_filter[index] = false;
1017 } else {
1018 any_found_since_last_header = true;
1019 }
1020 }
1021 SettingsPageItem::SubPageLink(sub_page_link) => {
1022 if !sub_page_link.files.contains(current_file) {
1023 page_filter[index] = false;
1024 } else {
1025 any_found_since_last_header = true;
1026 }
1027 }
1028 }
1029 }
1030 if let Some(last_header) = page_filter.get_mut(header_index)
1031 && !any_found_since_last_header
1032 {
1033 *last_header = false;
1034 }
1035 }
1036 }
1037
1038 fn update_matches(&mut self, cx: &mut Context<SettingsWindow>) {
1039 self.search_task.take();
1040 let query = self.search_bar.read(cx).text(cx);
1041 if query.is_empty() || self.search_index.is_none() {
1042 for page in &mut self.search_matches {
1043 page.fill(true);
1044 }
1045 self.filter_matches_to_file();
1046 cx.notify();
1047 return;
1048 }
1049
1050 let search_index = self.search_index.as_ref().unwrap().clone();
1051
1052 fn update_matches_inner(
1053 this: &mut SettingsWindow,
1054 search_index: &SearchIndex,
1055 match_indices: impl Iterator<Item = usize>,
1056 cx: &mut Context<SettingsWindow>,
1057 ) {
1058 for page in &mut this.search_matches {
1059 page.fill(false);
1060 }
1061
1062 for match_index in match_indices {
1063 let SearchItemKey {
1064 page_index,
1065 header_index,
1066 item_index,
1067 } = search_index.key_lut[match_index];
1068 let page = &mut this.search_matches[page_index];
1069 page[header_index] = true;
1070 page[item_index] = true;
1071 }
1072 this.filter_matches_to_file();
1073 this.open_first_nav_page();
1074 cx.notify();
1075 }
1076
1077 self.search_task = Some(cx.spawn(async move |this, cx| {
1078 let bm25_task = cx.background_spawn({
1079 let search_index = search_index.clone();
1080 let max_results = search_index.key_lut.len();
1081 let query = query.clone();
1082 async move { search_index.bm25_engine.search(&query, max_results) }
1083 });
1084 let cancel_flag = std::sync::atomic::AtomicBool::new(false);
1085 let fuzzy_search_task = fuzzy::match_strings(
1086 search_index.fuzzy_match_candidates.as_slice(),
1087 &query,
1088 false,
1089 true,
1090 search_index.fuzzy_match_candidates.len(),
1091 &cancel_flag,
1092 cx.background_executor().clone(),
1093 );
1094
1095 let fuzzy_matches = fuzzy_search_task.await;
1096
1097 _ = this
1098 .update(cx, |this, cx| {
1099 // For tuning the score threshold
1100 // for fuzzy_match in &fuzzy_matches {
1101 // let SearchItemKey {
1102 // page_index,
1103 // header_index,
1104 // item_index,
1105 // } = search_index.key_lut[fuzzy_match.candidate_id];
1106 // let SettingsPageItem::SectionHeader(header) =
1107 // this.pages[page_index].items[header_index]
1108 // else {
1109 // continue;
1110 // };
1111 // let SettingsPageItem::SettingItem(SettingItem {
1112 // title, description, ..
1113 // }) = this.pages[page_index].items[item_index]
1114 // else {
1115 // continue;
1116 // };
1117 // let score = fuzzy_match.score;
1118 // eprint!("# {header} :: QUERY = {query} :: SCORE = {score}\n{title}\n{description}\n\n");
1119 // }
1120 update_matches_inner(
1121 this,
1122 search_index.as_ref(),
1123 fuzzy_matches
1124 .into_iter()
1125 // MAGIC NUMBER: Was found to have right balance between not too many weird matches, but also
1126 // flexible enough to catch misspellings and <4 letter queries
1127 // More flexible is good for us here because fuzzy matches will only be used for things that don't
1128 // match using bm25
1129 .take_while(|fuzzy_match| fuzzy_match.score >= 0.3)
1130 .map(|fuzzy_match| fuzzy_match.candidate_id),
1131 cx,
1132 );
1133 })
1134 .ok();
1135
1136 let bm25_matches = bm25_task.await;
1137
1138 _ = this
1139 .update(cx, |this, cx| {
1140 if bm25_matches.is_empty() {
1141 return;
1142 }
1143 update_matches_inner(
1144 this,
1145 search_index.as_ref(),
1146 bm25_matches
1147 .into_iter()
1148 .map(|bm25_match| bm25_match.document.id),
1149 cx,
1150 );
1151 })
1152 .ok();
1153 }));
1154 }
1155
1156 fn build_search_matches(&mut self) {
1157 self.search_matches = self
1158 .pages
1159 .iter()
1160 .map(|page| vec![true; page.items.len()])
1161 .collect::<Vec<_>>();
1162 }
1163
1164 fn build_search_index(&mut self) {
1165 let mut key_lut: Vec<SearchItemKey> = vec![];
1166 let mut documents = Vec::default();
1167 let mut fuzzy_match_candidates = Vec::default();
1168
1169 fn push_candidates(
1170 fuzzy_match_candidates: &mut Vec<StringMatchCandidate>,
1171 key_index: usize,
1172 input: &str,
1173 ) {
1174 for word in input.split_ascii_whitespace() {
1175 fuzzy_match_candidates.push(StringMatchCandidate::new(key_index, word));
1176 }
1177 }
1178
1179 // PERF: We are currently searching all items even in project files
1180 // where many settings are filtered out, using the logic in filter_matches_to_file
1181 // we could only search relevant items based on the current file
1182 for (page_index, page) in self.pages.iter().enumerate() {
1183 let mut header_index = 0;
1184 let mut header_str = "";
1185 for (item_index, item) in page.items.iter().enumerate() {
1186 let key_index = key_lut.len();
1187 match item {
1188 SettingsPageItem::SettingItem(item) => {
1189 documents.push(bm25::Document {
1190 id: key_index,
1191 contents: [page.title, header_str, item.title, item.description]
1192 .join("\n"),
1193 });
1194 push_candidates(&mut fuzzy_match_candidates, key_index, item.title);
1195 push_candidates(&mut fuzzy_match_candidates, key_index, item.description);
1196 }
1197 SettingsPageItem::SectionHeader(header) => {
1198 documents.push(bm25::Document {
1199 id: key_index,
1200 contents: header.to_string(),
1201 });
1202 push_candidates(&mut fuzzy_match_candidates, key_index, header);
1203 header_index = item_index;
1204 header_str = *header;
1205 }
1206 SettingsPageItem::SubPageLink(sub_page_link) => {
1207 documents.push(bm25::Document {
1208 id: key_index,
1209 contents: [page.title, header_str, sub_page_link.title].join("\n"),
1210 });
1211 push_candidates(
1212 &mut fuzzy_match_candidates,
1213 key_index,
1214 sub_page_link.title,
1215 );
1216 }
1217 }
1218 push_candidates(&mut fuzzy_match_candidates, key_index, page.title);
1219 push_candidates(&mut fuzzy_match_candidates, key_index, header_str);
1220
1221 key_lut.push(SearchItemKey {
1222 page_index,
1223 header_index,
1224 item_index,
1225 });
1226 }
1227 }
1228 let engine =
1229 bm25::SearchEngineBuilder::with_documents(bm25::Language::English, documents).build();
1230 self.search_index = Some(Arc::new(SearchIndex {
1231 bm25_engine: engine,
1232 key_lut,
1233 fuzzy_match_candidates,
1234 }));
1235 }
1236
1237 fn build_content_handles(&mut self, window: &mut Window, cx: &mut Context<SettingsWindow>) {
1238 self.content_handles = self
1239 .pages
1240 .iter()
1241 .map(|page| {
1242 std::iter::repeat_with(|| NonFocusableHandle::new(0, false, window, cx))
1243 .take(page.items.len())
1244 .collect()
1245 })
1246 .collect::<Vec<_>>();
1247 }
1248
1249 fn build_ui(&mut self, window: &mut Window, cx: &mut Context<SettingsWindow>) {
1250 if self.pages.is_empty() {
1251 self.pages = page_data::settings_data();
1252 self.build_navbar(cx);
1253 self.build_content_handles(window, cx);
1254 }
1255 sub_page_stack_mut().clear();
1256 // PERF: doesn't have to be rebuilt, can just be filled with true. pages is constant once it is built
1257 self.build_search_matches();
1258 self.update_matches(cx);
1259
1260 cx.notify();
1261 }
1262
1263 fn fetch_files(&mut self, window: &mut Window, cx: &mut Context<SettingsWindow>) {
1264 self.worktree_root_dirs.clear();
1265 let prev_files = self.files.clone();
1266 let settings_store = cx.global::<SettingsStore>();
1267 let mut ui_files = vec![];
1268 let all_files = settings_store.get_all_files();
1269 for file in all_files {
1270 let Some(settings_ui_file) = SettingsUiFile::from_settings(file) else {
1271 continue;
1272 };
1273 if settings_ui_file.is_server() {
1274 continue;
1275 }
1276
1277 if let Some(worktree_id) = settings_ui_file.worktree_id() {
1278 let directory_name = all_projects(cx)
1279 .find_map(|project| project.read(cx).worktree_for_id(worktree_id, cx))
1280 .and_then(|worktree| worktree.read(cx).root_dir())
1281 .and_then(|root_dir| {
1282 root_dir
1283 .file_name()
1284 .map(|os_string| os_string.to_string_lossy().to_string())
1285 });
1286
1287 let Some(directory_name) = directory_name else {
1288 log::error!(
1289 "No directory name found for settings file at worktree ID: {}",
1290 worktree_id
1291 );
1292 continue;
1293 };
1294
1295 self.worktree_root_dirs.insert(worktree_id, directory_name);
1296 }
1297
1298 let focus_handle = prev_files
1299 .iter()
1300 .find_map(|(prev_file, handle)| {
1301 (prev_file == &settings_ui_file).then(|| handle.clone())
1302 })
1303 .unwrap_or_else(|| cx.focus_handle().tab_index(0).tab_stop(true));
1304 ui_files.push((settings_ui_file, focus_handle));
1305 }
1306 ui_files.reverse();
1307 self.files = ui_files;
1308 let current_file_still_exists = self
1309 .files
1310 .iter()
1311 .any(|(file, _)| file == &self.current_file);
1312 if !current_file_still_exists {
1313 self.change_file(0, window, cx);
1314 }
1315 }
1316
1317 fn open_navbar_entry_page(&mut self, navbar_entry: usize) {
1318 if !self.is_nav_entry_visible(navbar_entry) {
1319 self.open_first_nav_page();
1320 }
1321 self.navbar_entry = navbar_entry;
1322 sub_page_stack_mut().clear();
1323 }
1324
1325 fn open_first_nav_page(&mut self) {
1326 let Some(first_navbar_entry_index) = self.visible_navbar_entries().next().map(|e| e.0)
1327 else {
1328 return;
1329 };
1330 self.open_navbar_entry_page(first_navbar_entry_index);
1331 }
1332
1333 fn change_file(&mut self, ix: usize, window: &mut Window, cx: &mut Context<SettingsWindow>) {
1334 if ix >= self.files.len() {
1335 self.current_file = SettingsUiFile::User;
1336 self.build_ui(window, cx);
1337 return;
1338 }
1339 if self.files[ix].0 == self.current_file {
1340 return;
1341 }
1342 self.current_file = self.files[ix].0.clone();
1343
1344 self.build_ui(window, cx);
1345
1346 if self
1347 .visible_navbar_entries()
1348 .any(|(index, _)| index == self.navbar_entry)
1349 {
1350 self.open_and_scroll_to_navbar_entry(self.navbar_entry, window, cx);
1351 } else {
1352 self.open_first_nav_page();
1353 };
1354 }
1355
1356 fn render_files_header(
1357 &self,
1358 _window: &mut Window,
1359 cx: &mut Context<SettingsWindow>,
1360 ) -> impl IntoElement {
1361 h_flex()
1362 .w_full()
1363 .pb_4()
1364 .gap_1()
1365 .justify_between()
1366 .tab_group()
1367 .track_focus(&self.files_focus_handle)
1368 .tab_index(HEADER_GROUP_TAB_INDEX)
1369 .child(
1370 h_flex()
1371 .id("file_buttons_container")
1372 .w_64() // Temporary fix until long-term solution is a fixed set of buttons representing a file location (User, Project, and Remote)
1373 .gap_1()
1374 .overflow_x_scroll()
1375 .children(
1376 self.files
1377 .iter()
1378 .enumerate()
1379 .map(|(ix, (file, focus_handle))| {
1380 Button::new(
1381 ix,
1382 self.display_name(&file)
1383 .expect("Files should always have a name"),
1384 )
1385 .toggle_state(file == &self.current_file)
1386 .selected_style(ButtonStyle::Tinted(ui::TintColor::Accent))
1387 .track_focus(focus_handle)
1388 .on_click(cx.listener({
1389 let focus_handle = focus_handle.clone();
1390 move |this, _: &gpui::ClickEvent, window, cx| {
1391 this.change_file(ix, window, cx);
1392 focus_handle.focus(window);
1393 }
1394 }))
1395 }),
1396 ),
1397 )
1398 .child(
1399 Button::new("edit-in-json", "Edit in settings.json")
1400 .tab_index(0_isize)
1401 .style(ButtonStyle::OutlinedGhost)
1402 .on_click(cx.listener(|this, _, _, cx| {
1403 this.open_current_settings_file(cx);
1404 })),
1405 )
1406 }
1407
1408 pub(crate) fn display_name(&self, file: &SettingsUiFile) -> Option<String> {
1409 match file {
1410 SettingsUiFile::User => Some("User".to_string()),
1411 SettingsUiFile::Project((worktree_id, path)) => self
1412 .worktree_root_dirs
1413 .get(&worktree_id)
1414 .map(|directory_name| {
1415 let path_style = PathStyle::local();
1416 if path.is_empty() {
1417 directory_name.clone()
1418 } else {
1419 format!(
1420 "{}{}{}",
1421 directory_name,
1422 path_style.separator(),
1423 path.display(path_style)
1424 )
1425 }
1426 }),
1427 SettingsUiFile::Server(file) => Some(file.to_string()),
1428 }
1429 }
1430
1431 // TODO:
1432 // Reconsider this after preview launch
1433 // fn file_location_str(&self) -> String {
1434 // match &self.current_file {
1435 // SettingsUiFile::User => "settings.json".to_string(),
1436 // SettingsUiFile::Project((worktree_id, path)) => self
1437 // .worktree_root_dirs
1438 // .get(&worktree_id)
1439 // .map(|directory_name| {
1440 // let path_style = PathStyle::local();
1441 // let file_path = path.join(paths::local_settings_file_relative_path());
1442 // format!(
1443 // "{}{}{}",
1444 // directory_name,
1445 // path_style.separator(),
1446 // file_path.display(path_style)
1447 // )
1448 // })
1449 // .expect("Current file should always be present in root dir map"),
1450 // SettingsUiFile::Server(file) => file.to_string(),
1451 // }
1452 // }
1453
1454 fn render_search(&self, _window: &mut Window, cx: &mut App) -> Div {
1455 h_flex()
1456 .py_1()
1457 .px_1p5()
1458 .mb_3()
1459 .gap_1p5()
1460 .rounded_sm()
1461 .bg(cx.theme().colors().editor_background)
1462 .border_1()
1463 .border_color(cx.theme().colors().border)
1464 .child(Icon::new(IconName::MagnifyingGlass).color(Color::Muted))
1465 .child(self.search_bar.clone())
1466 }
1467
1468 fn render_nav(
1469 &self,
1470 window: &mut Window,
1471 cx: &mut Context<SettingsWindow>,
1472 ) -> impl IntoElement {
1473 let visible_count = self.visible_navbar_entries().count();
1474
1475 let focus_keybind_label = if self
1476 .navbar_focus_handle
1477 .read(cx)
1478 .handle
1479 .contains_focused(window, cx)
1480 {
1481 "Focus Content"
1482 } else {
1483 "Focus Navbar"
1484 };
1485
1486 v_flex()
1487 .w_64()
1488 .p_2p5()
1489 .when(cfg!(target_os = "macos"), |c| c.pt_10())
1490 .h_full()
1491 .flex_none()
1492 .border_r_1()
1493 .key_context("NavigationMenu")
1494 .on_action(cx.listener(|this, _: &CollapseNavEntry, window, cx| {
1495 let Some(focused_entry) = this.focused_nav_entry(window, cx) else {
1496 return;
1497 };
1498 let focused_entry_parent = this.root_entry_containing(focused_entry);
1499 if this.navbar_entries[focused_entry_parent].expanded {
1500 this.toggle_navbar_entry(focused_entry_parent);
1501 window.focus(&this.navbar_entries[focused_entry_parent].focus_handle);
1502 }
1503 cx.notify();
1504 }))
1505 .on_action(cx.listener(|this, _: &ExpandNavEntry, window, cx| {
1506 let Some(focused_entry) = this.focused_nav_entry(window, cx) else {
1507 return;
1508 };
1509 if !this.navbar_entries[focused_entry].is_root {
1510 return;
1511 }
1512 if !this.navbar_entries[focused_entry].expanded {
1513 this.toggle_navbar_entry(focused_entry);
1514 }
1515 cx.notify();
1516 }))
1517 .on_action(
1518 cx.listener(|this, _: &FocusPreviousRootNavEntry, window, cx| {
1519 let entry_index = this
1520 .focused_nav_entry(window, cx)
1521 .unwrap_or(this.navbar_entry);
1522 let mut root_index = None;
1523 for (index, entry) in this.visible_navbar_entries() {
1524 if index >= entry_index {
1525 break;
1526 }
1527 if entry.is_root {
1528 root_index = Some(index);
1529 }
1530 }
1531 let Some(previous_root_index) = root_index else {
1532 return;
1533 };
1534 this.focus_and_scroll_to_nav_entry(previous_root_index, window, cx);
1535 }),
1536 )
1537 .on_action(cx.listener(|this, _: &FocusNextRootNavEntry, window, cx| {
1538 let entry_index = this
1539 .focused_nav_entry(window, cx)
1540 .unwrap_or(this.navbar_entry);
1541 let mut root_index = None;
1542 for (index, entry) in this.visible_navbar_entries() {
1543 if index <= entry_index {
1544 continue;
1545 }
1546 if entry.is_root {
1547 root_index = Some(index);
1548 break;
1549 }
1550 }
1551 let Some(next_root_index) = root_index else {
1552 return;
1553 };
1554 this.focus_and_scroll_to_nav_entry(next_root_index, window, cx);
1555 }))
1556 .on_action(cx.listener(|this, _: &FocusFirstNavEntry, window, cx| {
1557 if let Some((first_entry_index, _)) = this.visible_navbar_entries().next() {
1558 this.focus_and_scroll_to_nav_entry(first_entry_index, window, cx);
1559 }
1560 }))
1561 .on_action(cx.listener(|this, _: &FocusLastNavEntry, window, cx| {
1562 if let Some((last_entry_index, _)) = this.visible_navbar_entries().last() {
1563 this.focus_and_scroll_to_nav_entry(last_entry_index, window, cx);
1564 }
1565 }))
1566 .border_color(cx.theme().colors().border)
1567 .bg(cx.theme().colors().panel_background)
1568 .child(self.render_search(window, cx))
1569 .child(
1570 v_flex()
1571 .flex_1()
1572 .overflow_hidden()
1573 .track_focus(&self.navbar_focus_handle.focus_handle(cx))
1574 .tab_group()
1575 .tab_index(NAVBAR_GROUP_TAB_INDEX)
1576 .child(
1577 uniform_list(
1578 "settings-ui-nav-bar",
1579 visible_count + 1,
1580 cx.processor(move |this, range: Range<usize>, _, cx| {
1581 this.visible_navbar_entries()
1582 .skip(range.start.saturating_sub(1))
1583 .take(range.len())
1584 .map(|(ix, entry)| {
1585 TreeViewItem::new(
1586 ("settings-ui-navbar-entry", ix),
1587 entry.title,
1588 )
1589 .track_focus(&entry.focus_handle)
1590 .root_item(entry.is_root)
1591 .toggle_state(this.is_navbar_entry_selected(ix))
1592 .when(entry.is_root, |item| {
1593 item.expanded(entry.expanded).on_toggle(cx.listener(
1594 move |this, _, window, cx| {
1595 this.toggle_navbar_entry(ix);
1596 window.focus(
1597 &this.navbar_entries[ix].focus_handle,
1598 );
1599 cx.notify();
1600 },
1601 ))
1602 })
1603 .on_click(
1604 cx.listener(move |this, _, window, cx| {
1605 this.open_and_scroll_to_navbar_entry(
1606 ix, window, cx,
1607 );
1608 }),
1609 )
1610 })
1611 .collect()
1612 }),
1613 )
1614 .size_full()
1615 .track_scroll(self.navbar_scroll_handle.clone()),
1616 )
1617 .vertical_scrollbar_for(self.navbar_scroll_handle.clone(), window, cx),
1618 )
1619 .child(
1620 h_flex()
1621 .w_full()
1622 .h_8()
1623 .p_2()
1624 .pb_0p5()
1625 .flex_shrink_0()
1626 .border_t_1()
1627 .border_color(cx.theme().colors().border_variant)
1628 .children(
1629 KeyBinding::for_action(&ToggleFocusNav, window, cx).map(|this| {
1630 KeybindingHint::new(
1631 this,
1632 cx.theme().colors().surface_background.opacity(0.5),
1633 )
1634 .suffix(focus_keybind_label)
1635 }),
1636 ),
1637 )
1638 }
1639
1640 fn open_and_scroll_to_navbar_entry(
1641 &mut self,
1642 navbar_entry_index: usize,
1643 window: &mut Window,
1644 cx: &mut Context<Self>,
1645 ) {
1646 self.open_navbar_entry_page(navbar_entry_index);
1647 cx.notify();
1648
1649 if self.navbar_entries[navbar_entry_index].is_root
1650 || !self.is_nav_entry_visible(navbar_entry_index)
1651 {
1652 let Some(first_item_index) = self.visible_page_items().next().map(|(index, _)| index)
1653 else {
1654 return;
1655 };
1656 self.focus_content_element(first_item_index, window, cx);
1657 self.page_scroll_handle.set_offset(point(px(0.), px(0.)));
1658 } else {
1659 let entry_item_index = self.navbar_entries[navbar_entry_index]
1660 .item_index
1661 .expect("Non-root items should have an item index");
1662 let Some(selected_item_index) = self
1663 .visible_page_items()
1664 .position(|(index, _)| index == entry_item_index)
1665 else {
1666 return;
1667 };
1668 self.page_scroll_handle
1669 .scroll_to_top_of_item(selected_item_index);
1670 self.focus_content_element(entry_item_index, window, cx);
1671 }
1672
1673 // Page scroll handle updates the active item index
1674 // in it's next paint call after using scroll_handle.scroll_to_top_of_item
1675 // The call after that updates the offset of the scroll handle. So to
1676 // ensure the scroll handle doesn't lag behind we need to render three frames
1677 // back to back.
1678 cx.on_next_frame(window, |_, window, cx| {
1679 cx.on_next_frame(window, |_, _, cx| {
1680 cx.notify();
1681 });
1682 cx.notify();
1683 });
1684 cx.notify();
1685 }
1686
1687 fn is_nav_entry_visible(&self, nav_entry_index: usize) -> bool {
1688 self.visible_navbar_entries()
1689 .any(|(index, _)| index == nav_entry_index)
1690 }
1691
1692 fn focus_and_scroll_to_nav_entry(
1693 &self,
1694 nav_entry_index: usize,
1695 window: &mut Window,
1696 cx: &mut Context<Self>,
1697 ) {
1698 let Some(position) = self
1699 .visible_navbar_entries()
1700 .position(|(index, _)| index == nav_entry_index)
1701 else {
1702 return;
1703 };
1704 self.navbar_scroll_handle
1705 .scroll_to_item(position, gpui::ScrollStrategy::Top);
1706 window.focus(&self.navbar_entries[nav_entry_index].focus_handle);
1707 cx.notify();
1708 }
1709
1710 fn visible_page_items(&self) -> impl Iterator<Item = (usize, &SettingsPageItem)> {
1711 let page_idx = self.current_page_index();
1712
1713 self.current_page()
1714 .items
1715 .iter()
1716 .enumerate()
1717 .filter_map(move |(item_index, item)| {
1718 self.search_matches[page_idx][item_index].then_some((item_index, item))
1719 })
1720 }
1721
1722 fn render_sub_page_breadcrumbs(&self) -> impl IntoElement {
1723 let mut items = vec![];
1724 items.push(self.current_page().title);
1725 items.extend(
1726 sub_page_stack()
1727 .iter()
1728 .flat_map(|page| [page.section_header, page.link.title]),
1729 );
1730
1731 let last = items.pop().unwrap();
1732 h_flex()
1733 .gap_1()
1734 .children(
1735 items
1736 .into_iter()
1737 .flat_map(|item| [item, "/"])
1738 .map(|item| Label::new(item).color(Color::Muted)),
1739 )
1740 .child(Label::new(last))
1741 }
1742
1743 fn render_page_items<'a, Items: Iterator<Item = (usize, &'a SettingsPageItem)>>(
1744 &self,
1745 items: Items,
1746 page_index: Option<usize>,
1747 window: &mut Window,
1748 cx: &mut Context<SettingsWindow>,
1749 ) -> impl IntoElement {
1750 let mut page_content = v_flex()
1751 .id("settings-ui-page")
1752 .size_full()
1753 .overflow_y_scroll()
1754 .track_scroll(&self.page_scroll_handle);
1755
1756 let items: Vec<_> = items.collect();
1757 let items_len = items.len();
1758 let mut section_header = None;
1759
1760 let has_active_search = !self.search_bar.read(cx).is_empty(cx);
1761 let has_no_results = items_len == 0 && has_active_search;
1762
1763 if has_no_results {
1764 let search_query = self.search_bar.read(cx).text(cx);
1765 page_content = page_content.child(
1766 v_flex()
1767 .size_full()
1768 .items_center()
1769 .justify_center()
1770 .gap_1()
1771 .child(div().child("No Results"))
1772 .child(
1773 div()
1774 .text_sm()
1775 .text_color(cx.theme().colors().text_muted)
1776 .child(format!("No settings match \"{}\"", search_query)),
1777 ),
1778 )
1779 } else {
1780 let last_non_header_index = items
1781 .iter()
1782 .enumerate()
1783 .rev()
1784 .find(|(_, (_, item))| !matches!(item, SettingsPageItem::SectionHeader(_)))
1785 .map(|(index, _)| index);
1786
1787 page_content = page_content
1788 .when(sub_page_stack().is_empty(), |this| {
1789 this.when_some(
1790 self.navbar_entries
1791 .get(self.navbar_entry)
1792 .map(|entry| entry.title),
1793 |this, title| this.child(Label::new(title).size(LabelSize::Large).mb_3()),
1794 )
1795 })
1796 .children(items.clone().into_iter().enumerate().map(
1797 |(index, (actual_item_index, item))| {
1798 let no_bottom_border = items
1799 .get(index + 1)
1800 .map(|(_, next_item)| {
1801 matches!(next_item, SettingsPageItem::SectionHeader(_))
1802 })
1803 .unwrap_or(false);
1804 let is_last = Some(index) == last_non_header_index;
1805
1806 if let SettingsPageItem::SectionHeader(header) = item {
1807 section_header = Some(*header);
1808 }
1809 v_flex()
1810 .w_full()
1811 .min_w_0()
1812 .id(("settings-page-item", actual_item_index))
1813 .when_some(page_index, |element, page_index| {
1814 element.track_focus(
1815 &self.content_handles[page_index][actual_item_index]
1816 .focus_handle(cx),
1817 )
1818 })
1819 .child(item.render(
1820 self,
1821 section_header.expect("All items rendered after a section header"),
1822 no_bottom_border || is_last,
1823 window,
1824 cx,
1825 ))
1826 },
1827 ))
1828 }
1829 page_content
1830 }
1831
1832 fn render_page(
1833 &mut self,
1834 window: &mut Window,
1835 cx: &mut Context<SettingsWindow>,
1836 ) -> impl IntoElement {
1837 let page_header;
1838 let page_content;
1839
1840 if sub_page_stack().is_empty() {
1841 page_header = self.render_files_header(window, cx).into_any_element();
1842
1843 page_content = self
1844 .render_page_items(
1845 self.visible_page_items(),
1846 Some(self.current_page_index()),
1847 window,
1848 cx,
1849 )
1850 .into_any_element();
1851 } else {
1852 page_header = h_flex()
1853 .ml_neg_1p5()
1854 .pb_4()
1855 .gap_1()
1856 .child(
1857 IconButton::new("back-btn", IconName::ArrowLeft)
1858 .icon_size(IconSize::Small)
1859 .shape(IconButtonShape::Square)
1860 .on_click(cx.listener(|this, _, _, cx| {
1861 this.pop_sub_page(cx);
1862 })),
1863 )
1864 .child(self.render_sub_page_breadcrumbs())
1865 .into_any_element();
1866
1867 let active_page_render_fn = sub_page_stack().last().unwrap().link.render.clone();
1868 page_content = (active_page_render_fn)(self, window, cx);
1869 }
1870
1871 return v_flex()
1872 .size_full()
1873 .pt_6()
1874 .pb_8()
1875 .px_8()
1876 .bg(cx.theme().colors().editor_background)
1877 .child(page_header)
1878 .vertical_scrollbar_for(self.page_scroll_handle.clone(), window, cx)
1879 .track_focus(&self.content_focus_handle.focus_handle(cx))
1880 .child(
1881 div()
1882 .size_full()
1883 .tab_group()
1884 .tab_index(CONTENT_GROUP_TAB_INDEX)
1885 .child(page_content),
1886 );
1887 }
1888
1889 fn open_current_settings_file(&mut self, cx: &mut Context<Self>) {
1890 match &self.current_file {
1891 SettingsUiFile::User => {
1892 let Some(original_window) = self.original_window else {
1893 return;
1894 };
1895 original_window
1896 .update(cx, |workspace, window, cx| {
1897 workspace
1898 .with_local_workspace(window, cx, |workspace, window, cx| {
1899 let create_task = workspace.project().update(cx, |project, cx| {
1900 project.find_or_create_worktree(
1901 paths::config_dir().as_path(),
1902 false,
1903 cx,
1904 )
1905 });
1906 let open_task = workspace.open_paths(
1907 vec![paths::settings_file().to_path_buf()],
1908 OpenOptions {
1909 visible: Some(OpenVisible::None),
1910 ..Default::default()
1911 },
1912 None,
1913 window,
1914 cx,
1915 );
1916
1917 cx.spawn_in(window, async move |workspace, cx| {
1918 create_task.await.ok();
1919 open_task.await;
1920
1921 workspace.update_in(cx, |_, window, cx| {
1922 window.activate_window();
1923 cx.notify();
1924 })
1925 })
1926 .detach();
1927 })
1928 .detach();
1929 })
1930 .ok();
1931 }
1932 SettingsUiFile::Project((worktree_id, path)) => {
1933 let mut corresponding_workspace: Option<WindowHandle<Workspace>> = None;
1934 let settings_path = path.join(paths::local_settings_file_relative_path());
1935 let Some(app_state) = workspace::AppState::global(cx).upgrade() else {
1936 return;
1937 };
1938 for workspace in app_state.workspace_store.read(cx).workspaces() {
1939 let contains_settings_file = workspace
1940 .read_with(cx, |workspace, cx| {
1941 workspace.project().read(cx).contains_local_settings_file(
1942 *worktree_id,
1943 settings_path.as_ref(),
1944 cx,
1945 )
1946 })
1947 .ok();
1948 if Some(true) == contains_settings_file {
1949 corresponding_workspace = Some(*workspace);
1950
1951 break;
1952 }
1953 }
1954
1955 let Some(corresponding_workspace) = corresponding_workspace else {
1956 log::error!(
1957 "No corresponding workspace found for settings file {}",
1958 settings_path.as_std_path().display()
1959 );
1960
1961 return;
1962 };
1963
1964 // TODO: move zed::open_local_file() APIs to this crate, and
1965 // re-implement the "initial_contents" behavior
1966 corresponding_workspace
1967 .update(cx, |workspace, window, cx| {
1968 let open_task = workspace.open_path(
1969 (*worktree_id, settings_path.clone()),
1970 None,
1971 true,
1972 window,
1973 cx,
1974 );
1975
1976 cx.spawn_in(window, async move |workspace, cx| {
1977 if open_task.await.log_err().is_some() {
1978 workspace
1979 .update_in(cx, |_, window, cx| {
1980 window.activate_window();
1981 cx.notify();
1982 })
1983 .ok();
1984 }
1985 })
1986 .detach();
1987 })
1988 .ok();
1989 }
1990 SettingsUiFile::Server(_) => {
1991 return;
1992 }
1993 };
1994 }
1995
1996 fn current_page_index(&self) -> usize {
1997 self.page_index_from_navbar_index(self.navbar_entry)
1998 }
1999
2000 fn current_page(&self) -> &SettingsPage {
2001 &self.pages[self.current_page_index()]
2002 }
2003
2004 fn page_index_from_navbar_index(&self, index: usize) -> usize {
2005 if self.navbar_entries.is_empty() {
2006 return 0;
2007 }
2008
2009 self.navbar_entries[index].page_index
2010 }
2011
2012 fn is_navbar_entry_selected(&self, ix: usize) -> bool {
2013 ix == self.navbar_entry
2014 }
2015
2016 fn push_sub_page(
2017 &mut self,
2018 sub_page_link: SubPageLink,
2019 section_header: &'static str,
2020 cx: &mut Context<SettingsWindow>,
2021 ) {
2022 sub_page_stack_mut().push(SubPage {
2023 link: sub_page_link,
2024 section_header,
2025 });
2026 cx.notify();
2027 }
2028
2029 fn pop_sub_page(&mut self, cx: &mut Context<SettingsWindow>) {
2030 sub_page_stack_mut().pop();
2031 cx.notify();
2032 }
2033
2034 fn focus_file_at_index(&mut self, index: usize, window: &mut Window) {
2035 if let Some((_, handle)) = self.files.get(index) {
2036 handle.focus(window);
2037 }
2038 }
2039
2040 fn focused_file_index(&self, window: &Window, cx: &Context<Self>) -> usize {
2041 if self.files_focus_handle.contains_focused(window, cx)
2042 && let Some(index) = self
2043 .files
2044 .iter()
2045 .position(|(_, handle)| handle.is_focused(window))
2046 {
2047 return index;
2048 }
2049 if let Some(current_file_index) = self
2050 .files
2051 .iter()
2052 .position(|(file, _)| file == &self.current_file)
2053 {
2054 return current_file_index;
2055 }
2056 0
2057 }
2058
2059 fn focus_content_element(&self, item_index: usize, window: &mut Window, cx: &mut App) {
2060 if !sub_page_stack().is_empty() {
2061 return;
2062 }
2063 let page_index = self.current_page_index();
2064 window.focus(&self.content_handles[page_index][item_index].focus_handle(cx));
2065 }
2066
2067 fn focused_nav_entry(&self, window: &Window, cx: &App) -> Option<usize> {
2068 if !self
2069 .navbar_focus_handle
2070 .focus_handle(cx)
2071 .contains_focused(window, cx)
2072 {
2073 return None;
2074 }
2075 for (index, entry) in self.navbar_entries.iter().enumerate() {
2076 if entry.focus_handle.is_focused(window) {
2077 return Some(index);
2078 }
2079 }
2080 None
2081 }
2082
2083 fn root_entry_containing(&self, nav_entry_index: usize) -> usize {
2084 let mut index = Some(nav_entry_index);
2085 while let Some(prev_index) = index
2086 && !self.navbar_entries[prev_index].is_root
2087 {
2088 index = prev_index.checked_sub(1);
2089 }
2090 return index.expect("No root entry found");
2091 }
2092}
2093
2094impl Render for SettingsWindow {
2095 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2096 let ui_font = theme::setup_ui_font(window, cx);
2097
2098 client_side_decorations(
2099 v_flex()
2100 .text_color(cx.theme().colors().text)
2101 .size_full()
2102 .children(self.title_bar.clone())
2103 .child(
2104 div()
2105 .id("settings-window")
2106 .key_context("SettingsWindow")
2107 .track_focus(&self.focus_handle)
2108 .on_action(cx.listener(|this, _: &OpenCurrentFile, _, cx| {
2109 this.open_current_settings_file(cx);
2110 }))
2111 .on_action(|_: &Minimize, window, _cx| {
2112 window.minimize_window();
2113 })
2114 .on_action(cx.listener(|this, _: &search::FocusSearch, window, cx| {
2115 this.search_bar.focus_handle(cx).focus(window);
2116 }))
2117 .on_action(cx.listener(|this, _: &ToggleFocusNav, window, cx| {
2118 if this
2119 .navbar_focus_handle
2120 .focus_handle(cx)
2121 .contains_focused(window, cx)
2122 {
2123 this.open_and_scroll_to_navbar_entry(this.navbar_entry, window, cx);
2124 } else {
2125 this.focus_and_scroll_to_nav_entry(this.navbar_entry, window, cx);
2126 }
2127 }))
2128 .on_action(cx.listener(
2129 |this, FocusFile(file_index): &FocusFile, window, _| {
2130 this.focus_file_at_index(*file_index as usize, window);
2131 },
2132 ))
2133 .on_action(cx.listener(|this, _: &FocusNextFile, window, cx| {
2134 let next_index = usize::min(
2135 this.focused_file_index(window, cx) + 1,
2136 this.files.len().saturating_sub(1),
2137 );
2138 this.focus_file_at_index(next_index, window);
2139 }))
2140 .on_action(cx.listener(|this, _: &FocusPreviousFile, window, cx| {
2141 let prev_index = this.focused_file_index(window, cx).saturating_sub(1);
2142 this.focus_file_at_index(prev_index, window);
2143 }))
2144 .on_action(|_: &menu::SelectNext, window, _| {
2145 window.focus_next();
2146 })
2147 .on_action(|_: &menu::SelectPrevious, window, _| {
2148 window.focus_prev();
2149 })
2150 .flex()
2151 .flex_row()
2152 .flex_1()
2153 .min_h_0()
2154 .font(ui_font)
2155 .bg(cx.theme().colors().background)
2156 .text_color(cx.theme().colors().text)
2157 .child(self.render_nav(window, cx))
2158 .child(self.render_page(window, cx)),
2159 ),
2160 window,
2161 cx,
2162 )
2163 }
2164}
2165
2166fn all_projects(cx: &App) -> impl Iterator<Item = Entity<project::Project>> {
2167 workspace::AppState::global(cx)
2168 .upgrade()
2169 .map(|app_state| {
2170 app_state
2171 .workspace_store
2172 .read(cx)
2173 .workspaces()
2174 .iter()
2175 .filter_map(|workspace| Some(workspace.read(cx).ok()?.project().clone()))
2176 })
2177 .into_iter()
2178 .flatten()
2179}
2180
2181fn update_settings_file(
2182 file: SettingsUiFile,
2183 cx: &mut App,
2184 update: impl 'static + Send + FnOnce(&mut SettingsContent, &App),
2185) -> Result<()> {
2186 match file {
2187 SettingsUiFile::Project((worktree_id, rel_path)) => {
2188 let rel_path = rel_path.join(paths::local_settings_file_relative_path());
2189 let project = all_projects(cx).find(|project| {
2190 project.read_with(cx, |project, cx| {
2191 project.contains_local_settings_file(worktree_id, &rel_path, cx)
2192 })
2193 });
2194 let Some(project) = project else {
2195 anyhow::bail!(
2196 "Could not find worktree containing settings file: {}",
2197 &rel_path.display(PathStyle::local())
2198 );
2199 };
2200 project.update(cx, |project, cx| {
2201 project.update_local_settings_file(worktree_id, rel_path, cx, update);
2202 });
2203 return Ok(());
2204 }
2205 SettingsUiFile::User => {
2206 // todo(settings_ui) error?
2207 SettingsStore::global(cx).update_settings_file(<dyn fs::Fs>::global(cx), update);
2208 Ok(())
2209 }
2210 SettingsUiFile::Server(_) => unimplemented!(),
2211 }
2212}
2213
2214fn render_text_field<T: From<String> + Into<String> + AsRef<str> + Clone>(
2215 field: SettingField<T>,
2216 file: SettingsUiFile,
2217 metadata: Option<&SettingsFieldMetadata>,
2218 _window: &mut Window,
2219 cx: &mut App,
2220) -> AnyElement {
2221 let (_, initial_text) =
2222 SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
2223 let initial_text = initial_text.filter(|s| !s.as_ref().is_empty());
2224
2225 SettingsEditor::new()
2226 .tab_index(0)
2227 .when_some(initial_text, |editor, text| {
2228 editor.with_initial_text(text.as_ref().to_string())
2229 })
2230 .when_some(
2231 metadata.and_then(|metadata| metadata.placeholder),
2232 |editor, placeholder| editor.with_placeholder(placeholder),
2233 )
2234 .on_confirm({
2235 move |new_text, cx| {
2236 update_settings_file(file.clone(), cx, move |settings, _cx| {
2237 *(field.pick_mut)(settings) = new_text.map(Into::into);
2238 })
2239 .log_err(); // todo(settings_ui) don't log err
2240 }
2241 })
2242 .into_any_element()
2243}
2244
2245fn render_toggle_button<B: Into<bool> + From<bool> + Copy>(
2246 field: SettingField<B>,
2247 file: SettingsUiFile,
2248 _metadata: Option<&SettingsFieldMetadata>,
2249 _window: &mut Window,
2250 cx: &mut App,
2251) -> AnyElement {
2252 let (_, value) = SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
2253
2254 let toggle_state = if value.copied().map_or(false, Into::into) {
2255 ToggleState::Selected
2256 } else {
2257 ToggleState::Unselected
2258 };
2259
2260 Switch::new("toggle_button", toggle_state)
2261 .color(ui::SwitchColor::Accent)
2262 .on_click({
2263 move |state, _window, cx| {
2264 let state = *state == ui::ToggleState::Selected;
2265 update_settings_file(file.clone(), cx, move |settings, _cx| {
2266 *(field.pick_mut)(settings) = Some(state.into());
2267 })
2268 .log_err(); // todo(settings_ui) don't log err
2269 }
2270 })
2271 .tab_index(0_isize)
2272 .color(SwitchColor::Accent)
2273 .into_any_element()
2274}
2275
2276fn render_font_picker(
2277 field: SettingField<settings::FontFamilyName>,
2278 file: SettingsUiFile,
2279 _metadata: Option<&SettingsFieldMetadata>,
2280 window: &mut Window,
2281 cx: &mut App,
2282) -> AnyElement {
2283 let current_value = SettingsStore::global(cx)
2284 .get_value_from_file(file.to_settings(), field.pick)
2285 .1
2286 .cloned()
2287 .unwrap_or_else(|| SharedString::default().into());
2288
2289 let font_picker = cx.new(|cx| {
2290 ui_input::font_picker(
2291 current_value.clone().into(),
2292 move |font_name, cx| {
2293 update_settings_file(file.clone(), cx, move |settings, _cx| {
2294 *(field.pick_mut)(settings) = Some(font_name.into());
2295 })
2296 .log_err(); // todo(settings_ui) don't log err
2297 },
2298 window,
2299 cx,
2300 )
2301 });
2302
2303 PopoverMenu::new("font-picker")
2304 .menu(move |_window, _cx| Some(font_picker.clone()))
2305 .trigger(
2306 Button::new("font-family-button", current_value)
2307 .tab_index(0_isize)
2308 .style(ButtonStyle::Outlined)
2309 .size(ButtonSize::Medium)
2310 .icon(IconName::ChevronUpDown)
2311 .icon_color(Color::Muted)
2312 .icon_size(IconSize::Small)
2313 .icon_position(IconPosition::End),
2314 )
2315 .anchor(gpui::Corner::TopLeft)
2316 .offset(gpui::Point {
2317 x: px(0.0),
2318 y: px(2.0),
2319 })
2320 .with_handle(ui::PopoverMenuHandle::default())
2321 .into_any_element()
2322}
2323
2324fn render_number_field<T: NumberFieldType + Send + Sync>(
2325 field: SettingField<T>,
2326 file: SettingsUiFile,
2327 _metadata: Option<&SettingsFieldMetadata>,
2328 window: &mut Window,
2329 cx: &mut App,
2330) -> AnyElement {
2331 let (_, value) = SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
2332 let value = value.copied().unwrap_or_else(T::min_value);
2333 NumberField::new("numeric_stepper", value, window, cx)
2334 .on_change({
2335 move |value, _window, cx| {
2336 let value = *value;
2337 update_settings_file(file.clone(), cx, move |settings, _cx| {
2338 *(field.pick_mut)(settings) = Some(value);
2339 })
2340 .log_err(); // todo(settings_ui) don't log err
2341 }
2342 })
2343 .into_any_element()
2344}
2345
2346fn render_dropdown<T>(
2347 field: SettingField<T>,
2348 file: SettingsUiFile,
2349 _metadata: Option<&SettingsFieldMetadata>,
2350 window: &mut Window,
2351 cx: &mut App,
2352) -> AnyElement
2353where
2354 T: strum::VariantArray + strum::VariantNames + Copy + PartialEq + Send + Sync + 'static,
2355{
2356 let variants = || -> &'static [T] { <T as strum::VariantArray>::VARIANTS };
2357 let labels = || -> &'static [&'static str] { <T as strum::VariantNames>::VARIANTS };
2358
2359 let (_, current_value) =
2360 SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
2361 let current_value = current_value.copied().unwrap_or(variants()[0]);
2362
2363 let current_value_label =
2364 labels()[variants().iter().position(|v| *v == current_value).unwrap()];
2365
2366 DropdownMenu::new(
2367 "dropdown",
2368 current_value_label.to_title_case(),
2369 ContextMenu::build(window, cx, move |mut menu, _, _| {
2370 for (&value, &label) in std::iter::zip(variants(), labels()) {
2371 let file = file.clone();
2372 menu = menu.toggleable_entry(
2373 label.to_title_case(),
2374 value == current_value,
2375 IconPosition::End,
2376 None,
2377 move |_, cx| {
2378 if value == current_value {
2379 return;
2380 }
2381 update_settings_file(file.clone(), cx, move |settings, _cx| {
2382 *(field.pick_mut)(settings) = Some(value);
2383 })
2384 .log_err(); // todo(settings_ui) don't log err
2385 },
2386 );
2387 }
2388 menu
2389 }),
2390 )
2391 .trigger_size(ButtonSize::Medium)
2392 .style(DropdownStyle::Outlined)
2393 .offset(gpui::Point {
2394 x: px(0.0),
2395 y: px(2.0),
2396 })
2397 .tab_index(0)
2398 .into_any_element()
2399}
2400
2401#[cfg(test)]
2402mod test {
2403
2404 use super::*;
2405
2406 impl SettingsWindow {
2407 fn navbar_entry(&self) -> usize {
2408 self.navbar_entry
2409 }
2410
2411 fn new_builder(window: &mut Window, cx: &mut Context<Self>) -> Self {
2412 let mut this = Self::new(None, window, cx);
2413 this.navbar_entries.clear();
2414 this.pages.clear();
2415 this
2416 }
2417
2418 fn build(mut self, cx: &App) -> Self {
2419 self.build_navbar(cx);
2420 self.build_search_matches();
2421 self.build_search_index();
2422 self
2423 }
2424
2425 fn add_page(
2426 mut self,
2427 title: &'static str,
2428 build_page: impl Fn(SettingsPage) -> SettingsPage,
2429 ) -> Self {
2430 let page = SettingsPage {
2431 title,
2432 items: Vec::default(),
2433 };
2434
2435 self.pages.push(build_page(page));
2436 self
2437 }
2438
2439 fn search(&mut self, search_query: &str, window: &mut Window, cx: &mut Context<Self>) {
2440 self.search_task.take();
2441 self.search_bar.update(cx, |editor, cx| {
2442 editor.set_text(search_query, window, cx);
2443 });
2444 self.update_matches(cx);
2445 }
2446
2447 fn assert_search_results(&self, other: &Self) {
2448 // page index could be different because of filtered out pages
2449 #[derive(Debug, PartialEq)]
2450 struct EntryMinimal {
2451 is_root: bool,
2452 title: &'static str,
2453 }
2454 pretty_assertions::assert_eq!(
2455 other
2456 .visible_navbar_entries()
2457 .map(|(_, entry)| EntryMinimal {
2458 is_root: entry.is_root,
2459 title: entry.title,
2460 })
2461 .collect::<Vec<_>>(),
2462 self.visible_navbar_entries()
2463 .map(|(_, entry)| EntryMinimal {
2464 is_root: entry.is_root,
2465 title: entry.title,
2466 })
2467 .collect::<Vec<_>>(),
2468 );
2469 assert_eq!(
2470 self.current_page().items.iter().collect::<Vec<_>>(),
2471 other
2472 .visible_page_items()
2473 .map(|(_, item)| item)
2474 .collect::<Vec<_>>()
2475 );
2476 }
2477 }
2478
2479 impl SettingsPage {
2480 fn item(mut self, item: SettingsPageItem) -> Self {
2481 self.items.push(item);
2482 self
2483 }
2484 }
2485
2486 impl PartialEq for NavBarEntry {
2487 fn eq(&self, other: &Self) -> bool {
2488 self.title == other.title
2489 && self.is_root == other.is_root
2490 && self.expanded == other.expanded
2491 && self.page_index == other.page_index
2492 && self.item_index == other.item_index
2493 // ignoring focus_handle
2494 }
2495 }
2496
2497 impl SettingsPageItem {
2498 fn basic_item(title: &'static str, description: &'static str) -> Self {
2499 SettingsPageItem::SettingItem(SettingItem {
2500 files: USER,
2501 title,
2502 description,
2503 field: Box::new(SettingField {
2504 pick: |settings_content| &settings_content.auto_update,
2505 pick_mut: |settings_content| &mut settings_content.auto_update,
2506 }),
2507 metadata: None,
2508 })
2509 }
2510 }
2511
2512 fn register_settings(cx: &mut App) {
2513 settings::init(cx);
2514 theme::init(theme::LoadThemes::JustBase, cx);
2515 workspace::init_settings(cx);
2516 project::Project::init_settings(cx);
2517 language::init(cx);
2518 editor::init(cx);
2519 menu::init();
2520 }
2521
2522 fn parse(input: &'static str, window: &mut Window, cx: &mut App) -> SettingsWindow {
2523 let mut pages: Vec<SettingsPage> = Vec::new();
2524 let mut expanded_pages = Vec::new();
2525 let mut selected_idx = None;
2526 let mut index = 0;
2527 let mut in_expanded_section = false;
2528
2529 for mut line in input
2530 .lines()
2531 .map(|line| line.trim())
2532 .filter(|line| !line.is_empty())
2533 {
2534 if let Some(pre) = line.strip_suffix('*') {
2535 assert!(selected_idx.is_none(), "Only one selected entry allowed");
2536 selected_idx = Some(index);
2537 line = pre;
2538 }
2539 let (kind, title) = line.split_once(" ").unwrap();
2540 assert_eq!(kind.len(), 1);
2541 let kind = kind.chars().next().unwrap();
2542 if kind == 'v' {
2543 let page_idx = pages.len();
2544 expanded_pages.push(page_idx);
2545 pages.push(SettingsPage {
2546 title,
2547 items: vec![],
2548 });
2549 index += 1;
2550 in_expanded_section = true;
2551 } else if kind == '>' {
2552 pages.push(SettingsPage {
2553 title,
2554 items: vec![],
2555 });
2556 index += 1;
2557 in_expanded_section = false;
2558 } else if kind == '-' {
2559 pages
2560 .last_mut()
2561 .unwrap()
2562 .items
2563 .push(SettingsPageItem::SectionHeader(title));
2564 if selected_idx == Some(index) && !in_expanded_section {
2565 panic!("Items in unexpanded sections cannot be selected");
2566 }
2567 index += 1;
2568 } else {
2569 panic!(
2570 "Entries must start with one of 'v', '>', or '-'\n line: {}",
2571 line
2572 );
2573 }
2574 }
2575
2576 let mut settings_window = SettingsWindow {
2577 title_bar: None,
2578 original_window: None,
2579 worktree_root_dirs: HashMap::default(),
2580 files: Vec::default(),
2581 current_file: crate::SettingsUiFile::User,
2582 pages,
2583 search_bar: cx.new(|cx| Editor::single_line(window, cx)),
2584 navbar_entry: selected_idx.expect("Must have a selected navbar entry"),
2585 navbar_entries: Vec::default(),
2586 navbar_scroll_handle: UniformListScrollHandle::default(),
2587 search_matches: vec![],
2588 content_handles: vec![],
2589 search_task: None,
2590 page_scroll_handle: ScrollHandle::new(),
2591 focus_handle: cx.focus_handle(),
2592 navbar_focus_handle: NonFocusableHandle::new(
2593 NAVBAR_CONTAINER_TAB_INDEX,
2594 false,
2595 window,
2596 cx,
2597 ),
2598 content_focus_handle: NonFocusableHandle::new(
2599 CONTENT_CONTAINER_TAB_INDEX,
2600 false,
2601 window,
2602 cx,
2603 ),
2604 files_focus_handle: cx.focus_handle(),
2605 search_index: None,
2606 };
2607
2608 settings_window.build_search_matches();
2609 settings_window.build_navbar(cx);
2610 for expanded_page_index in expanded_pages {
2611 for entry in &mut settings_window.navbar_entries {
2612 if entry.page_index == expanded_page_index && entry.is_root {
2613 entry.expanded = true;
2614 }
2615 }
2616 }
2617 settings_window
2618 }
2619
2620 #[track_caller]
2621 fn check_navbar_toggle(
2622 before: &'static str,
2623 toggle_page: &'static str,
2624 after: &'static str,
2625 window: &mut Window,
2626 cx: &mut App,
2627 ) {
2628 let mut settings_window = parse(before, window, cx);
2629 let toggle_page_idx = settings_window
2630 .pages
2631 .iter()
2632 .position(|page| page.title == toggle_page)
2633 .expect("page not found");
2634 let toggle_idx = settings_window
2635 .navbar_entries
2636 .iter()
2637 .position(|entry| entry.page_index == toggle_page_idx)
2638 .expect("page not found");
2639 settings_window.toggle_navbar_entry(toggle_idx);
2640
2641 let expected_settings_window = parse(after, window, cx);
2642
2643 pretty_assertions::assert_eq!(
2644 settings_window
2645 .visible_navbar_entries()
2646 .map(|(_, entry)| entry)
2647 .collect::<Vec<_>>(),
2648 expected_settings_window
2649 .visible_navbar_entries()
2650 .map(|(_, entry)| entry)
2651 .collect::<Vec<_>>(),
2652 );
2653 pretty_assertions::assert_eq!(
2654 settings_window.navbar_entries[settings_window.navbar_entry()],
2655 expected_settings_window.navbar_entries[expected_settings_window.navbar_entry()],
2656 );
2657 }
2658
2659 macro_rules! check_navbar_toggle {
2660 ($name:ident, before: $before:expr, toggle_page: $toggle_page:expr, after: $after:expr) => {
2661 #[gpui::test]
2662 fn $name(cx: &mut gpui::TestAppContext) {
2663 let window = cx.add_empty_window();
2664 window.update(|window, cx| {
2665 register_settings(cx);
2666 check_navbar_toggle($before, $toggle_page, $after, window, cx);
2667 });
2668 }
2669 };
2670 }
2671
2672 check_navbar_toggle!(
2673 navbar_basic_open,
2674 before: r"
2675 v General
2676 - General
2677 - Privacy*
2678 v Project
2679 - Project Settings
2680 ",
2681 toggle_page: "General",
2682 after: r"
2683 > General*
2684 v Project
2685 - Project Settings
2686 "
2687 );
2688
2689 check_navbar_toggle!(
2690 navbar_basic_close,
2691 before: r"
2692 > General*
2693 - General
2694 - Privacy
2695 v Project
2696 - Project Settings
2697 ",
2698 toggle_page: "General",
2699 after: r"
2700 v General*
2701 - General
2702 - Privacy
2703 v Project
2704 - Project Settings
2705 "
2706 );
2707
2708 check_navbar_toggle!(
2709 navbar_basic_second_root_entry_close,
2710 before: r"
2711 > General
2712 - General
2713 - Privacy
2714 v Project
2715 - Project Settings*
2716 ",
2717 toggle_page: "Project",
2718 after: r"
2719 > General
2720 > Project*
2721 "
2722 );
2723
2724 check_navbar_toggle!(
2725 navbar_toggle_subroot,
2726 before: r"
2727 v General Page
2728 - General
2729 - Privacy
2730 v Project
2731 - Worktree Settings Content*
2732 v AI
2733 - General
2734 > Appearance & Behavior
2735 ",
2736 toggle_page: "Project",
2737 after: r"
2738 v General Page
2739 - General
2740 - Privacy
2741 > Project*
2742 v AI
2743 - General
2744 > Appearance & Behavior
2745 "
2746 );
2747
2748 check_navbar_toggle!(
2749 navbar_toggle_close_propagates_selected_index,
2750 before: r"
2751 v General Page
2752 - General
2753 - Privacy
2754 v Project
2755 - Worktree Settings Content
2756 v AI
2757 - General*
2758 > Appearance & Behavior
2759 ",
2760 toggle_page: "General Page",
2761 after: r"
2762 > General Page
2763 v Project
2764 - Worktree Settings Content
2765 v AI
2766 - General*
2767 > Appearance & Behavior
2768 "
2769 );
2770
2771 check_navbar_toggle!(
2772 navbar_toggle_expand_propagates_selected_index,
2773 before: r"
2774 > General Page
2775 - General
2776 - Privacy
2777 v Project
2778 - Worktree Settings Content
2779 v AI
2780 - General*
2781 > Appearance & Behavior
2782 ",
2783 toggle_page: "General Page",
2784 after: r"
2785 v General Page
2786 - General
2787 - Privacy
2788 v Project
2789 - Worktree Settings Content
2790 v AI
2791 - General*
2792 > Appearance & Behavior
2793 "
2794 );
2795
2796 #[gpui::test]
2797 fn test_basic_search(cx: &mut gpui::TestAppContext) {
2798 let cx = cx.add_empty_window();
2799 let (actual, expected) = cx.update(|window, cx| {
2800 register_settings(cx);
2801
2802 let expected = cx.new(|cx| {
2803 SettingsWindow::new_builder(window, cx)
2804 .add_page("General", |page| {
2805 page.item(SettingsPageItem::SectionHeader("General settings"))
2806 .item(SettingsPageItem::basic_item("test title", "General test"))
2807 })
2808 .build(cx)
2809 });
2810
2811 let actual = cx.new(|cx| {
2812 SettingsWindow::new_builder(window, cx)
2813 .add_page("General", |page| {
2814 page.item(SettingsPageItem::SectionHeader("General settings"))
2815 .item(SettingsPageItem::basic_item("test title", "General test"))
2816 })
2817 .add_page("Theme", |page| {
2818 page.item(SettingsPageItem::SectionHeader("Theme settings"))
2819 })
2820 .build(cx)
2821 });
2822
2823 actual.update(cx, |settings, cx| settings.search("gen", window, cx));
2824
2825 (actual, expected)
2826 });
2827
2828 cx.cx.run_until_parked();
2829
2830 cx.update(|_window, cx| {
2831 let expected = expected.read(cx);
2832 let actual = actual.read(cx);
2833 expected.assert_search_results(&actual);
2834 })
2835 }
2836
2837 #[gpui::test]
2838 fn test_search_render_page_with_filtered_out_navbar_entries(cx: &mut gpui::TestAppContext) {
2839 let cx = cx.add_empty_window();
2840 let (actual, expected) = cx.update(|window, cx| {
2841 register_settings(cx);
2842
2843 let actual = cx.new(|cx| {
2844 SettingsWindow::new_builder(window, cx)
2845 .add_page("General", |page| {
2846 page.item(SettingsPageItem::SectionHeader("General settings"))
2847 .item(SettingsPageItem::basic_item(
2848 "Confirm Quit",
2849 "Whether to confirm before quitting Zed",
2850 ))
2851 .item(SettingsPageItem::basic_item(
2852 "Auto Update",
2853 "Automatically update Zed",
2854 ))
2855 })
2856 .add_page("AI", |page| {
2857 page.item(SettingsPageItem::basic_item(
2858 "Disable AI",
2859 "Whether to disable all AI features in Zed",
2860 ))
2861 })
2862 .add_page("Appearance & Behavior", |page| {
2863 page.item(SettingsPageItem::SectionHeader("Cursor")).item(
2864 SettingsPageItem::basic_item(
2865 "Cursor Shape",
2866 "Cursor shape for the editor",
2867 ),
2868 )
2869 })
2870 .build(cx)
2871 });
2872
2873 let expected = cx.new(|cx| {
2874 SettingsWindow::new_builder(window, cx)
2875 .add_page("Appearance & Behavior", |page| {
2876 page.item(SettingsPageItem::SectionHeader("Cursor")).item(
2877 SettingsPageItem::basic_item(
2878 "Cursor Shape",
2879 "Cursor shape for the editor",
2880 ),
2881 )
2882 })
2883 .build(cx)
2884 });
2885
2886 actual.update(cx, |settings, cx| settings.search("cursor", window, cx));
2887
2888 (actual, expected)
2889 });
2890
2891 cx.cx.run_until_parked();
2892
2893 cx.update(|_window, cx| {
2894 let expected = expected.read(cx);
2895 let actual = actual.read(cx);
2896 expected.assert_search_results(&actual);
2897 })
2898 }
2899}