-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
layoutState.ts
295 lines (239 loc) · 13.4 KB
/
layoutState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getClientArea } from 'vs/base/browser/dom';
import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { IConfigurationChangeEvent, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { PanelAlignment, Position, positionFromString, positionToString } from 'vs/workbench/services/layout/browser/layoutService';
interface IWorkbenchLayoutStateKey {
readonly name: string;
readonly runtime: boolean;
readonly defaultValue: unknown;
readonly scope: StorageScope;
readonly target: StorageTarget;
readonly zenModeIgnore?: boolean;
}
type StorageKeyType = string | boolean | number | object;
abstract class WorkbenchLayoutStateKey<T extends StorageKeyType> implements IWorkbenchLayoutStateKey {
abstract readonly runtime: boolean;
constructor(readonly name: string, readonly scope: StorageScope, readonly target: StorageTarget, public defaultValue: T) { }
}
class RuntimeStateKey<T extends StorageKeyType> extends WorkbenchLayoutStateKey<T> {
readonly runtime = true;
constructor(name: string, scope: StorageScope, target: StorageTarget, defaultValue: T, readonly zenModeIgnore?: boolean) {
super(name, scope, target, defaultValue);
}
}
class InitializationStateKey<T extends StorageKeyType> extends WorkbenchLayoutStateKey<T> {
readonly runtime = false;
}
export const LayoutStateKeys = {
// Editor
EDITOR_CENTERED: new RuntimeStateKey<boolean>('editor.centered', StorageScope.WORKSPACE, StorageTarget.USER, false),
// Zen Mode
ZEN_MODE_ACTIVE: new RuntimeStateKey<boolean>('zenMode.active', StorageScope.WORKSPACE, StorageTarget.USER, false),
ZEN_MODE_EXIT_INFO: new RuntimeStateKey('zenMode.exitInfo', StorageScope.WORKSPACE, StorageTarget.USER, {
transitionedToCenteredEditorLayout: false,
transitionedToFullScreen: false,
handleNotificationsDoNotDisturbMode: false,
wasVisible: {
auxiliaryBar: false,
panel: false,
sideBar: false,
},
}),
// Part Sizing
GRID_SIZE: new InitializationStateKey('grid.size', StorageScope.PROFILE, StorageTarget.MACHINE, { width: 800, height: 600 }),
SIDEBAR_SIZE: new InitializationStateKey<number>('sideBar.size', StorageScope.PROFILE, StorageTarget.MACHINE, 200),
AUXILIARYBAR_SIZE: new InitializationStateKey<number>('auxiliaryBar.size', StorageScope.PROFILE, StorageTarget.MACHINE, 200),
PANEL_SIZE: new InitializationStateKey<number>('panel.size', StorageScope.PROFILE, StorageTarget.MACHINE, 300),
PANEL_LAST_NON_MAXIMIZED_HEIGHT: new RuntimeStateKey<number>('panel.lastNonMaximizedHeight', StorageScope.PROFILE, StorageTarget.MACHINE, 300),
PANEL_LAST_NON_MAXIMIZED_WIDTH: new RuntimeStateKey<number>('panel.lastNonMaximizedWidth', StorageScope.PROFILE, StorageTarget.MACHINE, 300),
PANEL_WAS_LAST_MAXIMIZED: new RuntimeStateKey<boolean>('panel.wasLastMaximized', StorageScope.WORKSPACE, StorageTarget.USER, false),
// Part Positions
SIDEBAR_POSITON: new RuntimeStateKey<Position>('sideBar.position', StorageScope.WORKSPACE, StorageTarget.USER, Position.LEFT),
PANEL_POSITION: new RuntimeStateKey<Position>('panel.position', StorageScope.WORKSPACE, StorageTarget.USER, Position.BOTTOM),
PANEL_ALIGNMENT: new RuntimeStateKey<PanelAlignment>('panel.alignment', StorageScope.PROFILE, StorageTarget.USER, 'center'),
// Part Visibility
ACTIVITYBAR_HIDDEN: new RuntimeStateKey<boolean>('activityBar.hidden', StorageScope.WORKSPACE, StorageTarget.USER, false, true),
SIDEBAR_HIDDEN: new RuntimeStateKey<boolean>('sideBar.hidden', StorageScope.WORKSPACE, StorageTarget.USER, false),
EDITOR_HIDDEN: new RuntimeStateKey<boolean>('editor.hidden', StorageScope.WORKSPACE, StorageTarget.USER, false),
PANEL_HIDDEN: new RuntimeStateKey<boolean>('panel.hidden', StorageScope.WORKSPACE, StorageTarget.USER, true),
AUXILIARYBAR_HIDDEN: new RuntimeStateKey<boolean>('auxiliaryBar.hidden', StorageScope.WORKSPACE, StorageTarget.USER, true),
STATUSBAR_HIDDEN: new RuntimeStateKey<boolean>('statusBar.hidden', StorageScope.WORKSPACE, StorageTarget.USER, false, true)
} as const;
interface ILayoutStateChangeEvent<T extends StorageKeyType> {
readonly key: RuntimeStateKey<T>;
readonly value: T;
}
export enum WorkbenchLayoutSettings {
PANEL_POSITION = 'workbench.panel.defaultLocation',
PANEL_OPENS_MAXIMIZED = 'workbench.panel.opensMaximized',
ZEN_MODE_CONFIG = 'zenMode',
ZEN_MODE_SILENT_NOTIFICATIONS = 'zenMode.silentNotifications',
EDITOR_CENTERED_LAYOUT_AUTO_RESIZE = 'workbench.editor.centeredLayoutAutoResize',
}
enum LegacyWorkbenchLayoutSettings {
ACTIVITYBAR_VISIBLE = 'workbench.activityBar.visible', // Deprecated to UI State
STATUSBAR_VISIBLE = 'workbench.statusBar.visible', // Deprecated to UI State
SIDEBAR_POSITION = 'workbench.sideBar.location', // Deprecated to UI State
}
export class LayoutStateModel extends Disposable {
static readonly STORAGE_PREFIX = 'workbench.';
private readonly _onDidChangeState = this._register(new Emitter<ILayoutStateChangeEvent<StorageKeyType>>());
readonly onDidChangeState = this._onDidChangeState.event;
private readonly stateCache = new Map<string, unknown>();
constructor(
private readonly storageService: IStorageService,
private readonly configurationService: IConfigurationService,
private readonly contextService: IWorkspaceContextService,
private readonly container: HTMLElement
) {
super();
this._register(this.configurationService.onDidChangeConfiguration(configurationChange => this.updateStateFromLegacySettings(configurationChange)));
}
private updateStateFromLegacySettings(configurationChangeEvent: IConfigurationChangeEvent): void {
const isZenMode = this.getRuntimeValue(LayoutStateKeys.ZEN_MODE_ACTIVE);
if (configurationChangeEvent.affectsConfiguration(LegacyWorkbenchLayoutSettings.ACTIVITYBAR_VISIBLE) && !isZenMode) {
this.setRuntimeValueAndFire(LayoutStateKeys.ACTIVITYBAR_HIDDEN, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.ACTIVITYBAR_VISIBLE));
}
if (configurationChangeEvent.affectsConfiguration(LegacyWorkbenchLayoutSettings.STATUSBAR_VISIBLE) && !isZenMode) {
this.setRuntimeValueAndFire(LayoutStateKeys.STATUSBAR_HIDDEN, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.STATUSBAR_VISIBLE));
}
if (configurationChangeEvent.affectsConfiguration(LegacyWorkbenchLayoutSettings.SIDEBAR_POSITION)) {
this.setRuntimeValueAndFire(LayoutStateKeys.SIDEBAR_POSITON, positionFromString(this.configurationService.getValue(LegacyWorkbenchLayoutSettings.SIDEBAR_POSITION) ?? 'left'));
}
}
private updateLegacySettingsFromState<T extends StorageKeyType>(key: RuntimeStateKey<T>, value: T): void {
const isZenMode = this.getRuntimeValue(LayoutStateKeys.ZEN_MODE_ACTIVE);
if (key.zenModeIgnore && isZenMode) {
return;
}
if (key === LayoutStateKeys.ACTIVITYBAR_HIDDEN) {
this.configurationService.updateValue(LegacyWorkbenchLayoutSettings.ACTIVITYBAR_VISIBLE, !value);
} else if (key === LayoutStateKeys.STATUSBAR_HIDDEN) {
this.configurationService.updateValue(LegacyWorkbenchLayoutSettings.STATUSBAR_VISIBLE, !value);
} else if (key === LayoutStateKeys.SIDEBAR_POSITON) {
this.configurationService.updateValue(LegacyWorkbenchLayoutSettings.SIDEBAR_POSITION, positionToString(value as Position));
}
}
load(): void {
let key: keyof typeof LayoutStateKeys;
// Load stored values for all keys
for (key in LayoutStateKeys) {
const stateKey = LayoutStateKeys[key] as WorkbenchLayoutStateKey<StorageKeyType>;
const value = this.loadKeyFromStorage(stateKey);
if (value !== undefined) {
this.stateCache.set(stateKey.name, value);
}
}
// Apply legacy settings
this.stateCache.set(LayoutStateKeys.ACTIVITYBAR_HIDDEN.name, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.ACTIVITYBAR_VISIBLE));
this.stateCache.set(LayoutStateKeys.STATUSBAR_HIDDEN.name, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.STATUSBAR_VISIBLE));
this.stateCache.set(LayoutStateKeys.SIDEBAR_POSITON.name, positionFromString(this.configurationService.getValue(LegacyWorkbenchLayoutSettings.SIDEBAR_POSITION) ?? 'left'));
// Set dynamic defaults: part sizing and side bar visibility
const workbenchDimensions = getClientArea(this.container);
LayoutStateKeys.PANEL_POSITION.defaultValue = positionFromString(this.configurationService.getValue(WorkbenchLayoutSettings.PANEL_POSITION) ?? 'bottom');
LayoutStateKeys.GRID_SIZE.defaultValue = { height: workbenchDimensions.height, width: workbenchDimensions.width };
LayoutStateKeys.SIDEBAR_SIZE.defaultValue = Math.min(300, workbenchDimensions.width / 4);
LayoutStateKeys.AUXILIARYBAR_SIZE.defaultValue = Math.min(300, workbenchDimensions.width / 4);
LayoutStateKeys.PANEL_SIZE.defaultValue = (this.stateCache.get(LayoutStateKeys.PANEL_POSITION.name) ?? LayoutStateKeys.PANEL_POSITION.defaultValue) === 'bottom' ? workbenchDimensions.height / 3 : workbenchDimensions.width / 4;
LayoutStateKeys.SIDEBAR_HIDDEN.defaultValue = this.contextService.getWorkbenchState() === WorkbenchState.EMPTY;
// Apply all defaults
for (key in LayoutStateKeys) {
const stateKey = LayoutStateKeys[key];
if (this.stateCache.get(stateKey.name) === undefined) {
this.stateCache.set(stateKey.name, stateKey.defaultValue);
}
}
// Register for runtime key changes
this._register(this.storageService.onDidChangeValue(storageChangeEvent => {
let key: keyof typeof LayoutStateKeys;
for (key in LayoutStateKeys) {
const stateKey = LayoutStateKeys[key] as WorkbenchLayoutStateKey<StorageKeyType>;
if (stateKey instanceof RuntimeStateKey && stateKey.scope === StorageScope.PROFILE && stateKey.target === StorageTarget.USER) {
if (`${LayoutStateModel.STORAGE_PREFIX}${stateKey.name}` === storageChangeEvent.key) {
const value = this.loadKeyFromStorage(stateKey) ?? stateKey.defaultValue;
if (this.stateCache.get(stateKey.name) !== value) {
this.stateCache.set(stateKey.name, value);
this._onDidChangeState.fire({ key: stateKey, value });
}
}
}
}
}));
}
save(workspace: boolean, global: boolean): void {
let key: keyof typeof LayoutStateKeys;
const isZenMode = this.getRuntimeValue(LayoutStateKeys.ZEN_MODE_ACTIVE);
for (key in LayoutStateKeys) {
const stateKey = LayoutStateKeys[key] as WorkbenchLayoutStateKey<StorageKeyType>;
if ((workspace && stateKey.scope === StorageScope.WORKSPACE) ||
(global && stateKey.scope === StorageScope.PROFILE)) {
if (isZenMode && stateKey instanceof RuntimeStateKey && stateKey.zenModeIgnore) {
continue; // Don't write out specific keys while in zen mode
}
this.saveKeyToStorage(stateKey);
}
}
}
getInitializationValue<T extends StorageKeyType>(key: InitializationStateKey<T>): T {
return this.stateCache.get(key.name) as T;
}
setInitializationValue<T extends StorageKeyType>(key: InitializationStateKey<T>, value: T): void {
this.stateCache.set(key.name, value);
}
getRuntimeValue<T extends StorageKeyType>(key: RuntimeStateKey<T>, fallbackToSetting?: boolean): T {
if (fallbackToSetting) {
switch (key) {
case LayoutStateKeys.ACTIVITYBAR_HIDDEN:
this.stateCache.set(key.name, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.ACTIVITYBAR_VISIBLE));
break;
case LayoutStateKeys.STATUSBAR_HIDDEN:
this.stateCache.set(key.name, !this.configurationService.getValue(LegacyWorkbenchLayoutSettings.STATUSBAR_VISIBLE));
break;
case LayoutStateKeys.SIDEBAR_POSITON:
this.stateCache.set(key.name, this.configurationService.getValue(LegacyWorkbenchLayoutSettings.SIDEBAR_POSITION) ?? 'left');
break;
}
}
return this.stateCache.get(key.name) as T;
}
setRuntimeValue<T extends StorageKeyType>(key: RuntimeStateKey<T>, value: T): void {
this.stateCache.set(key.name, value);
const isZenMode = this.getRuntimeValue(LayoutStateKeys.ZEN_MODE_ACTIVE);
if (key.scope === StorageScope.PROFILE) {
if (!isZenMode || !key.zenModeIgnore) {
this.saveKeyToStorage<T>(key);
this.updateLegacySettingsFromState(key, value);
}
}
}
private setRuntimeValueAndFire<T extends StorageKeyType>(key: RuntimeStateKey<T>, value: T): void {
const previousValue = this.stateCache.get(key.name);
if (previousValue === value) {
return;
}
this.setRuntimeValue(key, value);
this._onDidChangeState.fire({ key, value });
}
private saveKeyToStorage<T extends StorageKeyType>(key: WorkbenchLayoutStateKey<T>): void {
const value = this.stateCache.get(key.name) as T;
this.storageService.store(`${LayoutStateModel.STORAGE_PREFIX}${key.name}`, typeof value === 'object' ? JSON.stringify(value) : value, key.scope, key.target);
}
private loadKeyFromStorage<T extends StorageKeyType>(key: WorkbenchLayoutStateKey<T>): T | undefined {
let value: any = this.storageService.get(`${LayoutStateModel.STORAGE_PREFIX}${key.name}`, key.scope);
if (value !== undefined) {
switch (typeof key.defaultValue) {
case 'boolean': value = value === 'true'; break;
case 'number': value = parseInt(value); break;
case 'object': value = JSON.parse(value); break;
}
}
return value as T | undefined;
}
}