windows.ts 75.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as fs from 'fs';
7
import { basename, normalize, join, dirname } from 'vs/base/common/path';
B
Benjamin Pasero 已提交
8
import { localize } from 'vs/nls';
J
Joao Moreno 已提交
9
import * as arrays from 'vs/base/common/arrays';
10
import { assign, mixin, equals } from 'vs/base/common/objects';
11
import { IBackupMainService, IEmptyWindowBackupInfo } from 'vs/platform/backup/common/backup';
J
Joao Moreno 已提交
12
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
13
import { IStateService } from 'vs/platform/state/common/state';
14
import { CodeWindow, defaultWindowState } from 'vs/code/electron-main/window';
M
Martin Aeschlimann 已提交
15
import { hasArgs, asArray } from 'vs/platform/environment/node/argv';
B
Benjamin Pasero 已提交
16
import { ipcMain as ipc, screen, BrowserWindow, dialog, systemPreferences } from 'electron';
17
import { parseLineAndColumnAware } from 'vs/code/node/paths';
M
polish  
Martin Aeschlimann 已提交
18
import { ILifecycleService, UnloadReason, LifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
19
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
20
import { ILogService } from 'vs/platform/log/common/log';
21
import { IWindowSettings, OpenContext, IPath, IWindowConfiguration, INativeOpenDialogOptions, IPathsToWaitFor, IEnterWorkspaceResult, IMessageBoxResult, INewWindowOptions, IURIToOpen, URIType, OpenDialogOptions } from 'vs/platform/windows/common/windows';
22
import { getLastActiveWindow, findBestWindowOrFolderForFile, findWindowOnWorkspace, findWindowOnExtensionDevelopmentPath, findWindowOnWorkspaceOrFolderUri } from 'vs/code/node/windowsFinder';
M
Matt Bierner 已提交
23
import { Event as CommonEvent, Emitter } from 'vs/base/common/event';
24
import product from 'vs/platform/product/node/product';
B
Benjamin Pasero 已提交
25
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
26
import { IWindowsMainService, IOpenConfiguration, IWindowsCountChangedEvent, ICodeWindow, IWindowState as ISingleWindowState, WindowMode } from 'vs/platform/windows/electron-main/windows';
M
Martin Aeschlimann 已提交
27
import { IHistoryMainService, IRecent } from 'vs/platform/history/common/history';
28
import { IProcessEnvironment, isMacintosh, isWindows } from 'vs/base/common/platform';
M
Martin Aeschlimann 已提交
29
import { IWorkspacesMainService, IWorkspaceIdentifier, WORKSPACE_FILTER, isSingleFolderWorkspaceIdentifier, hasWorkspaceFileExtension } from 'vs/platform/workspaces/common/workspaces';
B
Benjamin Pasero 已提交
30
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
31
import { mnemonicButtonLabel } from 'vs/base/common/labels';
J
Johannes Rieken 已提交
32
import { Schemas } from 'vs/base/common/network';
33
import { normalizeNFC } from 'vs/base/common/normalization';
34
import { URI } from 'vs/base/common/uri';
35
import { Queue } from 'vs/base/common/async';
B
Benjamin Pasero 已提交
36
import { exists } from 'vs/base/node/pfs';
37
import { getComparisonKey, isEqual, normalizePath, basename as resourcesBasename, originalFSPath, hasTrailingPathSeparator, removeTrailingPathSeparator } from 'vs/base/common/resources';
M
Martin Aeschlimann 已提交
38
import { getRemoteAuthority } from 'vs/platform/remote/common/remoteHosts';
M
Martin Aeschlimann 已提交
39
import { restoreWindowsState, WindowsStateStorageData, getWindowsStateStoreData } from 'vs/code/electron-main/windowsStateStorage';
M
Martin Aeschlimann 已提交
40
import { getWorkspaceIdentifier } from 'vs/platform/workspaces/electron-main/workspacesMainService';
E
Erich Gamma 已提交
41

42 43 44
const enum WindowError {
	UNRESPONSIVE = 1,
	CRASHED = 2
E
Erich Gamma 已提交
45 46
}

M
Martin Aeschlimann 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
export interface IWindowState {
	workspace?: IWorkspaceIdentifier;
	folderUri?: URI;
	backupPath?: string;
	remoteAuthority?: string;
	uiState: ISingleWindowState;
}

export interface IWindowsState {
	lastActiveWindow?: IWindowState;
	lastPluginDevelopmentHostWindow?: IWindowState;
	openedWindows: IWindowState[];
}

61 62 63 64
interface INewWindowState extends ISingleWindowState {
	hasDefaultState?: boolean;
}

65
type RestoreWindowsSetting = 'all' | 'folders' | 'one' | 'none';
66

B
Benjamin Pasero 已提交
67 68 69
interface IOpenBrowserWindowOptions {
	userEnv?: IProcessEnvironment;
	cli?: ParsedArgs;
70

71
	workspace?: IWorkspaceIdentifier;
72
	folderUri?: URI;
B
Benjamin Pasero 已提交
73

M
Matt Bierner 已提交
74
	remoteAuthority?: string;
M
Martin Aeschlimann 已提交
75

B
Benjamin Pasero 已提交
76 77
	initialStartup?: boolean;

78
	fileInputs?: IFileInputs;
B
Benjamin Pasero 已提交
79 80

	forceNewWindow?: boolean;
81
	forceNewTabbedWindow?: boolean;
82
	windowToUse?: ICodeWindow;
B
Benjamin Pasero 已提交
83

84 85 86 87 88 89 90
	emptyWindowBackupInfo?: IEmptyWindowBackupInfo;
}

interface IPathParseOptions {
	ignoreFileNotFound?: boolean;
	gotoLineMode?: boolean;
	forceOpenWorkspaceAsFile?: boolean;
M
Martin Aeschlimann 已提交
91
	remoteAuthority?: string;
92 93 94 95 96 97 98
}

interface IFileInputs {
	filesToOpen: IPath[];
	filesToCreate: IPath[];
	filesToDiff: IPath[];
	filesToWait?: IPathsToWaitFor;
M
Martin Aeschlimann 已提交
99
	remoteAuthority?: string;
B
Benjamin Pasero 已提交
100 101
}

B
Benjamin Pasero 已提交
102
interface IPathToOpen extends IPath {
103

104
	// the workspace for a Code instance to open
105
	workspace?: IWorkspaceIdentifier;
106

107
	// the folder path for a Code instance to open
108
	folderUri?: URI;
109

110
	// the backup path for a Code instance to use
111 112
	backupPath?: string;

M
Martin Aeschlimann 已提交
113 114 115
	// the remote authority for the Code instance to open. Undefined if not remote.
	remoteAuthority?: string;

116 117
	// indicator to create the file path in the Code instance
	createFilePath?: boolean;
M
Martin Aeschlimann 已提交
118 119 120

	// optional label for the recent history
	label?: string;
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136
function isFolderPathToOpen(path: IPathToOpen): path is IFolderPathToOpen {
	return !!path.folderUri;
}

interface IFolderPathToOpen {

	// the folder path for a Code instance to open
	folderUri: URI;

	// the backup path for a Code instance to use
	backupPath?: string;

	// the remote authority for the Code instance to open. Undefined if not remote.
	remoteAuthority?: string;
M
Martin Aeschlimann 已提交
137 138 139

	// optional label for the recent history
	label?: string;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

function isWorkspacePathToOpen(path: IPathToOpen): path is IWorkspacePathToOpen {
	return !!path.workspace;
}

interface IWorkspacePathToOpen {

	// the workspace for a Code instance to open
	workspace: IWorkspaceIdentifier;

	// the backup path for a Code instance to use
	backupPath?: string;

	// the remote authority for the Code instance to open. Undefined if not remote.
	remoteAuthority?: string;
M
Martin Aeschlimann 已提交
156 157 158

	// optional label for the recent history
	label?: string;
159 160
}

J
Joao Moreno 已提交
161
export class WindowsManager implements IWindowsMainService {
J
Joao Moreno 已提交
162

163
	_serviceBrand: any;
E
Erich Gamma 已提交
164

165
	private static readonly windowsStateStorageKey = 'windowsState';
E
Erich Gamma 已提交
166

167
	private static WINDOWS: ICodeWindow[] = [];
E
Erich Gamma 已提交
168

B
Benjamin Pasero 已提交
169
	private initialUserEnv: IProcessEnvironment;
170

171
	private readonly windowsState: IWindowsState;
172
	private lastClosedWindowState?: IWindowState;
E
Erich Gamma 已提交
173

174 175
	private readonly dialogs: Dialogs;
	private readonly workspacesManager: WorkspacesManager;
B
Benjamin Pasero 已提交
176

177 178
	private _onWindowReady = new Emitter<ICodeWindow>();
	onWindowReady: CommonEvent<ICodeWindow> = this._onWindowReady.event;
179 180 181 182

	private _onWindowClose = new Emitter<number>();
	onWindowClose: CommonEvent<number> = this._onWindowClose.event;

183 184 185
	private _onWindowLoad = new Emitter<number>();
	onWindowLoad: CommonEvent<number> = this._onWindowLoad.event;

B
Benjamin Pasero 已提交
186 187 188
	private _onWindowsCountChanged = new Emitter<IWindowsCountChangedEvent>();
	onWindowsCountChanged: CommonEvent<IWindowsCountChangedEvent> = this._onWindowsCountChanged.event;

J
Joao Moreno 已提交
189
	constructor(
B
Benjamin Pasero 已提交
190
		private readonly machineId: string,
191 192 193 194 195 196 197 198 199 200
		@ILogService private readonly logService: ILogService,
		@IStateService private readonly stateService: IStateService,
		@IEnvironmentService private readonly environmentService: IEnvironmentService,
		@ILifecycleService private readonly lifecycleService: ILifecycleService,
		@IBackupMainService private readonly backupMainService: IBackupMainService,
		@ITelemetryService private readonly telemetryService: ITelemetryService,
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IHistoryMainService private readonly historyMainService: IHistoryMainService,
		@IWorkspacesMainService private readonly workspacesMainService: IWorkspacesMainService,
		@IInstantiationService private readonly instantiationService: IInstantiationService
201
	) {
M
Martin Aeschlimann 已提交
202
		const windowsStateStoreData = this.stateService.getItem<WindowsStateStorageData>(WindowsManager.windowsStateStorageKey);
203 204

		this.windowsState = restoreWindowsState(windowsStateStoreData);
205 206 207
		if (!Array.isArray(this.windowsState.openedWindows)) {
			this.windowsState.openedWindows = [];
		}
208

B
Benjamin Pasero 已提交
209
		this.dialogs = new Dialogs(environmentService, telemetryService, stateService, this);
210
		this.workspacesManager = new WorkspacesManager(workspacesMainService, backupMainService, this);
211
	}
J
Joao Moreno 已提交
212

B
Benjamin Pasero 已提交
213
	ready(initialUserEnv: IProcessEnvironment): void {
214
		this.initialUserEnv = initialUserEnv;
215 216

		this.registerListeners();
E
Erich Gamma 已提交
217 218 219
	}

	private registerListeners(): void {
220

B
Benjamin Pasero 已提交
221 222 223
		// React to workbench ready events from windows
		ipc.on('vscode:workbenchReady', (event: any, windowId: number) => {
			this.logService.trace('IPC#vscode-workbenchReady');
E
Erich Gamma 已提交
224

B
Benjamin Pasero 已提交
225
			const win = this.getWindowById(windowId);
E
Erich Gamma 已提交
226 227 228 229
			if (win) {
				win.setReady();

				// Event
230
				this._onWindowReady.fire(win);
E
Erich Gamma 已提交
231 232 233
			}
		});

234 235 236 237 238 239 240 241 242 243 244
		// React to HC color scheme changes (Windows)
		if (isWindows) {
			systemPreferences.on('inverted-color-scheme-changed', () => {
				if (systemPreferences.isInvertedColorScheme()) {
					this.sendToAll('vscode:enterHighContrast');
				} else {
					this.sendToAll('vscode:leaveHighContrast');
				}
			});
		}

245
		// Handle various lifecycle events around windows
B
Benjamin Pasero 已提交
246
		this.lifecycleService.onBeforeWindowClose(window => this.onBeforeWindowClose(window));
247
		this.lifecycleService.onBeforeShutdown(() => this.onBeforeShutdown());
248 249 250 251 252
		this.onWindowsCountChanged(e => {
			if (e.newCount - e.oldCount > 0) {
				// clear last closed window state when a new window opens. this helps on macOS where
				// otherwise closing the last window, opening a new window and then quitting would
				// use the state of the previously closed window when restarting.
R
Rob Lourens 已提交
253
				this.lastClosedWindowState = undefined;
254 255
			}
		});
256 257
	}

258
	// Note that onBeforeShutdown() and onBeforeWindowClose() are fired in different order depending on the OS:
259
	// - macOS: since the app will not quit when closing the last window, you will always first get
260
	//          the onBeforeShutdown() event followed by N onbeforeWindowClose() events for each window
261 262
	// - other: on other OS, closing the last window will quit the app so the order depends on the
	//          user interaction: closing the last window will first trigger onBeforeWindowClose()
263
	//          and then onBeforeShutdown(). Using the quit action however will first issue onBeforeShutdown()
264
	//          and then onBeforeWindowClose().
265 266 267 268 269 270 271
	//
	// Here is the behaviour on different OS dependig on action taken (Electron 1.7.x):
	//
	// Legend
	// -  quit(N): quit application with N windows opened
	// - close(1): close one window via the window close button
	// - closeAll: close all windows via the taskbar command
272
	// - onBeforeShutdown(N): number of windows reported in this event handler
273 274 275
	// - onBeforeWindowClose(N, M): number of windows reported and quitRequested boolean in this event handler
	//
	// macOS
276 277 278
	// 	-     quit(1): onBeforeShutdown(1), onBeforeWindowClose(1, true)
	// 	-     quit(2): onBeforeShutdown(2), onBeforeWindowClose(2, true), onBeforeWindowClose(2, true)
	// 	-     quit(0): onBeforeShutdown(0)
279 280 281
	// 	-    close(1): onBeforeWindowClose(1, false)
	//
	// Windows
282 283
	// 	-     quit(1): onBeforeShutdown(1), onBeforeWindowClose(1, true)
	// 	-     quit(2): onBeforeShutdown(2), onBeforeWindowClose(2, true), onBeforeWindowClose(2, true)
284
	// 	-    close(1): onBeforeWindowClose(2, false)[not last window]
285 286
	// 	-    close(1): onBeforeWindowClose(1, false), onBeforeShutdown(0)[last window]
	// 	- closeAll(2): onBeforeWindowClose(2, false), onBeforeWindowClose(2, false), onBeforeShutdown(0)
287 288
	//
	// Linux
289 290
	// 	-     quit(1): onBeforeShutdown(1), onBeforeWindowClose(1, true)
	// 	-     quit(2): onBeforeShutdown(2), onBeforeWindowClose(2, true), onBeforeWindowClose(2, true)
291
	// 	-    close(1): onBeforeWindowClose(2, false)[not last window]
292 293
	// 	-    close(1): onBeforeWindowClose(1, false), onBeforeShutdown(0)[last window]
	// 	- closeAll(2): onBeforeWindowClose(2, false), onBeforeWindowClose(2, false), onBeforeShutdown(0)
294
	//
295
	private onBeforeShutdown(): void {
296
		const currentWindowsState: IWindowsState = {
297
			openedWindows: [],
298
			lastPluginDevelopmentHostWindow: this.windowsState.lastPluginDevelopmentHostWindow,
299
			lastActiveWindow: this.lastClosedWindowState
300 301 302 303 304 305 306 307
		};

		// 1.) Find a last active window (pick any other first window otherwise)
		if (!currentWindowsState.lastActiveWindow) {
			let activeWindow = this.getLastActiveWindow();
			if (!activeWindow || activeWindow.isExtensionDevelopmentHost) {
				activeWindow = WindowsManager.WINDOWS.filter(w => !w.isExtensionDevelopmentHost)[0];
			}
E
Erich Gamma 已提交
308

309
			if (activeWindow) {
310
				currentWindowsState.lastActiveWindow = this.toWindowState(activeWindow);
E
Erich Gamma 已提交
311
			}
312 313 314 315 316
		}

		// 2.) Find extension host window
		const extensionHostWindow = WindowsManager.WINDOWS.filter(w => w.isExtensionDevelopmentHost && !w.isExtensionTestHost)[0];
		if (extensionHostWindow) {
317
			currentWindowsState.lastPluginDevelopmentHostWindow = this.toWindowState(extensionHostWindow);
318
		}
E
Erich Gamma 已提交
319

320
		// 3.) All windows (except extension host) for N >= 2 to support restoreWindows: all or for auto update
321 322 323 324 325
		//
		// Carefull here: asking a window for its window state after it has been closed returns bogus values (width: 0, height: 0)
		// so if we ever want to persist the UI state of the last closed window (window count === 1), it has
		// to come from the stored lastClosedWindowState on Win/Linux at least
		if (this.getWindowCount() > 1) {
326
			currentWindowsState.openedWindows = WindowsManager.WINDOWS.filter(w => !w.isExtensionDevelopmentHost).map(w => this.toWindowState(w));
327
		}
E
Erich Gamma 已提交
328

329
		// Persist
330
		this.stateService.setItem(WindowsManager.windowsStateStorageKey, getWindowsStateStoreData(currentWindowsState));
331
	}
332

333
	// See note on #onBeforeShutdown() for details how these events are flowing
334
	private onBeforeWindowClose(win: ICodeWindow): void {
B
Benjamin Pasero 已提交
335
		if (this.lifecycleService.quitRequested) {
336 337 338 339
			return; // during quit, many windows close in parallel so let it be handled in the before-quit handler
		}

		// On Window close, update our stored UI state of this window
340
		const state: IWindowState = this.toWindowState(win);
341 342 343 344
		if (win.isExtensionDevelopmentHost && !win.isExtensionTestHost) {
			this.windowsState.lastPluginDevelopmentHostWindow = state; // do not let test run window state overwrite our extension development state
		}

345
		// Any non extension host window with same workspace or folder
346
		else if (!win.isExtensionDevelopmentHost && (!!win.openedWorkspace || !!win.openedFolderUri)) {
347
			this.windowsState.openedWindows.forEach(o => {
B
fix npe  
Benjamin Pasero 已提交
348
				const sameWorkspace = win.openedWorkspace && o.workspace && o.workspace.id === win.openedWorkspace.id;
349
				const sameFolder = win.openedFolderUri && o.folderUri && isEqual(o.folderUri, win.openedFolderUri);
350 351

				if (sameWorkspace || sameFolder) {
352 353 354 355 356 357 358
					o.uiState = state.uiState;
				}
			});
		}

		// On Windows and Linux closing the last window will trigger quit. Since we are storing all UI state
		// before quitting, we need to remember the UI state of this window to be able to persist it.
359 360 361
		// On macOS we keep the last closed window state ready in case the user wants to quit right after or
		// wants to open another window, in which case we use this state over the persisted one.
		if (this.getWindowCount() === 1) {
362 363
			this.lastClosedWindowState = state;
		}
E
Erich Gamma 已提交
364 365
	}

366
	private toWindowState(win: ICodeWindow): IWindowState {
367
		return {
368
			workspace: win.openedWorkspace,
369
			folderUri: win.openedFolderUri,
370
			backupPath: win.backupPath,
M
Martin Aeschlimann 已提交
371
			remoteAuthority: win.remoteAuthority,
372 373 374 375
			uiState: win.serializeWindowState()
		};
	}

B
Benjamin Pasero 已提交
376
	open(openConfig: IOpenConfiguration): ICodeWindow[] {
377
		this.logService.trace('windowsManager#open');
378
		openConfig = this.validateOpenConfig(openConfig);
379

380
		const pathsToOpen = this.getPathsToOpen(openConfig);
381

382 383 384 385 386 387
		const foldersToAdd: IFolderPathToOpen[] = [];
		const foldersToOpen: IFolderPathToOpen[] = [];
		const workspacesToOpen: IWorkspacePathToOpen[] = [];
		const emptyToRestore: IEmptyWindowBackupInfo[] = []; // empty windows with backupPath
		let emptyToOpen: number = 0;
		let fileInputs: IFileInputs | undefined; 		// collect all file inputs
388
		for (const path of pathsToOpen) {
389 390 391 392 393 394 395 396 397 398 399
			if (isFolderPathToOpen(path)) {
				if (openConfig.addMode) {
					// When run with --add, take the folders that are to be opened as
					// folders that should be added to the currently active window.
					foldersToAdd.push(path);
				} else {
					foldersToOpen.push(path);
				}
			} else if (isWorkspacePathToOpen(path)) {
				workspacesToOpen.push(path);
			} else if (path.fileUri) {
400
				if (!fileInputs) {
M
Martin Aeschlimann 已提交
401
					fileInputs = { filesToCreate: [], filesToOpen: [], filesToDiff: [], remoteAuthority: path.remoteAuthority };
402 403 404 405 406 407
				}
				if (!path.createFilePath) {
					fileInputs.filesToOpen.push(path);
				} else {
					fileInputs.filesToCreate.push(path);
				}
408 409 410 411
			} else if (path.backupPath) {
				emptyToRestore.push({ backupFolder: basename(path.backupPath), remoteAuthority: path.remoteAuthority });
			} else {
				emptyToOpen++;
412 413
			}
		}
414 415 416

		// When run with --diff, take the files to open as files to diff
		// if there are exactly two files provided.
417 418 419 420
		if (fileInputs && openConfig.diffMode && fileInputs.filesToOpen.length === 2) {
			fileInputs.filesToDiff = fileInputs.filesToOpen;
			fileInputs.filesToOpen = [];
			fileInputs.filesToCreate = []; // diff ignores other files that do not exist
E
Erich Gamma 已提交
421 422
		}

423
		// When run with --wait, make sure we keep the paths to wait for
424
		if (fileInputs && openConfig.cli.wait && openConfig.cli.waitMarkerFilePath) {
M
Martin Aeschlimann 已提交
425
			fileInputs.filesToWait = { paths: [...fileInputs.filesToDiff, ...fileInputs.filesToOpen, ...fileInputs.filesToCreate], waitMarkerFileUri: URI.file(openConfig.cli.waitMarkerFilePath) };
426 427
		}

428
		//
429
		// These are windows to restore because of hot-exit or from previous session (only performed once on startup!)
430
		//
431 432
		let foldersToRestore: URI[] = [];
		let workspacesToRestore: IWorkspacePathToOpen[] = [];
B
Benjamin Pasero 已提交
433
		if (openConfig.initialStartup && !openConfig.cli.extensionDevelopmentPath && !openConfig.cli['disable-restore-windows']) {
434
			let foldersToRestore = this.backupMainService.getFolderBackupPaths();
435
			foldersToAdd.push(...foldersToRestore.map(f => ({ folderUri: f, remoteAuhority: getRemoteAuthority(f), isRestored: true })));
436

437 438 439
			// collect from workspaces with hot-exit backups and from previous window session
			workspacesToRestore = [...this.backupMainService.getWorkspaceBackups(), ...this.workspacesMainService.getUntitledWorkspacesSync()];
			workspacesToOpen.push(...workspacesToRestore);
440

441 442 443
			emptyToRestore.push(...this.backupMainService.getEmptyWindowBackupPaths());
		} else {
			emptyToRestore.length = 0;
444
		}
445 446

		// Open based on config
447
		const usedWindows = this.doOpen(openConfig, workspacesToOpen, foldersToOpen, emptyToRestore, emptyToOpen, fileInputs, foldersToAdd);
448

449
		// Make sure to pass focus to the most relevant of the windows if we open multiple
450
		if (usedWindows.length > 1) {
451

452
			const focusLastActive = this.windowsState.lastActiveWindow && !openConfig.forceEmpty && !hasArgs(openConfig.cli._) && !hasArgs(openConfig.cli['file-uri']) && !hasArgs(openConfig.cli['folder-uri']) && !(openConfig.urisToOpen && openConfig.urisToOpen.length);
453 454
			let focusLastOpened = true;
			let focusLastWindow = true;
455

456 457
			// 1.) focus last active window if we are not instructed to open any paths
			if (focusLastActive) {
458 459 460
				const lastActiveWindow = usedWindows.filter(w => w.backupPath === this.windowsState.lastActiveWindow!.backupPath);
				if (lastActiveWindow.length) {
					lastActiveWindow[0].focus();
461 462
					focusLastOpened = false;
					focusLastWindow = false;
463 464 465
				}
			}

466 467 468 469 470
			// 2.) if instructed to open paths, focus last window which is not restored
			if (focusLastOpened) {
				for (let i = usedWindows.length - 1; i >= 0; i--) {
					const usedWindow = usedWindows[i];
					if (
471
						(usedWindow.openedWorkspace && workspacesToRestore.some(workspace => workspace.workspace.id === usedWindow.openedWorkspace!.id)) ||	// skip over restored workspace
472
						(usedWindow.openedFolderUri && foldersToRestore.some(uri => isEqual(uri, usedWindow.openedFolderUri))) ||			// skip over restored folder
M
Matt Bierner 已提交
473
						(usedWindow.backupPath && emptyToRestore.some(empty => empty.backupFolder === basename(usedWindow.backupPath!)))				// skip over restored empty window
474 475 476 477 478 479 480 481 482 483 484 485
					) {
						continue;
					}

					usedWindow.focus();
					focusLastWindow = false;
					break;
				}
			}

			// 3.) finally, always ensure to have at least last used window focused
			if (focusLastWindow) {
486
				usedWindows[usedWindows.length - 1].focus();
487 488
			}
		}
489

490 491
		// Remember in recent document list (unless this opens for extension development)
		// Also do not add paths when files are opened for diffing, only if opened individually
492
		if (!usedWindows.some(w => w.isExtensionDevelopmentHost) && !openConfig.diffMode && !openConfig.noRecentEntry) {
M
Martin Aeschlimann 已提交
493 494 495 496 497 498 499 500
			const recents: IRecent[] = [];
			for (let pathToOpen of pathsToOpen) {
				if (pathToOpen.workspace) {
					recents.push({ label: pathToOpen.label, workspace: pathToOpen.workspace });
				} else if (pathToOpen.folderUri) {
					recents.push({ label: pathToOpen.label, folderUri: pathToOpen.folderUri });
				} else if (pathToOpen.fileUri) {
					recents.push({ label: pathToOpen.label, fileUri: pathToOpen.fileUri });
501
				}
502
			}
M
Martin Aeschlimann 已提交
503
			this.historyMainService.addRecentlyOpened(recents);
504
		}
505

506
		// If we got started with --wait from the CLI, we need to signal to the outside when the window
507 508
		// used for the edit operation is closed or loaded to a different folder so that the waiting
		// process can continue. We do this by deleting the waitMarkerFilePath.
509
		if (openConfig.context === OpenContext.CLI && openConfig.cli.wait && openConfig.cli.waitMarkerFilePath && usedWindows.length === 1 && usedWindows[0]) {
M
Matt Bierner 已提交
510
			this.waitForWindowCloseOrLoad(usedWindows[0].id).then(() => fs.unlink(openConfig.cli.waitMarkerFilePath!, _error => undefined));
511 512
		}

513 514 515
		return usedWindows;
	}

516 517 518 519 520 521 522 523 524 525
	private validateOpenConfig(config: IOpenConfiguration): IOpenConfiguration {

		// Make sure addMode is only enabled if we have an active window
		if (config.addMode && (config.initialStartup || !this.getLastActiveWindow())) {
			config.addMode = false;
		}

		return config;
	}

526 527
	private doOpen(
		openConfig: IOpenConfiguration,
528 529
		workspacesToOpen: IWorkspacePathToOpen[],
		foldersToOpen: IFolderPathToOpen[],
530
		emptyToRestore: IEmptyWindowBackupInfo[],
531
		emptyToOpen: number,
532
		fileInputs: IFileInputs | undefined,
533
		foldersToAdd: IFolderPathToOpen[]
534
	) {
535
		const usedWindows: ICodeWindow[] = [];
536

B
Benjamin Pasero 已提交
537 538
		// Settings can decide if files/folders open in new window or not
		let { openFolderInNewWindow, openFilesInNewWindow } = this.shouldOpenNewWindow(openConfig);
539

540 541
		// Handle folders to add by looking for the last active workspace (not on initial startup)
		if (!openConfig.initialStartup && foldersToAdd.length > 0) {
542
			const authority = foldersToAdd[0].remoteAuthority;
543
			const lastActiveWindow = this.getLastActiveWindowForAuthority(authority);
544
			if (lastActiveWindow) {
545
				usedWindows.push(this.doAddFoldersToExistingWindow(lastActiveWindow, foldersToAdd.map(f => f.folderUri)));
546 547 548
			}
		}

B
Benjamin Pasero 已提交
549
		// Handle files to open/diff or to create when we dont open a folder and we do not restore any folder/untitled from hot-exit
550
		const potentialWindowsCount = foldersToOpen.length + workspacesToOpen.length + emptyToRestore.length;
551
		if (potentialWindowsCount === 0 && fileInputs) {
E
Erich Gamma 已提交
552

553
			// Find suitable window or folder path to open files in
554
			const fileToCheck = fileInputs.filesToOpen[0] || fileInputs.filesToCreate[0] || fileInputs.filesToDiff[0];
M
Martin Aeschlimann 已提交
555
			// only look at the windows with correct authority
M
Matt Bierner 已提交
556
			const windows = WindowsManager.WINDOWS.filter(w => w.remoteAuthority === fileInputs!.remoteAuthority);
557

558
			const bestWindowOrFolder = findBestWindowOrFolderForFile({
559
				windows,
560 561
				newWindow: openFilesInNewWindow,
				context: openConfig.context,
562
				fileUri: fileToCheck && fileToCheck.fileUri,
563
				localWorkspaceResolver: workspace => workspace.configPath.scheme === Schemas.file ? this.workspacesMainService.resolveLocalWorkspaceSync(workspace.configPath) : null
564
			});
B
Benjamin Pasero 已提交
565

566 567 568 569 570
			// We found a window to open the files in
			if (bestWindowOrFolder instanceof CodeWindow) {

				// Window is workspace
				if (bestWindowOrFolder.openedWorkspace) {
571
					workspacesToOpen.push({ workspace: bestWindowOrFolder.openedWorkspace, remoteAuthority: bestWindowOrFolder.remoteAuthority });
572 573 574
				}

				// Window is single folder
575
				else if (bestWindowOrFolder.openedFolderUri) {
576
					foldersToOpen.push({ folderUri: bestWindowOrFolder.openedFolderUri, remoteAuthority: bestWindowOrFolder.remoteAuthority });
577 578 579 580 581 582
				}

				// Window is empty
				else {

					// Do open files
583
					usedWindows.push(this.doOpenFilesInExistingWindow(openConfig, bestWindowOrFolder, fileInputs));
584 585

					// Reset these because we handled them
R
Rob Lourens 已提交
586
					fileInputs = undefined;
587
				}
588 589 590
			}

			// Finally, if no window or folder is found, just open the files in an empty window
E
Erich Gamma 已提交
591
			else {
B
Benjamin Pasero 已提交
592
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
593 594 595
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
596
					fileInputs,
597
					forceNewWindow: true,
M
Martin Aeschlimann 已提交
598
					remoteAuthority: fileInputs.remoteAuthority,
599
					forceNewTabbedWindow: openConfig.forceNewTabbedWindow
B
Benjamin Pasero 已提交
600
				}));
E
Erich Gamma 已提交
601

602
				// Reset these because we handled them
R
Rob Lourens 已提交
603
				fileInputs = undefined;
E
Erich Gamma 已提交
604 605 606
			}
		}

607
		// Handle workspaces to open (instructed and to restore)
608
		const allWorkspacesToOpen = arrays.distinct(workspacesToOpen, workspace => workspace.workspace.id); // prevent duplicates
609 610 611
		if (allWorkspacesToOpen.length > 0) {

			// Check for existing instances
612
			const windowsOnWorkspace = arrays.coalesce(allWorkspacesToOpen.map(workspaceToOpen => findWindowOnWorkspace(WindowsManager.WINDOWS, workspaceToOpen.workspace)));
613 614
			if (windowsOnWorkspace.length > 0) {
				const windowOnWorkspace = windowsOnWorkspace[0];
R
Rob Lourens 已提交
615
				const fileInputsForWindow = (fileInputs && fileInputs.remoteAuthority === windowOnWorkspace.remoteAuthority) ? fileInputs : undefined;
616 617

				// Do open files
618
				usedWindows.push(this.doOpenFilesInExistingWindow(openConfig, windowOnWorkspace, fileInputsForWindow));
619 620

				// Reset these because we handled them
621
				if (fileInputsForWindow) {
R
Rob Lourens 已提交
622
					fileInputs = undefined;
623
				}
624 625 626 627 628 629

				openFolderInNewWindow = true; // any other folders to open must open in new window then
			}

			// Open remaining ones
			allWorkspacesToOpen.forEach(workspaceToOpen => {
630
				if (windowsOnWorkspace.some(win => win.openedWorkspace!.id === workspaceToOpen.workspace.id)) {
631 632 633
					return; // ignore folders that are already open
				}

634
				const remoteAuthority = workspaceToOpen.remoteAuthority;
635
				const fileInputsForWindow = (fileInputs && fileInputs.remoteAuthority === remoteAuthority) ? fileInputs : undefined;
636

637
				// Do open folder
638
				usedWindows.push(this.doOpenFolderOrWorkspace(openConfig, workspaceToOpen, openFolderInNewWindow, fileInputsForWindow));
639 640

				// Reset these because we handled them
641
				if (fileInputsForWindow) {
R
Rob Lourens 已提交
642
					fileInputs = undefined;
643
				}
644 645 646 647 648

				openFolderInNewWindow = true; // any other folders to open must open in new window then
			});
		}

649
		// Handle folders to open (instructed and to restore)
650
		const allFoldersToOpen = arrays.distinct(foldersToOpen, folder => getComparisonKey(folder.folderUri)); // prevent duplicates
651

652
		if (allFoldersToOpen.length > 0) {
E
Erich Gamma 已提交
653 654

			// Check for existing instances
655
			const windowsOnFolderPath = arrays.coalesce(allFoldersToOpen.map(folderToOpen => findWindowOnWorkspace(WindowsManager.WINDOWS, folderToOpen.folderUri)));
656
			if (windowsOnFolderPath.length > 0) {
657
				const windowOnFolderPath = windowsOnFolderPath[0];
R
Rob Lourens 已提交
658
				const fileInputsForWindow = fileInputs && fileInputs.remoteAuthority === windowOnFolderPath.remoteAuthority ? fileInputs : undefined;
E
Erich Gamma 已提交
659

660
				// Do open files
661
				usedWindows.push(this.doOpenFilesInExistingWindow(openConfig, windowOnFolderPath, fileInputsForWindow));
662

E
Erich Gamma 已提交
663
				// Reset these because we handled them
664
				if (fileInputsForWindow) {
R
Rob Lourens 已提交
665
					fileInputs = undefined;
666
				}
E
Erich Gamma 已提交
667

B
Benjamin Pasero 已提交
668
				openFolderInNewWindow = true; // any other folders to open must open in new window then
E
Erich Gamma 已提交
669 670 671
			}

			// Open remaining ones
672
			allFoldersToOpen.forEach(folderToOpen => {
673

674
				if (windowsOnFolderPath.some(win => isEqual(win.openedFolderUri, folderToOpen.folderUri))) {
E
Erich Gamma 已提交
675 676 677
					return; // ignore folders that are already open
				}

678
				const remoteAuthority = folderToOpen.remoteAuthority;
R
Rob Lourens 已提交
679
				const fileInputsForWindow = (fileInputs && fileInputs.remoteAuthority === remoteAuthority) ? fileInputs : undefined;
680

681
				// Do open folder
682
				usedWindows.push(this.doOpenFolderOrWorkspace(openConfig, folderToOpen, openFolderInNewWindow, fileInputsForWindow));
E
Erich Gamma 已提交
683 684

				// Reset these because we handled them
685
				if (fileInputsForWindow) {
R
Rob Lourens 已提交
686
					fileInputs = undefined;
687
				}
E
Erich Gamma 已提交
688

B
Benjamin Pasero 已提交
689
				openFolderInNewWindow = true; // any other folders to open must open in new window then
E
Erich Gamma 已提交
690 691 692
			});
		}

693
		// Handle empty to restore
694
		const allEmptyToRestore = arrays.distinct(emptyToRestore, info => info.backupFolder); // prevent duplicates
695 696
		if (allEmptyToRestore.length > 0) {
			allEmptyToRestore.forEach(emptyWindowBackupInfo => {
M
Martin Aeschlimann 已提交
697
				const remoteAuthority = emptyWindowBackupInfo.remoteAuthority;
R
Rob Lourens 已提交
698
				const fileInputsForWindow = (fileInputs && fileInputs.remoteAuthority === remoteAuthority) ? fileInputs : undefined;
699

B
Benjamin Pasero 已提交
700
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
701 702 703
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
704
					fileInputs: fileInputsForWindow,
M
Martin Aeschlimann 已提交
705
					remoteAuthority,
B
Benjamin Pasero 已提交
706
					forceNewWindow: true,
707
					forceNewTabbedWindow: openConfig.forceNewTabbedWindow,
708
					emptyWindowBackupInfo
B
Benjamin Pasero 已提交
709
				}));
710

B
wip  
Benjamin Pasero 已提交
711
				// Reset these because we handled them
712
				if (fileInputsForWindow) {
R
Rob Lourens 已提交
713
					fileInputs = undefined;
714
				}
B
wip  
Benjamin Pasero 已提交
715

B
Benjamin Pasero 已提交
716
				openFolderInNewWindow = true; // any other folders to open must open in new window then
717 718
			});
		}
B
Benjamin Pasero 已提交
719

720
		// Handle empty to open (only if no other window opened)
721 722 723 724
		if (usedWindows.length === 0 || fileInputs) {
			if (fileInputs && !emptyToOpen) {
				emptyToOpen++;
			}
R
Rob Lourens 已提交
725
			const remoteAuthority = fileInputs ? fileInputs.remoteAuthority : (openConfig.cli && openConfig.cli.remote || undefined);
726
			for (let i = 0; i < emptyToOpen; i++) {
B
Benjamin Pasero 已提交
727
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
728 729 730
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
M
Martin Aeschlimann 已提交
731
					remoteAuthority,
732
					forceNewWindow: openFolderInNewWindow,
733 734
					forceNewTabbedWindow: openConfig.forceNewTabbedWindow,
					fileInputs
B
Benjamin Pasero 已提交
735
				}));
E
Erich Gamma 已提交
736

737
				// Reset these because we handled them
R
Rob Lourens 已提交
738
				fileInputs = undefined;
739
				openFolderInNewWindow = true; // any other window to open must open in new window then
740 741
			}
		}
E
Erich Gamma 已提交
742

743
		return arrays.distinct(usedWindows);
E
Erich Gamma 已提交
744 745
	}

746
	private doOpenFilesInExistingWindow(configuration: IOpenConfiguration, window: ICodeWindow, fileInputs?: IFileInputs): ICodeWindow {
747 748
		window.focus(); // make sure window has focus

M
Matt Bierner 已提交
749
		const params: { filesToOpen?: IPath[], filesToCreate?: IPath[], filesToDiff?: IPath[], filesToWait?: IPathsToWaitFor, termProgram?: string } = {};
B
Benjamin Pasero 已提交
750 751 752 753 754 755 756 757 758 759 760 761
		if (fileInputs) {
			params.filesToOpen = fileInputs.filesToOpen;
			params.filesToCreate = fileInputs.filesToCreate;
			params.filesToDiff = fileInputs.filesToDiff;
			params.filesToWait = fileInputs.filesToWait;
		}

		if (configuration.userEnv) {
			params.termProgram = configuration.userEnv['TERM_PROGRAM'];
		}

		window.sendWhenReady('vscode:openFiles', params);
B
Benjamin Pasero 已提交
762 763

		return window;
764 765
	}

766
	private doAddFoldersToExistingWindow(window: ICodeWindow, foldersToAdd: URI[]): ICodeWindow {
767 768
		window.focus(); // make sure window has focus

B
Benjamin Pasero 已提交
769
		window.sendWhenReady('vscode:addFolders', { foldersToAdd });
770 771 772 773

		return window;
	}

M
Matt Bierner 已提交
774
	private doOpenFolderOrWorkspace(openConfig: IOpenConfiguration, folderOrWorkspace: IPathToOpen, forceNewWindow: boolean, fileInputs: IFileInputs | undefined, windowToUse?: ICodeWindow): ICodeWindow {
B
Benjamin Pasero 已提交
775 776 777 778
		if (!forceNewWindow && !windowToUse && typeof openConfig.contextWindowId === 'number') {
			windowToUse = this.getWindowById(openConfig.contextWindowId); // fix for https://github.com/Microsoft/vscode/issues/49587
		}

779 780 781 782
		const browserWindow = this.openInBrowserWindow({
			userEnv: openConfig.userEnv,
			cli: openConfig.cli,
			initialStartup: openConfig.initialStartup,
783
			workspace: folderOrWorkspace.workspace,
784
			folderUri: folderOrWorkspace.folderUri,
785
			fileInputs,
M
Martin Aeschlimann 已提交
786
			remoteAuthority: folderOrWorkspace.remoteAuthority,
B
Benjamin Pasero 已提交
787
			forceNewWindow,
788
			forceNewTabbedWindow: openConfig.forceNewTabbedWindow,
789
			windowToUse
790 791 792 793 794
		});

		return browserWindow;
	}

B
Benjamin Pasero 已提交
795 796
	private getPathsToOpen(openConfig: IOpenConfiguration): IPathToOpen[] {
		let windowsToOpen: IPathToOpen[];
797
		let isCommandLineOrAPICall = false;
E
Erich Gamma 已提交
798

799
		// Extract paths: from API
S
Sandeep Somavarapu 已提交
800
		if (openConfig.urisToOpen && openConfig.urisToOpen.length > 0) {
801
			windowsToOpen = this.doExtractPathsFromAPI(openConfig);
802
			isCommandLineOrAPICall = true;
E
Erich Gamma 已提交
803 804
		}

B
Benjamin Pasero 已提交
805 806
		// Check for force empty
		else if (openConfig.forceEmpty) {
807
			windowsToOpen = [Object.create(null)];
E
Erich Gamma 已提交
808 809
		}

810
		// Extract paths: from CLI
811
		else if (hasArgs(openConfig.cli._) || hasArgs(openConfig.cli['folder-uri']) || hasArgs(openConfig.cli['file-uri'])) {
812
			windowsToOpen = this.doExtractPathsFromCLI(openConfig.cli);
813
			isCommandLineOrAPICall = true;
B
Benjamin Pasero 已提交
814 815
		}

816
		// Extract windows: from previous session
B
Benjamin Pasero 已提交
817
		else {
818
			windowsToOpen = this.doGetWindowsFromLastSession();
B
Benjamin Pasero 已提交
819 820
		}

821 822
		// Convert multiple folders into workspace (if opened via API or CLI)
		// This will ensure to open these folders in one window instead of multiple
823 824
		// If we are in addMode, we should not do this because in that case all
		// folders should be added to the existing window.
825
		if (!openConfig.addMode && isCommandLineOrAPICall) {
826
			const foldersToOpen = windowsToOpen.filter(path => !!path.folderUri);
827
			if (foldersToOpen.length > 1) {
828
				const remoteAuthority = foldersToOpen[0].remoteAuthority;
829 830 831 832 833 834 835
				if (foldersToOpen.every(f => f.remoteAuthority === remoteAuthority)) { // only if all folder have the same authority
					const workspace = this.workspacesMainService.createUntitledWorkspaceSync(foldersToOpen.map(folder => ({ uri: folder.folderUri! })));

					// Add workspace and remove folders thereby
					windowsToOpen.push({ workspace, remoteAuthority });
					windowsToOpen = windowsToOpen.filter(path => !path.folderUri);
				}
836 837 838
			}
		}

839
		return windowsToOpen;
E
Erich Gamma 已提交
840 841
	}

842
	private doExtractPathsFromAPI(openConfig: IOpenConfiguration): IPathToOpen[] {
M
Matt Bierner 已提交
843
		const pathsToOpen: IPathToOpen[] = [];
844
		const cli = openConfig.cli;
845
		const parseOptions: IPathParseOptions = { gotoLineMode: cli && cli.goto, forceOpenWorkspaceAsFile: openConfig.forceOpenWorkspaceAsFile };
M
Matt Bierner 已提交
846
		for (const pathToOpen of openConfig.urisToOpen || []) {
M
Martin Aeschlimann 已提交
847 848 849 850
			if (!pathToOpen) {
				continue;
			}

851
			const path = this.parseUri(pathToOpen.uri, pathToOpen.typeHint, parseOptions);
M
Martin Aeschlimann 已提交
852
			if (path) {
M
Martin Aeschlimann 已提交
853
				path.label = pathToOpen.label;
M
Martin Aeschlimann 已提交
854 855
				pathsToOpen.push(path);
			} else {
856

B
Benjamin Pasero 已提交
857
				// Warn about the invalid URI or path
M
Martin Aeschlimann 已提交
858
				let message, detail;
859
				if (pathToOpen.uri.scheme === Schemas.file) {
M
Martin Aeschlimann 已提交
860
					message = localize('pathNotExistTitle', "Path does not exist");
861
					detail = localize('pathNotExistDetail', "The path '{0}' does not seem to exist anymore on disk.", pathToOpen.uri.fsPath);
M
Martin Aeschlimann 已提交
862 863
				} else {
					message = localize('uriInvalidTitle', "URI can not be opened");
864
					detail = localize('uriInvalidDetail', "The URI '{0}' is not valid and can not be opened.", pathToOpen.uri.toString());
M
Martin Aeschlimann 已提交
865
				}
866
				const options: Electron.MessageBoxOptions = {
867 868
					title: product.nameLong,
					type: 'info',
869
					buttons: [localize('ok', "OK")],
M
Martin Aeschlimann 已提交
870 871
					message,
					detail,
872 873 874
					noLink: true
				};

875
				this.dialogs.showMessageBox(options, this.getFocusedWindow());
876
			}
M
Martin Aeschlimann 已提交
877
		}
878 879 880 881
		return pathsToOpen;
	}

	private doExtractPathsFromCLI(cli: ParsedArgs): IPath[] {
M
Matt Bierner 已提交
882
		const pathsToOpen: IPathToOpen[] = [];
R
Rob Lourens 已提交
883
		const parseOptions: IPathParseOptions = { ignoreFileNotFound: true, gotoLineMode: cli.goto, remoteAuthority: cli.remote || undefined };
884 885

		// folder uris
886
		const folderUris = asArray(cli['folder-uri']);
M
Martin Aeschlimann 已提交
887
		for (let folderUri of folderUris) {
888
			const path = this.parseUri(this.argToUri(folderUri), 'folder', parseOptions);
M
Martin Aeschlimann 已提交
889 890 891
			if (path) {
				pathsToOpen.push(path);
			}
892 893 894 895
		}

		// file uris
		const fileUris = asArray(cli['file-uri']);
M
Martin Aeschlimann 已提交
896
		for (let fileUri of fileUris) {
897
			const path = this.parseUri(this.argToUri(fileUri), 'file');
M
Martin Aeschlimann 已提交
898 899 900
			if (path) {
				pathsToOpen.push(path);
			}
901 902 903
		}

		// folder or file paths
M
Martin Aeschlimann 已提交
904 905 906 907 908 909
		const cliArgs = asArray(cli._);
		for (let cliArg of cliArgs) {
			const path = this.parsePath(cliArg, parseOptions);
			if (path) {
				pathsToOpen.push(path);
			}
910 911
		}

M
Martin Aeschlimann 已提交
912
		if (pathsToOpen.length) {
913
			return pathsToOpen;
B
Benjamin Pasero 已提交
914 915 916 917 918 919
		}

		// No path provided, return empty to open empty
		return [Object.create(null)];
	}

B
Benjamin Pasero 已提交
920
	private doGetWindowsFromLastSession(): IPathToOpen[] {
921
		const restoreWindows = this.getRestoreWindowsSetting();
B
Benjamin Pasero 已提交
922

923
		switch (restoreWindows) {
B
Benjamin Pasero 已提交
924

925
			// none: we always open an empty window
926 927
			case 'none':
				return [Object.create(null)];
B
Benjamin Pasero 已提交
928

929
			// one: restore last opened workspace/folder or empty window
930 931
			// all: restore all windows
			// folders: restore last opened folders only
932
			case 'one':
933 934
			case 'all':
			case 'folders':
935 936 937
				const openedWindows: IWindowState[] = [];
				if (restoreWindows !== 'one') {
					openedWindows.push(...this.windowsState.openedWindows);
938
				}
939 940
				if (this.windowsState.lastActiveWindow) {
					openedWindows.push(this.windowsState.lastActiveWindow);
941
				}
942

943 944 945
				const windowsToOpen: IPathToOpen[] = [];
				for (const openedWindow of openedWindows) {
					if (openedWindow.workspace) { // Workspaces
946
						const pathToOpen = this.parseUri(openedWindow.workspace.configPath, 'file', { remoteAuthority: openedWindow.remoteAuthority });
947 948 949 950
						if (pathToOpen && pathToOpen.workspace) {
							windowsToOpen.push(pathToOpen);
						}
					} else if (openedWindow.folderUri) { // Folders
951
						const pathToOpen = this.parseUri(openedWindow.folderUri, 'folder', { remoteAuthority: openedWindow.remoteAuthority });
952 953 954 955
						if (pathToOpen && pathToOpen.folderUri) {
							windowsToOpen.push(pathToOpen);
						}
					} else if (restoreWindows !== 'folders' && openedWindow.backupPath) { // Windows that were Empty
M
Martin Aeschlimann 已提交
956
						windowsToOpen.push({ backupPath: openedWindow.backupPath, remoteAuthority: openedWindow.remoteAuthority });
957
					}
958 959 960 961 962 963 964
				}

				if (windowsToOpen.length > 0) {
					return windowsToOpen;
				}

				break;
B
Benjamin Pasero 已提交
965
		}
E
Erich Gamma 已提交
966

967
		// Always fallback to empty window
B
Benjamin Pasero 已提交
968
		return [Object.create(null)];
E
Erich Gamma 已提交
969 970
	}

971 972 973 974 975
	private getRestoreWindowsSetting(): RestoreWindowsSetting {
		let restoreWindows: RestoreWindowsSetting;
		if (this.lifecycleService.wasRestarted) {
			restoreWindows = 'all'; // always reopen all windows when an update was applied
		} else {
976
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
977
			restoreWindows = ((windowConfig && windowConfig.restoreWindows) || 'one') as RestoreWindowsSetting;
978 979 980 981 982 983 984 985 986

			if (['all', 'folders', 'one', 'none'].indexOf(restoreWindows) === -1) {
				restoreWindows = 'one';
			}
		}

		return restoreWindows;
	}

987
	private argToUri(arg: string): URI | undefined {
M
Martin Aeschlimann 已提交
988
		try {
989
			const uri = URI.parse(arg);
M
Martin Aeschlimann 已提交
990
			if (!uri.scheme) {
M
Martin Aeschlimann 已提交
991
				this.logService.error(`Invalid URI input string, scheme missing: ${arg}`);
992
				return undefined;
M
Martin Aeschlimann 已提交
993 994 995
			}
			return uri;
		} catch (e) {
M
Martin Aeschlimann 已提交
996
			this.logService.error(`Invalid URI input string: ${arg}, ${e.message}`);
997
		}
998
		return undefined;
999 1000
	}

1001
	private parseUri(uri: URI | undefined, typeHint?: URIType, options: IPathParseOptions = {}): IPathToOpen | undefined {
M
Martin Aeschlimann 已提交
1002
		if (!uri || !uri.scheme) {
1003
			return undefined;
1004
		}
M
Martin Aeschlimann 已提交
1005 1006
		if (uri.scheme === Schemas.file) {
			return this.parsePath(uri.fsPath, options);
1007
		}
M
Martin Aeschlimann 已提交
1008 1009

		// open remote if either specified in the cli or if it's a remotehost URI
1010
		const remoteAuthority = options.remoteAuthority || getRemoteAuthority(uri);
M
Martin Aeschlimann 已提交
1011

1012 1013
		// normalize URI
		uri = normalizePath(uri);
1014 1015 1016


		// remove trailing slash
1017 1018
		if (hasTrailingPathSeparator(uri)) {
			uri = removeTrailingPathSeparator(uri);
1019 1020 1021 1022 1023 1024
			if (!typeHint) {
				typeHint = 'folder';
			}
		}

		// if there's no type hint
1025
		if (!typeHint && (hasWorkspaceFileExtension(uri.path) || options.gotoLineMode)) {
1026
			typeHint = 'file';
1027
		}
1028 1029 1030

		if (typeHint === 'file') {
			if (options.gotoLineMode) {
1031 1032 1033 1034
				const parsedPath = parseLineAndColumnAware(uri.path);
				return {
					fileUri: uri.with({ path: parsedPath.path }),
					lineNumber: parsedPath.line,
M
Martin Aeschlimann 已提交
1035 1036
					columnNumber: parsedPath.column,
					remoteAuthority
1037 1038
				};
			}
1039
			if (hasWorkspaceFileExtension(uri.path) && !options.forceOpenWorkspaceAsFile) {
1040
				return {
M
Martin Aeschlimann 已提交
1041
					workspace: getWorkspaceIdentifier(uri),
1042 1043 1044
					remoteAuthority
				};
			}
1045
			return {
M
Martin Aeschlimann 已提交
1046 1047
				fileUri: uri,
				remoteAuthority
1048 1049
			};
		}
1050
		return {
M
Martin Aeschlimann 已提交
1051 1052
			folderUri: uri,
			remoteAuthority
1053 1054 1055
		};
	}

1056
	private parsePath(anyPath: string, options: IPathParseOptions): IPathToOpen | undefined {
E
Erich Gamma 已提交
1057
		if (!anyPath) {
1058
			return undefined;
E
Erich Gamma 已提交
1059 1060
		}

1061
		let lineNumber, columnNumber: number | undefined;
1062

1063
		if (options.gotoLineMode) {
1064 1065 1066
			const parsedPath = parseLineAndColumnAware(anyPath);
			lineNumber = parsedPath.line;
			columnNumber = parsedPath.column;
1067

E
Erich Gamma 已提交
1068 1069 1070
			anyPath = parsedPath.path;
		}

M
Martin Aeschlimann 已提交
1071
		// open remote if either specified in the cli even if it is a local file. TODO: Future idea: resolve in remote host context.
1072
		const remoteAuthority = options.remoteAuthority;
M
Martin Aeschlimann 已提交
1073

1074
		const candidate = normalize(anyPath);
E
Erich Gamma 已提交
1075
		try {
B
Benjamin Pasero 已提交
1076
			const candidateStat = fs.statSync(candidate);
E
Erich Gamma 已提交
1077
			if (candidateStat) {
1078
				if (candidateStat.isFile()) {
1079

1080
					// Workspace (unless disabled via flag)
1081
					if (!options.forceOpenWorkspaceAsFile) {
1082
						const workspace = this.workspacesMainService.resolveLocalWorkspaceSync(URI.file(candidate));
1083
						if (workspace) {
1084
							return { workspace: { id: workspace.id, configPath: workspace.configPath }, remoteAuthority: workspace.remoteAuthority };
1085
						}
1086 1087 1088
					}

					// File
1089
					return {
1090
						fileUri: URI.file(candidate),
1091 1092
						lineNumber,
						columnNumber,
M
Martin Aeschlimann 已提交
1093
						remoteAuthority
1094 1095 1096
					};
				}

1097 1098 1099 1100 1101
				// Folder (we check for isDirectory() because e.g. paths like /dev/null
				// are neither file nor folder but some external tools might pass them
				// over to us)
				else if (candidateStat.isDirectory()) {
					return {
M
Martin Aeschlimann 已提交
1102 1103
						folderUri: URI.file(candidate),
						remoteAuthority
1104 1105
					};
				}
E
Erich Gamma 已提交
1106 1107
			}
		} catch (error) {
S
Sandeep Somavarapu 已提交
1108
			const fileUri = URI.file(candidate);
M
Martin Aeschlimann 已提交
1109
			this.historyMainService.removeFromRecentlyOpened([fileUri]); // since file does not seem to exist anymore, remove from recent
1110
			if (options && options.ignoreFileNotFound) {
M
Martin Aeschlimann 已提交
1111
				return { fileUri, createFilePath: true, remoteAuthority }; // assume this is a file that does not yet exist
E
Erich Gamma 已提交
1112 1113 1114
			}
		}

1115
		return undefined;
E
Erich Gamma 已提交
1116 1117
	}

B
Benjamin Pasero 已提交
1118 1119 1120
	private shouldOpenNewWindow(openConfig: IOpenConfiguration): { openFolderInNewWindow: boolean; openFilesInNewWindow: boolean; } {

		// let the user settings override how folders are open in a new window or same window unless we are forced
1121
		const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
1122 1123 1124
		const openFolderInNewWindowConfig = (windowConfig && windowConfig.openFoldersInNewWindow) || 'default' /* default */;
		const openFilesInNewWindowConfig = (windowConfig && windowConfig.openFilesInNewWindow) || 'off' /* default */;

B
Benjamin Pasero 已提交
1125
		let openFolderInNewWindow = (openConfig.preferNewWindow || openConfig.forceNewWindow) && !openConfig.forceReuseWindow;
1126 1127
		if (!openConfig.forceNewWindow && !openConfig.forceReuseWindow && (openFolderInNewWindowConfig === 'on' || openFolderInNewWindowConfig === 'off')) {
			openFolderInNewWindow = (openFolderInNewWindowConfig === 'on');
B
Benjamin Pasero 已提交
1128 1129 1130
		}

		// let the user settings override how files are open in a new window or same window unless we are forced (not for extension development though)
M
Matt Bierner 已提交
1131
		let openFilesInNewWindow: boolean = false;
B
Benjamin Pasero 已提交
1132
		if (openConfig.forceNewWindow || openConfig.forceReuseWindow) {
M
Matt Bierner 已提交
1133
			openFilesInNewWindow = !!openConfig.forceNewWindow && !openConfig.forceReuseWindow;
B
Benjamin Pasero 已提交
1134
		} else {
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

			// macOS: by default we open files in a new window if this is triggered via DOCK context
			if (isMacintosh) {
				if (openConfig.context === OpenContext.DOCK) {
					openFilesInNewWindow = true;
				}
			}

			// Linux/Windows: by default we open files in the new window unless triggered via DIALOG or MENU context
			else {
				if (openConfig.context !== OpenContext.DIALOG && openConfig.context !== OpenContext.MENU) {
					openFilesInNewWindow = true;
				}
B
Benjamin Pasero 已提交
1148 1149
			}

1150
			// finally check for overrides of default
1151 1152
			if (!openConfig.cli.extensionDevelopmentPath && (openFilesInNewWindowConfig === 'on' || openFilesInNewWindowConfig === 'off')) {
				openFilesInNewWindow = (openFilesInNewWindowConfig === 'on');
B
Benjamin Pasero 已提交
1153 1154 1155
			}
		}

M
Matt Bierner 已提交
1156
		return { openFolderInNewWindow: !!openFolderInNewWindow, openFilesInNewWindow };
B
Benjamin Pasero 已提交
1157 1158
	}

1159
	openExtensionDevelopmentHostWindow(extensionDevelopmentPath: string, openConfig: IOpenConfiguration): void {
E
Erich Gamma 已提交
1160

B
Benjamin Pasero 已提交
1161 1162 1163
		// Reload an existing extension development host window on the same path
		// We currently do not allow more than one extension development window
		// on the same extension path.
1164
		const existingWindow = findWindowOnExtensionDevelopmentPath(WindowsManager.WINDOWS, extensionDevelopmentPath);
1165 1166 1167
		if (existingWindow) {
			this.reload(existingWindow, openConfig.cli);
			existingWindow.focus(); // make sure it gets focus and is restored
E
Erich Gamma 已提交
1168

B
Benjamin Pasero 已提交
1169 1170
			return;
		}
1171 1172 1173
		let folderUris = asArray(openConfig.cli['folder-uri']);
		let fileUris = asArray(openConfig.cli['file-uri']);
		let cliArgs = openConfig.cli._;
E
Erich Gamma 已提交
1174

1175
		// Fill in previously opened workspace unless an explicit path is provided and we are not unit testing
1176
		if (!cliArgs.length && !folderUris.length && !fileUris.length && !openConfig.cli.extensionTestsPath) {
1177
			const extensionDevelopmentWindowState = this.windowsState.lastPluginDevelopmentHostWindow;
1178
			const workspaceToOpen = extensionDevelopmentWindowState && (extensionDevelopmentWindowState.workspace || extensionDevelopmentWindowState.folderUri);
1179
			if (workspaceToOpen) {
1180
				if (isSingleFolderWorkspaceIdentifier(workspaceToOpen)) {
1181
					if (workspaceToOpen.scheme === Schemas.file) {
1182
						cliArgs = [workspaceToOpen.fsPath];
1183
					} else {
1184
						folderUris = [workspaceToOpen.toString()];
1185 1186
					}
				} else {
M
Martin Aeschlimann 已提交
1187
					if (workspaceToOpen.configPath.scheme === Schemas.file) {
1188
						cliArgs = [originalFSPath(workspaceToOpen.configPath)];
M
Martin Aeschlimann 已提交
1189
					} else {
1190
						fileUris = [workspaceToOpen.configPath.toString()];
M
Martin Aeschlimann 已提交
1191
					}
1192
				}
E
Erich Gamma 已提交
1193 1194 1195
			}
		}

1196
		// Make sure we are not asked to open a workspace or folder that is already opened
1197 1198
		if (cliArgs.length && cliArgs.some(path => !!findWindowOnWorkspaceOrFolderUri(WindowsManager.WINDOWS, URI.file(path)))) {
			cliArgs = [];
E
Erich Gamma 已提交
1199
		}
1200

M
Martin Aeschlimann 已提交
1201
		if (folderUris.length && folderUris.some(uri => !!findWindowOnWorkspaceOrFolderUri(WindowsManager.WINDOWS, this.argToUri(uri)))) {
1202 1203 1204
			folderUris = [];
		}

M
Martin Aeschlimann 已提交
1205
		if (fileUris.length && fileUris.some(uri => !!findWindowOnWorkspaceOrFolderUri(WindowsManager.WINDOWS, this.argToUri(uri)))) {
1206
			fileUris = [];
1207
		}
E
Erich Gamma 已提交
1208

1209 1210 1211 1212
		openConfig.cli._ = cliArgs;
		openConfig.cli['folder-uri'] = folderUris;
		openConfig.cli['file-uri'] = fileUris;

1213 1214
		const match = extensionDevelopmentPath.match(/^vscode-remote:\/\/([^\/]+)/);
		if (match) {
1215
			openConfig.cli['remote'] = URI.parse(extensionDevelopmentPath).authority;
1216 1217
		}

B
Benjamin Pasero 已提交
1218
		// Open it
1219
		this.open({ context: openConfig.context, cli: openConfig.cli, forceNewWindow: true, forceEmpty: !cliArgs.length && !folderUris.length && !fileUris.length, userEnv: openConfig.userEnv, noRecentEntry: true });
E
Erich Gamma 已提交
1220 1221
	}

1222
	private openInBrowserWindow(options: IOpenBrowserWindowOptions): ICodeWindow {
1223

B
Benjamin Pasero 已提交
1224 1225 1226
		// Build IWindowConfiguration from config and options
		const configuration: IWindowConfiguration = mixin({}, options.cli); // inherit all properties from CLI
		configuration.appRoot = this.environmentService.appRoot;
1227
		configuration.machineId = this.machineId;
1228
		configuration.nodeCachedDataDir = this.environmentService.nodeCachedDataDir;
1229
		configuration.mainPid = process.pid;
B
Benjamin Pasero 已提交
1230 1231 1232
		configuration.execPath = process.execPath;
		configuration.userEnv = assign({}, this.initialUserEnv, options.userEnv || {});
		configuration.isInitialStartup = options.initialStartup;
1233
		configuration.workspace = options.workspace;
1234
		configuration.folderUri = options.folderUri;
M
Martin Aeschlimann 已提交
1235
		configuration.remoteAuthority = options.remoteAuthority;
1236 1237 1238 1239 1240 1241 1242 1243

		const fileInputs = options.fileInputs;
		if (fileInputs) {
			configuration.filesToOpen = fileInputs.filesToOpen;
			configuration.filesToCreate = fileInputs.filesToCreate;
			configuration.filesToDiff = fileInputs.filesToDiff;
			configuration.filesToWait = fileInputs.filesToWait;
		}
B
Benjamin Pasero 已提交
1244

1245
		// if we know the backup folder upfront (for empty windows to restore), we can set it
1246
		// directly here which helps for restoring UI state associated with that window.
B
Benjamin Pasero 已提交
1247
		// For all other cases we first call into registerEmptyWindowBackupSync() to set it before
1248
		// loading the window.
1249
		if (options.emptyWindowBackupInfo) {
1250
			configuration.backupPath = join(this.environmentService.backupHome, options.emptyWindowBackupInfo.backupFolder);
1251 1252
		}

1253
		let window: ICodeWindow | undefined;
1254
		if (!options.forceNewWindow && !options.forceNewTabbedWindow) {
1255 1256 1257
			window = options.windowToUse || this.getLastActiveWindow();
			if (window) {
				window.focus();
E
Erich Gamma 已提交
1258 1259 1260 1261
			}
		}

		// New window
1262
		if (!window) {
1263
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
			const state = this.getNewWindowState(configuration);

			// Window state is not from a previous session: only allow fullscreen if we inherit it or user wants fullscreen
			let allowFullscreen: boolean;
			if (state.hasDefaultState) {
				allowFullscreen = (windowConfig && windowConfig.newWindowDimensions && ['fullscreen', 'inherit'].indexOf(windowConfig.newWindowDimensions) >= 0);
			}

			// Window state is from a previous session: only allow fullscreen when we got updated or user wants to restore
			else {
1274
				allowFullscreen = this.lifecycleService.wasRestarted || (windowConfig && windowConfig.restoreFullscreen);
1275 1276 1277 1278 1279
			}

			if (state.mode === WindowMode.Fullscreen && !allowFullscreen) {
				state.mode = WindowMode.Normal;
			}
1280

1281
			// Create the window
1282
			window = this.instantiationService.createInstance(CodeWindow, {
1283
				state,
1284
				extensionDevelopmentPath: configuration.extensionDevelopmentPath,
1285
				isExtensionTestHost: !!configuration.extensionTestsPath
1286
			});
1287

1288 1289 1290 1291 1292 1293 1294 1295
			// Add as window tab if configured (macOS only)
			if (options.forceNewTabbedWindow) {
				const activeWindow = this.getLastActiveWindow();
				if (activeWindow) {
					activeWindow.addTabbedWindow(window);
				}
			}

B
Benjamin Pasero 已提交
1296
			// Add to our list of windows
1297
			WindowsManager.WINDOWS.push(window);
E
Erich Gamma 已提交
1298

B
Benjamin Pasero 已提交
1299 1300 1301
			// Indicate number change via event
			this._onWindowsCountChanged.fire({ oldCount: WindowsManager.WINDOWS.length - 1, newCount: WindowsManager.WINDOWS.length });

E
Erich Gamma 已提交
1302
			// Window Events
1303
			window.win.webContents.removeAllListeners('devtools-reload-page'); // remove built in listener so we can handle this on our own
M
Matt Bierner 已提交
1304 1305 1306 1307
			window.win.webContents.on('devtools-reload-page', () => this.reload(window!));
			window.win.webContents.on('crashed', () => this.onWindowError(window!, WindowError.CRASHED));
			window.win.on('unresponsive', () => this.onWindowError(window!, WindowError.UNRESPONSIVE));
			window.win.on('closed', () => this.onWindowClosed(window!));
E
Erich Gamma 已提交
1308 1309

			// Lifecycle
B
Benjamin Pasero 已提交
1310
			(this.lifecycleService as LifecycleService).registerWindow(window);
E
Erich Gamma 已提交
1311 1312 1313 1314 1315 1316
		}

		// Existing window
		else {

			// Some configuration things get inherited if the window is being reused and we are
B
Benjamin Pasero 已提交
1317
			// in extension development host mode. These options are all development related.
1318
			const currentWindowConfig = window.config;
A
Alex Dima 已提交
1319 1320
			if (!configuration.extensionDevelopmentPath && currentWindowConfig && !!currentWindowConfig.extensionDevelopmentPath) {
				configuration.extensionDevelopmentPath = currentWindowConfig.extensionDevelopmentPath;
B
Benjamin Pasero 已提交
1321
				configuration.verbose = currentWindowConfig.verbose;
1322
				configuration['inspect-brk-extensions'] = currentWindowConfig['inspect-brk-extensions'];
1323
				configuration.debugId = currentWindowConfig.debugId;
1324
				configuration['inspect-extensions'] = currentWindowConfig['inspect-extensions'];
1325
				configuration['extensions-dir'] = currentWindowConfig['extensions-dir'];
E
Erich Gamma 已提交
1326 1327 1328
			}
		}

1329 1330 1331 1332 1333 1334
		// If the window was already loaded, make sure to unload it
		// first and only load the new configuration if that was
		// not vetoed
		if (window.isReady) {
			this.lifecycleService.unload(window, UnloadReason.LOAD).then(veto => {
				if (!veto) {
M
Matt Bierner 已提交
1335
					this.doOpenInBrowserWindow(window!, configuration, options);
B
Benjamin Pasero 已提交
1336
				}
1337 1338 1339 1340 1341 1342 1343
			});
		} else {
			this.doOpenInBrowserWindow(window, configuration, options);
		}

		return window;
	}
B
Benjamin Pasero 已提交
1344

1345
	private doOpenInBrowserWindow(window: ICodeWindow, configuration: IWindowConfiguration, options: IOpenBrowserWindowOptions): void {
1346

1347 1348 1349
		// Register window for backups
		if (!configuration.extensionDevelopmentPath) {
			if (configuration.workspace) {
1350
				configuration.backupPath = this.backupMainService.registerWorkspaceBackupSync({ workspace: configuration.workspace, remoteAuthority: configuration.remoteAuthority });
1351 1352 1353 1354
			} else if (configuration.folderUri) {
				configuration.backupPath = this.backupMainService.registerFolderBackupSync(configuration.folderUri);
			} else {
				const backupFolder = options.emptyWindowBackupInfo && options.emptyWindowBackupInfo.backupFolder;
1355
				configuration.backupPath = this.backupMainService.registerEmptyWindowBackupSync(backupFolder, configuration.remoteAuthority);
E
Erich Gamma 已提交
1356
			}
1357
		}
1358

1359 1360 1361 1362 1363
		// Load it
		window.load(configuration);

		// Signal event
		this._onWindowLoad.fire(window.id);
E
Erich Gamma 已提交
1364 1365
	}

1366
	private getNewWindowState(configuration: IWindowConfiguration): INewWindowState {
1367
		const lastActive = this.getLastActiveWindow();
E
Erich Gamma 已提交
1368

1369 1370
		// Restore state unless we are running extension tests
		if (!configuration.extensionTestsPath) {
E
Erich Gamma 已提交
1371

1372 1373 1374
			// extension development host Window - load from stored settings if any
			if (!!configuration.extensionDevelopmentPath && this.windowsState.lastPluginDevelopmentHostWindow) {
				return this.windowsState.lastPluginDevelopmentHostWindow.uiState;
1375 1376
			}

1377
			// Known Workspace - load from stored settings
M
Matt Bierner 已提交
1378 1379 1380
			const workspace = configuration.workspace;
			if (workspace) {
				const stateForWorkspace = this.windowsState.openedWindows.filter(o => o.workspace && o.workspace.id === workspace.id).map(o => o.uiState);
1381 1382 1383 1384 1385 1386
				if (stateForWorkspace.length) {
					return stateForWorkspace[0];
				}
			}

			// Known Folder - load from stored settings
1387
			if (configuration.folderUri) {
1388
				const stateForFolder = this.windowsState.openedWindows.filter(o => o.folderUri && isEqual(o.folderUri, configuration.folderUri)).map(o => o.uiState);
1389 1390 1391
				if (stateForFolder.length) {
					return stateForFolder[0];
				}
1392 1393
			}

1394 1395 1396 1397 1398 1399
			// Empty windows with backups
			else if (configuration.backupPath) {
				const stateForEmptyWindow = this.windowsState.openedWindows.filter(o => o.backupPath === configuration.backupPath).map(o => o.uiState);
				if (stateForEmptyWindow.length) {
					return stateForEmptyWindow[0];
				}
E
Erich Gamma 已提交
1400 1401
			}

1402 1403 1404 1405 1406
			// First Window
			const lastActiveState = this.lastClosedWindowState || this.windowsState.lastActiveWindow;
			if (!lastActive && lastActiveState) {
				return lastActiveState.uiState;
			}
E
Erich Gamma 已提交
1407 1408 1409 1410 1411 1412 1413
		}

		//
		// In any other case, we do not have any stored settings for the window state, so we come up with something smart
		//

		// We want the new window to open on the same display that the last active one is in
1414
		let displayToUse: Electron.Display | undefined;
B
Benjamin Pasero 已提交
1415
		const displays = screen.getAllDisplays();
E
Erich Gamma 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

		// Single Display
		if (displays.length === 1) {
			displayToUse = displays[0];
		}

		// Multi Display
		else {

			// on mac there is 1 menu per window so we need to use the monitor where the cursor currently is
B
Benjamin Pasero 已提交
1426
			if (isMacintosh) {
B
Benjamin Pasero 已提交
1427
				const cursorPoint = screen.getCursorScreenPoint();
E
Erich Gamma 已提交
1428 1429 1430 1431 1432 1433 1434 1435
				displayToUse = screen.getDisplayNearestPoint(cursorPoint);
			}

			// if we have a last active window, use that display for the new window
			if (!displayToUse && lastActive) {
				displayToUse = screen.getDisplayMatching(lastActive.getBounds());
			}

1436
			// fallback to primary display or first display
E
Erich Gamma 已提交
1437
			if (!displayToUse) {
1438
				displayToUse = screen.getPrimaryDisplay() || displays[0];
E
Erich Gamma 已提交
1439 1440 1441
			}
		}

1442 1443 1444
		// Compute x/y based on display bounds
		// Note: important to use Math.round() because Electron does not seem to be too happy about
		// display coordinates that are not absolute numbers.
1445
		let state = defaultWindowState() as INewWindowState;
M
Matt Bierner 已提交
1446 1447
		state.x = Math.round(displayToUse.bounds.x + (displayToUse.bounds.width / 2) - (state.width! / 2));
		state.y = Math.round(displayToUse.bounds.y + (displayToUse.bounds.height / 2) - (state.height! / 2));
E
Erich Gamma 已提交
1448

1449
		// Check for newWindowDimensions setting and adjust accordingly
1450
		const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
1451 1452 1453 1454 1455 1456 1457 1458 1459
		let ensureNoOverlap = true;
		if (windowConfig && windowConfig.newWindowDimensions) {
			if (windowConfig.newWindowDimensions === 'maximized') {
				state.mode = WindowMode.Maximized;
				ensureNoOverlap = false;
			} else if (windowConfig.newWindowDimensions === 'fullscreen') {
				state.mode = WindowMode.Fullscreen;
				ensureNoOverlap = false;
			} else if (windowConfig.newWindowDimensions === 'inherit' && lastActive) {
B
Benjamin Pasero 已提交
1460 1461 1462 1463 1464 1465 1466
				const lastActiveState = lastActive.serializeWindowState();
				if (lastActiveState.mode === WindowMode.Fullscreen) {
					state.mode = WindowMode.Fullscreen; // only take mode (fixes https://github.com/Microsoft/vscode/issues/19331)
				} else {
					state = lastActiveState;
				}

1467 1468 1469 1470 1471 1472 1473 1474
				ensureNoOverlap = false;
			}
		}

		if (ensureNoOverlap) {
			state = this.ensureNoOverlap(state);
		}

1475 1476
		state.hasDefaultState = true; // flag as default state

1477
		return state;
E
Erich Gamma 已提交
1478 1479
	}

J
Joao Moreno 已提交
1480
	private ensureNoOverlap(state: ISingleWindowState): ISingleWindowState {
E
Erich Gamma 已提交
1481 1482 1483 1484
		if (WindowsManager.WINDOWS.length === 0) {
			return state;
		}

M
Matt Bierner 已提交
1485 1486 1487
		state.x = typeof state.x === 'number' ? state.x : 0;
		state.y = typeof state.y === 'number' ? state.y : 0;

1488 1489
		const existingWindowBounds = WindowsManager.WINDOWS.map(win => win.getBounds());
		while (existingWindowBounds.some(b => b.x === state.x || b.y === state.y)) {
E
Erich Gamma 已提交
1490 1491 1492 1493 1494 1495 1496
			state.x += 30;
			state.y += 30;
		}

		return state;
	}

B
Benjamin Pasero 已提交
1497
	reload(win: ICodeWindow, cli?: ParsedArgs): void {
B
Benjamin Pasero 已提交
1498 1499

		// Only reload when the window has not vetoed this
1500
		this.lifecycleService.unload(win, UnloadReason.RELOAD).then(veto => {
B
Benjamin Pasero 已提交
1501
			if (!veto) {
R
Rob Lourens 已提交
1502
				win.reload(undefined, cli);
B
Benjamin Pasero 已提交
1503 1504 1505 1506
			}
		});
	}

B
Benjamin Pasero 已提交
1507
	closeWorkspace(win: ICodeWindow): void {
1508 1509
		this.openInBrowserWindow({
			cli: this.environmentService.args,
M
Martin Aeschlimann 已提交
1510 1511
			windowToUse: win,
			remoteAuthority: win.remoteAuthority
1512 1513 1514
		});
	}

M
Matt Bierner 已提交
1515 1516
	enterWorkspace(win: ICodeWindow, path: URI): Promise<IEnterWorkspaceResult | undefined> {
		return this.workspacesManager.enterWorkspace(win, path).then(result => result ? this.doEnterWorkspace(win, result) : undefined);
1517 1518
	}

1519
	private doEnterWorkspace(win: ICodeWindow, result: IEnterWorkspaceResult): IEnterWorkspaceResult {
1520

1521
		// Mark as recently opened
M
Martin Aeschlimann 已提交
1522
		this.historyMainService.addRecentlyOpened([{ workspace: result.workspace }]);
1523

1524 1525 1526
		// Trigger Eevent to indicate load of workspace into window
		this._onWindowReady.fire(win);

1527
		return result;
1528 1529
	}

B
Benjamin Pasero 已提交
1530
	pickWorkspaceAndOpen(options: INativeOpenDialogOptions): void {
1531
		this.workspacesManager.pickWorkspaceAndOpen(options);
1532 1533
	}

B
Benjamin Pasero 已提交
1534
	focusLastActive(cli: ParsedArgs, context: OpenContext): ICodeWindow {
B
Benjamin Pasero 已提交
1535
		const lastActive = this.getLastActiveWindow();
E
Erich Gamma 已提交
1536
		if (lastActive) {
B
Benjamin Pasero 已提交
1537
			lastActive.focus();
1538 1539

			return lastActive;
E
Erich Gamma 已提交
1540 1541
		}

B
Benjamin Pasero 已提交
1542
		// No window - open new empty one
1543
		return this.open({ context, cli, forceEmpty: true })[0];
E
Erich Gamma 已提交
1544 1545
	}

M
Matt Bierner 已提交
1546
	getLastActiveWindow(): ICodeWindow | undefined {
1547
		return getLastActiveWindow(WindowsManager.WINDOWS);
E
Erich Gamma 已提交
1548 1549
	}

1550
	getLastActiveWindowForAuthority(remoteAuthority: string | undefined): ICodeWindow | undefined {
M
Martin Aeschlimann 已提交
1551 1552 1553
		return getLastActiveWindow(WindowsManager.WINDOWS.filter(w => w.remoteAuthority === remoteAuthority));
	}

1554 1555
	openNewWindow(context: OpenContext, options?: INewWindowOptions): ICodeWindow[] {
		let cli = this.environmentService.args;
1556
		const remote = options && options.remoteAuthority || undefined;
M
Martin Aeschlimann 已提交
1557 1558 1559
		if (cli && (cli.remote !== remote)) {
			cli = { ...cli, remote };
		}
1560
		return this.open({ context, cli, forceNewWindow: true, forceEmpty: true });
E
Erich Gamma 已提交
1561 1562
	}

1563 1564 1565 1566
	openNewTabbedWindow(context: OpenContext): ICodeWindow[] {
		return this.open({ context, cli: this.environmentService.args, forceNewTabbedWindow: true, forceEmpty: true });
	}

J
Johannes Rieken 已提交
1567
	waitForWindowCloseOrLoad(windowId: number): Promise<void> {
B
Benjamin Pasero 已提交
1568
		return new Promise<void>(resolve => {
1569
			function handler(id: number) {
1570
				if (id === windowId) {
1571 1572 1573
					closeListener.dispose();
					loadListener.dispose();

1574
					resolve();
1575
				}
1576 1577 1578 1579
			}

			const closeListener = this.onWindowClose(id => handler(id));
			const loadListener = this.onWindowLoad(id => handler(id));
1580 1581 1582
		});
	}

B
Benjamin Pasero 已提交
1583
	sendToFocused(channel: string, ...args: any[]): void {
E
Erich Gamma 已提交
1584 1585 1586
		const focusedWindow = this.getFocusedWindow() || this.getLastActiveWindow();

		if (focusedWindow) {
1587
			focusedWindow.sendWhenReady(channel, ...args);
E
Erich Gamma 已提交
1588 1589 1590
		}
	}

B
Benjamin Pasero 已提交
1591
	sendToAll(channel: string, payload?: any, windowIdsToIgnore?: number[]): void {
1592
		WindowsManager.WINDOWS.forEach(w => {
B
Benjamin Pasero 已提交
1593
			if (windowIdsToIgnore && windowIdsToIgnore.indexOf(w.id) >= 0) {
E
Erich Gamma 已提交
1594 1595 1596
				return; // do not send if we are instructed to ignore it
			}

1597
			w.sendWhenReady(channel, payload);
E
Erich Gamma 已提交
1598 1599 1600
		});
	}

M
Matt Bierner 已提交
1601
	getFocusedWindow(): ICodeWindow | undefined {
B
Benjamin Pasero 已提交
1602
		const win = BrowserWindow.getFocusedWindow();
E
Erich Gamma 已提交
1603 1604 1605 1606
		if (win) {
			return this.getWindowById(win.id);
		}

M
Matt Bierner 已提交
1607
		return undefined;
E
Erich Gamma 已提交
1608 1609
	}

M
Matt Bierner 已提交
1610
	getWindowById(windowId: number): ICodeWindow | undefined {
1611
		const res = WindowsManager.WINDOWS.filter(w => w.id === windowId);
E
Erich Gamma 已提交
1612 1613 1614 1615
		if (res && res.length === 1) {
			return res[0];
		}

M
Matt Bierner 已提交
1616
		return undefined;
E
Erich Gamma 已提交
1617 1618
	}

B
Benjamin Pasero 已提交
1619
	getWindows(): ICodeWindow[] {
E
Erich Gamma 已提交
1620 1621 1622
		return WindowsManager.WINDOWS;
	}

B
Benjamin Pasero 已提交
1623
	getWindowCount(): number {
E
Erich Gamma 已提交
1624 1625 1626
		return WindowsManager.WINDOWS.length;
	}

1627
	private onWindowError(window: ICodeWindow, error: WindowError): void {
B
Benjamin Pasero 已提交
1628
		this.logService.error(error === WindowError.CRASHED ? '[VS Code]: render process crashed!' : '[VS Code]: detected unresponsive');
E
Erich Gamma 已提交
1629

1630 1631
		/* __GDPR__
			"windowerror" : {
K
kieferrm 已提交
1632
				"type" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true }
1633 1634 1635 1636
			}
		*/
		this.telemetryService.publicLog('windowerror', { type: error });

E
Erich Gamma 已提交
1637 1638
		// Unresponsive
		if (error === WindowError.UNRESPONSIVE) {
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
			if (window.isExtensionDevelopmentHost || window.isExtensionTestHost || (window.win && window.win.webContents && window.win.webContents.isDevToolsOpened())) {
				// TODO@Ben Workaround for https://github.com/Microsoft/vscode/issues/56994
				// In certain cases the window can report unresponsiveness because a breakpoint was hit
				// and the process is stopped executing. The most typical cases are:
				// - devtools are opened and debugging happens
				// - window is an extensions development host that is being debugged
				// - window is an extension test development host that is being debugged
				return;
			}

			// Show Dialog
1650
			this.dialogs.showMessageBox({
B
Benjamin Pasero 已提交
1651
				title: product.nameLong,
E
Erich Gamma 已提交
1652
				type: 'warning',
1653
				buttons: [mnemonicButtonLabel(localize({ key: 'reopen', comment: ['&& denotes a mnemonic'] }, "&&Reopen")), mnemonicButtonLabel(localize({ key: 'wait', comment: ['&& denotes a mnemonic'] }, "&&Keep Waiting")), mnemonicButtonLabel(localize({ key: 'close', comment: ['&& denotes a mnemonic'] }, "&&Close"))],
1654 1655
				message: localize('appStalled', "The window is no longer responding"),
				detail: localize('appStalledDetail', "You can reopen or close the window or keep waiting."),
E
Erich Gamma 已提交
1656
				noLink: true
1657 1658 1659 1660
			}, window).then(result => {
				if (!window.win) {
					return; // Return early if the window has been going down already
				}
1661

1662 1663 1664 1665 1666 1667 1668
				if (result.button === 0) {
					window.reload();
				} else if (result.button === 2) {
					this.onBeforeWindowClose(window); // 'close' event will not be fired on destroy(), so run it manually
					window.win.destroy(); // make sure to destroy the window as it is unresponsive
				}
			});
E
Erich Gamma 已提交
1669 1670 1671 1672
		}

		// Crashed
		else {
1673
			this.dialogs.showMessageBox({
B
Benjamin Pasero 已提交
1674
				title: product.nameLong,
E
Erich Gamma 已提交
1675
				type: 'warning',
1676
				buttons: [mnemonicButtonLabel(localize({ key: 'reopen', comment: ['&& denotes a mnemonic'] }, "&&Reopen")), mnemonicButtonLabel(localize({ key: 'close', comment: ['&& denotes a mnemonic'] }, "&&Close"))],
1677 1678
				message: localize('appCrashed', "The window has crashed"),
				detail: localize('appCrashedDetail', "We are sorry for the inconvenience! You can reopen the window to continue where you left off."),
E
Erich Gamma 已提交
1679
				noLink: true
1680 1681 1682 1683
			}, window).then(result => {
				if (!window.win) {
					return; // Return early if the window has been going down already
				}
1684

1685 1686 1687 1688 1689 1690 1691
				if (result.button === 0) {
					window.reload();
				} else if (result.button === 1) {
					this.onBeforeWindowClose(window); // 'close' event will not be fired on destroy(), so run it manually
					window.win.destroy(); // make sure to destroy the window as it has crashed
				}
			});
E
Erich Gamma 已提交
1692 1693 1694
		}
	}

1695
	private onWindowClosed(win: ICodeWindow): void {
E
Erich Gamma 已提交
1696 1697 1698 1699 1700

		// Tell window
		win.dispose();

		// Remove from our list so that Electron can clean it up
B
Benjamin Pasero 已提交
1701
		const index = WindowsManager.WINDOWS.indexOf(win);
E
Erich Gamma 已提交
1702 1703 1704
		WindowsManager.WINDOWS.splice(index, 1);

		// Emit
B
Benjamin Pasero 已提交
1705
		this._onWindowsCountChanged.fire({ oldCount: WindowsManager.WINDOWS.length + 1, newCount: WindowsManager.WINDOWS.length });
1706
		this._onWindowClose.fire(win.id);
E
Erich Gamma 已提交
1707
	}
B
Benjamin Pasero 已提交
1708

B
Benjamin Pasero 已提交
1709
	pickFileFolderAndOpen(options: INativeOpenDialogOptions): void {
B
Benjamin Pasero 已提交
1710
		this.doPickAndOpen(options, true /* pick folders */, true /* pick files */);
1711 1712
	}

B
Benjamin Pasero 已提交
1713
	pickFolderAndOpen(options: INativeOpenDialogOptions): void {
B
Benjamin Pasero 已提交
1714
		this.doPickAndOpen(options, true /* pick folders */, false /* pick files */);
1715 1716
	}

B
Benjamin Pasero 已提交
1717
	pickFileAndOpen(options: INativeOpenDialogOptions): void {
B
Benjamin Pasero 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726
		this.doPickAndOpen(options, false /* pick folders */, true /* pick files */);
	}

	private doPickAndOpen(options: INativeOpenDialogOptions, pickFolders: boolean, pickFiles: boolean): void {
		const internalOptions = options as IInternalNativeOpenDialogOptions;

		internalOptions.pickFolders = pickFolders;
		internalOptions.pickFiles = pickFiles;

1727 1728
		const dialogOptions: OpenDialogOptions = internalOptions.dialogOptions || Object.create(null);
		internalOptions.dialogOptions = dialogOptions;
B
Benjamin Pasero 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741

		if (!internalOptions.dialogOptions.title) {
			if (pickFolders && pickFiles) {
				internalOptions.dialogOptions.title = localize('open', "Open");
			} else if (pickFolders) {
				internalOptions.dialogOptions.title = localize('openFolder', "Open Folder");
			} else {
				internalOptions.dialogOptions.title = localize('openFile', "Open File");
			}
		}

		if (!internalOptions.telemetryEventName) {
			if (pickFolders && pickFiles) {
K
kieferrm 已提交
1742
				// __GDPR__TODO__ classify event
B
Benjamin Pasero 已提交
1743 1744 1745 1746 1747 1748 1749 1750
				internalOptions.telemetryEventName = 'openFileFolder';
			} else if (pickFolders) {
				internalOptions.telemetryEventName = 'openFolder';
			} else {
				internalOptions.telemetryEventName = 'openFile';
			}
		}

1751 1752 1753
		this.dialogs.pickAndOpen(internalOptions);
	}

J
Johannes Rieken 已提交
1754
	showMessageBox(options: Electron.MessageBoxOptions, win?: ICodeWindow): Promise<IMessageBoxResult> {
1755 1756 1757
		return this.dialogs.showMessageBox(options, win);
	}

J
Johannes Rieken 已提交
1758
	showSaveDialog(options: Electron.SaveDialogOptions, win?: ICodeWindow): Promise<string> {
1759 1760 1761
		return this.dialogs.showSaveDialog(options, win);
	}

J
Johannes Rieken 已提交
1762
	showOpenDialog(options: Electron.OpenDialogOptions, win?: ICodeWindow): Promise<string[]> {
1763
		return this.dialogs.showOpenDialog(options, win);
B
Benjamin Pasero 已提交
1764 1765
	}

B
Benjamin Pasero 已提交
1766
	quit(): void {
B
Benjamin Pasero 已提交
1767 1768 1769

		// If the user selected to exit from an extension development host window, do not quit, but just
		// close the window unless this is the last window that is opened.
1770 1771 1772
		const window = this.getFocusedWindow();
		if (window && window.isExtensionDevelopmentHost && this.getWindowCount() > 1) {
			window.win.close();
B
Benjamin Pasero 已提交
1773 1774 1775 1776 1777 1778 1779 1780
		}

		// Otherwise: normal quit
		else {
			setTimeout(() => {
				this.lifecycleService.quit();
			}, 10 /* delay to unwind callback stack (IPC) */);
		}
1781
	}
B
Benjamin Pasero 已提交
1782 1783
}

B
Benjamin Pasero 已提交
1784
interface IInternalNativeOpenDialogOptions extends INativeOpenDialogOptions {
B
Benjamin Pasero 已提交
1785 1786 1787 1788
	pickFolders?: boolean;
	pickFiles?: boolean;
}

1789
class Dialogs {
B
Benjamin Pasero 已提交
1790

1791
	private static readonly workingDirPickerStorageKey = 'pickerWorkingDir';
1792

1793 1794
	private readonly mapWindowToDialogQueue: Map<number, Queue<any>>;
	private readonly noWindowDialogQueue: Queue<any>;
1795

B
Benjamin Pasero 已提交
1796
	constructor(
1797 1798 1799 1800
		private readonly environmentService: IEnvironmentService,
		private readonly telemetryService: ITelemetryService,
		private readonly stateService: IStateService,
		private readonly windowsMainService: IWindowsMainService,
B
Benjamin Pasero 已提交
1801
	) {
1802 1803
		this.mapWindowToDialogQueue = new Map<number, Queue<any>>();
		this.noWindowDialogQueue = new Queue<any>();
B
Benjamin Pasero 已提交
1804 1805
	}

B
Benjamin Pasero 已提交
1806
	pickAndOpen(options: INativeOpenDialogOptions): void {
1807
		this.getFileOrFolderUris(options).then(paths => {
B
Benjamin Pasero 已提交
1808 1809 1810 1811
			const numberOfPaths = paths ? paths.length : 0;

			// Telemetry
			if (options.telemetryEventName) {
K
kieferrm 已提交
1812
				// __GDPR__TODO__ Dynamic event names and dynamic properties. Can not be registered statically.
B
Benjamin Pasero 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821
				this.telemetryService.publicLog(options.telemetryEventName, {
					...options.telemetryExtraData,
					outcome: numberOfPaths ? 'success' : 'canceled',
					numberOfPaths
				});
			}

			// Open
			if (numberOfPaths) {
1822 1823
				this.windowsMainService.open({
					context: OpenContext.DIALOG,
B
Benjamin Pasero 已提交
1824
					contextWindowId: options.windowId,
1825
					cli: this.environmentService.args,
S
Sandeep Somavarapu 已提交
1826
					urisToOpen: paths,
1827 1828 1829
					forceNewWindow: options.forceNewWindow,
					forceOpenWorkspaceAsFile: options.dialogOptions && !equals(options.dialogOptions.filters, WORKSPACE_FILTER)
				});
1830 1831 1832 1833
			}
		});
	}

1834
	private getFileOrFolderUris(options: IInternalNativeOpenDialogOptions): Promise<IURIToOpen[] | undefined> {
1835

B
Benjamin Pasero 已提交
1836
		// Ensure dialog options
M
Matt Bierner 已提交
1837 1838
		const dialogOptions = options.dialogOptions || Object.create(null);
		options.dialogOptions = dialogOptions;
B
Benjamin Pasero 已提交
1839 1840

		// Ensure defaultPath
M
Matt Bierner 已提交
1841 1842
		if (!dialogOptions.defaultPath) {
			dialogOptions.defaultPath = this.stateService.getItem<string>(Dialogs.workingDirPickerStorageKey);
1843 1844
		}

B
Benjamin Pasero 已提交
1845 1846
		// Ensure properties
		if (typeof options.pickFiles === 'boolean' || typeof options.pickFolders === 'boolean') {
M
Matt Bierner 已提交
1847
			dialogOptions.properties = undefined; // let it override based on the booleans
B
Benjamin Pasero 已提交
1848 1849

			if (options.pickFiles && options.pickFolders) {
M
Matt Bierner 已提交
1850
				dialogOptions.properties = ['multiSelections', 'openDirectory', 'openFile', 'createDirectory'];
B
Benjamin Pasero 已提交
1851 1852 1853
			}
		}

M
Matt Bierner 已提交
1854 1855
		if (!dialogOptions.properties) {
			dialogOptions.properties = ['multiSelections', options.pickFolders ? 'openDirectory' : 'openFile', 'createDirectory'];
B
Benjamin Pasero 已提交
1856 1857
		}

1858
		if (isMacintosh) {
M
Matt Bierner 已提交
1859
			dialogOptions.properties.push('treatPackageAsDirectory'); // always drill into .app files
1860 1861
		}

B
Benjamin Pasero 已提交
1862
		// Show Dialog
M
Matt Bierner 已提交
1863
		const focusedWindow = (typeof options.windowId === 'number' ? this.windowsMainService.getWindowById(options.windowId) : undefined) || this.windowsMainService.getFocusedWindow();
1864

M
Matt Bierner 已提交
1865
		return this.showOpenDialog(dialogOptions, focusedWindow).then(paths => {
1866 1867 1868 1869 1870
			if (paths && paths.length > 0) {

				// Remember path in storage for next time
				this.stateService.setItem(Dialogs.workingDirPickerStorageKey, dirname(paths[0]));

1871 1872 1873 1874 1875 1876
				const result: IURIToOpen[] = [];
				for (const path of paths) {
					result.push({ uri: URI.file(path) });
				}

				return result;
1877 1878
			}

R
Rob Lourens 已提交
1879
			return undefined;
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
		});
	}

	private getDialogQueue(window?: ICodeWindow): Queue<any> {
		if (!window) {
			return this.noWindowDialogQueue;
		}

		let windowDialogQueue = this.mapWindowToDialogQueue.get(window.id);
		if (!windowDialogQueue) {
			windowDialogQueue = new Queue<any>();
			this.mapWindowToDialogQueue.set(window.id, windowDialogQueue);
		}

		return windowDialogQueue;
	}

J
Johannes Rieken 已提交
1897
	showMessageBox(options: Electron.MessageBoxOptions, window?: ICodeWindow): Promise<IMessageBoxResult> {
1898
		return this.getDialogQueue(window).queue(() => {
B
Benjamin Pasero 已提交
1899
			return new Promise(resolve => {
1900
				const callback = (response: number, checkboxChecked: boolean) => {
B
Benjamin Pasero 已提交
1901
					resolve({ button: response, checkboxChecked });
1902 1903 1904 1905 1906 1907 1908
				};

				if (window) {
					dialog.showMessageBox(window.win, options, callback);
				} else {
					dialog.showMessageBox(options, callback);
				}
1909 1910 1911 1912
			});
		});
	}

J
Johannes Rieken 已提交
1913
	showSaveDialog(options: Electron.SaveDialogOptions, window?: ICodeWindow): Promise<string> {
B
Benjamin Pasero 已提交
1914

1915 1916 1917
		function normalizePath(path: string): string {
			if (path && isMacintosh) {
				path = normalizeNFC(path); // normalize paths returned from the OS
1918
			}
1919

1920 1921
			return path;
		}
1922

1923
		return this.getDialogQueue(window).queue(() => {
B
Benjamin Pasero 已提交
1924
			return new Promise(resolve => {
1925
				const callback = (path: string) => {
B
Benjamin Pasero 已提交
1926
					resolve(normalizePath(path));
1927 1928 1929 1930 1931 1932 1933
				};

				if (window) {
					dialog.showSaveDialog(window.win, options, callback);
				} else {
					dialog.showSaveDialog(options, callback);
				}
1934 1935 1936 1937
			});
		});
	}

J
Johannes Rieken 已提交
1938
	showOpenDialog(options: Electron.OpenDialogOptions, window?: ICodeWindow): Promise<string[]> {
B
Benjamin Pasero 已提交
1939

1940 1941 1942 1943 1944 1945
		function normalizePaths(paths: string[]): string[] {
			if (paths && paths.length > 0 && isMacintosh) {
				paths = paths.map(path => normalizeNFC(path)); // normalize paths returned from the OS
			}

			return paths;
1946
		}
B
Benjamin Pasero 已提交
1947

1948
		return this.getDialogQueue(window).queue(() => {
B
Benjamin Pasero 已提交
1949
			return new Promise(resolve => {
B
Benjamin Pasero 已提交
1950 1951

				// Ensure the path exists (if provided)
B
Benjamin Pasero 已提交
1952
				let validatePathPromise: Promise<void> = Promise.resolve();
B
Benjamin Pasero 已提交
1953 1954 1955
				if (options.defaultPath) {
					validatePathPromise = exists(options.defaultPath).then(exists => {
						if (!exists) {
R
Rob Lourens 已提交
1956
							options.defaultPath = undefined;
B
Benjamin Pasero 已提交
1957 1958 1959 1960 1961 1962
						}
					});
				}

				// Show dialog and wrap as promise
				validatePathPromise.then(() => {
1963
					const callback = (paths: string[]) => {
B
Benjamin Pasero 已提交
1964
						resolve(normalizePaths(paths));
1965 1966 1967 1968 1969 1970 1971
					};

					if (window) {
						dialog.showOpenDialog(window.win, options, callback);
					} else {
						dialog.showOpenDialog(options, callback);
					}
B
Benjamin Pasero 已提交
1972
				});
1973 1974
			});
		});
1975
	}
1976 1977 1978 1979 1980
}

class WorkspacesManager {

	constructor(
1981 1982 1983 1984
		private readonly workspacesMainService: IWorkspacesMainService,
		private readonly backupMainService: IBackupMainService,
		private readonly windowsMainService: IWindowsMainService,
	) { }
1985

M
Martin Aeschlimann 已提交
1986
	enterWorkspace(window: ICodeWindow, path: URI): Promise<IEnterWorkspaceResult | null> {
B
Benjamin Pasero 已提交
1987
		if (!window || !window.win || !window.isReady) {
B
Benjamin Pasero 已提交
1988
			return Promise.resolve(null); // return early if the window is not ready or disposed
1989 1990 1991 1992
		}

		return this.isValidTargetWorkspacePath(window, path).then(isValid => {
			if (!isValid) {
B
Benjamin Pasero 已提交
1993
				return null; // return early if the workspace is not valid
1994
			}
M
Martin Aeschlimann 已提交
1995
			const workspaceIdentifier = getWorkspaceIdentifier(path);
M
Martin Aeschlimann 已提交
1996
			return this.doOpenWorkspace(window, workspaceIdentifier);
1997 1998 1999 2000
		});

	}

M
Martin Aeschlimann 已提交
2001
	private isValidTargetWorkspacePath(window: ICodeWindow, path?: URI): Promise<boolean> {
2002
		if (!path) {
B
Benjamin Pasero 已提交
2003
			return Promise.resolve(true);
2004 2005
		}

M
Martin Aeschlimann 已提交
2006
		if (window.openedWorkspace && isEqual(window.openedWorkspace.configPath, path)) {
B
Benjamin Pasero 已提交
2007
			return Promise.resolve(false); // window is already opened on a workspace with that path
2008 2009 2010
		}

		// Prevent overwriting a workspace that is currently opened in another window
M
Martin Aeschlimann 已提交
2011
		if (findWindowOnWorkspace(this.windowsMainService.getWindows(), getWorkspaceIdentifier(path))) {
2012 2013 2014 2015
			const options: Electron.MessageBoxOptions = {
				title: product.nameLong,
				type: 'info',
				buttons: [localize('ok', "OK")],
M
Martin Aeschlimann 已提交
2016
				message: localize('workspaceOpenedMessage', "Unable to save workspace '{0}'", resourcesBasename(path)),
2017
				detail: localize('workspaceOpenedDetail', "The workspace is already opened in another window. Please close that window first and then try again."),
2018 2019 2020
				noLink: true
			};

2021
			return this.windowsMainService.showMessageBox(options, this.windowsMainService.getFocusedWindow()).then(() => false);
2022 2023
		}

B
Benjamin Pasero 已提交
2024
		return Promise.resolve(true); // OK
2025 2026
	}

2027 2028
	private doOpenWorkspace(window: ICodeWindow, workspace: IWorkspaceIdentifier): IEnterWorkspaceResult {
		window.focus();
2029

2030
		// Register window for backups and migrate current backups over
2031
		let backupPath: string | undefined;
2032
		if (!window.config.extensionDevelopmentPath) {
2033
			backupPath = this.backupMainService.registerWorkspaceBackupSync({ workspace, remoteAuthority: window.remoteAuthority }, window.config.backupPath);
2034
		}
2035

2036 2037 2038 2039 2040
		// if the window was opened on an untitled workspace, delete it.
		if (window.openedWorkspace && this.workspacesMainService.isUntitledWorkspace(window.openedWorkspace)) {
			this.workspacesMainService.deleteUntitledWorkspaceSync(window.openedWorkspace);
		}

2041
		// Update window configuration properly based on transition to workspace
R
Rob Lourens 已提交
2042
		window.config.folderUri = undefined;
2043 2044 2045 2046
		window.config.workspace = workspace;
		window.config.backupPath = backupPath;

		return { workspace, backupPath };
2047 2048
	}

B
Benjamin Pasero 已提交
2049
	pickWorkspaceAndOpen(options: INativeOpenDialogOptions): void {
M
Matt Bierner 已提交
2050
		const window = (typeof options.windowId === 'number' ? this.windowsMainService.getWindowById(options.windowId) : undefined) || this.windowsMainService.getFocusedWindow() || this.windowsMainService.getLastActiveWindow();
2051 2052

		this.windowsMainService.pickFileAndOpen({
R
Rob Lourens 已提交
2053
			windowId: window ? window.id : undefined,
2054 2055 2056 2057 2058
			dialogOptions: {
				buttonLabel: mnemonicButtonLabel(localize({ key: 'openWorkspace', comment: ['&& denotes a mnemonic'] }, "&&Open")),
				title: localize('openWorkspaceTitle', "Open Workspace"),
				filters: WORKSPACE_FILTER,
				properties: ['openFile'],
2059
				defaultPath: options.dialogOptions && options.dialogOptions.defaultPath
2060
			},
2061 2062 2063
			forceNewWindow: options.forceNewWindow,
			telemetryEventName: options.telemetryEventName,
			telemetryExtraData: options.telemetryExtraData
2064 2065
		});
	}
J
Johannes Rieken 已提交
2066
}