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, SettingsSources};
 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}
 33
 34#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
 35#[serde(rename_all = "snake_case")]
 36pub enum OnLastWindowClosed {
 37    /// Match platform conventions by default, so don't quit on macOS, and quit on other platforms
 38    #[default]
 39    PlatformDefault,
 40    /// Quit the application the last window is closed
 41    QuitApp,
 42}
 43
 44impl OnLastWindowClosed {
 45    pub fn is_quit_app(&self) -> bool {
 46        match self {
 47            OnLastWindowClosed::PlatformDefault => false,
 48            OnLastWindowClosed::QuitApp => true,
 49        }
 50    }
 51}
 52
 53#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
 54#[serde(rename_all = "snake_case")]
 55pub struct ActivePanelModifiers {
 56    /// Size of the border surrounding the active pane.
 57    /// When set to 0, the active pane doesn't have any border.
 58    /// The border is drawn inset.
 59    ///
 60    /// Default: `0.0`
 61    pub border_size: Option<f32>,
 62    /// Opacity of inactive panels.
 63    /// When set to 1.0, the inactive panes have the same opacity as the active one.
 64    /// If set to 0, the inactive panes content will not be visible at all.
 65    /// Values are clamped to the [0.0, 1.0] range.
 66    ///
 67    /// Default: `1.0`
 68    pub inactive_opacity: Option<f32>,
 69}
 70
 71#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, JsonSchema)]
 72#[serde(rename_all = "snake_case")]
 73pub enum BottomDockLayout {
 74    /// Contained between the left and right docks
 75    #[default]
 76    Contained,
 77    /// Takes up the full width of the window
 78    Full,
 79    /// Extends under the left dock while snapping to the right dock
 80    LeftAligned,
 81    /// Extends under the right dock while snapping to the left dock
 82    RightAligned,
 83}
 84
 85#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
 86#[serde(rename_all = "snake_case")]
 87pub enum CloseWindowWhenNoItems {
 88    /// Match platform conventions by default, so "on" on macOS and "off" everywhere else
 89    #[default]
 90    PlatformDefault,
 91    /// Close the window when there are no tabs
 92    CloseWindow,
 93    /// Leave the window open when there are no tabs
 94    KeepWindowOpen,
 95}
 96
 97impl CloseWindowWhenNoItems {
 98    pub fn should_close(&self) -> bool {
 99        match self {
100            CloseWindowWhenNoItems::PlatformDefault => cfg!(target_os = "macos"),
101            CloseWindowWhenNoItems::CloseWindow => true,
102            CloseWindowWhenNoItems::KeepWindowOpen => false,
103        }
104    }
105}
106
107#[derive(Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
108#[serde(rename_all = "snake_case")]
109pub enum RestoreOnStartupBehavior {
110    /// Always start with an empty editor
111    None,
112    /// Restore the workspace that was closed last.
113    LastWorkspace,
114    /// Restore all workspaces that were open when quitting Zed.
115    #[default]
116    LastSession,
117}
118
119#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
120pub struct WorkspaceSettingsContent {
121    /// Active pane styling settings.
122    pub active_pane_modifiers: Option<ActivePanelModifiers>,
123    /// Layout mode for the bottom dock
124    ///
125    /// Default: contained
126    pub bottom_dock_layout: Option<BottomDockLayout>,
127    /// Direction to split horizontally.
128    ///
129    /// Default: "up"
130    pub pane_split_direction_horizontal: Option<PaneSplitDirectionHorizontal>,
131    /// Direction to split vertically.
132    ///
133    /// Default: "left"
134    pub pane_split_direction_vertical: Option<PaneSplitDirectionVertical>,
135    /// Centered layout related settings.
136    pub centered_layout: Option<CenteredLayoutSettings>,
137    /// Whether or not to prompt the user to confirm before closing the application.
138    ///
139    /// Default: false
140    pub confirm_quit: Option<bool>,
141    /// Whether or not to show the call status icon in the status bar.
142    ///
143    /// Default: true
144    pub show_call_status_icon: Option<bool>,
145    /// When to automatically save edited buffers.
146    ///
147    /// Default: off
148    pub autosave: Option<AutosaveSetting>,
149    /// Controls previous session restoration in freshly launched Zed instance.
150    /// Values: none, last_workspace, last_session
151    /// Default: last_session
152    pub restore_on_startup: Option<RestoreOnStartupBehavior>,
153    /// Whether to attempt to restore previous file's state when opening it again.
154    /// The state is stored per pane.
155    /// When disabled, defaults are applied instead of the state restoration.
156    ///
157    /// 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.
158    /// When disabled, a single selection in the very beginning of the file, zero scroll position and no folds state is used as a default.
159    ///
160    /// Default: true
161    pub restore_on_file_reopen: Option<bool>,
162    /// The size of the workspace split drop targets on the outer edges.
163    /// Given as a fraction that will be multiplied by the smaller dimension of the workspace.
164    ///
165    /// Default: `0.2` (20% of the smaller dimension of the workspace)
166    pub drop_target_size: Option<f32>,
167    /// Whether to close the window when using 'close active item' on a workspace with no tabs
168    ///
169    /// Default: auto ("on" on macOS, "off" otherwise)
170    pub when_closing_with_no_tabs: Option<CloseWindowWhenNoItems>,
171    /// Whether to use the system provided dialogs for Open and Save As.
172    /// When set to false, Zed will use the built-in keyboard-first pickers.
173    ///
174    /// Default: true
175    pub use_system_path_prompts: Option<bool>,
176    /// Whether to use the system provided prompts.
177    /// When set to false, Zed will use the built-in prompts.
178    /// Note that this setting has no effect on Linux, where Zed will always
179    /// use the built-in prompts.
180    ///
181    /// Default: true
182    pub use_system_prompts: Option<bool>,
183    /// Aliases for the command palette. When you type a key in this map,
184    /// it will be assumed to equal the value.
185    ///
186    /// Default: true
187    pub command_aliases: Option<HashMap<String, String>>,
188    /// Maximum open tabs in a pane. Will not close an unsaved
189    /// tab. Set to `None` for unlimited tabs.
190    ///
191    /// Default: none
192    pub max_tabs: Option<NonZeroUsize>,
193    /// What to do when the last window is closed
194    ///
195    /// Default: auto (nothing on macOS, "app quit" otherwise)
196    pub on_last_window_closed: Option<OnLastWindowClosed>,
197    /// Whether to resize all the panels in a dock when resizing the dock.
198    ///
199    /// Default: ["left"]
200    pub resize_all_panels_in_dock: Option<Vec<DockPosition>>,
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}