settings_ui.rs

  1mod appearance_settings_controls;
  2
  3use std::any::TypeId;
  4
  5use command_palette_hooks::CommandPaletteFilter;
  6use editor::EditorSettingsControls;
  7use feature_flags::{FeatureFlag, FeatureFlagViewExt};
  8use gpui::{App, Entity, EventEmitter, FocusHandle, Focusable, actions};
  9use ui::prelude::*;
 10use workspace::Workspace;
 11use workspace::item::{Item, ItemEvent};
 12
 13use crate::appearance_settings_controls::AppearanceSettingsControls;
 14
 15pub struct SettingsUiFeatureFlag;
 16
 17impl FeatureFlag for SettingsUiFeatureFlag {
 18    const NAME: &'static str = "settings-ui";
 19}
 20
 21actions!(zed, [OpenSettingsEditor]);
 22
 23pub fn init(cx: &mut App) {
 24    cx.observe_new(|workspace: &mut Workspace, window, cx| {
 25        let Some(window) = window else {
 26            return;
 27        };
 28
 29        workspace.register_action(|workspace, _: &OpenSettingsEditor, window, cx| {
 30            let existing = workspace
 31                .active_pane()
 32                .read(cx)
 33                .items()
 34                .find_map(|item| item.downcast::<SettingsPage>());
 35
 36            if let Some(existing) = existing {
 37                workspace.activate_item(&existing, true, true, window, cx);
 38            } else {
 39                let settings_page = SettingsPage::new(workspace, cx);
 40                workspace.add_item_to_active_pane(Box::new(settings_page), None, true, window, cx)
 41            }
 42        });
 43
 44        let settings_ui_actions = [TypeId::of::<OpenSettingsEditor>()];
 45
 46        CommandPaletteFilter::update_global(cx, |filter, _cx| {
 47            filter.hide_action_types(&settings_ui_actions);
 48        });
 49
 50        cx.observe_flag::<SettingsUiFeatureFlag, _>(
 51            window,
 52            move |is_enabled, _workspace, _, cx| {
 53                if is_enabled {
 54                    CommandPaletteFilter::update_global(cx, |filter, _cx| {
 55                        filter.show_action_types(settings_ui_actions.iter());
 56                    });
 57                } else {
 58                    CommandPaletteFilter::update_global(cx, |filter, _cx| {
 59                        filter.hide_action_types(&settings_ui_actions);
 60                    });
 61                }
 62            },
 63        )
 64        .detach();
 65    })
 66    .detach();
 67}
 68
 69pub struct SettingsPage {
 70    focus_handle: FocusHandle,
 71}
 72
 73impl SettingsPage {
 74    pub fn new(_workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
 75        cx.new(|cx| Self {
 76            focus_handle: cx.focus_handle(),
 77        })
 78    }
 79}
 80
 81impl EventEmitter<ItemEvent> for SettingsPage {}
 82
 83impl Focusable for SettingsPage {
 84    fn focus_handle(&self, _cx: &App) -> FocusHandle {
 85        self.focus_handle.clone()
 86    }
 87}
 88
 89impl Item for SettingsPage {
 90    type Event = ItemEvent;
 91
 92    fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
 93        Some(Icon::new(IconName::Settings))
 94    }
 95
 96    fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
 97        Some("Settings".into())
 98    }
 99
100    fn show_toolbar(&self) -> bool {
101        false
102    }
103
104    fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
105        f(*event)
106    }
107}
108
109impl Render for SettingsPage {
110    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
111        v_flex()
112            .p_4()
113            .size_full()
114            .gap_4()
115            .child(Label::new("Settings").size(LabelSize::Large))
116            .child(
117                v_flex().gap_1().child(Label::new("Appearance")).child(
118                    v_flex()
119                        .elevation_2(cx)
120                        .child(AppearanceSettingsControls::new()),
121                ),
122            )
123            .child(
124                v_flex().gap_1().child(Label::new("Editor")).child(
125                    v_flex()
126                        .elevation_2(cx)
127                        .child(EditorSettingsControls::new()),
128                ),
129            )
130    }
131}