main.ts 7.3 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';

8
import nls = require('vs/nls');
J
Johannes Rieken 已提交
9 10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { WorkbenchShell } from 'vs/workbench/electron-browser/shell';
import { IOptions } from 'vs/workbench/common/options';
12
import * as browser from 'vs/base/browser/browser';
J
Johannes Rieken 已提交
13
import { domContentLoaded } from 'vs/base/browser/dom';
E
Erich Gamma 已提交
14
import errors = require('vs/base/common/errors');
15
import comparer = require('vs/base/common/comparers');
E
Erich Gamma 已提交
16 17 18 19
import platform = require('vs/base/common/platform');
import paths = require('vs/base/common/paths');
import uri from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
J
Johannes Rieken 已提交
20
import { IResourceInput } from 'vs/platform/editor/common/editor';
J
Joao Moreno 已提交
21
import { IWorkspace, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
J
Johannes Rieken 已提交
22
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configurationService';
J
Joao Moreno 已提交
23
import { ParsedArgs } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
24
import { realpath, stat } from 'vs/base/node/pfs';
J
Johannes Rieken 已提交
25
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
26 27
import path = require('path');
import gracefulFs = require('graceful-fs');
J
Johannes Rieken 已提交
28
import { IPath, IOpenFileRequest } from 'vs/workbench/electron-browser/common';
B
Benjamin Pasero 已提交
29 30
import { IInitData } from 'vs/workbench/services/timer/common/timerService';
import { TimerService } from 'vs/workbench/services/timer/node/timerService';
A
Alex Dima 已提交
31
import { KeyboardMapperFactory } from "vs/workbench/services/keybinding/electron-browser/keybindingService";
B
Benjamin Pasero 已提交
32

33 34
import { webFrame } from 'electron';

B
Benjamin Pasero 已提交
35
import fs = require('fs');
B
Benjamin Pasero 已提交
36
gracefulFs.gracefulify(fs); // enable gracefulFs
E
Erich Gamma 已提交
37

38
export interface IWindowConfiguration extends ParsedArgs, IOpenFileRequest {
A
Alex Dima 已提交
39 40

	/**
41
	 * The physical keyboard is of ISO type (on OSX).
A
Alex Dima 已提交
42 43 44
	 */
	isISOKeyboard?: boolean;

45 46 47 48 49
	/**
	 * Accessibility support is enabled.
	 */
	accessibilitySupportEnabled?: boolean;

50 51 52
	appRoot: string;
	execPath: string;

53
	userEnv: any; /* vs/code/electron-main/env/IProcessEnvironment*/
54

E
Erich Gamma 已提交
55
	workspacePath?: string;
56 57 58

	zoomLevel?: number;
	fullscreen?: boolean;
E
Erich Gamma 已提交
59 60
}

61
export function startup(configuration: IWindowConfiguration): TPromise<void> {
62

63 64
	// Ensure others can listen to zoom level changes
	browser.setZoomFactor(webFrame.getZoomFactor());
65 66 67 68
	// See https://github.com/Microsoft/vscode/issues/26151
	// Can be trusted because we are not setting it ourselves.
	browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/true);

69 70
	browser.setFullscreen(!!configuration.fullscreen);

A
Alex Dima 已提交
71 72
	KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged(configuration.isISOKeyboard);

73
	browser.setAccessibilitySupport(configuration.accessibilitySupportEnabled ? platform.AccessibilitySupport.Enabled : platform.AccessibilitySupport.Disabled);
74

75 76 77
	// Setup Intl
	comparer.setFileNameComparer(new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }));

E
Erich Gamma 已提交
78
	// Shell Options
B
polish  
Benjamin Pasero 已提交
79 80 81
	const filesToOpen = configuration.filesToOpen && configuration.filesToOpen.length ? toInputs(configuration.filesToOpen) : null;
	const filesToCreate = configuration.filesToCreate && configuration.filesToCreate.length ? toInputs(configuration.filesToCreate) : null;
	const filesToDiff = configuration.filesToDiff && configuration.filesToDiff.length ? toInputs(configuration.filesToDiff) : null;
B
Benjamin Pasero 已提交
82
	const shellOptions: IOptions = {
83 84
		filesToOpen,
		filesToCreate,
B
Benjamin Pasero 已提交
85
		filesToDiff
E
Erich Gamma 已提交
86 87
	};

88 89 90 91 92 93
	// Resolve workspace
	return getWorkspace(configuration.workspacePath).then(workspace => {

		// Open workbench
		return openWorkbench(configuration, workspace, shellOptions);
	});
E
Erich Gamma 已提交
94 95
}

96
function toInputs(paths: IPath[], isUntitledFile?: boolean): IResourceInput[] {
E
Erich Gamma 已提交
97
	return paths.map(p => {
98 99 100 101 102 103 104
		const input = <IResourceInput>{};

		if (isUntitledFile) {
			input.resource = uri.from({ scheme: 'untitled', path: p.filePath });
		} else {
			input.resource = uri.file(p.filePath);
		}
E
Erich Gamma 已提交
105

106 107 108 109
		input.options = {
			pinned: true // opening on startup is always pinned and not preview
		};

E
Erich Gamma 已提交
110
		if (p.lineNumber) {
111 112 113
			input.options.selection = {
				startLineNumber: p.lineNumber,
				startColumn: p.columnNumber
E
Erich Gamma 已提交
114 115 116 117 118 119 120
			};
		}

		return input;
	});
}

121
function getWorkspace(workspacePath: string): TPromise<IWorkspace> {
B
polish  
Benjamin Pasero 已提交
122
	if (!workspacePath) {
123
		return TPromise.as(null);
E
Erich Gamma 已提交
124 125
	}

126 127
	return realpath(workspacePath).then(realWorkspacePath => {

E
Erich Gamma 已提交
128 129 130 131
		// for some weird reason, node adds a trailing slash to UNC paths
		// we never ever want trailing slashes as our workspace path unless
		// someone opens root ("/").
		// See also https://github.com/nodejs/io.js/issues/1765
132 133 134
		if (paths.isUNC(realWorkspacePath) && strings.endsWith(realWorkspacePath, paths.nativeSep)) {
			realWorkspacePath = strings.rtrim(realWorkspacePath, paths.nativeSep);
		}
E
Erich Gamma 已提交
135

136 137
		const workspaceResource = uri.file(realWorkspacePath);
		const folderName = path.basename(realWorkspacePath) || realWorkspacePath;
E
Erich Gamma 已提交
138

B
Benjamin Pasero 已提交
139 140 141 142 143 144 145
		return stat(realWorkspacePath).then(folderStat => {
			return <IWorkspace>{
				'resource': workspaceResource,
				'name': folderName,
				'uid': platform.isLinux ? folderStat.ino : folderStat.birthtime.getTime() // On Linux, birthtime is ctime, so we cannot use it! We use the ino instead!
			};
		});
146 147 148 149 150
	}, (error) => {
		errors.onUnexpectedError(error);

		return null; // treat invalid paths as empty workspace
	});
E
Erich Gamma 已提交
151 152
}

153
function openWorkbench(environment: IWindowConfiguration, workspace: IWorkspace, options: IOptions): TPromise<void> {
154
	const environmentService = new EnvironmentService(environment, environment.execPath);
155
	const contextService = new WorkspaceContextService(workspace);
156
	const configurationService = new WorkspaceConfigurationService(contextService, environmentService);
B
Benjamin Pasero 已提交
157
	const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !contextService.hasWorkspace());
158 159 160 161

	// Since the configuration service is one of the core services that is used in so many places, we initialize it
	// right before startup of the workbench shell to have its data ready for consumers
	return configurationService.initialize().then(() => {
162
		timerService.beforeDOMContentLoaded = Date.now();
163

A
Alex Dima 已提交
164
		return domContentLoaded().then(() => {
165
			timerService.afterDOMContentLoaded = Date.now();
166 167

			// Open Shell
168
			timerService.beforeWorkbenchOpen = Date.now();
169
			const shell = new WorkbenchShell(document.body, {
170
				configurationService,
171
				contextService,
B
Benjamin Pasero 已提交
172 173
				environmentService,
				timerService
174
			}, options);
175 176 177 178 179 180
			shell.open();

			// Inform user about loading issues from the loader
			(<any>self).require.config({
				onError: (err: any) => {
					if (err.errorCode === 'load') {
181
						shell.onUnexpectedError(loaderError(err));
182
					}
E
Erich Gamma 已提交
183
				}
184
			});
A
Alex Dima 已提交
185
		});
186
	});
187 188 189 190 191 192 193 194
}

function loaderError(err: Error): Error {
	if (platform.isWeb) {
		return new Error(nls.localize('loaderError', "Failed to load a required file. Either you are no longer connected to the internet or the server you are connected to is offline. Please refresh the browser to try again."));
	}

	return new Error(nls.localize('loaderErrorNative', "Failed to load a required file. Please restart the application to try again. Details: {0}", JSON.stringify(err)));
195
}