workspace_settings.rs

  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}
 30
 31#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
 32#[serde(rename_all = "snake_case")]
 33pub enum OnLastWindowClosed {
 34    /// Match platform conventions by default, so don't quit on macOS, and quit on other platforms
 35    #[default]
 36    PlatformDefault,
 37    /// Quit the application the last window is closed
 38    QuitApp,
 39}
 40
 41impl OnLastWindowClosed {
 42    pub fn is_quit_app(&self) -> bool {
 43        match self {
 44            OnLastWindowClosed::PlatformDefault => false,
 45            OnLastWindowClosed::QuitApp => true,
 46        }
 47    }
 48}
 49
 50#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
 51#[serde(rename_all = "snake_case")]
 52pub struct ActivePanelModifiers {
 53    /// Scale by which to zoom the active pane.
 54    /// When set to 1.0, the active pane has the same size as others,
 55    /// but when set to a larger value, the active pane takes up more space.
 56    ///
 57    /// Default: `1.0`
 58    pub magnification: Option<f32>,
 59    /// Size of the border surrounding the active pane.
 60    /// When set to 0, the active pane doesn't have any border.
 61    /// The border is drawn inset.
 62    ///
 63    /// Default: `0.0`
 64    pub border_size: Option<f32>,
 65    /// Opacity of inactive panels.
 66    /// When set to 1.0, the inactive panes have the same opacity as the active one.
 67    /// If set to 0, the inactive panes content will not be visible at all.
 68    /// Values are clamped to the [0.0, 1.0] range.
 69    ///
 70    /// Default: `1.0`
 71    pub inactive_opacity: Option<f32>,
 72}
 73
 74#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, JsonSchema)]
 75#[serde(rename_all = "snake_case")]
 76pub enum BottomDockLayout {
 77    /// Contained between the left and right docks
 78    #[default]
 79    Contained,
 80    /// Takes up the full width of the window
 81    Full,
 82    /// Extends under the left dock while snapping to the right dock
 83    LeftAligned,
 84    /// Extends under the right dock while snapping to the left dock
 85    RightAligned,
 86}
 87
 88#[derive(Copy, Clone, Default, Serialize, Deserialize, JsonSchema)]
 89#[serde(rename_all = "snake_case")]
 90pub enum CloseWindowWhenNoItems {
 91    /// Match platform conventions by default, so "on" on macOS and "off" everywhere else
 92    #[default]
 93    PlatformDefault,
 94    /// Close the window when there are no tabs
 95    CloseWindow,
 96    /// Leave the window open when there are no tabs
 97    KeepWindowOpen,
 98}
 99
100impl CloseWindowWhenNoItems {
101    pub fn should_close(&self) -> bool {
102        match self {
103            CloseWindowWhenNoItems::PlatformDefault => cfg!(target_os = "macos"),
104            CloseWindowWhenNoItems::CloseWindow => true,
105            CloseWindowWhenNoItems::KeepWindowOpen => false,
106        }
107    }
108}
109
110#[derive(Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
111#[serde(rename_all = "snake_case")]
112pub enum RestoreOnStartupBehavior {
113    /// Always start with an empty editor
114    None,
115    /// Restore the workspace that was closed last.
116    LastWorkspace,
117    /// Restore all workspaces that were open when quitting Zed.
118    #[default]
119    LastSession,
120}
121
122#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
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}
201
202#[derive(Deserialize)]
203pub struct TabBarSettings {
204    pub show: bool,
205    pub show_nav_history_buttons: bool,
206    pub show_tab_bar_buttons: bool,
207}
208
209#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
210pub struct TabBarSettingsContent {
211    /// Whether or not to show the tab bar in the editor.
212    ///
213    /// Default: true
214    pub show: Option<bool>,
215    /// Whether or not to show the navigation history buttons in the tab bar.
216    ///
217    /// Default: true
218    pub show_nav_history_buttons: Option<bool>,
219    /// Whether or not to show the tab bar buttons.
220    ///
221    /// Default: true
222    pub show_tab_bar_buttons: Option<bool>,
223}
224
225#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
226#[serde(rename_all = "snake_case")]
227pub enum AutosaveSetting {
228    /// Disable autosave.
229    Off,
230    /// Save after inactivity period of `milliseconds`.
231    AfterDelay { milliseconds: u64 },
232    /// Autosave when focus changes.
233    OnFocusChange,
234    /// Autosave when the active window changes.
235    OnWindowChange,
236}
237
238#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
239#[serde(rename_all = "snake_case")]
240pub enum PaneSplitDirectionHorizontal {
241    Up,
242    Down,
243}
244
245#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
246#[serde(rename_all = "snake_case")]
247pub enum PaneSplitDirectionVertical {
248    Left,
249    Right,
250}
251
252#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
253#[serde(rename_all = "snake_case")]
254pub struct CenteredLayoutSettings {
255    /// The relative width of the left padding of the central pane from the
256    /// workspace when the centered layout is used.
257    ///
258    /// Default: 0.2
259    pub left_padding: Option<f32>,
260    // The relative width of the right padding of the central pane from the
261    // workspace when the centered layout is used.
262    ///
263    /// Default: 0.2
264    pub right_padding: Option<f32>,
265}
266
267impl Settings for WorkspaceSettings {
268    const KEY: Option<&'static str> = None;
269
270    type FileContent = WorkspaceSettingsContent;
271
272    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
273        sources.json_merge()
274    }
275
276    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
277        if vscode
278            .read_bool("accessibility.dimUnfocused.enabled")
279            .unwrap_or_default()
280        {
281            if let Some(opacity) = vscode
282                .read_value("accessibility.dimUnfocused.opacity")
283                .and_then(|v| v.as_f64())
284            {
285                if let Some(settings) = current.active_pane_modifiers.as_mut() {
286                    settings.inactive_opacity = Some(opacity as f32)
287                } else {
288                    current.active_pane_modifiers = Some(ActivePanelModifiers {
289                        inactive_opacity: Some(opacity as f32),
290                        ..Default::default()
291                    })
292                }
293            }
294        }
295
296        vscode.enum_setting(
297            "window.confirmBeforeClose",
298            &mut current.confirm_quit,
299            |s| match s {
300                "always" | "keyboardOnly" => Some(true),
301                "never" => Some(false),
302                _ => None,
303            },
304        );
305
306        vscode.bool_setting(
307            "workbench.editor.restoreViewState",
308            &mut current.restore_on_file_reopen,
309        );
310
311        if let Some(b) = vscode.read_bool("window.closeWhenEmpty") {
312            current.when_closing_with_no_tabs = Some(if b {
313                CloseWindowWhenNoItems::CloseWindow
314            } else {
315                CloseWindowWhenNoItems::KeepWindowOpen
316            })
317        }
318
319        if let Some(b) = vscode.read_bool("files.simpleDialog.enable") {
320            current.use_system_path_prompts = Some(!b);
321        }
322
323        vscode.enum_setting("files.autoSave", &mut current.autosave, |s| match s {
324            "off" => Some(AutosaveSetting::Off),
325            "afterDelay" => Some(AutosaveSetting::AfterDelay {
326                milliseconds: vscode
327                    .read_value("files.autoSaveDelay")
328                    .and_then(|v| v.as_u64())
329                    .unwrap_or(1000),
330            }),
331            "onFocusChange" => Some(AutosaveSetting::OnFocusChange),
332            "onWindowChange" => Some(AutosaveSetting::OnWindowChange),
333            _ => None,
334        });
335
336        // workbench.editor.limit contains "enabled", "value", and "perEditorGroup"
337        // our semantics match if those are set to true, some N, and true respectively.
338        // we'll ignore "perEditorGroup" for now since we only support a global max
339        if let Some(n) = vscode
340            .read_value("workbench.editor.limit.value")
341            .and_then(|v| v.as_u64())
342            .and_then(|n| NonZeroUsize::new(n as usize))
343        {
344            if vscode
345                .read_bool("workbench.editor.limit.enabled")
346                .unwrap_or_default()
347            {
348                current.max_tabs = Some(n)
349            }
350        }
351
352        // some combination of "window.restoreWindows" and "workbench.startupEditor" might
353        // map to our "restore_on_startup"
354
355        // there doesn't seem to be a way to read whether the bottom dock's "justified"
356        // setting is enabled in vscode. that'd be our equivalent to "bottom_dock_layout"
357    }
358}
359
360impl Settings for TabBarSettings {
361    const KEY: Option<&'static str> = Some("tab_bar");
362
363    type FileContent = TabBarSettingsContent;
364
365    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
366        sources.json_merge()
367    }
368
369    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
370        vscode.enum_setting(
371            "workbench.editor.showTabs",
372            &mut current.show,
373            |s| match s {
374                "multiple" => Some(true),
375                "single" | "none" => Some(false),
376                _ => None,
377            },
378        );
379        if Some("hidden") == vscode.read_string("workbench.editor.editorActionsLocation") {
380            current.show_tab_bar_buttons = Some(false)
381        }
382    }
383}