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
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, Action)]
30#[action(namespace = zed)]
31pub struct ImportVsCodeSettings {
32 #[serde(default)]
33 pub skip_prompt: bool,
34}
35
36#[derive(Copy, Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)]
37#[action(namespace = zed)]
38pub struct ImportCursorSettings {
39 #[serde(default)]
40 pub skip_prompt: bool,
41}
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_settings =
133 match settings::VsCodeSettings::load_user_settings(source, fs.clone()).await {
134 Ok(vscode_settings) => vscode_settings,
135 Err(err) => {
136 log::error!("{err}");
137 let _ = cx.prompt(
138 gpui::PromptLevel::Info,
139 &format!("Could not find or load a {source} settings file"),
140 None,
141 &["Ok"],
142 );
143 return;
144 }
145 };
146
147 let prompt = if skip_prompt {
148 Task::ready(Some(0))
149 } else {
150 let prompt = cx.prompt(
151 gpui::PromptLevel::Warning,
152 &format!(
153 "Importing {} settings may overwrite your existing settings. \
154 Will import settings from {}",
155 vscode_settings.source,
156 truncate_and_remove_front(&vscode_settings.path.to_string_lossy(), 128),
157 ),
158 None,
159 &["Ok", "Cancel"],
160 );
161 cx.spawn(async move |_| prompt.await.ok())
162 };
163 if prompt.await != Some(0) {
164 return;
165 }
166
167 cx.update(|_, cx| {
168 let source = vscode_settings.source;
169 let path = vscode_settings.path.clone();
170 cx.global::<SettingsStore>()
171 .import_vscode_settings(fs, vscode_settings);
172 log::info!("Imported {source} settings from {}", path.display());
173 })
174 .ok();
175}
176
177pub struct SettingsPage {
178 focus_handle: FocusHandle,
179}
180
181impl SettingsPage {
182 pub fn new(_workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
183 cx.new(|cx| Self {
184 focus_handle: cx.focus_handle(),
185 })
186 }
187}
188
189impl EventEmitter<ItemEvent> for SettingsPage {}
190
191impl Focusable for SettingsPage {
192 fn focus_handle(&self, _cx: &App) -> FocusHandle {
193 self.focus_handle.clone()
194 }
195}
196
197impl Item for SettingsPage {
198 type Event = ItemEvent;
199
200 fn tab_icon(&self, _window: &Window, _cx: &App) -> Option<Icon> {
201 Some(Icon::new(IconName::Settings))
202 }
203
204 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
205 "Settings".into()
206 }
207
208 fn show_toolbar(&self) -> bool {
209 false
210 }
211
212 fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
213 f(*event)
214 }
215}
216
217impl Render for SettingsPage {
218 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
219 v_flex()
220 .p_4()
221 .size_full()
222 .gap_4()
223 .child(Label::new("Settings").size(LabelSize::Large))
224 .child(
225 v_flex().gap_1().child(Label::new("Appearance")).child(
226 v_flex()
227 .elevation_2(cx)
228 .child(AppearanceSettingsControls::new()),
229 ),
230 )
231 .child(
232 v_flex().gap_1().child(Label::new("Editor")).child(
233 v_flex()
234 .elevation_2(cx)
235 .child(EditorSettingsControls::new()),
236 ),
237 )
238 }
239}