1use std::num::NonZeroUsize;
2
3use crate::DockPosition;
4use collections::HashMap;
5use gpui::App;
6use serde::Deserialize;
7pub use settings::AutosaveSetting;
8use settings::Settings;
9pub use settings::{
10 BottomDockLayout, PaneSplitDirectionHorizontal, PaneSplitDirectionVertical,
11 RestoreOnStartupBehavior,
12};
13use util::MergeFrom as _;
14
15pub struct WorkspaceSettings {
16 pub active_pane_modifiers: ActivePanelModifiers,
17 pub bottom_dock_layout: settings::BottomDockLayout,
18 pub pane_split_direction_horizontal: settings::PaneSplitDirectionHorizontal,
19 pub pane_split_direction_vertical: settings::PaneSplitDirectionVertical,
20 pub centered_layout: settings::CenteredLayoutSettings,
21 pub confirm_quit: bool,
22 pub show_call_status_icon: bool,
23 pub autosave: AutosaveSetting,
24 pub restore_on_startup: settings::RestoreOnStartupBehavior,
25 pub restore_on_file_reopen: bool,
26 pub drop_target_size: f32,
27 pub use_system_path_prompts: bool,
28 pub use_system_prompts: bool,
29 pub command_aliases: HashMap<String, String>,
30 pub max_tabs: Option<NonZeroUsize>,
31 pub when_closing_with_no_tabs: settings::CloseWindowWhenNoItems,
32 pub on_last_window_closed: settings::OnLastWindowClosed,
33 pub resize_all_panels_in_dock: Vec<DockPosition>,
34 pub close_on_file_delete: bool,
35 pub use_system_window_tabs: bool,
36 pub zoomed_padding: bool,
37}
38
39#[derive(Copy, Clone, PartialEq, Debug, Default)]
40pub struct ActivePanelModifiers {
41 /// Size of the border surrounding the active pane.
42 /// When set to 0, the active pane doesn't have any border.
43 /// The border is drawn inset.
44 ///
45 /// Default: `0.0`
46 // TODO: make this not an option, it is never None
47 pub border_size: Option<f32>,
48 /// Opacity of inactive panels.
49 /// When set to 1.0, the inactive panes have the same opacity as the active one.
50 /// If set to 0, the inactive panes content will not be visible at all.
51 /// Values are clamped to the [0.0, 1.0] range.
52 ///
53 /// Default: `1.0`
54 // TODO: make this not an option, it is never None
55 pub inactive_opacity: Option<f32>,
56}
57
58#[derive(Deserialize)]
59pub struct TabBarSettings {
60 pub show: bool,
61 pub show_nav_history_buttons: bool,
62 pub show_tab_bar_buttons: bool,
63}
64
65impl Settings for WorkspaceSettings {
66 fn from_defaults(content: &settings::SettingsContent, _cx: &mut App) -> Self {
67 let workspace = &content.workspace;
68 Self {
69 active_pane_modifiers: ActivePanelModifiers {
70 border_size: Some(
71 workspace
72 .active_pane_modifiers
73 .unwrap()
74 .border_size
75 .unwrap(),
76 ),
77 inactive_opacity: Some(
78 workspace
79 .active_pane_modifiers
80 .unwrap()
81 .inactive_opacity
82 .unwrap(),
83 ),
84 },
85 bottom_dock_layout: workspace.bottom_dock_layout.unwrap(),
86 pane_split_direction_horizontal: workspace.pane_split_direction_horizontal.unwrap(),
87 pane_split_direction_vertical: workspace.pane_split_direction_vertical.unwrap(),
88 centered_layout: workspace.centered_layout.unwrap(),
89 confirm_quit: workspace.confirm_quit.unwrap(),
90 show_call_status_icon: workspace.show_call_status_icon.unwrap(),
91 autosave: workspace.autosave.unwrap(),
92 restore_on_startup: workspace.restore_on_startup.unwrap(),
93 restore_on_file_reopen: workspace.restore_on_file_reopen.unwrap(),
94 drop_target_size: workspace.drop_target_size.unwrap(),
95 use_system_path_prompts: workspace.use_system_path_prompts.unwrap(),
96 use_system_prompts: workspace.use_system_prompts.unwrap(),
97 command_aliases: workspace.command_aliases.clone(),
98 max_tabs: workspace.max_tabs,
99 when_closing_with_no_tabs: workspace.when_closing_with_no_tabs.unwrap(),
100 on_last_window_closed: workspace.on_last_window_closed.unwrap(),
101 resize_all_panels_in_dock: workspace
102 .resize_all_panels_in_dock
103 .clone()
104 .unwrap()
105 .into_iter()
106 .map(Into::into)
107 .collect(),
108 close_on_file_delete: workspace.close_on_file_delete.unwrap(),
109 use_system_window_tabs: workspace.use_system_window_tabs.unwrap(),
110 zoomed_padding: workspace.zoomed_padding.unwrap(),
111 }
112 }
113
114 fn refine(&mut self, content: &settings::SettingsContent, _cx: &mut App) {
115 let workspace = &content.workspace;
116 if let Some(border_size) = workspace
117 .active_pane_modifiers
118 .and_then(|modifier| modifier.border_size)
119 {
120 self.active_pane_modifiers.border_size = Some(border_size);
121 }
122
123 if let Some(inactive_opacity) = workspace
124 .active_pane_modifiers
125 .and_then(|modifier| modifier.inactive_opacity)
126 {
127 self.active_pane_modifiers.inactive_opacity = Some(inactive_opacity);
128 }
129
130 self.bottom_dock_layout
131 .merge_from(&workspace.bottom_dock_layout);
132 self.pane_split_direction_horizontal
133 .merge_from(&workspace.pane_split_direction_horizontal);
134 self.pane_split_direction_vertical
135 .merge_from(&workspace.pane_split_direction_vertical);
136 self.centered_layout.merge_from(&workspace.centered_layout);
137 self.confirm_quit.merge_from(&workspace.confirm_quit);
138 self.show_call_status_icon
139 .merge_from(&workspace.show_call_status_icon);
140 self.autosave.merge_from(&workspace.autosave);
141 self.restore_on_startup
142 .merge_from(&workspace.restore_on_startup);
143 self.restore_on_file_reopen
144 .merge_from(&workspace.restore_on_file_reopen);
145 self.drop_target_size
146 .merge_from(&workspace.drop_target_size);
147 self.use_system_path_prompts
148 .merge_from(&workspace.use_system_path_prompts);
149 self.use_system_prompts
150 .merge_from(&workspace.use_system_prompts);
151 self.command_aliases
152 .extend(workspace.command_aliases.clone());
153 if let Some(max_tabs) = workspace.max_tabs {
154 self.max_tabs = Some(max_tabs);
155 }
156 self.when_closing_with_no_tabs
157 .merge_from(&workspace.when_closing_with_no_tabs);
158 self.on_last_window_closed
159 .merge_from(&workspace.on_last_window_closed);
160 self.resize_all_panels_in_dock.merge_from(
161 &workspace
162 .resize_all_panels_in_dock
163 .as_ref()
164 .map(|resize| resize.clone().into_iter().map(Into::into).collect()),
165 );
166 self.close_on_file_delete
167 .merge_from(&workspace.close_on_file_delete);
168 self.use_system_window_tabs
169 .merge_from(&workspace.use_system_window_tabs);
170 self.zoomed_padding.merge_from(&workspace.zoomed_padding);
171 }
172
173 fn import_from_vscode(
174 vscode: &settings::VsCodeSettings,
175 current: &mut settings::SettingsContent,
176 ) {
177 if vscode
178 .read_bool("accessibility.dimUnfocused.enabled")
179 .unwrap_or_default()
180 && let Some(opacity) = vscode
181 .read_value("accessibility.dimUnfocused.opacity")
182 .and_then(|v| v.as_f64())
183 {
184 current
185 .workspace
186 .active_pane_modifiers
187 .get_or_insert_default()
188 .inactive_opacity = Some(opacity as f32);
189 }
190
191 vscode.enum_setting(
192 "window.confirmBeforeClose",
193 &mut current.workspace.confirm_quit,
194 |s| match s {
195 "always" | "keyboardOnly" => Some(true),
196 "never" => Some(false),
197 _ => None,
198 },
199 );
200
201 vscode.bool_setting(
202 "workbench.editor.restoreViewState",
203 &mut current.workspace.restore_on_file_reopen,
204 );
205
206 if let Some(b) = vscode.read_bool("window.closeWhenEmpty") {
207 current.workspace.when_closing_with_no_tabs = Some(if b {
208 settings::CloseWindowWhenNoItems::CloseWindow
209 } else {
210 settings::CloseWindowWhenNoItems::KeepWindowOpen
211 });
212 }
213
214 if let Some(b) = vscode.read_bool("files.simpleDialog.enable") {
215 current.workspace.use_system_path_prompts = Some(!b);
216 }
217
218 if let Some(v) = vscode.read_enum("files.autoSave", |s| match s {
219 "off" => Some(AutosaveSetting::Off),
220 "afterDelay" => Some(AutosaveSetting::AfterDelay {
221 milliseconds: vscode
222 .read_value("files.autoSaveDelay")
223 .and_then(|v| v.as_u64())
224 .unwrap_or(1000),
225 }),
226 "onFocusChange" => Some(AutosaveSetting::OnFocusChange),
227 "onWindowChange" => Some(AutosaveSetting::OnWindowChange),
228 _ => None,
229 }) {
230 current.workspace.autosave = Some(v);
231 }
232
233 // workbench.editor.limit contains "enabled", "value", and "perEditorGroup"
234 // our semantics match if those are set to true, some N, and true respectively.
235 // we'll ignore "perEditorGroup" for now since we only support a global max
236 if let Some(n) = vscode
237 .read_value("workbench.editor.limit.value")
238 .and_then(|v| v.as_u64())
239 .and_then(|n| NonZeroUsize::new(n as usize))
240 && vscode
241 .read_bool("workbench.editor.limit.enabled")
242 .unwrap_or_default()
243 {
244 current.workspace.max_tabs = Some(n)
245 }
246
247 if let Some(b) = vscode.read_bool("window.nativeTabs") {
248 current.workspace.use_system_window_tabs = Some(b);
249 }
250
251 // some combination of "window.restoreWindows" and "workbench.startupEditor" might
252 // map to our "restore_on_startup"
253
254 // there doesn't seem to be a way to read whether the bottom dock's "justified"
255 // setting is enabled in vscode. that'd be our equivalent to "bottom_dock_layout"
256 }
257}
258
259impl Settings for TabBarSettings {
260 fn from_defaults(content: &settings::SettingsContent, _cx: &mut App) -> Self {
261 let tab_bar = content.tab_bar.clone().unwrap();
262 TabBarSettings {
263 show: tab_bar.show.unwrap(),
264 show_nav_history_buttons: tab_bar.show_nav_history_buttons.unwrap(),
265 show_tab_bar_buttons: tab_bar.show_tab_bar_buttons.unwrap(),
266 }
267 }
268
269 fn refine(&mut self, content: &settings::SettingsContent, _cx: &mut App) {
270 let Some(tab_bar) = &content.tab_bar else {
271 return;
272 };
273 self.show.merge_from(&tab_bar.show);
274 self.show_nav_history_buttons
275 .merge_from(&tab_bar.show_nav_history_buttons);
276 self.show_tab_bar_buttons
277 .merge_from(&tab_bar.show_tab_bar_buttons);
278 }
279
280 fn import_from_vscode(
281 vscode: &settings::VsCodeSettings,
282 current: &mut settings::SettingsContent,
283 ) {
284 if let Some(b) = vscode.read_enum("workbench.editor.showTabs", |s| match s {
285 "multiple" => Some(true),
286 "single" | "none" => Some(false),
287 _ => None,
288 }) {
289 current.tab_bar.get_or_insert_default().show = Some(b);
290 }
291 if Some("hidden") == vscode.read_string("workbench.editor.editorActionsLocation") {
292 current.tab_bar.get_or_insert_default().show_tab_bar_buttons = Some(false)
293 }
294 }
295}