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