Skip to content

Commit 147d632

Browse files
committed
Test windows state serialization
1 parent bcba7a5 commit 147d632

3 files changed

Lines changed: 396 additions & 88 deletions

File tree

src/vs/code/electron-main/windows.ts

Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
3131
import { mnemonicButtonLabel } from 'vs/base/common/labels';
3232
import { Schemas } from 'vs/base/common/network';
3333
import { normalizeNFC } from 'vs/base/common/normalization';
34-
import { URI, UriComponents } from 'vs/base/common/uri';
34+
import { URI } from 'vs/base/common/uri';
3535
import { Queue, timeout } from 'vs/base/common/async';
3636
import { exists } from 'vs/base/node/pfs';
3737
import { getComparisonKey, isEqual, normalizePath, basename as resourcesBasename, fsPath } from 'vs/base/common/resources';
3838
import { endsWith } from 'vs/base/common/strings';
3939
import { getRemoteAuthority } from 'vs/platform/remote/common/remoteHosts';
40+
import { IWindowsState, restoreWindowsState, WindowsStateStoreData, IWindowState, getWindowsStateStoreData } from 'vs/code/electron-main/windowsState';
4041

4142
const enum WindowError {
4243
UNRESPONSIVE = 1,
@@ -47,40 +48,6 @@ interface INewWindowState extends ISingleWindowState {
4748
hasDefaultState?: boolean;
4849
}
4950

50-
interface IWindowState {
51-
workspace?: IWorkspaceIdentifier;
52-
folderUri?: URI;
53-
backupPath: string;
54-
remoteAuthority?: string;
55-
uiState: ISingleWindowState;
56-
}
57-
58-
interface IWindowsState {
59-
lastActiveWindow?: IWindowState;
60-
lastPluginDevelopmentHostWindow?: IWindowState;
61-
openedWindows: IWindowState[];
62-
}
63-
64-
interface ISerializedWindowsState {
65-
lastActiveWindow?: ISerializedWindowState;
66-
lastPluginDevelopmentHostWindow?: ISerializedWindowState;
67-
openedWindows: ISerializedWindowState[];
68-
}
69-
70-
interface ISerializedWindowState {
71-
workspaceIdentifier?: { id: string; configURIPath: string };
72-
folder?: string;
73-
backupPath: string;
74-
remoteAuthority?: string;
75-
uiState: ISingleWindowState;
76-
77-
// deprecated
78-
folderUri?: UriComponents;
79-
folderPath?: string;
80-
workspace?: { id: string; configPath: string };
81-
82-
}
83-
8451
type RestoreWindowsSetting = 'all' | 'folders' | 'one' | 'none';
8552

8653
interface IOpenBrowserWindowOptions {
@@ -177,7 +144,9 @@ export class WindowsManager implements IWindowsMainService {
177144
@IWorkspacesMainService private readonly workspacesMainService: IWorkspacesMainService,
178145
@IInstantiationService private readonly instantiationService: IInstantiationService
179146
) {
180-
this.windowsState = this.getWindowsState();
147+
const windowsStateStoreData = this.stateService.getItem<WindowsStateStoreData>(WindowsManager.windowsStateStorageKey);
148+
149+
this.windowsState = restoreWindowsState(windowsStateStoreData);
181150
if (!Array.isArray(this.windowsState.openedWindows)) {
182151
this.windowsState.openedWindows = [];
183152
}
@@ -186,39 +155,6 @@ export class WindowsManager implements IWindowsMainService {
186155
this.workspacesManager = new WorkspacesManager(workspacesMainService, backupMainService, environmentService, historyMainService, this);
187156
}
188157

189-
private getWindowsState(): IWindowsState {
190-
const result: IWindowsState = { openedWindows: [] };
191-
const windowsState = this.stateService.getItem<ISerializedWindowsState>(WindowsManager.windowsStateStorageKey) || { openedWindows: [] };
192-
193-
if (windowsState.lastActiveWindow) {
194-
result.lastActiveWindow = this.deserialize(windowsState.lastActiveWindow);
195-
}
196-
if (windowsState.lastPluginDevelopmentHostWindow) {
197-
result.lastPluginDevelopmentHostWindow = this.deserialize(windowsState.lastPluginDevelopmentHostWindow);
198-
}
199-
if (Array.isArray(windowsState.openedWindows)) {
200-
result.openedWindows = windowsState.openedWindows.map(windowState => this.deserialize(windowState));
201-
}
202-
return result;
203-
}
204-
205-
private deserialize(windowState: ISerializedWindowState): IWindowState {
206-
const result: IWindowState = { backupPath: windowState.backupPath, remoteAuthority: windowState.remoteAuthority, uiState: windowState.uiState };
207-
if (windowState.folder) {
208-
result.folderUri = URI.parse(windowState.folder);
209-
} else if (windowState.folderUri) {
210-
result.folderUri = URI.revive(windowState.folderUri);
211-
} else if (windowState.folderPath) {
212-
result.folderUri = URI.file(windowState.folderPath);
213-
}
214-
if (windowState.workspaceIdentifier) {
215-
result.workspace = { id: windowState.workspaceIdentifier.id, configPath: URI.parse(windowState.workspaceIdentifier.configURIPath) };
216-
} else if (windowState.workspace) {
217-
result.workspace = { id: windowState.workspace.id, configPath: URI.file(windowState.workspace.configPath) };
218-
}
219-
return result;
220-
}
221-
222158
ready(initialUserEnv: IProcessEnvironment): void {
223159
this.initialUserEnv = initialUserEnv;
224160

@@ -337,25 +273,7 @@ export class WindowsManager implements IWindowsMainService {
337273
}
338274

339275
// Persist
340-
this.stateService.setItem(WindowsManager.windowsStateStorageKey, this.serializeWindowsState(currentWindowsState));
341-
}
342-
343-
private serializeWindowsState(windowsState: IWindowsState): ISerializedWindowsState {
344-
return {
345-
lastActiveWindow: windowsState.lastActiveWindow && this.serialize(windowsState.lastActiveWindow),
346-
lastPluginDevelopmentHostWindow: windowsState.lastPluginDevelopmentHostWindow && this.serialize(windowsState.lastPluginDevelopmentHostWindow),
347-
openedWindows: windowsState.openedWindows.map(ws => this.serialize(ws))
348-
};
349-
}
350-
351-
private serialize(windowState: IWindowState): ISerializedWindowState {
352-
return {
353-
workspaceIdentifier: windowState.workspace && { id: windowState.workspace.id, configURIPath: windowState.workspace.configPath.toString() },
354-
folder: windowState.folderUri && windowState.folderUri.toString(),
355-
backupPath: windowState.backupPath,
356-
remoteAuthority: windowState.remoteAuthority,
357-
uiState: windowState.uiState
358-
};
276+
this.stateService.setItem(WindowsManager.windowsStateStorageKey, getWindowsStateStoreData(currentWindowsState));
359277
}
360278

361279
// See note on #onBeforeShutdown() for details how these events are flowing
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { URI, UriComponents } from 'vs/base/common/uri';
7+
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
8+
import { IWindowState as IWindowUIState, } from 'vs/platform/windows/electron-main/windows';
9+
10+
export interface IWindowState {
11+
workspace?: IWorkspaceIdentifier;
12+
folderUri?: URI;
13+
backupPath?: string;
14+
remoteAuthority?: string;
15+
uiState: IWindowUIState;
16+
}
17+
18+
export interface IWindowsState {
19+
lastActiveWindow?: IWindowState;
20+
lastPluginDevelopmentHostWindow?: IWindowState;
21+
openedWindows: IWindowState[];
22+
}
23+
24+
export type WindowsStateStoreData = object;
25+
26+
interface ISerializedWindowsState {
27+
lastActiveWindow?: ISerializedWindowState;
28+
lastPluginDevelopmentHostWindow?: ISerializedWindowState;
29+
openedWindows: ISerializedWindowState[];
30+
}
31+
32+
interface ISerializedWindowState {
33+
workspaceIdentifier?: { id: string; configURIPath: string };
34+
folder?: string;
35+
backupPath?: string;
36+
remoteAuthority?: string;
37+
uiState: IWindowUIState;
38+
39+
// deprecated
40+
folderUri?: UriComponents;
41+
folderPath?: string;
42+
workspace?: { id: string; configPath: string };
43+
}
44+
45+
export function restoreWindowsState(data: WindowsStateStoreData | undefined): IWindowsState {
46+
const result: IWindowsState = { openedWindows: [] };
47+
const windowsState = data as ISerializedWindowsState || { openedWindows: [] };
48+
49+
if (windowsState.lastActiveWindow) {
50+
result.lastActiveWindow = restoreWindowState(windowsState.lastActiveWindow);
51+
}
52+
if (windowsState.lastPluginDevelopmentHostWindow) {
53+
result.lastPluginDevelopmentHostWindow = restoreWindowState(windowsState.lastPluginDevelopmentHostWindow);
54+
}
55+
if (Array.isArray(windowsState.openedWindows)) {
56+
result.openedWindows = windowsState.openedWindows.map(windowState => restoreWindowState(windowState));
57+
}
58+
return result;
59+
}
60+
61+
function restoreWindowState(windowState: ISerializedWindowState): IWindowState {
62+
const result: IWindowState = { uiState: windowState.uiState };
63+
if (windowState.backupPath) {
64+
result.backupPath = windowState.backupPath;
65+
}
66+
if (windowState.remoteAuthority) {
67+
result.remoteAuthority = windowState.remoteAuthority;
68+
}
69+
if (windowState.folder) {
70+
result.folderUri = URI.parse(windowState.folder);
71+
} else if (windowState.folderUri) {
72+
result.folderUri = URI.revive(windowState.folderUri);
73+
} else if (windowState.folderPath) {
74+
result.folderUri = URI.file(windowState.folderPath);
75+
}
76+
if (windowState.workspaceIdentifier) {
77+
result.workspace = { id: windowState.workspaceIdentifier.id, configPath: URI.parse(windowState.workspaceIdentifier.configURIPath) };
78+
} else if (windowState.workspace) {
79+
result.workspace = { id: windowState.workspace.id, configPath: URI.file(windowState.workspace.configPath) };
80+
}
81+
return result;
82+
}
83+
84+
export function getWindowsStateStoreData(windowsState: IWindowsState): WindowsStateStoreData {
85+
return {
86+
lastActiveWindow: windowsState.lastActiveWindow && serializeWindowState(windowsState.lastActiveWindow),
87+
lastPluginDevelopmentHostWindow: windowsState.lastPluginDevelopmentHostWindow && serializeWindowState(windowsState.lastPluginDevelopmentHostWindow),
88+
openedWindows: windowsState.openedWindows.map(ws => serializeWindowState(ws))
89+
};
90+
}
91+
92+
function serializeWindowState(windowState: IWindowState): ISerializedWindowState {
93+
return {
94+
workspaceIdentifier: windowState.workspace && { id: windowState.workspace.id, configURIPath: windowState.workspace.configPath.toString() },
95+
folder: windowState.folderUri && windowState.folderUri.toString(),
96+
backupPath: windowState.backupPath,
97+
remoteAuthority: windowState.remoteAuthority,
98+
uiState: windowState.uiState
99+
};
100+
}

0 commit comments

Comments
 (0)