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

'use strict';

J
Joao Moreno 已提交
8
import * as path from 'path';
B
Benjamin Pasero 已提交
9
import * as fs from 'original-fs';
J
Joao Moreno 已提交
10 11
import * as nls from 'vs/nls';
import * as arrays from 'vs/base/common/arrays';
12
import { assign, mixin } from 'vs/base/common/objects';
D
Daniel Imms 已提交
13
import { IBackupMainService } from 'vs/platform/backup/common/backup';
J
Joao Moreno 已提交
14
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
15
import { IStorageService } from 'vs/platform/storage/node/storage';
16
import { CodeWindow, IWindowState as ISingleWindowState, defaultWindowState, WindowMode } from 'vs/code/electron-main/window';
17
import { ipcMain as ipc, screen, BrowserWindow, dialog, systemPreferences } from 'electron';
B
Benjamin Pasero 已提交
18
import { IPathWithLineAndColumn, parseLineAndColumnAware } from 'vs/code/node/paths';
19
import { ILifecycleService, UnloadReason } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
20
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
21
import { ILogService } from 'vs/platform/log/common/log';
B
Benjamin Pasero 已提交
22
import { IWindowSettings, OpenContext, IPath, IWindowConfiguration } from 'vs/platform/windows/common/windows';
23
import { getLastActiveWindow, findBestWindowOrFolderForFile, findWindowOnFolder, findWindowOnWorkspace } from 'vs/code/node/windowsFinder';
24
import CommonEvent, { Emitter } from 'vs/base/common/event';
25
import product from 'vs/platform/node/product';
26
import { ITelemetryService, ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
27
import { isEqual } from 'vs/base/common/paths';
28
import { IWindowsMainService, IOpenConfiguration } from "vs/platform/windows/electron-main/windows";
29
import { IHistoryMainService } from "vs/platform/history/common/history";
B
Benjamin Pasero 已提交
30
import { IProcessEnvironment, isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
31
import { TPromise } from "vs/base/common/winjs.base";
32 33
import { IWorkspacesMainService, IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, IWorkspaceSavedEvent } from "vs/platform/workspaces/common/workspaces";
import { IInstantiationService } from "vs/platform/instantiation/common/instantiation";
E
Erich Gamma 已提交
34 35 36 37 38 39

enum WindowError {
	UNRESPONSIVE,
	CRASHED
}

40 41 42 43
interface INewWindowState extends ISingleWindowState {
	hasDefaultState?: boolean;
}

44
interface ILegacyWindowState extends IWindowState {
E
Erich Gamma 已提交
45
	workspacePath?: string;
46 47 48
}

interface IWindowState {
49
	workspace?: IWorkspaceIdentifier;
50
	folderPath?: string;
51
	backupPath: string;
J
Joao Moreno 已提交
52
	uiState: ISingleWindowState;
E
Erich Gamma 已提交
53 54
}

55 56 57 58
interface ILegacyWindowsState extends IWindowsState {
	openedFolders?: IWindowState[];
}

E
Erich Gamma 已提交
59 60 61
interface IWindowsState {
	lastActiveWindow?: IWindowState;
	lastPluginDevelopmentHostWindow?: IWindowState;
62
	openedWindows: IWindowState[];
E
Erich Gamma 已提交
63 64
}

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
	folderPath?: string;
B
Benjamin Pasero 已提交
73 74 75 76 77 78 79 80 81 82

	initialStartup?: boolean;

	filesToOpen?: IPath[];
	filesToCreate?: IPath[];
	filesToDiff?: IPath[];

	forceNewWindow?: boolean;
	windowToUse?: CodeWindow;

83
	emptyWindowBackupFolder?: string;
B
Benjamin Pasero 已提交
84 85
}

86 87
interface IWindowToOpen extends IPath {

88
	// the workspace for a Code instance to open
89
	workspace?: IWorkspaceIdentifier;
90

91 92
	// the folder path for a Code instance to open
	folderPath?: string;
93 94 95 96 97 98 99 100

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

	// indicator to create the file path in the Code instance
	createFilePath?: boolean;
}

J
Joao Moreno 已提交
101
export class WindowsManager implements IWindowsMainService {
J
Joao Moreno 已提交
102

103
	_serviceBrand: any;
E
Erich Gamma 已提交
104 105 106

	private static windowsStateStorageKey = 'windowsState';

B
Benjamin Pasero 已提交
107
	private static WINDOWS: CodeWindow[] = [];
E
Erich Gamma 已提交
108

B
Benjamin Pasero 已提交
109
	private initialUserEnv: IProcessEnvironment;
110

E
Erich Gamma 已提交
111
	private windowsState: IWindowsState;
112
	private lastClosedWindowState: IWindowState;
E
Erich Gamma 已提交
113

B
Benjamin Pasero 已提交
114 115
	private fileDialog: FileDialog;

B
Benjamin Pasero 已提交
116 117
	private _onWindowReady = new Emitter<CodeWindow>();
	onWindowReady: CommonEvent<CodeWindow> = this._onWindowReady.event;
118 119 120 121

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

122 123 124
	private _onWindowReload = new Emitter<number>();
	onWindowReload: CommonEvent<number> = this._onWindowReload.event;

J
Joao Moreno 已提交
125 126
	constructor(
		@ILogService private logService: ILogService,
J
Joao Moreno 已提交
127
		@IStorageService private storageService: IStorageService,
128
		@IEnvironmentService private environmentService: IEnvironmentService,
B
Benjamin Pasero 已提交
129
		@ILifecycleService private lifecycleService: ILifecycleService,
130
		@IBackupMainService private backupService: IBackupMainService,
131
		@ITelemetryService private telemetryService: ITelemetryService,
132
		@IConfigurationService private configurationService: IConfigurationService,
B
Benjamin Pasero 已提交
133
		@IHistoryMainService private historyService: IHistoryMainService,
134 135
		@IWorkspacesMainService private workspacesService: IWorkspacesMainService,
		@IInstantiationService private instantiationService: IInstantiationService
136
	) {
137
		this.windowsState = this.storageService.getItem<IWindowsState>(WindowsManager.windowsStateStorageKey) || { openedWindows: [] };
138
		this.fileDialog = new FileDialog(environmentService, telemetryService, storageService, this);
139

140 141
		this.migrateLegacyWindowState();
	}
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	private migrateLegacyWindowState(): void {
		const state: ILegacyWindowsState = this.windowsState;

		// TODO@Ben migration from previous openedFolders to new openedWindows property
		if (Array.isArray(state.openedFolders) && state.openedFolders.length > 0) {
			state.openedWindows = state.openedFolders;
			state.openedFolders = void 0;
		} else if (!state.openedWindows) {
			state.openedWindows = [];
		}

		// TODO@Ben migration from previous workspacePath in window state to folderPath
		const states: ILegacyWindowState[] = [];
		states.push(state.lastActiveWindow);
		states.push(state.lastPluginDevelopmentHostWindow);
		states.push(...state.openedWindows);
		states.forEach(state => {
			if (state && typeof state.workspacePath === 'string') {
				state.folderPath = state.workspacePath;
				state.workspacePath = void 0;
			}
		});
165
	}
J
Joao Moreno 已提交
166

B
Benjamin Pasero 已提交
167
	public ready(initialUserEnv: IProcessEnvironment): void {
168
		this.initialUserEnv = initialUserEnv;
169 170

		this.registerListeners();
E
Erich Gamma 已提交
171 172 173
	}

	private registerListeners(): void {
174

175
		// React to workbench loaded events from windows
B
Benjamin Pasero 已提交
176
		ipc.on('vscode:workbenchLoaded', (event, windowId: number) => {
J
Joao Moreno 已提交
177
			this.logService.log('IPC#vscode-workbenchLoaded');
E
Erich Gamma 已提交
178

B
Benjamin Pasero 已提交
179
			const win = this.getWindowById(windowId);
E
Erich Gamma 已提交
180 181 182 183
			if (win) {
				win.setReady();

				// Event
184
				this._onWindowReady.fire(win);
E
Erich Gamma 已提交
185 186 187
			}
		});

188 189 190 191 192 193 194 195 196 197 198
		// 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');
				}
			});
		}

199
		// Update our windows state before quitting and before closing windows
B
Benjamin Pasero 已提交
200
		this.lifecycleService.onBeforeWindowClose(win => this.onBeforeWindowClose(win as CodeWindow));
201
		this.lifecycleService.onBeforeQuit(() => this.onBeforeQuit());
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

		// Handle workspace save event
		this.workspacesService.onWorkspaceSaved(e => this.onWorkspaceSaved(e));
	}

	private onWorkspaceSaved(e: IWorkspaceSavedEvent): void {

		// A workspace was saved to a different config location. Make sure to update our
		// window states with this new location.
		const states = [this.windowsState.lastActiveWindow, this.windowsState.lastPluginDevelopmentHostWindow, ...this.windowsState.openedWindows];
		states.forEach(state => {
			if (state && state.workspace && state.workspace.id === e.workspace.id && state.workspace.configPath !== e.workspace.configPath) {
				state.workspace.configPath = e.workspace.configPath;
			}
		});
217 218 219 220 221 222 223 224 225 226
	}

	// Note that onBeforeQuit() and onBeforeWindowClose() are fired in different order depending on the OS:
	// - macOS: since the app will not quit when closing the last window, you will always first get
	//          the onBeforeQuit() event followed by N onbeforeWindowClose() events for each window
	// - 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()
	//          and then onBeforeQuit(). Using the quit action however will first issue onBeforeQuit()
	//          and then onBeforeWindowClose().
	private onBeforeQuit(): void {
227
		const currentWindowsState: ILegacyWindowsState = {
228 229
			openedWindows: [],
			openedFolders: [], // TODO@Ben migration so that old clients do not fail over data (prevents NPEs)
230
			lastPluginDevelopmentHostWindow: this.windowsState.lastPluginDevelopmentHostWindow,
231
			lastActiveWindow: this.lastClosedWindowState
232 233 234 235 236 237 238 239
		};

		// 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 已提交
240

241
			if (activeWindow) {
242
				currentWindowsState.lastActiveWindow = this.toWindowState(activeWindow);
E
Erich Gamma 已提交
243
			}
244 245 246 247 248
		}

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

252
		// 3.) All windows (except extension host) for N >= 2 to support restoreWindows: all or for auto update
253 254 255 256 257
		//
		// 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) {
258
			currentWindowsState.openedWindows = WindowsManager.WINDOWS.filter(w => !w.isExtensionDevelopmentHost).map(w => this.toWindowState(w));
259
		}
E
Erich Gamma 已提交
260

261 262 263
		// Persist
		this.storageService.setItem(WindowsManager.windowsStateStorageKey, currentWindowsState);
	}
264

265
	// See note on #onBeforeQuit() for details how these events are flowing
B
Benjamin Pasero 已提交
266
	private onBeforeWindowClose(win: CodeWindow): void {
267 268 269 270 271
		if (this.lifecycleService.isQuitRequested()) {
			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
272
		const state: IWindowState = this.toWindowState(win);
273 274 275 276
		if (win.isExtensionDevelopmentHost && !win.isExtensionTestHost) {
			this.windowsState.lastPluginDevelopmentHostWindow = state; // do not let test run window state overwrite our extension development state
		}

277
		// Any non extension host window with same workspace or folder
278
		else if (!win.isExtensionDevelopmentHost && (!!win.openedWorkspace || !!win.openedFolderPath)) {
279
			this.windowsState.openedWindows.forEach(o => {
280
				const sameWorkspace = win.openedWorkspace && o.workspace.id === win.openedWorkspace.id;
281 282 283
				const sameFolder = win.openedFolderPath && isEqual(o.folderPath, win.openedFolderPath, !isLinux /* ignorecase */);

				if (sameWorkspace || sameFolder) {
284 285 286 287 288 289 290
					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.
291 292 293
		// 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) {
294 295
			this.lastClosedWindowState = state;
		}
E
Erich Gamma 已提交
296 297
	}

298 299
	private toWindowState(win: CodeWindow): IWindowState {
		return {
300
			workspace: win.openedWorkspace,
301 302 303 304 305 306
			folderPath: win.openedFolderPath,
			backupPath: win.backupPath,
			uiState: win.serializeWindowState()
		};
	}

307 308 309 310 311 312 313
	public closeWorkspace(win: CodeWindow): void {
		this.openInBrowserWindow({
			cli: this.environmentService.args,
			windowToUse: win
		});
	}

B
Benjamin Pasero 已提交
314
	public open(openConfig: IOpenConfiguration): CodeWindow[] {
315
		const windowsToOpen = this.getWindowsToOpen(openConfig);
E
Erich Gamma 已提交
316

317 318 319 320 321 322 323 324 325
		let filesToOpen = windowsToOpen.filter(path => !!path.filePath && !path.createFilePath);
		let filesToCreate = windowsToOpen.filter(path => !!path.filePath && path.createFilePath);
		let filesToDiff: IPath[];
		if (openConfig.diffMode && filesToOpen.length === 2) {
			filesToDiff = filesToOpen;
			filesToOpen = [];
			filesToCreate = []; // diff ignores other files that do not exist
		} else {
			filesToDiff = [];
E
Erich Gamma 已提交
326 327
		}

328 329 330
		//
		// These are windows to open to show workspaces
		//
331
		const workspacesToOpen = arrays.distinct(windowsToOpen.filter(win => !!win.workspace).map(win => win.workspace), workspace => workspace.id); // prevent duplicates
332 333 334 335 336 337

		//
		// These are windows to open to show either folders or files (including diffing files or creating them)
		//
		const foldersToOpen = arrays.distinct(windowsToOpen.filter(win => win.folderPath && !win.filePath).map(win => win.folderPath), folder => isLinux ? folder : folder.toLowerCase()); // prevent duplicates

338
		//
339
		// These are windows to restore because of hot-exit or empty windows from previous session
340 341
		//
		const hotExitRestore = (openConfig.initialStartup && !openConfig.cli.extensionDevelopmentPath);
342
		const foldersToRestore = hotExitRestore ? this.backupService.getFolderBackupPaths() : [];
343
		const workspacesToRestore = hotExitRestore ? this.backupService.getWorkspaceBackups() : [];
344
		let emptyToRestore = hotExitRestore ? this.backupService.getEmptyWindowBackupPaths() : [];
345
		emptyToRestore.push(...windowsToOpen.filter(w => !w.workspace && !w.folderPath && w.backupPath).map(w => path.basename(w.backupPath))); // add empty windows with backupPath
346
		emptyToRestore = arrays.distinct(emptyToRestore); // prevent duplicates
347

348 349 350
		//
		// These are empty windows to open
		//
351
		const emptyToOpen = windowsToOpen.filter(win => !win.workspace && !win.folderPath && !win.filePath && !win.backupPath).length;
352

353
		// Open based on config
354
		const usedWindows = this.doOpen(openConfig, workspacesToOpen, workspacesToRestore, foldersToOpen, foldersToRestore, emptyToRestore, emptyToOpen, filesToOpen, filesToCreate, filesToDiff);
355 356 357 358 359 360 361 362

		// Make sure the last active window gets focus if we opened multiple
		if (usedWindows.length > 1 && this.windowsState.lastActiveWindow) {
			let lastActiveWindw = usedWindows.filter(w => w.backupPath === this.windowsState.lastActiveWindow.backupPath);
			if (lastActiveWindw.length) {
				lastActiveWindw[0].focus();
			}
		}
363

364 365 366
		// 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
		if (!usedWindows.some(w => w.isExtensionDevelopmentHost) && !openConfig.cli.diff) {
367
			const recentlyOpenedWorkspaces: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier)[] = [];
368
			const recentlyOpenedFiles: string[] = [];
369 370

			windowsToOpen.forEach(win => {
371 372 373 374
				if (win.workspace || win.folderPath) {
					recentlyOpenedWorkspaces.push(win.workspace || win.folderPath);
				} else if (win.filePath) {
					recentlyOpenedFiles.push(win.filePath);
375 376 377
				}
			});

378
			this.historyService.addRecentlyOpened(recentlyOpenedWorkspaces, recentlyOpenedFiles);
379
		}
380

381 382 383 384 385 386 387
		// If we got started with --wait from the CLI, we need to signal to the outside when the window
		// used for the edit operation is closed so that the waiting process can continue. We do this by
		// deleting the waitMarkerFilePath.
		if (openConfig.context === OpenContext.CLI && openConfig.cli.wait && openConfig.cli.waitMarkerFilePath && usedWindows.length === 1 && usedWindows[0]) {
			this.waitForWindowClose(usedWindows[0].id).done(() => fs.unlink(openConfig.cli.waitMarkerFilePath, error => void 0));
		}

388 389 390 391 392
		return usedWindows;
	}

	private doOpen(
		openConfig: IOpenConfiguration,
393 394
		workspacesToOpen: IWorkspaceIdentifier[],
		workspacesToRestore: IWorkspaceIdentifier[],
395 396 397 398 399 400 401 402 403
		foldersToOpen: string[],
		foldersToRestore: string[],
		emptyToRestore: string[],
		emptyToOpen: number,
		filesToOpen: IPath[],
		filesToCreate: IPath[],
		filesToDiff: IPath[]
	) {

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

B
Benjamin Pasero 已提交
407
		// 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
B
Benjamin Pasero 已提交
408
		const usedWindows: CodeWindow[] = [];
B
wip  
Benjamin Pasero 已提交
409
		if (!foldersToOpen.length && !foldersToRestore.length && !emptyToRestore.length && (filesToOpen.length > 0 || filesToCreate.length > 0 || filesToDiff.length > 0)) {
E
Erich Gamma 已提交
410

411
			// Find suitable window or folder path to open files in
412
			const fileToCheck = filesToOpen[0] || filesToCreate[0] || filesToDiff[0];
B
Benjamin Pasero 已提交
413
			const bestWindowOrFolder = findBestWindowOrFolderForFile({
414 415 416 417 418 419 420
				windows: WindowsManager.WINDOWS,
				newWindow: openFilesInNewWindow,
				reuseWindow: openConfig.forceReuseWindow,
				context: openConfig.context,
				filePath: fileToCheck && fileToCheck.filePath,
				userHome: this.environmentService.userHome
			});
B
Benjamin Pasero 已提交
421

422
			// We found a suitable window to open the files within: send the files to open over
423 424
			if (bestWindowOrFolder instanceof CodeWindow && bestWindowOrFolder.openedFolderPath) {
				foldersToOpen.push(bestWindowOrFolder.openedFolderPath);
425 426 427 428 429
			}

			// We found a suitable folder to open: add it to foldersToOpen
			else if (typeof bestWindowOrFolder === 'string') {
				foldersToOpen.push(bestWindowOrFolder);
E
Erich Gamma 已提交
430 431
			}

432
			// Finally, if no window or folder is found, just open the files in an empty window
E
Erich Gamma 已提交
433
			else {
B
Benjamin Pasero 已提交
434
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
435 436 437 438 439 440 441
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
					filesToOpen,
					filesToCreate,
					filesToDiff,
					forceNewWindow: true
B
Benjamin Pasero 已提交
442
				}));
E
Erich Gamma 已提交
443

444 445 446 447
				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
				filesToDiff = [];
E
Erich Gamma 已提交
448 449 450
			}
		}

451
		// Handle workspaces to open (instructed and to restore)
452
		const allWorkspacesToOpen = arrays.distinct([...workspacesToOpen, ...workspacesToRestore], workspace => workspace.id); // prevent duplicates
453 454
		if (allWorkspacesToOpen.length > 0) {

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
			// Check for existing instances that have same workspace ID but different configuration path
			// For now we reload that window with the new configuration so that the configuration path change
			// can travel properly.
			allWorkspacesToOpen.forEach(workspaceToOpen => {
				const existingWindow = findWindowOnWorkspace(WindowsManager.WINDOWS, workspaceToOpen);
				if (existingWindow && existingWindow.openedWorkspace.configPath !== workspaceToOpen.configPath) {
					usedWindows.push(this.doOpenFolderOrWorkspace(openConfig, { workspace: workspaceToOpen }, false, filesToOpen, filesToCreate, filesToDiff, existingWindow));

					// Reset these because we handled them
					filesToOpen = [];
					filesToCreate = [];
					filesToDiff = [];

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

472 473 474 475 476 477
			// Check for existing instances
			const windowsOnWorkspace = arrays.coalesce(allWorkspacesToOpen.map(workspaceToOpen => findWindowOnWorkspace(WindowsManager.WINDOWS, workspaceToOpen)));
			if (windowsOnWorkspace.length > 0) {
				const windowOnWorkspace = windowsOnWorkspace[0];

				// Do open files
B
Benjamin Pasero 已提交
478
				usedWindows.push(this.doOpenFilesInExistingWindow(windowOnWorkspace, filesToOpen, filesToCreate, filesToDiff));
479 480 481 482 483 484 485 486 487 488 489

				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
				filesToDiff = [];

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

			// Open remaining ones
			allWorkspacesToOpen.forEach(workspaceToOpen => {
490
				if (windowsOnWorkspace.some(win => win.openedWorkspace.id === workspaceToOpen.id)) {
491 492 493 494
					return; // ignore folders that are already open
				}

				// Do open folder
495
				usedWindows.push(this.doOpenFolderOrWorkspace(openConfig, { workspace: workspaceToOpen }, openFolderInNewWindow, filesToOpen, filesToCreate, filesToDiff));
496 497 498 499 500 501 502 503 504 505

				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
				filesToDiff = [];

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

506
		// Handle folders to open (instructed and to restore)
507
		const allFoldersToOpen = arrays.distinct([...foldersToOpen, ...foldersToRestore], folder => isLinux ? folder : folder.toLowerCase()); // prevent duplicates
508
		if (allFoldersToOpen.length > 0) {
E
Erich Gamma 已提交
509 510

			// Check for existing instances
511 512
			const windowsOnFolderPath = arrays.coalesce(allFoldersToOpen.map(folderToOpen => findWindowOnFolder(WindowsManager.WINDOWS, folderToOpen)));
			if (windowsOnFolderPath.length > 0) {
513
				const windowOnFolderPath = windowsOnFolderPath[0];
E
Erich Gamma 已提交
514

515
				// Do open files
B
Benjamin Pasero 已提交
516
				usedWindows.push(this.doOpenFilesInExistingWindow(windowOnFolderPath, filesToOpen, filesToCreate, filesToDiff));
517

E
Erich Gamma 已提交
518 519 520
				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
521
				filesToDiff = [];
E
Erich Gamma 已提交
522

B
Benjamin Pasero 已提交
523
				openFolderInNewWindow = true; // any other folders to open must open in new window then
E
Erich Gamma 已提交
524 525 526
			}

			// Open remaining ones
527
			allFoldersToOpen.forEach(folderToOpen => {
528
				if (windowsOnFolderPath.some(win => isEqual(win.openedFolderPath, folderToOpen, !isLinux /* ignorecase */))) {
E
Erich Gamma 已提交
529 530 531
					return; // ignore folders that are already open
				}

532 533
				// Do open folder
				usedWindows.push(this.doOpenFolderOrWorkspace(openConfig, { folderPath: folderToOpen }, openFolderInNewWindow, filesToOpen, filesToCreate, filesToDiff));
E
Erich Gamma 已提交
534 535 536 537

				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
538
				filesToDiff = [];
E
Erich Gamma 已提交
539

B
Benjamin Pasero 已提交
540
				openFolderInNewWindow = true; // any other folders to open must open in new window then
E
Erich Gamma 已提交
541 542 543
			});
		}

544
		// Handle empty to restore
545
		if (emptyToRestore.length > 0) {
546
			emptyToRestore.forEach(emptyWindowBackupFolder => {
B
Benjamin Pasero 已提交
547
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
548 549 550 551 552 553 554
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
					filesToOpen,
					filesToCreate,
					filesToDiff,
					forceNewWindow: true,
555
					emptyWindowBackupFolder
B
Benjamin Pasero 已提交
556
				}));
557

B
wip  
Benjamin Pasero 已提交
558 559 560 561 562
				// Reset these because we handled them
				filesToOpen = [];
				filesToCreate = [];
				filesToDiff = [];

B
Benjamin Pasero 已提交
563
				openFolderInNewWindow = true; // any other folders to open must open in new window then
564 565
			});
		}
B
Benjamin Pasero 已提交
566

567
		// Only open empty if no empty windows were restored
568 569
		else if (emptyToOpen > 0) {
			for (let i = 0; i < emptyToOpen; i++) {
B
Benjamin Pasero 已提交
570
				usedWindows.push(this.openInBrowserWindow({
B
Benjamin Pasero 已提交
571 572 573
					userEnv: openConfig.userEnv,
					cli: openConfig.cli,
					initialStartup: openConfig.initialStartup,
574
					forceNewWindow: openFolderInNewWindow
B
Benjamin Pasero 已提交
575
				}));
E
Erich Gamma 已提交
576

B
Benjamin Pasero 已提交
577
				openFolderInNewWindow = true; // any other folders to open must open in new window then
578 579
			}
		}
E
Erich Gamma 已提交
580

581
		return arrays.distinct(usedWindows);
E
Erich Gamma 已提交
582 583
	}

B
Benjamin Pasero 已提交
584
	private doOpenFilesInExistingWindow(window: CodeWindow, filesToOpen: IPath[], filesToCreate: IPath[], filesToDiff: IPath[]): CodeWindow {
585 586 587 588 589
		window.focus(); // make sure window has focus

		window.ready().then(readyWindow => {
			readyWindow.send('vscode:openFiles', { filesToOpen, filesToCreate, filesToDiff });
		});
B
Benjamin Pasero 已提交
590 591

		return window;
592 593
	}

594
	private doOpenFolderOrWorkspace(openConfig: IOpenConfiguration, folderOrWorkspace: IWindowToOpen, openInNewWindow: boolean, filesToOpen: IPath[], filesToCreate: IPath[], filesToDiff: IPath[], windowToUse?: CodeWindow): CodeWindow {
595 596 597 598
		const browserWindow = this.openInBrowserWindow({
			userEnv: openConfig.userEnv,
			cli: openConfig.cli,
			initialStartup: openConfig.initialStartup,
599
			workspace: folderOrWorkspace.workspace,
600 601 602 603
			folderPath: folderOrWorkspace.folderPath,
			filesToOpen,
			filesToCreate,
			filesToDiff,
604 605
			forceNewWindow: openInNewWindow,
			windowToUse
606 607 608 609 610
		});

		return browserWindow;
	}

611 612
	private getWindowsToOpen(openConfig: IOpenConfiguration): IWindowToOpen[] {
		let windowsToOpen: IWindowToOpen[];
E
Erich Gamma 已提交
613

614
		// Extract paths: from API
B
Benjamin Pasero 已提交
615
		if (openConfig.pathsToOpen && openConfig.pathsToOpen.length > 0) {
616
			windowsToOpen = this.doExtractPathsFromAPI(openConfig.pathsToOpen, openConfig.cli && openConfig.cli.goto);
E
Erich Gamma 已提交
617 618
		}

B
Benjamin Pasero 已提交
619 620
		// Check for force empty
		else if (openConfig.forceEmpty) {
621
			windowsToOpen = [Object.create(null)];
E
Erich Gamma 已提交
622 623
		}

624
		// Extract paths: from CLI
B
Benjamin Pasero 已提交
625
		else if (openConfig.cli._.length > 0) {
626
			windowsToOpen = this.doExtractPathsFromCLI(openConfig.cli);
B
Benjamin Pasero 已提交
627 628
		}

629
		// Extract windows: from previous session
B
Benjamin Pasero 已提交
630
		else {
631
			windowsToOpen = this.doGetWindowsFromLastSession();
B
Benjamin Pasero 已提交
632 633
		}

634
		return windowsToOpen;
E
Erich Gamma 已提交
635 636
	}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	private doExtractPathsFromAPI(paths: string[], gotoLineMode: boolean): IPath[] {
		let pathsToOpen = paths.map(pathToOpen => {
			const path = this.parsePath(pathToOpen, false, gotoLineMode);

			// Warn if the requested path to open does not exist
			if (!path) {
				const options: Electron.ShowMessageBoxOptions = {
					title: product.nameLong,
					type: 'info',
					buttons: [nls.localize('ok', "OK")],
					message: nls.localize('pathNotExistTitle', "Path does not exist"),
					detail: nls.localize('pathNotExistDetail', "The path '{0}' does not seem to exist anymore on disk.", pathToOpen),
					noLink: true
				};

				const activeWindow = BrowserWindow.getFocusedWindow();
				if (activeWindow) {
					dialog.showMessageBox(activeWindow, options);
				} else {
					dialog.showMessageBox(options);
				}
			}
B
Benjamin Pasero 已提交
659

660 661 662 663 664 665 666 667 668 669 670 671 672
			return path;
		});

		// get rid of nulls
		pathsToOpen = arrays.coalesce(pathsToOpen);

		return pathsToOpen;
	}

	private doExtractPathsFromCLI(cli: ParsedArgs): IPath[] {
		const pathsToOpen = cli._.map(candidate => this.parsePath(candidate, true /* ignoreFileNotFound */, cli.goto)).filter(path => !!path);
		if (pathsToOpen.length > 0) {
			return pathsToOpen;
B
Benjamin Pasero 已提交
673 674 675 676 677 678
		}

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

679 680 681
	private doGetWindowsFromLastSession(): IWindowToOpen[] {
		const restoreWindows = this.getRestoreWindowsSetting();
		const lastActiveWindow = this.windowsState.lastActiveWindow;
B
Benjamin Pasero 已提交
682

683
		switch (restoreWindows) {
B
Benjamin Pasero 已提交
684

685
			// none: we always open an empty window
686 687
			case 'none':
				return [Object.create(null)];
B
Benjamin Pasero 已提交
688

689
			// one: restore last opened workspace/folder or empty window
690 691
			case 'one':
				if (lastActiveWindow) {
B
Benjamin Pasero 已提交
692

693 694 695 696 697 698 699 700 701 702
					// workspace
					if (lastActiveWindow.workspace) {
						return [{ workspace: lastActiveWindow.workspace }];
					}

					// folder (if path is valid)
					else if (lastActiveWindow.folderPath) {
						const validatedFolder = this.parsePath(lastActiveWindow.folderPath);
						if (validatedFolder) {
							return [validatedFolder];
703 704
						}
					}
B
Benjamin Pasero 已提交
705

706
					// otherwise use backup path to restore empty windows
707 708 709 710 711 712 713 714 715 716
					else if (lastActiveWindow.backupPath) {
						return [{ backupPath: lastActiveWindow.backupPath }];
					}
				}
				break;

			// all: restore all windows
			// folders: restore last opened folders only
			case 'all':
			case 'folders':
717
				const windowsToOpen: IWindowToOpen[] = [];
718

719 720 721 722
				// Workspaces
				const workspaces = this.windowsState.openedWindows.filter(w => !!w.workspace).map(w => w.workspace);
				if (lastActiveWindow && lastActiveWindow.workspace) {
					workspaces.push(lastActiveWindow.workspace);
723
				}
724
				windowsToOpen.push(...workspaces.map(workspace => ({ workspace })));
B
Benjamin Pasero 已提交
725

726 727 728 729 730 731
				// Folders
				const folders = this.windowsState.openedWindows.filter(w => !!w.folderPath).map(w => w.folderPath);
				if (lastActiveWindow && lastActiveWindow.folderPath) {
					folders.push(lastActiveWindow.folderPath);
				}
				windowsToOpen.push(...folders.map(candidate => this.parsePath(candidate)).filter(path => !!path));
B
Benjamin Pasero 已提交
732

733 734
				// Windows that were Empty
				if (restoreWindows === 'all') {
735 736
					const lastOpenedEmpty = this.windowsState.openedWindows.filter(w => !w.workspace && !w.folderPath && w.backupPath).map(w => w.backupPath);
					const lastActiveEmpty = lastActiveWindow && !lastActiveWindow.workspace && !lastActiveWindow.folderPath && lastActiveWindow.backupPath;
737 738 739 740 741 742 743 744 745 746 747 748
					if (lastActiveEmpty) {
						lastOpenedEmpty.push(lastActiveEmpty);
					}

					windowsToOpen.push(...lastOpenedEmpty.map(backupPath => ({ backupPath })));
				}

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

				break;
B
Benjamin Pasero 已提交
749
		}
E
Erich Gamma 已提交
750

751
		// Always fallback to empty window
B
Benjamin Pasero 已提交
752
		return [Object.create(null)];
E
Erich Gamma 已提交
753 754
	}

755 756 757 758 759 760
	private getRestoreWindowsSetting(): RestoreWindowsSetting {
		let restoreWindows: RestoreWindowsSetting;
		if (this.lifecycleService.wasRestarted) {
			restoreWindows = 'all'; // always reopen all windows when an update was applied
		} else {
			const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
761
			restoreWindows = ((windowConfig && windowConfig.restoreWindows) || 'one') as RestoreWindowsSetting;
762

B
fix npe  
Benjamin Pasero 已提交
763
			if (restoreWindows === 'one' /* default */ && windowConfig && windowConfig.reopenFolders) {
764 765 766 767 768 769 770 771 772 773 774 775
				restoreWindows = windowConfig.reopenFolders; // TODO@Ben migration
			}

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

		return restoreWindows;
	}

	private parsePath(anyPath: string, ignoreFileNotFound?: boolean, gotoLineMode?: boolean): IWindowToOpen {
E
Erich Gamma 已提交
776 777 778 779
		if (!anyPath) {
			return null;
		}

780
		let parsedPath: IPathWithLineAndColumn;
E
Erich Gamma 已提交
781
		if (gotoLineMode) {
J
Joao Moreno 已提交
782
			parsedPath = parseLineAndColumnAware(anyPath);
E
Erich Gamma 已提交
783 784 785
			anyPath = parsedPath.path;
		}

B
Benjamin Pasero 已提交
786
		const candidate = path.normalize(anyPath);
E
Erich Gamma 已提交
787
		try {
B
Benjamin Pasero 已提交
788
			const candidateStat = fs.statSync(candidate);
E
Erich Gamma 已提交
789
			if (candidateStat) {
790
				if (candidateStat.isFile()) {
791 792 793 794 795 796 797 798

					// Workspace
					const workspace = this.workspacesService.resolveWorkspaceSync(candidate);
					if (workspace) {
						return { workspace };
					}

					// File
799
					return {
800
						filePath: candidate,
E
Erich Gamma 已提交
801
						lineNumber: gotoLineMode ? parsedPath.line : void 0,
802
						columnNumber: gotoLineMode ? parsedPath.column : void 0
803 804 805 806 807 808 809
					};
				}

				// Folder
				return {
					folderPath: candidate
				};
E
Erich Gamma 已提交
810 811
			}
		} catch (error) {
812
			this.historyService.removeFromRecentlyOpened([candidate]); // since file does not seem to exist anymore, remove from recent
813

E
Erich Gamma 已提交
814 815 816 817 818 819 820 821
			if (ignoreFileNotFound) {
				return { filePath: candidate, createFilePath: true }; // assume this is a file that does not yet exist
			}
		}

		return null;
	}

B
Benjamin Pasero 已提交
822 823 824 825
	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
		const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
826 827 828
		const openFolderInNewWindowConfig = (windowConfig && windowConfig.openFoldersInNewWindow) || 'default' /* default */;
		const openFilesInNewWindowConfig = (windowConfig && windowConfig.openFilesInNewWindow) || 'off' /* default */;

B
Benjamin Pasero 已提交
829
		let openFolderInNewWindow = (openConfig.preferNewWindow || openConfig.forceNewWindow) && !openConfig.forceReuseWindow;
830 831
		if (!openConfig.forceNewWindow && !openConfig.forceReuseWindow && (openFolderInNewWindowConfig === 'on' || openFolderInNewWindowConfig === 'off')) {
			openFolderInNewWindow = (openFolderInNewWindowConfig === 'on');
B
Benjamin Pasero 已提交
832 833 834 835 836 837 838 839 840 841 842
		}

		// 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)
		let openFilesInNewWindow: boolean;
		if (openConfig.forceNewWindow || openConfig.forceReuseWindow) {
			openFilesInNewWindow = openConfig.forceNewWindow && !openConfig.forceReuseWindow;
		} else {
			if (openConfig.context === OpenContext.DOCK) {
				openFilesInNewWindow = true; // only on macOS do we allow to open files in a new window if this is triggered via DOCK context
			}

843 844
			if (!openConfig.cli.extensionDevelopmentPath && (openFilesInNewWindowConfig === 'on' || openFilesInNewWindowConfig === 'off')) {
				openFilesInNewWindow = (openFilesInNewWindowConfig === 'on');
B
Benjamin Pasero 已提交
845 846 847 848 849 850
			}
		}

		return { openFolderInNewWindow, openFilesInNewWindow };
	}

B
Benjamin Pasero 已提交
851
	public openExtensionDevelopmentHostWindow(openConfig: IOpenConfiguration): void {
E
Erich Gamma 已提交
852

B
Benjamin Pasero 已提交
853 854 855 856 857 858 859
		// 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.
		let res = WindowsManager.WINDOWS.filter(w => w.config && isEqual(w.config.extensionDevelopmentPath, openConfig.cli.extensionDevelopmentPath, !isLinux /* ignorecase */));
		if (res && res.length === 1) {
			this.reload(res[0], openConfig.cli);
			res[0].focus(); // make sure it gets focus and is restored
E
Erich Gamma 已提交
860

B
Benjamin Pasero 已提交
861 862
			return;
		}
E
Erich Gamma 已提交
863

864
		// Fill in previously opened folder unless an explicit path is provided and we are not unit testing
B
Benjamin Pasero 已提交
865
		if (openConfig.cli._.length === 0 && !openConfig.cli.extensionTestsPath) {
866 867 868
			const folderToOpen = this.windowsState.lastPluginDevelopmentHostWindow && this.windowsState.lastPluginDevelopmentHostWindow.folderPath;
			if (folderToOpen) {
				openConfig.cli._ = [folderToOpen];
E
Erich Gamma 已提交
869 870 871
			}
		}

B
Benjamin Pasero 已提交
872 873
		// Make sure we are not asked to open a path that is already opened
		if (openConfig.cli._.length > 0) {
874
			res = WindowsManager.WINDOWS.filter(w => w.openedFolderPath && openConfig.cli._.indexOf(w.openedFolderPath) >= 0);
B
Benjamin Pasero 已提交
875 876 877
			if (res.length) {
				openConfig.cli._ = [];
			}
E
Erich Gamma 已提交
878 879
		}

B
Benjamin Pasero 已提交
880 881
		// Open it
		this.open({ context: openConfig.context, cli: openConfig.cli, forceNewWindow: true, forceEmpty: openConfig.cli._.length === 0, userEnv: openConfig.userEnv });
E
Erich Gamma 已提交
882 883
	}

B
Benjamin Pasero 已提交
884 885 886 887 888 889 890 891
	private openInBrowserWindow(options: IOpenBrowserWindowOptions): CodeWindow {

		// Build IWindowConfiguration from config and options
		const configuration: IWindowConfiguration = mixin({}, options.cli); // inherit all properties from CLI
		configuration.appRoot = this.environmentService.appRoot;
		configuration.execPath = process.execPath;
		configuration.userEnv = assign({}, this.initialUserEnv, options.userEnv || {});
		configuration.isInitialStartup = options.initialStartup;
892
		configuration.workspace = options.workspace;
893
		configuration.folderPath = options.folderPath;
B
Benjamin Pasero 已提交
894 895 896 897 898
		configuration.filesToOpen = options.filesToOpen;
		configuration.filesToCreate = options.filesToCreate;
		configuration.filesToDiff = options.filesToDiff;
		configuration.nodeCachedDataDir = this.environmentService.nodeCachedDataDir;

899
		// if we know the backup folder upfront (for empty windows to restore), we can set it
900
		// directly here which helps for restoring UI state associated with that window.
B
Benjamin Pasero 已提交
901
		// For all other cases we first call into registerEmptyWindowBackupSync() to set it before
902
		// loading the window.
903 904
		if (options.emptyWindowBackupFolder) {
			configuration.backupPath = path.join(this.environmentService.backupHome, options.emptyWindowBackupFolder);
905 906
		}

B
Benjamin Pasero 已提交
907
		let codeWindow: CodeWindow;
B
Benjamin Pasero 已提交
908 909
		if (!options.forceNewWindow) {
			codeWindow = options.windowToUse || this.getLastActiveWindow();
B
Benjamin Pasero 已提交
910 911
			if (codeWindow) {
				codeWindow.focus();
E
Erich Gamma 已提交
912 913 914 915
			}
		}

		// New window
B
Benjamin Pasero 已提交
916
		if (!codeWindow) {
917
			const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
918 919 920 921 922 923 924 925 926 927
			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 {
928
				allowFullscreen = this.lifecycleService.wasRestarted || (windowConfig && windowConfig.restoreFullscreen);
929 930 931 932 933
			}

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

935
			codeWindow = this.instantiationService.createInstance(CodeWindow, {
936
				state,
937
				extensionDevelopmentPath: configuration.extensionDevelopmentPath,
938
				isExtensionTestHost: !!configuration.extensionTestsPath
939
			});
940

B
Benjamin Pasero 已提交
941
			WindowsManager.WINDOWS.push(codeWindow);
E
Erich Gamma 已提交
942 943

			// Window Events
B
Benjamin Pasero 已提交
944 945 946 947 948
			codeWindow.win.webContents.removeAllListeners('devtools-reload-page'); // remove built in listener so we can handle this on our own
			codeWindow.win.webContents.on('devtools-reload-page', () => this.reload(codeWindow));
			codeWindow.win.webContents.on('crashed', () => this.onWindowError(codeWindow, WindowError.CRASHED));
			codeWindow.win.on('unresponsive', () => this.onWindowError(codeWindow, WindowError.UNRESPONSIVE));
			codeWindow.win.on('closed', () => this.onWindowClosed(codeWindow));
E
Erich Gamma 已提交
949 950

			// Lifecycle
B
Benjamin Pasero 已提交
951
			this.lifecycleService.registerWindow(codeWindow);
E
Erich Gamma 已提交
952 953 954 955 956 957
		}

		// Existing window
		else {

			// Some configuration things get inherited if the window is being reused and we are
B
Benjamin Pasero 已提交
958
			// in extension development host mode. These options are all development related.
B
Benjamin Pasero 已提交
959
			const currentWindowConfig = codeWindow.config;
A
Alex Dima 已提交
960 961
			if (!configuration.extensionDevelopmentPath && currentWindowConfig && !!currentWindowConfig.extensionDevelopmentPath) {
				configuration.extensionDevelopmentPath = currentWindowConfig.extensionDevelopmentPath;
B
Benjamin Pasero 已提交
962
				configuration.verbose = currentWindowConfig.verbose;
963
				configuration.debugBrkPluginHost = currentWindowConfig.debugBrkPluginHost;
964
				configuration.debugPluginHost = currentWindowConfig.debugPluginHost;
965
				configuration['extensions-dir'] = currentWindowConfig['extensions-dir'];
E
Erich Gamma 已提交
966 967 968 969
			}
		}

		// Only load when the window has not vetoed this
B
Benjamin Pasero 已提交
970
		this.lifecycleService.unload(codeWindow, UnloadReason.LOAD).done(veto => {
E
Erich Gamma 已提交
971 972
			if (!veto) {

B
Benjamin Pasero 已提交
973 974
				// Register window for backups
				if (!configuration.extensionDevelopmentPath) {
975 976
					if (configuration.workspace) {
						configuration.backupPath = this.backupService.registerWorkspaceBackupSync(configuration.workspace);
977
					} else if (configuration.folderPath) {
B
Benjamin Pasero 已提交
978
						configuration.backupPath = this.backupService.registerFolderBackupSync(configuration.folderPath);
B
Benjamin Pasero 已提交
979 980 981
					} else {
						configuration.backupPath = this.backupService.registerEmptyWindowBackupSync(options.emptyWindowBackupFolder);
					}
B
Benjamin Pasero 已提交
982 983
				}

E
Erich Gamma 已提交
984
				// Load it
B
Benjamin Pasero 已提交
985
				codeWindow.load(configuration);
E
Erich Gamma 已提交
986 987
			}
		});
988

B
Benjamin Pasero 已提交
989
		return codeWindow;
E
Erich Gamma 已提交
990 991
	}

992
	private getNewWindowState(configuration: IWindowConfiguration): INewWindowState {
993
		const lastActive = this.getLastActiveWindow();
E
Erich Gamma 已提交
994

995 996
		// Restore state unless we are running extension tests
		if (!configuration.extensionTestsPath) {
E
Erich Gamma 已提交
997

998 999 1000
			// extension development host Window - load from stored settings if any
			if (!!configuration.extensionDevelopmentPath && this.windowsState.lastPluginDevelopmentHostWindow) {
				return this.windowsState.lastPluginDevelopmentHostWindow.uiState;
1001 1002
			}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
			// Known Workspace - load from stored settings
			if (configuration.workspace) {
				const stateForWorkspace = this.windowsState.openedWindows.filter(o => o.workspace && o.workspace.id === configuration.workspace.id).map(o => o.uiState);
				if (stateForWorkspace.length) {
					return stateForWorkspace[0];
				}
			}

			// Known Folder - load from stored settings
			if (configuration.folderPath) {
				const stateForFolder = this.windowsState.openedWindows.filter(o => isEqual(o.folderPath, configuration.folderPath, !isLinux /* ignorecase */)).map(o => o.uiState);
				if (stateForFolder.length) {
					return stateForFolder[0];
				}
1017 1018
			}

1019 1020 1021 1022 1023 1024
			// 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 已提交
1025 1026
			}

1027 1028 1029 1030 1031
			// First Window
			const lastActiveState = this.lastClosedWindowState || this.windowsState.lastActiveWindow;
			if (!lastActive && lastActiveState) {
				return lastActiveState.uiState;
			}
E
Erich Gamma 已提交
1032 1033 1034 1035 1036 1037 1038
		}

		//
		// 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
1039
		let displayToUse: Electron.Display;
B
Benjamin Pasero 已提交
1040
		const displays = screen.getAllDisplays();
E
Erich Gamma 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

		// 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 已提交
1051
			if (isMacintosh) {
B
Benjamin Pasero 已提交
1052
				const cursorPoint = screen.getCursorScreenPoint();
E
Erich Gamma 已提交
1053 1054 1055 1056 1057 1058 1059 1060
				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());
			}

1061
			// fallback to primary display or first display
E
Erich Gamma 已提交
1062
			if (!displayToUse) {
1063
				displayToUse = screen.getPrimaryDisplay() || displays[0];
E
Erich Gamma 已提交
1064 1065 1066
			}
		}

1067
		let state = defaultWindowState() as INewWindowState;
1068 1069
		state.x = displayToUse.bounds.x + (displayToUse.bounds.width / 2) - (state.width / 2);
		state.y = displayToUse.bounds.y + (displayToUse.bounds.height / 2) - (state.height / 2);
E
Erich Gamma 已提交
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
		// Check for newWindowDimensions setting and adjust accordingly
		const windowConfig = this.configurationService.getConfiguration<IWindowSettings>('window');
		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 已提交
1082 1083 1084 1085 1086 1087 1088
				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;
				}

1089 1090 1091 1092 1093 1094 1095 1096
				ensureNoOverlap = false;
			}
		}

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

1097 1098
		state.hasDefaultState = true; // flag as default state

1099
		return state;
E
Erich Gamma 已提交
1100 1101
	}

J
Joao Moreno 已提交
1102
	private ensureNoOverlap(state: ISingleWindowState): ISingleWindowState {
E
Erich Gamma 已提交
1103 1104 1105 1106
		if (WindowsManager.WINDOWS.length === 0) {
			return state;
		}

1107 1108
		const existingWindowBounds = WindowsManager.WINDOWS.map(win => win.getBounds());
		while (existingWindowBounds.some(b => b.x === state.x || b.y === state.y)) {
E
Erich Gamma 已提交
1109 1110 1111 1112 1113 1114 1115
			state.x += 30;
			state.y += 30;
		}

		return state;
	}

B
Benjamin Pasero 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	public reload(win: CodeWindow, cli?: ParsedArgs): void {

		// Only reload when the window has not vetoed this
		this.lifecycleService.unload(win, UnloadReason.RELOAD).done(veto => {
			if (!veto) {
				win.reload(cli);

				// Emit
				this._onWindowReload.fire(win.id);
			}
		});
	}

B
Benjamin Pasero 已提交
1129
	public focusLastActive(cli: ParsedArgs, context: OpenContext): CodeWindow {
B
Benjamin Pasero 已提交
1130
		const lastActive = this.getLastActiveWindow();
E
Erich Gamma 已提交
1131
		if (lastActive) {
B
Benjamin Pasero 已提交
1132
			lastActive.focus();
1133 1134

			return lastActive;
E
Erich Gamma 已提交
1135 1136
		}

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

B
Benjamin Pasero 已提交
1141
	public getLastActiveWindow(): CodeWindow {
1142
		return getLastActiveWindow(WindowsManager.WINDOWS);
E
Erich Gamma 已提交
1143 1144
	}

1145 1146
	public openNewWindow(context: OpenContext): void {
		this.open({ context, cli: this.environmentService.args, forceNewWindow: true, forceEmpty: true });
E
Erich Gamma 已提交
1147 1148
	}

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	public waitForWindowClose(windowId: number): TPromise<void> {
		return new TPromise<void>(c => {
			const toDispose = this.onWindowClose(id => {
				if (id === windowId) {
					toDispose.dispose();
					c(null);
				}
			});
		});
	}

E
Erich Gamma 已提交
1160 1161 1162 1163
	public sendToFocused(channel: string, ...args: any[]): void {
		const focusedWindow = this.getFocusedWindow() || this.getLastActiveWindow();

		if (focusedWindow) {
1164
			focusedWindow.sendWhenReady(channel, ...args);
E
Erich Gamma 已提交
1165 1166 1167
		}
	}

1168
	public sendToAll(channel: string, payload?: any, windowIdsToIgnore?: number[]): void {
1169
		WindowsManager.WINDOWS.forEach(w => {
B
Benjamin Pasero 已提交
1170
			if (windowIdsToIgnore && windowIdsToIgnore.indexOf(w.id) >= 0) {
E
Erich Gamma 已提交
1171 1172 1173
				return; // do not send if we are instructed to ignore it
			}

1174
			w.sendWhenReady(channel, payload);
E
Erich Gamma 已提交
1175 1176 1177
		});
	}

B
Benjamin Pasero 已提交
1178
	public getFocusedWindow(): CodeWindow {
B
Benjamin Pasero 已提交
1179
		const win = BrowserWindow.getFocusedWindow();
E
Erich Gamma 已提交
1180 1181 1182 1183 1184 1185 1186
		if (win) {
			return this.getWindowById(win.id);
		}

		return null;
	}

B
Benjamin Pasero 已提交
1187
	public getWindowById(windowId: number): CodeWindow {
1188
		const res = WindowsManager.WINDOWS.filter(w => w.id === windowId);
E
Erich Gamma 已提交
1189 1190 1191 1192 1193 1194 1195
		if (res && res.length === 1) {
			return res[0];
		}

		return null;
	}

B
Benjamin Pasero 已提交
1196
	public getWindows(): CodeWindow[] {
E
Erich Gamma 已提交
1197 1198 1199 1200 1201 1202 1203
		return WindowsManager.WINDOWS;
	}

	public getWindowCount(): number {
		return WindowsManager.WINDOWS.length;
	}

B
Benjamin Pasero 已提交
1204
	private onWindowError(codeWindow: CodeWindow, error: WindowError): void {
B
Benjamin Pasero 已提交
1205
		this.logService.error(error === WindowError.CRASHED ? '[VS Code]: render process crashed!' : '[VS Code]: detected unresponsive');
E
Erich Gamma 已提交
1206 1207 1208

		// Unresponsive
		if (error === WindowError.UNRESPONSIVE) {
B
Benjamin Pasero 已提交
1209
			dialog.showMessageBox(codeWindow.win, {
B
Benjamin Pasero 已提交
1210
				title: product.nameLong,
E
Erich Gamma 已提交
1211
				type: 'warning',
B
Benjamin Pasero 已提交
1212
				buttons: [nls.localize('reopen', "Reopen"), nls.localize('wait', "Keep Waiting"), nls.localize('close', "Close")],
1213
				message: nls.localize('appStalled', "The window is no longer responding"),
B
Benjamin Pasero 已提交
1214
				detail: nls.localize('appStalledDetail', "You can reopen or close the window or keep waiting."),
E
Erich Gamma 已提交
1215
				noLink: true
1216
			}, result => {
B
Benjamin Pasero 已提交
1217
				if (!codeWindow.win) {
1218 1219 1220
					return; // Return early if the window has been going down already
				}

E
Erich Gamma 已提交
1221
				if (result === 0) {
B
Benjamin Pasero 已提交
1222
					codeWindow.reload();
1223
				} else if (result === 2) {
B
Benjamin Pasero 已提交
1224 1225
					this.onBeforeWindowClose(codeWindow); // 'close' event will not be fired on destroy(), so run it manually
					codeWindow.win.destroy(); // make sure to destroy the window as it is unresponsive
E
Erich Gamma 已提交
1226 1227 1228 1229 1230 1231
				}
			});
		}

		// Crashed
		else {
B
Benjamin Pasero 已提交
1232
			dialog.showMessageBox(codeWindow.win, {
B
Benjamin Pasero 已提交
1233
				title: product.nameLong,
E
Erich Gamma 已提交
1234
				type: 'warning',
B
Benjamin Pasero 已提交
1235
				buttons: [nls.localize('reopen', "Reopen"), nls.localize('close', "Close")],
1236
				message: nls.localize('appCrashed', "The window has crashed"),
B
Benjamin Pasero 已提交
1237
				detail: nls.localize('appCrashedDetail', "We are sorry for the inconvenience! You can reopen the window to continue where you left off."),
E
Erich Gamma 已提交
1238
				noLink: true
1239
			}, result => {
B
Benjamin Pasero 已提交
1240
				if (!codeWindow.win) {
1241 1242 1243
					return; // Return early if the window has been going down already
				}

1244
				if (result === 0) {
B
Benjamin Pasero 已提交
1245
					codeWindow.reload();
1246
				} else if (result === 1) {
B
Benjamin Pasero 已提交
1247 1248
					this.onBeforeWindowClose(codeWindow); // 'close' event will not be fired on destroy(), so run it manually
					codeWindow.win.destroy(); // make sure to destroy the window as it has crashed
1249
				}
E
Erich Gamma 已提交
1250 1251 1252 1253
			});
		}
	}

B
Benjamin Pasero 已提交
1254
	private onWindowClosed(win: CodeWindow): void {
E
Erich Gamma 已提交
1255 1256 1257 1258 1259

		// Tell window
		win.dispose();

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

		// Emit
1264
		this._onWindowClose.fire(win.id);
E
Erich Gamma 已提交
1265
	}
B
Benjamin Pasero 已提交
1266

B
renames  
Benjamin Pasero 已提交
1267
	public pickFileFolderAndOpen(forceNewWindow?: boolean, data?: ITelemetryData): void {
B
Benjamin Pasero 已提交
1268
		this.fileDialog.pickAndOpen({ pickFolders: true, pickFiles: true, forceNewWindow }, 'openFileFolder', data);
1269 1270
	}

B
renames  
Benjamin Pasero 已提交
1271
	public pickFileAndOpen(forceNewWindow?: boolean, path?: string, window?: CodeWindow, data?: ITelemetryData): void {
B
Benjamin Pasero 已提交
1272
		this.fileDialog.pickAndOpen({ pickFiles: true, forceNewWindow, path, window, title: nls.localize('openFile', "Open File") }, 'openFile', data);
1273 1274
	}

B
renames  
Benjamin Pasero 已提交
1275
	public pickFolderAndOpen(forceNewWindow?: boolean, window?: CodeWindow, data?: ITelemetryData): void {
B
Benjamin Pasero 已提交
1276
		this.fileDialog.pickAndOpen({ pickFolders: true, forceNewWindow, window, title: nls.localize('openFolder', "Open Folder") }, 'openFolder', data);
B
Benjamin Pasero 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	}

	public quit(): void {

		// 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.
		const codeWindow = this.getFocusedWindow();
		if (codeWindow && codeWindow.isExtensionDevelopmentHost && this.getWindowCount() > 1) {
			codeWindow.win.close();
		}

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

interface INativeOpenDialogOptions {
B
Benjamin Pasero 已提交
1298
	title?: string;
B
Benjamin Pasero 已提交
1299 1300 1301 1302 1303
	pickFolders?: boolean;
	pickFiles?: boolean;
	path?: string;
	forceNewWindow?: boolean;
	window?: CodeWindow;
1304
	buttonLabel?: string;
B
Benjamin Pasero 已提交
1305 1306 1307 1308 1309
}

class FileDialog {

	private static workingDirPickerStorageKey = 'pickerWorkingDir';
1310

B
Benjamin Pasero 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319
	constructor(
		private environmentService: IEnvironmentService,
		private telemetryService: ITelemetryService,
		private storageService: IStorageService,
		private windowsMainService: IWindowsMainService
	) {
	}

	public pickAndOpen(options: INativeOpenDialogOptions, eventName: string, data?: ITelemetryData): void {
1320 1321 1322
		this.getFileOrFolderPaths(options, (paths: string[]) => {
			const nOfPaths = paths ? paths.length : 0;
			if (nOfPaths) {
B
Benjamin Pasero 已提交
1323
				this.windowsMainService.open({ context: OpenContext.DIALOG, cli: this.environmentService.args, pathsToOpen: paths, forceNewWindow: options.forceNewWindow });
1324 1325 1326 1327 1328 1329 1330 1331 1332
			}
			this.telemetryService.publicLog(eventName, {
				...data,
				outcome: nOfPaths ? 'success' : 'canceled',
				nOfPaths
			});
		});
	}

1333
	public getFileOrFolderPaths(options: INativeOpenDialogOptions, clb: (paths: string[]) => void): void {
B
Benjamin Pasero 已提交
1334 1335
		const workingDir = options.path || this.storageService.getItem<string>(FileDialog.workingDirPickerStorageKey);
		const focussedWindow = options.window || this.windowsMainService.getFocusedWindow();
1336 1337 1338 1339 1340 1341 1342 1343 1344

		let pickerProperties: ('openFile' | 'openDirectory' | 'multiSelections' | 'createDirectory')[];
		if (options.pickFiles && options.pickFolders) {
			pickerProperties = ['multiSelections', 'openDirectory', 'openFile', 'createDirectory'];
		} else {
			pickerProperties = ['multiSelections', options.pickFolders ? 'openDirectory' : 'openFile', 'createDirectory'];
		}

		dialog.showOpenDialog(focussedWindow && focussedWindow.win, {
B
Benjamin Pasero 已提交
1345
			title: options && options.title ? options.title : void 0,
1346
			defaultPath: workingDir,
1347 1348
			properties: pickerProperties,
			buttonLabel: options && options.buttonLabel ? options.buttonLabel : void 0
1349 1350 1351 1352
		}, paths => {
			if (paths && paths.length > 0) {

				// Remember path in storage for next time
B
Benjamin Pasero 已提交
1353
				this.storageService.setItem(FileDialog.workingDirPickerStorageKey, path.dirname(paths[0]));
1354 1355 1356 1357 1358 1359 1360 1361

				// Return
				clb(paths);
			} else {
				clb(void (0));
			}
		});
	}
1362
}