index.js 6.8 KB
Newer Older
J
Joao Moreno 已提交
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.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6 7
// Warning: Do not use the `let` declarator in this file, it breaks our minification

J
Joao Moreno 已提交
8 9
'use strict';

J
Johannes Rieken 已提交
10
if (window.location.search.indexOf('prof-startup') >= 0) {
11
	var profiler = require('v8-profiler');
12
	profiler.startProfiling('renderer', true);
13 14
}

J
Joao Moreno 已提交
15 16
/*global window,document,define*/

J
Johannes Rieken 已提交
17
const startTimer = require('../../../base/node/startupTimers').startTimer;
J
Joao Moreno 已提交
18 19 20 21 22
const path = require('path');
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;

J
Johannes Rieken 已提交
23
process.lazyEnv = new Promise(function (resolve) {
J
Joao Moreno 已提交
24 25
	const handle = setTimeout(function () {
		resolve();
J
Joao Moreno 已提交
26
		console.warn('renderer did not receive lazyEnv in time');
J
Joao Moreno 已提交
27
	}, 10000);
28
	ipc.once('vscode:acceptShellEnv', function (event, shellEnv) {
J
Joao Moreno 已提交
29
		clearTimeout(handle);
30 31 32 33 34 35
		assign(process.env, shellEnv);
		resolve(process.env);
	});
	ipc.send('vscode:fetchShellEnv', remote.getCurrentWindow().id);
});

J
Joao Moreno 已提交
36 37
function onError(error, enableDeveloperTools) {
	if (enableDeveloperTools) {
J
Joao Moreno 已提交
38
		remote.getCurrentWebContents().openDevTools();
J
Joao Moreno 已提交
39 40 41 42 43 44 45 46 47 48 49
	}

	console.error('[uncaught exception]: ' + error);

	if (error.stack) {
		console.error(error.stack);
	}
}

function assign(destination, source) {
	return Object.keys(source)
J
Joao Moreno 已提交
50
		.reduce(function (r, key) { r[key] = source[key]; return r; }, destination);
J
Joao Moreno 已提交
51 52 53 54 55 56
}

function parseURLQueryArgs() {
	const search = window.location.search || '';

	return search.split(/[?&]/)
J
Joao Moreno 已提交
57 58 59 60
		.filter(function (param) { return !!param; })
		.map(function (param) { return param.split('='); })
		.filter(function (param) { return param.length === 2; })
		.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
J
Joao Moreno 已提交
61 62 63 64 65 66 67 68 69 70 71 72
}

function createScript(src, onload) {
	const script = document.createElement('script');
	script.src = src;
	script.addEventListener('load', onload);

	const head = document.getElementsByTagName('head')[0];
	head.insertBefore(script, head.lastChild);
}

function uriFromPath(_path) {
J
Joao Moreno 已提交
73
	var pathName = path.resolve(_path).replace(/\\/g, '/');
J
Joao Moreno 已提交
74 75 76 77 78 79 80 81 82 83
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

	return encodeURI('file://' + pathName);
}

function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
B
Benjamin Pasero 已提交
84
	var listener;
J
Joao Moreno 已提交
85
	if (enableDeveloperTools) {
86
		const extractKey = function (e) {
J
Joao Moreno 已提交
87 88 89 90 91 92 93 94 95 96 97 98
			return [
				e.ctrlKey ? 'ctrl-' : '',
				e.metaKey ? 'meta-' : '',
				e.altKey ? 'alt-' : '',
				e.shiftKey ? 'shift-' : '',
				e.keyCode
			].join('');
		};

		const TOGGLE_DEV_TOOLS_KB = (process.platform === 'darwin' ? 'meta-alt-73' : 'ctrl-shift-73'); // mac: Cmd-Alt-I, rest: Ctrl-Shift-I
		const RELOAD_KB = (process.platform === 'darwin' ? 'meta-82' : 'ctrl-82'); // mac: Cmd-R, rest: Ctrl-R

B
Benjamin Pasero 已提交
99
		listener = function (e) {
J
Joao Moreno 已提交
100 101
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
J
Joao Moreno 已提交
102
				remote.getCurrentWebContents().toggleDevTools();
J
Joao Moreno 已提交
103
			} else if (key === RELOAD_KB) {
104
				remote.getCurrentWindow().reload();
J
Joao Moreno 已提交
105
			}
B
Benjamin Pasero 已提交
106 107
		};
		window.addEventListener('keydown', listener);
J
Joao Moreno 已提交
108 109
	}

J
Joao Moreno 已提交
110
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools); });
B
Benjamin Pasero 已提交
111 112 113 114 115 116

	return function () {
		if (listener) {
			window.removeEventListener('keydown', listener);
			listener = void 0;
		}
J
Joao Moreno 已提交
117
	};
J
Joao Moreno 已提交
118 119 120 121 122 123 124 125 126 127 128
}

function main() {
	const webFrame = require('electron').webFrame;
	const args = parseURLQueryArgs();
	const configuration = JSON.parse(args['config'] || '{}') || {};

	// Correctly inherit the parent's environment
	assign(process.env, configuration.userEnv);

	// Get the nls configuration into the process.env as early as possible.
J
Joao Moreno 已提交
129
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
130 131 132 133 134 135 136 137
	const config = process.env['VSCODE_NLS_CONFIG'];
	if (config) {
		process.env['VSCODE_NLS_CONFIG'] = config;
		try {
			nlsConfig = JSON.parse(config);
		} catch (e) { /*noop*/ }
	}

J
Joao Moreno 已提交
138
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
139 140 141 142 143 144 145 146
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}

	window.document.documentElement.setAttribute('lang', locale);

147
	const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
B
Benjamin Pasero 已提交
148
	const unbind = registerListeners(enableDeveloperTools);
J
Joao Moreno 已提交
149 150

	// disable pinch zoom & apply zoom level early to avoid glitches
151
	const zoomLevel = configuration.zoomLevel;
152
	webFrame.setVisualZoomLevelLimits(1, 1);
153 154
	if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
		webFrame.setZoomLevel(zoomLevel);
J
Joao Moreno 已提交
155 156 157 158
	}

	// Load the loader and start loading the workbench
	const rootUrl = uriFromPath(configuration.appRoot) + '/out';
B
Benjamin Pasero 已提交
159

J
Joao Moreno 已提交
160 161
	// In the bundled version the nls plugin is packaged with the loader so the NLS Plugins
	// loads as soon as the loader loads. To be able to have pseudo translation
J
Joao Moreno 已提交
162
	const loaderTimer = startTimer('load:loader');
163 164
	createScript(rootUrl + '/vs/loader.js', function () {
		define('fs', ['original-fs'], function (originalFS) { return originalFS; }); // replace the patched electron fs with the original node fs for all AMD code
165
		loaderTimer.stop();
B
Benjamin Pasero 已提交
166

167 168
		window.MonacoEnvironment = {};

169
		const onNodeCachedData = window.MonacoEnvironment.onNodeCachedData = [];
J
Joao Moreno 已提交
170 171 172
		require.config({
			baseUrl: rootUrl,
			'vs/nls': nlsConfig,
173 174
			recordStats: !!configuration.performance,
			nodeCachedDataDir: configuration.nodeCachedDataDir,
J
Joao Moreno 已提交
175
			onNodeCachedData: function () { onNodeCachedData.push(arguments); },
176
			nodeModules: [/*BUILD->INSERT_NODE_MODULES*/]
J
Joao Moreno 已提交
177
		});
B
Benjamin Pasero 已提交
178

J
Joao Moreno 已提交
179
		if (nlsConfig.pseudo) {
180
			require(['vs/nls'], function (nlsPlugin) {
J
Joao Moreno 已提交
181 182 183 184
				nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
			});
		}

185
		// Perf Counters
J
Joao Moreno 已提交
186
		const timers = window.MonacoEnvironment.timers = {
187 188
			isInitialStartup: !!configuration.isInitialStartup,
			hasAccessibilitySupport: !!configuration.accessibilitySupport,
189 190 191 192
			start: configuration.perfStartTime,
			appReady: configuration.perfAppReady,
			windowLoad: configuration.perfWindowLoadTime,
			beforeLoadWorkbenchMain: Date.now()
J
Joao Moreno 已提交
193 194
		};

J
Joao Moreno 已提交
195
		const workbenchMainTimer = startTimer('load:workbench.main');
J
Joao Moreno 已提交
196
		require([
197 198 199
			'vs/workbench/electron-browser/workbench.main',
			'vs/nls!vs/workbench/electron-browser/workbench.main',
			'vs/css!vs/workbench/electron-browser/workbench.main'
J
Joao Moreno 已提交
200
		], function () {
201
			workbenchMainTimer.stop();
202
			timers.afterLoadWorkbenchMain = Date.now();
J
Joao Moreno 已提交
203

204 205 206 207 208 209 210 211 212
			process.lazyEnv.then(function () {
				require('vs/workbench/electron-browser/main')
					.startup(configuration)
					.done(function () {
						unbind(); // since the workbench is running, unbind our developer related listeners and let the workbench handle them
					}, function (error) {
						onError(error, enableDeveloperTools);
					});
			});
J
Joao Moreno 已提交
213 214 215 216
		});
	});
}

217
main();