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