workspace_settings.rs

  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
257#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
258#[serde(rename_all = "snake_case")]
259pub enum PaneSplitDirectionHorizontal {
260    Up,
261    Down,
262}
263
264#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
265#[serde(rename_all = "snake_case")]
266pub enum PaneSplitDirectionVertical {
267    Left,
268    Right,
269}
270
271#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, SettingsUi)]
272#[serde(rename_all = "snake_case")]
273pub struct CenteredLayoutSettings {
274    /// The relative width of the left padding of the central pane from the
275    /// workspace when the centered layout is used.
276    ///
277    /// Default: 0.2
278    pub left_padding: Option<f32>,
279    // The relative width of the right padding of the central pane from the
280    // workspace when the centered layout is used.
281    ///
282    /// Default: 0.2
283    pub right_padding: Option<f32>,
284}
285
286impl Settings for WorkspaceSettings {
287    type FileContent = WorkspaceSettingsContent;
288
289    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
290        sources.json_merge()
291    }
292
293    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
294        if vscode
295            .read_bool("accessibility.dimUnfocused.enabled")
296            .unwrap_or_default()
297            && let Some(opacity) = vscode
298                .read_value("accessibility.dimUnfocused.opacity")
299                .and_then(|v| v.as_f64())
300        {
301            if let Some(settings) = current.active_pane_modifiers.as_mut() {
302                settings.inactive_opacity = Some(opacity as f32)
303            } else {
304                current.active_pane_modifiers = Some(ActivePanelModifiers {
305                    inactive_opacity: Some(opacity as f32),
306                    ..Default::default()
307                })
308            }
309        }
310
311        vscode.enum_setting(
312            "window.confirmBeforeClose",
313            &mut current.confirm_quit,
314            |s| match s {
315                "always" | "keyboardOnly" => Some(true),
316                "never" => Some(false),
317                _ => None,
318            },
319        );
320
321        vscode.bool_setting(
322            "workbench.editor.restoreViewState",
323            &mut current.restore_on_file_reopen,
324        );
325
326        if let Some(b) = vscode.read_bool("window.closeWhenEmpty") {
327            current.when_closing_with_no_tabs = Some(if b {
328                CloseWindowWhenNoItems::CloseWindow
329            } else {
330                CloseWindowWhenNoItems::KeepWindowOpen
331            })
332        }
333
334        if let Some(b) = vscode.read_bool("files.simpleDialog.enable") {
335            current.use_system_path_prompts = Some(!b);
336        }
337
338        vscode.enum_setting("files.autoSave", &mut current.autosave, |s| match s {
339            "off" => Some(AutosaveSetting::Off),
340            "afterDelay" => Some(AutosaveSetting::AfterDelay {
341                milliseconds: vscode
342                    .read_value("files.autoSaveDelay")
343                    .and_then(|v| v.as_u64())
344                    .unwrap_or(1000),
345            }),
346            "onFocusChange" => Some(AutosaveSetting::OnFocusChange),
347            "onWindowChange" => Some(AutosaveSetting::OnWindowChange),
348            _ => None,
349        });
350
351        // workbench.editor.limit contains "enabled", "value", and "perEditorGroup"
352        // our semantics match if those are set to true, some N, and true respectively.
353        // we'll ignore "perEditorGroup" for now since we only support a global max
354        if let Some(n) = vscode
355            .read_value("workbench.editor.limit.value")
356            .and_then(|v| v.as_u64())
357            .and_then(|n| NonZeroUsize::new(n as usize))
358            && vscode
359                .read_bool("workbench.editor.limit.enabled")
360                .unwrap_or_default()
361        {
362            current.max_tabs = Some(n)
363        }
364
365        vscode.bool_setting("window.nativeTabs", &mut current.use_system_window_tabs);
366
367        // some combination of "window.restoreWindows" and "workbench.startupEditor" might
368        // map to our "restore_on_startup"
369
370        // there doesn't seem to be a way to read whether the bottom dock's "justified"
371        // setting is enabled in vscode. that'd be our equivalent to "bottom_dock_layout"
372    }
373}
374
375impl Settings for TabBarSettings {
376    type FileContent = TabBarSettingsContent;
377
378    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
379        sources.json_merge()
380    }
381
382    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
383        vscode.enum_setting(
384            "workbench.editor.showTabs",
385            &mut current.show,
386            |s| match s {
387                "multiple" => Some(true),
388                "single" | "none" => Some(false),
389                _ => None,
390            },
391        );
392        if Some("hidden") == vscode.read_string("workbench.editor.editorActionsLocation") {
393            current.show_tab_bar_buttons = Some(false)
394        }
395    }
396}