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