settings_ui.rs

  1mod appearance_settings_controls;
  2
  3use std::any::TypeId;
  4use std::sync::Arc;
  5
  6use command_palette_hooks::CommandPaletteFilter;
  7use editor::EditorSettingsControls;
  8use feature_flags::{FeatureFlag, FeatureFlagViewExt};
  9use fs::Fs;
 10use gpui::{
 11    Action, App, AsyncWindowContext, Entity, EventEmitter, FocusHandle, Focusable, Task, actions,
 12};
 13use schemars::JsonSchema;
 14use serde::Deserialize;
 15use settings::{SettingsStore, VsCodeSettingsSource};
 16use ui::prelude::*;
 17use util::truncate_and_remove_front;
 18use workspace::item::{Item, ItemEvent};
 19use workspace::{Workspace, with_active_or_new_workspace};
 20
 21use crate::appearance_settings_controls::AppearanceSettingsControls;
 22
 23mod keybindings;
 24
 25pub struct SettingsUiFeatureFlag;
 26
 27impl FeatureFlag for SettingsUiFeatureFlag {
 28    const NAME: &'static str = "settings-ui";
 29}
 30
 31#[derive(Copy, Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
 32#[action(namespace = zed)]
 33pub struct ImportVsCodeSettings {
 34    #[serde(default)]
 35    pub skip_prompt: bool,
 36}
 37
 38#[derive(Copy, Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
 39#[action(namespace = zed)]
 40pub struct ImportCursorSettings {
 41    #[serde(default)]
 42    pub skip_prompt: bool,
 43}
 44actions!(zed, [OpenSettingsEditor]);
 45
 46pub fn init(cx: &mut App) {
 47    cx.on_action(|_: &OpenSettingsEditor, cx| {
 48        with_active_or_new_workspace(cx, move |workspace, window, cx| {
 49            let existing = workspace
 50                .active_pane()
 51                .read(cx)
 52                .items()
 53                .find_map(|item| item.downcast::<SettingsPage>());
 54
 55            if let Some(existing) = existing {
 56                workspace.activate_item(&existing, true, true, window, cx);
 57            } else {
 58                let settings_page = SettingsPage::new(workspace, cx);
 59                workspace.add_item_to_active_pane(Box::new(settings_page), None, true, window, cx)
 60            }
 61        });
 62    });
 63
 64    cx.observe_new(|workspace: &mut Workspace, window, cx| {
 65        let Some(window) = window else {
 66            return;
 67        };
 68
 69        workspace.register_action(|_workspace, action: &ImportVsCodeSettings, window, cx| {
 70            let fs = <dyn Fs>::global(cx);
 71            let action = *action;
 72
 73            window
 74                .spawn(cx, async move |cx: &mut AsyncWindowContext| {
 75                    handle_import_vscode_settings(
 76                        VsCodeSettingsSource::VsCode,
 77                        action.skip_prompt,
 78                        fs,
 79                        cx,
 80                    )
 81                    .await
 82                })
 83                .detach();
 84        });
 85
 86        workspace.register_action(|_workspace, action: &ImportCursorSettings, window, cx| {
 87            let fs = <dyn Fs>::global(cx);
 88            let action = *action;
 89
 90            window
 91                .spawn(cx, async move |cx: &mut AsyncWindowContext| {
 92                    handle_import_vscode_settings(
 93                        VsCodeSettingsSource::Cursor,
 94                        action.skip_prompt,
 95                        fs,
 96                        cx,
 97                    )
 98                    .await
 99                })
100                .detach();
101        });
102
103        let settings_ui_actions = [TypeId::of::<OpenSettingsEditor>()];
104
105        CommandPaletteFilter::update_global(cx, |filter, _cx| {
106            filter.hide_action_types(&settings_ui_actions);
107        });
108
109        cx.observe_flag::<SettingsUiFeatureFlag, _>(
110            window,
111            move |is_enabled, _workspace, _, cx| {
112                if is_enabled {
113                    CommandPaletteFilter::update_global(cx, |filter, _cx| {
114                        filter.show_action_types(settings_ui_actions.iter());
115                    });
116                } else {
117                    CommandPaletteFilter::update_global(cx, |filter, _cx| {
118                        filter.hide_action_types(&settings_ui_actions);
119                    });
120                }
121            },
122        )
123        .detach();
124    })
125    .detach();
126
127    keybindings::init(cx);
128}
129
130async fn handle_import_vscode_settings(
131    source: VsCodeSettingsSource,
132    skip_prompt: bool,
133    fs: Arc<dyn Fs>,
134    cx: &mut AsyncWindowContext,
135) {
136    let vscode_settings =
137        match settings::VsCodeSettings::load_user_settings(source, fs.clone()).await {
138            Ok(vscode_settings) => vscode_settings,
139            Err(err) => {
140                log::error!("{err}");
141                let _ = cx.prompt(
142                    gpui::PromptLevel::Info,
143                    &format!("Could not find or load a {source} settings file"),
144                    None,
145                    &["Ok"],
146                );
147                return;
148            }
149        };
150
151    let prompt = if skip_prompt {
152        Task::ready(Some(0))
153    } else {
154        let prompt = cx.prompt(
155            gpui::PromptLevel::Warning,
156            &format!(
157                "Importing {} settings may overwrite your existing settings. \
158                Will import settings from {}",
159                vscode_settings.source,
160                truncate_and_remove_front(&vscode_settings.path.to_string_lossy(), 128),
161            ),
162            None,
163            &["Ok", "Cancel"],
164        );
165        cx.spawn(async move |_| prompt.await.ok())
166    };
167    if prompt.await != Some(0) {
168        return;
169    }
170
171    cx.update(|_, cx| {
172        let source = vscode_settings.source;
173        let path = vscode_settings.path.clone();
174        cx.global::<SettingsStore>()
175            .import_vscode_settings(fs, vscode_settings);
176        log::info!("Imported {source} settings from {}", path.display());
177    })
178    .ok();
179}
180
181pub struct SettingsPage {
182    focus_handle: FocusHandle,
183}
184
185impl SettingsPage {
186    pub fn new(_workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
187        cx.new(|cx| Self {
188            focus_handle: cx.focus_handle(),
189        })
190    }
191}
192
193impl EventEmitter<ItemEvent> for SettingsPage {}
194
195impl Focusable for SettingsPage {
196    fn focus_handle(&self, _cx: &App) -> FocusHandle {
197        self.focus_handle.clone()
198    }
199}
200
201impl Item for SettingsPage {
202    type Event = ItemEvent;
203
204    fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
205        Some(Icon::new(IconName::Settings))
206    }
207
208    fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
209        "Settings".into()
210    }
211
212    fn show_toolbar(&self) -> bool {
213        false
214    }
215
216    fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
217        f(*event)
218    }
219}
220
221impl Render for SettingsPage {
222    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
223        v_flex()
224            .p_4()
225            .size_full()
226            .gap_4()
227            .child(Label::new("Settings").size(LabelSize::Large))
228            .child(
229                v_flex().gap_1().child(Label::new("Appearance")).child(
230                    v_flex()
231                        .elevation_2(cx)
232                        .child(AppearanceSettingsControls::new()),
233                ),
234            )
235            .child(
236                v_flex().gap_1().child(Label::new("Editor")).child(
237                    v_flex()
238                        .elevation_2(cx)
239                        .child(EditorSettingsControls::new()),
240                ),
241            )
242    }
243}