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