index.js 5.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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'use strict';

/*global window,document,define*/

const path = require('path');
const electron = require('electron');
const remote = electron.remote;
const ipc = electron.ipcRenderer;
const windowId = remote.getCurrentWindow().id;

function onError(error, enableDeveloperTools) {
	if (enableDeveloperTools) {
		ipc.send('vscode:openDevTools', windowId);
	}

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

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

function assign(destination, source) {
	return Object.keys(source)
J
Joao Moreno 已提交
32
		.reduce(function (r, key) { r[key] = source[key]; return r; }, destination);
J
Joao Moreno 已提交
33 34 35 36 37 38
}

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

	return search.split(/[?&]/)
J
Joao Moreno 已提交
39 40 41 42
		.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 已提交
43 44 45 46 47 48 49 50 51 52 53 54
}

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 已提交
55
	var pathName = path.resolve(_path).replace(/\\/g, '/');
J
Joao Moreno 已提交
56 57 58 59 60 61 62 63 64 65 66
	if (pathName.length > 0 && pathName.charAt(0) !== '/') {
		pathName = '/' + pathName;
	}

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

function registerListeners(enableDeveloperTools) {

	// Devtools & reload support
	if (enableDeveloperTools) {
67
		const extractKey = function (e) {
J
Joao Moreno 已提交
68 69 70 71 72 73 74 75 76 77 78 79
			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

80
		window.addEventListener('keydown', function (e) {
J
Joao Moreno 已提交
81 82 83 84 85 86 87 88 89
			const key = extractKey(e);
			if (key === TOGGLE_DEV_TOOLS_KB) {
				ipc.send('vscode:toggleDevTools', windowId);
			} else if (key === RELOAD_KB) {
				ipc.send('vscode:reloadWindow', windowId);
			}
		});
	}

90
	process.on('uncaughtException', function (error) { onError(error, enableDeveloperTools) });
J
Joao Moreno 已提交
91 92 93 94 95 96 97 98 99 100 101
}

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 已提交
102
	var nlsConfig = { availableLanguages: {} };
J
Joao Moreno 已提交
103 104 105 106 107 108 109 110
	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 已提交
111
	var locale = nlsConfig.availableLanguages['*'] || 'en';
J
Joao Moreno 已提交
112 113 114 115 116 117 118 119
	if (locale === 'zh-tw') {
		locale = 'zh-Hant';
	} else if (locale === 'zh-cn') {
		locale = 'zh-Hans';
	}

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

120
	const enableDeveloperTools = process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath;
J
Joao Moreno 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	registerListeners(enableDeveloperTools);

	// We get the global settings through a remote call from the browser
	// because its value can change dynamically.
	const rawGlobalSettings = remote.getGlobal('globalSettingsValue') || '{"settings":{},"keybindings":[]}';
	const globalSettings = JSON.parse(rawGlobalSettings);

	// disable pinch zoom & apply zoom level early to avoid glitches
	const windowConfiguration = globalSettings.settings && globalSettings.settings.window;
	webFrame.setZoomLevelLimits(1, 1);
	if (windowConfiguration && typeof windowConfiguration.zoomLevel === 'number' && windowConfiguration.zoomLevel !== 0) {
		webFrame.setZoomLevel(windowConfiguration.zoomLevel);
	}

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

J
Joao Moreno 已提交
138 139
	// 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
140 141
	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
B
Benjamin Pasero 已提交
142

J
Joao Moreno 已提交
143 144 145
		require.config({
			baseUrl: rootUrl,
			'vs/nls': nlsConfig,
B
Benjamin Pasero 已提交
146
			recordStats: !!configuration.performance,
J
Joao Moreno 已提交
147 148 149 150
			ignoreDuplicateModules: [
				'vs/workbench/parts/search/common/searchQuery'
			]
		});
B
Benjamin Pasero 已提交
151

J
Joao Moreno 已提交
152
		if (nlsConfig.pseudo) {
153
			require(['vs/nls'], function (nlsPlugin) {
J
Joao Moreno 已提交
154 155 156 157 158
				nlsPlugin.setPseudoTranslation(nlsConfig.pseudo);
			});
		}

		window.MonacoEnvironment = {};
B
Benjamin Pasero 已提交
159

J
Joao Moreno 已提交
160 161 162 163
		const timers = window.MonacoEnvironment.timers = {
			start: new Date()
		};

164
		if (configuration.performance) {
J
Joao Moreno 已提交
165 166
			const vscodeStart = remote.getGlobal('vscodeStart');
			timers.vscodeStart = new Date(vscodeStart);
J
Joao Moreno 已提交
167
			timers.start = new Date(vscodeStart);
J
Joao Moreno 已提交
168 169 170 171 172 173 174 175
		}

		timers.beforeLoad = new Date();

		require([
			'vs/workbench/workbench.main',
			'vs/nls!vs/workbench/workbench.main',
			'vs/css!vs/workbench/workbench.main'
J
Joao Moreno 已提交
176
		], function () {
J
Joao Moreno 已提交
177 178 179 180
			timers.afterLoad = new Date();

			require('vs/workbench/electron-browser/main')
				.startup(configuration, globalSettings)
J
Joao Moreno 已提交
181
				.done(null, function (error) { onError(error, enableDeveloperTools); });
J
Joao Moreno 已提交
182 183 184 185 186
		});
	});
}

main();