1use std::num::NonZeroUsize;
2
3use anyhow::Result;
4use collections::HashMap;
5use gpui::App;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use settings::{Settings, SettingsSources};
9
10#[derive(Deserialize)]
11pub struct WorkspaceSettings {
12 pub active_pane_modifiers: ActivePanelModifiers,
13 pub bottom_dock_layout: BottomDockLayout,
14 pub pane_split_direction_horizontal: PaneSplitDirectionHorizontal,
15 pub pane_split_direction_vertical: PaneSplitDirectionVertical,
16 pub centered_layout: CenteredLayoutSettings,
17 pub confirm_quit: bool,
18 pub show_call_status_icon: bool,
19 pub autosave: AutosaveSetting,
20 pub restore_on_startup: RestoreOnStartupBehavior,
21 pub restore_on_file_reopen: bool,
22 pub drop_target_size: f32,
23 pub use_system_path_prompts: bool,
24 pub use_system_prompts: bool,
25 pub command_aliases: HashMap<String, String>,
26 pub max_tabs: Option<NonZeroUsize>,
27 pub when_closing_with_no_tabs: CloseWindowWhenNoItems,
28 pub on_last_window_closed: OnLastWindowClosed,
29 pub close_on_file_delete: bool,
30}
31
32#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
33#[serde(rename_all = "snake_case")]
34pub enum OnLastWindowClosed {
35 /// Match platform conventions by default, so don't quit on macOS, and quit on other platforms
36 #[default]
37 PlatformDefault,
38 /// Quit the application the last window is closed
39 QuitApp,
40}
41
42impl OnLastWindowClosed {
43 pub fn is_quit_app(&self) -> bool {
44 match self {
45 OnLastWindowClosed::PlatformDefault => false,
46 OnLastWindowClosed::QuitApp => true,
47 }
48 }
49}
50
51#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
52#[serde(rename_all = "snake_case")]
53pub struct ActivePanelModifiers {
54 /// Scale by which to zoom the active pane.
55 /// When set to 1.0, the active pane has the same size as others,
56 /// but when set to a larger value, the active pane takes up more space.
57 ///
58 /// Default: `1.0`
59 pub magnification: Option<f32>,
60 /// Size of the border surrounding the active pane.
61 /// When set to 0, the active pane doesn't have any border.
62 /// The border is drawn inset.
63 ///
64 /// Default: `0.0`
65 pub border_size: Option<f32>,
66 /// Opacity of inactive panels.
67 /// When set to 1.0, the inactive panes have the same opacity as the active one.
68 /// If set to 0, the inactive panes content will not be visible at all.
69 /// Values are clamped to the [0.0, 1.0] range.
70 ///
71 /// Default: `1.0`
72 pub inactive_opacity: Option<f32>,
73}
74
75#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, JsonSchema)]
76#[serde(rename_all = "snake_case")]
77pub enum BottomDockLayout {
78 /// Contained between the left and right docks
79 #[default]
80 Contained,
81 /// Takes up the full width of the window
82 Full,
83 /// Extends under the left dock while snapping to the right dock
84 LeftAligned,
85 /// Extends under the right dock while snapping to the left dock
86 RightAligned,
87}
88
89#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
90#[serde(rename_all = "snake_case")]
91pub enum CloseWindowWhenNoItems {
92 /// Match platform conventions by default, so "on" on macOS and "off" everywhere else
93 #[default]
94 PlatformDefault,
95 /// Close the window when there are no tabs
96 CloseWindow,
97 /// Leave the window open when there are no tabs
98 KeepWindowOpen,
99}
100
101impl CloseWindowWhenNoItems {
102 pub fn should_close(&self) -> bool {
103 match self {
104 CloseWindowWhenNoItems::PlatformDefault => cfg!(target_os = "macos"),
105 CloseWindowWhenNoItems::CloseWindow => true,
106 CloseWindowWhenNoItems::KeepWindowOpen => false,
107 }
108 }
109}
110
111#[derive(Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
112#[serde(rename_all = "snake_case")]
113pub enum RestoreOnStartupBehavior {
114 /// Always start with an empty editor
115 None,
116 /// Restore the workspace that was closed last.
117 LastWorkspace,
118 /// Restore all workspaces that were open when quitting Zed.
119 #[default]
120 LastSession,
121}
122
123#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
124pub struct WorkspaceSettingsContent {
125 /// Active pane styling settings.
126 pub active_pane_modifiers: Option<ActivePanelModifiers>,
127 /// Layout mode for the bottom dock
128 ///
129 /// Default: contained
130 pub bottom_dock_layout: Option<BottomDockLayout>,
131 /// Direction to split horizontally.
132 ///
133 /// Default: "up"
134 pub pane_split_direction_horizontal: Option<PaneSplitDirectionHorizontal>,
135 /// Direction to split vertically.
136 ///
137 /// Default: "left"
138 pub pane_split_direction_vertical: Option<PaneSplitDirectionVertical>,
139 /// Centered layout related settings.
140 pub centered_layout: Option<CenteredLayoutSettings>,
141 /// Whether or not to prompt the user to confirm before closing the application.
142 ///
143 /// Default: false
144 pub confirm_quit: Option<bool>,
145 /// Whether or not to show the call status icon in the status bar.
146 ///
147 /// Default: true
148 pub show_call_status_icon: Option<bool>,
149 /// When to automatically save edited buffers.
150 ///
151 /// Default: off
152 pub autosave: Option<AutosaveSetting>,
153 /// Controls previous session restoration in freshly launched Zed instance.
154 /// Values: none, last_workspace, last_session
155 /// Default: last_session
156 pub restore_on_startup: Option<RestoreOnStartupBehavior>,
157 /// Whether to attempt to restore previous file's state when opening it again.
158 /// The state is stored per pane.
159 /// When disabled, defaults are applied instead of the state restoration.
160 ///
161 /// E.g. for editors, selections, folds and scroll positions are restored, if the same file is closed and, later, opened again in the same pane.
162 /// When disabled, a single selection in the very beginning of the file, zero scroll position and no folds state is used as a default.
163 ///
164 /// Default: true
165 pub restore_on_file_reopen: Option<bool>,
166 /// The size of the workspace split drop targets on the outer edges.
167 /// Given as a fraction that will be multiplied by the smaller dimension of the workspace.
168 ///
169 /// Default: `0.2` (20% of the smaller dimension of the workspace)
170 pub drop_target_size: Option<f32>,
171 /// Whether to close the window when using 'close active item' on a workspace with no tabs
172 ///
173 /// Default: auto ("on" on macOS, "off" otherwise)
174 pub when_closing_with_no_tabs: Option<CloseWindowWhenNoItems>,
175 /// Whether to use the system provided dialogs for Open and Save As.
176 /// When set to false, Zed will use the built-in keyboard-first pickers.
177 ///
178 /// Default: true
179 pub use_system_path_prompts: Option<bool>,
180 /// Whether to use the system provided prompts.
181 /// When set to false, Zed will use the built-in prompts.
182 /// Note that this setting has no effect on Linux, where Zed will always
183 /// use the built-in prompts.
184 ///
185 /// Default: true
186 pub use_system_prompts: Option<bool>,
187 /// Aliases for the command palette. When you type a key in this map,
188 /// it will be assumed to equal the value.
189 ///
190 /// Default: true
191 pub command_aliases: Option<HashMap<String, String>>,
192 /// Maximum open tabs in a pane. Will not close an unsaved
193 /// tab. Set to `None` for unlimited tabs.
194 ///
195 /// Default: none
196 pub max_tabs: Option<NonZeroUsize>,
197 /// What to do when the last window is closed
198 ///
199 /// Default: auto (nothing on macOS, "app quit" otherwise)
200 pub on_last_window_closed: Option<OnLastWindowClosed>,
201 /// Whether to automatically close files that have been deleted on disk.
202 ///
203 /// Default: false
204 pub close_on_file_delete: Option<bool>,
205}
206
207#[derive(Deserialize)]
208pub struct TabBarSettings {
209 pub show: bool,
210 pub show_nav_history_buttons: bool,
211 pub show_tab_bar_buttons: bool,
212}
213
214#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
215pub struct TabBarSettingsContent {
216 /// Whether or not to show the tab bar in the editor.
217 ///
218 /// Default: true
219 pub show: Option<bool>,
220 /// Whether or not to show the navigation history buttons in the tab bar.
221 ///
222 /// Default: true
223 pub show_nav_history_buttons: Option<bool>,
224 /// Whether or not to show the tab bar buttons.
225 ///
226 /// Default: true
227 pub show_tab_bar_buttons: Option<bool>,
228}
229
230#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
231#[serde(rename_all = "snake_case")]
232pub enum AutosaveSetting {
233 /// Disable autosave.
234 Off,
235 /// Save after inactivity period of `milliseconds`.
236 AfterDelay { milliseconds: u64 },
237 /// Autosave when focus changes.
238 OnFocusChange,
239 /// Autosave when the active window changes.
240 OnWindowChange,
241}
242
243#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
244#[serde(rename_all = "snake_case")]
245pub enum PaneSplitDirectionHorizontal {
246 Up,
247 Down,
248}
249
250#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
251#[serde(rename_all = "snake_case")]
252pub enum PaneSplitDirectionVertical {
253 Left,
254 Right,
255}
256
257#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
258#[serde(rename_all = "snake_case")]
259pub struct CenteredLayoutSettings {
260 /// The relative width of the left padding of the central pane from the
261 /// workspace when the centered layout is used.
262 ///
263 /// Default: 0.2
264 pub left_padding: Option<f32>,
265 // The relative width of the right padding of the central pane from the
266 // workspace when the centered layout is used.
267 ///
268 /// Default: 0.2
269 pub right_padding: Option<f32>,
270}
271
272impl Settings for WorkspaceSettings {
273 const KEY: Option<&'static str> = None;
274
275 type FileContent = WorkspaceSettingsContent;
276
277 fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
278 sources.json_merge()
279 }
280
281 fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
282 if vscode
283 .read_bool("accessibility.dimUnfocused.enabled")
284 .unwrap_or_default()
285 {
286 if let Some(opacity) = vscode
287 .read_value("accessibility.dimUnfocused.opacity")
288 .and_then(|v| v.as_f64())
289 {
290 if let Some(settings) = current.active_pane_modifiers.as_mut() {
291 settings.inactive_opacity = Some(opacity as f32)
292 } else {
293 current.active_pane_modifiers = Some(ActivePanelModifiers {
294 inactive_opacity: Some(opacity as f32),
295 ..Default::default()
296 })
297 }
298 }
299 }
300
301 vscode.enum_setting(
302 "window.confirmBeforeClose",
303 &mut current.confirm_quit,
304 |s| match s {
305 "always" | "keyboardOnly" => Some(true),
306 "never" => Some(false),
307 _ => None,
308 },
309 );
310
311 vscode.bool_setting(
312 "workbench.editor.restoreViewState",
313 &mut current.restore_on_file_reopen,
314 );
315
316 if let Some(b) = vscode.read_bool("window.closeWhenEmpty") {
317 current.when_closing_with_no_tabs = Some(if b {
318 CloseWindowWhenNoItems::CloseWindow
319 } else {
320 CloseWindowWhenNoItems::KeepWindowOpen
321 })
322 }
323
324 if let Some(b) = vscode.read_bool("files.simpleDialog.enable") {
325 current.use_system_path_prompts = Some(!b);
326 }
327
328 vscode.enum_setting("files.autoSave", &mut current.autosave, |s| match s {
329 "off" => Some(AutosaveSetting::Off),
330 "afterDelay" => Some(AutosaveSetting::AfterDelay {
331 milliseconds: vscode
332 .read_value("files.autoSaveDelay")
333 .and_then(|v| v.as_u64())
334 .unwrap_or(1000),
335 }),
336 "onFocusChange" => Some(AutosaveSetting::OnFocusChange),
337 "onWindowChange" => Some(AutosaveSetting::OnWindowChange),
338 _ => None,
339 });
340
341 // workbench.editor.limit contains "enabled", "value", and "perEditorGroup"
342 // our semantics match if those are set to true, some N, and true respectively.
343 // we'll ignore "perEditorGroup" for now since we only support a global max
344 if let Some(n) = vscode
345 .read_value("workbench.editor.limit.value")
346 .and_then(|v| v.as_u64())
347 .and_then(|n| NonZeroUsize::new(n as usize))
348 {
349 if vscode
350 .read_bool("workbench.editor.limit.enabled")
351 .unwrap_or_default()
352 {
353 current.max_tabs = Some(n)
354 }
355 }
356
357 // some combination of "window.restoreWindows" and "workbench.startupEditor" might
358 // map to our "restore_on_startup"
359
360 // there doesn't seem to be a way to read whether the bottom dock's "justified"
361 // setting is enabled in vscode. that'd be our equivalent to "bottom_dock_layout"
362 }
363}
364
365impl Settings for TabBarSettings {
366 const KEY: Option<&'static str> = Some("tab_bar");
367
368 type FileContent = TabBarSettingsContent;
369
370 fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
371 sources.json_merge()
372 }
373
374 fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
375 vscode.enum_setting(
376 "workbench.editor.showTabs",
377 &mut current.show,
378 |s| match s {
379 "multiple" => Some(true),
380 "single" | "none" => Some(false),
381 _ => None,
382 },
383 );
384 if Some("hidden") == vscode.read_string("workbench.editor.editorActionsLocation") {
385 current.show_tab_bar_buttons = Some(false)
386 }
387 }
388}