settings_ui.rs

  1use std::any::TypeId;
  2
  3use command_palette_hooks::CommandPaletteFilter;
  4use feature_flags::{FeatureFlag, FeatureFlagViewExt};
  5use gpui::{actions, AppContext, EventEmitter, FocusHandle, FocusableView, View};
  6use ui::prelude::*;
  7use workspace::item::{Item, ItemEvent};
  8use workspace::Workspace;
  9
 10pub struct SettingsUiFeatureFlag;
 11
 12impl FeatureFlag for SettingsUiFeatureFlag {
 13    const NAME: &'static str = "settings-ui";
 14}
 15
 16actions!(zed, [OpenSettingsEditor]);
 17
 18pub fn init(cx: &mut AppContext) {
 19    cx.observe_new_views(|workspace: &mut Workspace, cx| {
 20        workspace.register_action(|workspace, _: &OpenSettingsEditor, cx| {
 21            let existing = workspace
 22                .active_pane()
 23                .read(cx)
 24                .items()
 25                .find_map(|item| item.downcast::<SettingsPage>());
 26
 27            if let Some(existing) = existing {
 28                workspace.activate_item(&existing, true, true, cx);
 29            } else {
 30                let settings_page = SettingsPage::new(workspace, cx);
 31                workspace.add_item_to_active_pane(Box::new(settings_page), None, true, cx)
 32            }
 33        });
 34
 35        let settings_ui_actions = [TypeId::of::<OpenSettingsEditor>()];
 36
 37        CommandPaletteFilter::update_global(cx, |filter, _cx| {
 38            filter.hide_action_types(&settings_ui_actions);
 39        });
 40
 41        cx.observe_flag::<SettingsUiFeatureFlag, _>(move |is_enabled, _view, cx| {
 42            if is_enabled {
 43                CommandPaletteFilter::update_global(cx, |filter, _cx| {
 44                    filter.show_action_types(settings_ui_actions.iter());
 45                });
 46            } else {
 47                CommandPaletteFilter::update_global(cx, |filter, _cx| {
 48                    filter.hide_action_types(&settings_ui_actions);
 49                });
 50            }
 51        })
 52        .detach();
 53    })
 54    .detach();
 55}
 56
 57pub struct SettingsPage {
 58    focus_handle: FocusHandle,
 59}
 60
 61impl SettingsPage {
 62    pub fn new(_workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
 63        cx.new_view(|cx| Self {
 64            focus_handle: cx.focus_handle(),
 65        })
 66    }
 67}
 68
 69impl EventEmitter<ItemEvent> for SettingsPage {}
 70
 71impl FocusableView for SettingsPage {
 72    fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
 73        self.focus_handle.clone()
 74    }
 75}
 76
 77impl Item for SettingsPage {
 78    type Event = ItemEvent;
 79
 80    fn tab_icon(&self, _cx: &WindowContext) -> Option<Icon> {
 81        Some(Icon::new(IconName::Settings))
 82    }
 83
 84    fn tab_content_text(&self, _cx: &WindowContext) -> Option<SharedString> {
 85        Some("Settings".into())
 86    }
 87
 88    fn show_toolbar(&self) -> bool {
 89        false
 90    }
 91
 92    fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
 93        f(*event)
 94    }
 95}
 96
 97impl Render for SettingsPage {
 98    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 99        v_flex()
100            .p_4()
101            .size_full()
102            .child(Label::new("Settings").size(LabelSize::Large))
103            .child(Label::new(
104                "Nothing to see here yet. Feature-flagged for staff.",
105            ))
106    }
107}