Files
azuredatastudio/src/vs/code/electron-main/window.ts
Karl Burtram 81329fa7fa Merge VS Code 1.26.1 (#2394)
* Squash merge commits for 1.26 (#1) (#2323)

* Polish tag search as per feedback (#55269)

* Polish tag search as per feedback

* Updated regex

* Allow users to opt-out of features that send online requests in the background (#55097)

* settings sweep #54690

* Minor css tweaks to enable eoverflow elipsis in more places (#55277)

* fix an issue with titlebarheight when not scaling with zoom

* Settings descriptions update #54690

* fixes #55209

* Settings editor - many padding fixes

* More space above level 2 label

* Fixing Cannot debug npm script using Yarn #55103

* Settings editor - show ellipsis when description overflows

* Settings editor - ... fix measuring around links, relayout

* Setting descriptions

* Settings editor - fix ... for some short lines, fix select container width

* Settings editor - overlay trees so scrollable shadow is full width

* Fix #54133 - missing extension settings after reload

* Settings color token description tweak

* Settings editor - disable overflow indicator temporarily, needs to be faster

* Added command to Run the selected npm script

* fixes #54452

* fixes #54929

* fixes #55248

* prefix command with extension name

* Contribute run selected to the context menu

* node-debug@1.26.6

* Allow terminal rendererType to be swapped out at runtime

Part of #53274
Fixes #55344

* Settings editor - fix not focusing search when restoring editor
setInput must be actually async. Will be fixed naturally when we aren't using winJS promises...

* Settings editor - TOC should only expand the section with a selected item

* Bump node-debug2

* Settings editor - Tree focus outlines

* Settings editor - don't blink the scrollbar when toc selection changes
And hide TOC correctly when the editor is narrow

* Settings editor - header rows should not be selectable

* fixes #54877

* change debug assignee to isi

* Settings sweep (#54690)

* workaround for #55051

* Settings sweep (#54690)

* settings sweep

#54690

* Don't try closing tags when you type > after another >

* Describe what implementation code lens does

Fixes #55370

* fix javadoc formatter setting description

* fixes #55325

* update to officical TS version

* Settings editor - Even more padding, use semibold instead of bold

* Fix #55357 - fix TOC twistie

* fixes #55288

* explorer: refresh on di change file system provider registration

fixes #53256

* Disable push to Linux repo to test standalone publisher

* New env var to notify log level to extensions #54001

* Disable snippets in extension search (when not in suggest dropdown) (#55281)

* Disable snippits in extension search (when not in suggest dropdown)

* Add monaco input contributions

* Fix bug preventing snippetSuggestions from taking effect in sub-editors

* Latest emmet helper to fix #52366

* Fix comment updates for threads within same file

* Allow extensions to log telemetry to log files #54001

* Pull latest css grammar

* files.exclude control - use same style for "add" vs "edit"

* files.exclude control - focus/keyboard behavior

* don't show menubar too early

* files.exclude - better styling

* Place cursor at end of extensions search box on autofill (#55254)

* Place cursor at end of extensions search box on autofill

* Use position instead of selection

* fix linux build issue (empty if block)

* Settings editor - fix extension category prefixes

* Settings editor - add simple ellipsis for first line that overflows, doesn't cover case when first line does not overflow but there is more text, TODO

* File/Text search provider docs

* Fixes #52655

* Include epoch (#55008)

* Fixes #53385

* Fixes #49480

*  VS Code Insiders (Users) not opening Fixes #55353

* Better handling of the case when the extension host fails to start

* Fixes #53966

*  Remove confusing Start from wordPartLeft commands ID

* vscode-xterm@3.6.0-beta12

Fixes #55488

* Initial size is set to infinity!! Fixes #55461

* Polish embeddedEditorBackground

* configuration service misses event

* Fix #55224 - fix duplicate results in multiroot workspace from splitting the diskseach query

* Select all not working in issue reporter on mac, fixes #55424

* Disable fuzzy matching for extensions autosuggest (#55498)

* Fix clipping of extensions search border in some third party themes (#55504)

* fixes #55538

* Fix bug causing an aria alert to not be shown the third time
 (and odd numbers thereafter)

* Settings editor - work around rendering glitch with webkit-line-clamp

* Settings editor - revert earlier '...' changes

* Settings editor - move enumDescription to its own div, because it disturbs -webkit-line-clamp for some reason

* Settings editor - better overflow indicator

* Don't show existing filters in autocomplete (#55495)

* Dont show existing filters in autocomplete

* Simplify

* Settings Editor: Add aria labels for input elements Fixes: #54836 (#55543)

* fixes #55223

* Update vscode-css-languageservice to 3.0.10-next.1

* Fix #55509 - settings navigation

* Fix #55519

* Fix #55520

* FIx #55524

* Fix #55556 - include wordSeparators in all search queries, so findTextInFiles can respect isWordMatch correctly

* oss updates for endgame

* Fix unit tests

* fixes #55522

* Avoid missing manifest error from bubbling up #54757

* Settings format crawl

* Search provider - Fix FileSearchProvider to return array, not progress

* Fix #55598

* Settings editor - fix NPE rendering settings with no description

* dont render inden guides in search box (#55600)

* fixes #55454

* More settings crawl

* Another change for #55598 - maxResults applies to FileSearch and TextSearch but not FileIndex

* Fix FileSearchProvider unit tests for progress change

* fixes #55561

* Settings description update for #54690

* Update setting descriptions for online services

* Minor edits

* fixes #55513

* fixes #55451

* Fix #55612 - fix findTextInFiles cancellation

* fixes #55539

* More setting description tweaks

* Setting to disable online experiments #54354

* fixes #55507

* fixes #55515

* Show online services action only in Insiders for now

* Settings editor - change toc behavior default to 'filter'

* Settings editor - nicer filter count style during search

* Fix #55617 - search viewlet icons

* Settings editor - better styling for element count indicator

* SearchProvider - fix NPE when searching extraFileResources

* Allow extends to work without json suffix

Fixes #16905

* Remove accessability options logic entirely

Follow up on #55451

* use latest version of DAP

* fixes #55490

* fixes #55122

* fixes #52332

* Avoid assumptions about git: URIs (fixes #36236)

* relative path for descriptions

* resourece: get rid of isFile context key

fixes #48275

* Register previous ids for compatibility (#53497)

* more tuning for #48275

* no need to always re-read "files explorer"

fixes #52003

* read out active composites properly

fixes #51967

* Update link colors for hc theme to meet color contrast ratio, fixes #55651

Also updated link color for `textLinkActiveForeground` to be the same as `textLinkForeground` as it wasn't properly updated

* detect 'winpty-agent.exe'; fixes #55672

* node-debug@1.26.7

* reset counter on new label

* Settings editor - fix multiple setting links in one description

* Settings editor - color code blocks in setting descriptions, fix #55532

* Settings editor - hover color in TOC

* Settings editor - fix navigation NPE

* Settings editor - fix text control width

* Settings editor - maybe fix #55684

* Fix bug causing cursor to not move on paste

* fixes #53582

* Use ctrlCmd instead of ctrl for go down from search box

* fixes #55264

* fixes #55456

* filter for spcaes before triggering search (#55611)

* Fix #55698 - don't lose filtered TOC counts when refreshing TOC

* fixes #55421

* fixes #28979

* fixes #55576

* only add check for updates to windows/linux help

* readonly files: append decoration to label

fixes #53022

* debug: do not show toolbar while initialising

fixes #55026

* Opening launch.json should not activate debug extensions

fixes #55029

* fixes #55435

* fixes #55434

* fixes #55439

* trigger menu only on altkey up

* Fix #50555 - fix settings editor memory leak

* Fix #55712 - no need to focus 'a' anymore when restoring control focus after tree render

* fixes #55335

* proper fix for readonly model

fixes #53022

* improve FoldingRangeKind spec (for #55686)

* Use class with static fields (fixes #55494)

* Fixes #53671

* fixes #54630

* [html] should disable ionic suggestions by default. Currently forces deprecated Ionic v1 suggestions in .html files while typing. Fixes #53324

* cleanup deps

* debug issues back to andre

* update electron for smoketest

* Fix #55757 - prevent settings tabs from overflowing

* Fix #53897 - revert setting menu defaults to old editor

* Add enum descriptions to `typescript.preferences.importModuleSpecifier`

* Fix #55767 - leaking style elements from settings editor

* Fix #55521 - prevent flashing when clicking in exclude control

* Update Git modified color for contrast ratio, fixes #53140

* Revert "Merge branch 'master' of github.com:Microsoft/vscode"

This reverts commit bf46b6bfbae0cab99c2863e1244a916181fa9fbc, reversing
changes made to e275a424483dfb4ed33b428c97d5e2c441d6b917.

* Revert "Revert "Merge branch 'master' of github.com:Microsoft/vscode""

This reverts commit 53949d963f39e40757557c6526332354a31d9154.

* don't ask to install an incomplete menu

* Fix NPE in terminal AccessibilityManager

Fixes #55744

* don't display fallback menu unless we've closed the last window

* fixes #55547

* Fix smoke tests for extension search box

* Update OSSREADME.json for Electron 2.0.5

* Update distro

Includes Chromium license changes

* fix #55455

* fix #55865

* fixes #55893

* Fix bug causing workspace recommendations to go away upon ignoring a recommendation (#55805)

* Fix bug causing workspace recommendations to go away upon ignoring a recommendation

* ONly show on @recommended or @recommended:workspace

* Make more consistant

* Fix #55911

* Understand json activity (#55926)

* Understand json file activity

* Refactoring

* adding composer.json

* Distro update for experiments

* use terminal.processId for auto-attach; fixes #55918

* Reject invalid URI with vscode.openFolder (for #55891)

* improve win32 setup system vs user detection

fixes #55840

fixes #55840

delay winreg import

related to #55840

show notification earlier

related to #55840

fix #55840

update inno setup message

related to #55840

* Fix #55593 - this code only operates on local paths, so use fsPath and Uri.file instead

* Bring back the old menu due to electron 2.0 issues (#55913)

* add the old menu back for native menus

* make menu labels match

* `vscode.openFolder`: treat missing URI schema gracefully (for #55891)

* delay EH reattach; fixes #55955

* Mark all json files under appSettingsHome as settings

* Use localized strings for telemetry opt-out

* Exception when saving file editor opened from remote file provider (fixes #55051)

* Remove terminal menu from stable

Fixes 56003

* VSCode Insiders crashes on open with TypeError: Cannot read property 'lastIndexOf' of undefined. Fixes #54933

* improve fix for #55891

* fix #55916

* Improve #55891

* increase EH debugging restart delay; fixes #55955

* Revert "Don't include non-resource entries in history quick pick"

This reverts commit 37209a838e9f7e9abe6dc53ed73cdf1e03b72060.

* Diff editor: horizontal scrollbar height is smaller (fixes #56062)

* improve openFolder uri fix (correctly treat backslashes)

* fixes #56116
repair ipc for native menubar keybindings

* Fix #56240 - Open the JSON settings editor instead of the UI editor

* Fix #55536

* uriDisplay: if no formatter is registered fall back to getPathlabel

fixes #56104

* VSCode hangs when opening python file. Fixes #56377

* VS Code Hangs When Opening Specific PowerShell File. Fixes #56430

* Fix #56433 - search extraFileResources even when no folders open

* Workaround #55649

* Fix in master #56371

* Fix tests #56371

* Fix in master #56317

* increase version to 1.26.1

* Fixes #56387: Handle SIGPIPE in extension host

* fixes #56185

* Fix merge issues (part 1)

* Fix build breaks (part 1)

* Build breaks (part 2)

* Build breaks (part 3)

* More build breaks (part 4)

* Fix build breaks (part 5)

* WIP

* Fix menus

* Render query result and message panels (#2363)

* Put back query editor hot exit changes

* Fix grid changes that broke profiler (#2365)

* Update APIs for saving query editor state

* Fix restore view state for profiler and edit data

* Updating custom default themes to support 4.5:1 contrast ratio

* Test updates

* Fix Extension Manager and Windows Setup

* Update license headers

* Add appveyor and travis files back

* Fix hidden modal dropdown issue
2018-09-04 14:55:00 -07:00

1024 lines
32 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as path from 'path';
import * as objects from 'vs/base/common/objects';
import * as nls from 'vs/nls';
import URI from 'vs/base/common/uri';
import { IStateService } from 'vs/platform/state/common/state';
import { shell, screen, BrowserWindow, systemPreferences, app, TouchBar, nativeImage } from 'electron';
import { TPromise, TValueCallback } from 'vs/base/common/winjs.base';
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
import { ILogService } from 'vs/platform/log/common/log';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { parseArgs } from 'vs/platform/environment/node/argv';
import product from 'vs/platform/node/product';
import { IWindowSettings, MenuBarVisibility, IWindowConfiguration, ReadyState, IRunActionInWindowRequest } from 'vs/platform/windows/common/windows';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
import { ICodeWindow, IWindowState, WindowMode } from 'vs/platform/windows/electron-main/windows';
import { IWorkspaceIdentifier, IWorkspacesMainService } from 'vs/platform/workspaces/common/workspaces';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { ISerializableCommandAction } from 'vs/platform/actions/common/actions';
import { mark, exportEntries } from 'vs/base/common/performance';
import { resolveMarketplaceHeaders } from 'vs/platform/extensionManagement/node/extensionGalleryService';
export interface IWindowCreationOptions {
state: IWindowState;
extensionDevelopmentPath?: string;
isExtensionTestHost?: boolean;
}
export const defaultWindowState = function (mode = WindowMode.Normal): IWindowState {
return {
width: 1024,
height: 768,
mode
};
};
interface IWorkbenchEditorConfiguration {
workbench: {
editor: {
swipeToNavigate: boolean
}
};
}
interface ITouchBarSegment extends Electron.SegmentedControlSegment {
id: string;
}
export class CodeWindow implements ICodeWindow {
static readonly themeStorageKey = 'theme';
static readonly themeBackgroundStorageKey = 'themeBackground';
private static readonly DEFAULT_BG_LIGHT = '#FFFFFF';
private static readonly DEFAULT_BG_DARK = '#1E1E1E';
private static readonly DEFAULT_BG_HC_BLACK = '#000000';
private static readonly MIN_WIDTH = 200;
private static readonly MIN_HEIGHT = 120;
private hiddenTitleBarStyle: boolean;
private showTimeoutHandle: any;
private _id: number;
private _win: Electron.BrowserWindow;
private _lastFocusTime: number;
private _readyState: ReadyState;
private windowState: IWindowState;
private currentMenuBarVisibility: MenuBarVisibility;
private toDispose: IDisposable[];
private representedFilename: string;
private whenReadyCallbacks: TValueCallback<ICodeWindow>[];
private currentConfig: IWindowConfiguration;
private pendingLoadConfig: IWindowConfiguration;
private marketplaceHeadersPromise: TPromise<object>;
private touchBarGroups: Electron.TouchBarSegmentedControl[];
constructor(
config: IWindowCreationOptions,
@ILogService private logService: ILogService,
@IEnvironmentService private environmentService: IEnvironmentService,
@IConfigurationService private configurationService: IConfigurationService,
@IStateService private stateService: IStateService,
@IWorkspacesMainService private workspacesMainService: IWorkspacesMainService,
@IBackupMainService private backupMainService: IBackupMainService
) {
this.touchBarGroups = [];
this._lastFocusTime = -1;
this._readyState = ReadyState.NONE;
this.whenReadyCallbacks = [];
this.toDispose = [];
// create browser window
this.createBrowserWindow(config);
// respect configured menu bar visibility
this.onConfigurationUpdated();
// macOS: touch bar support
this.createTouchBar();
// Request handling
this.handleMarketplaceRequests();
// Eventing
this.registerListeners();
}
private createBrowserWindow(config: IWindowCreationOptions): void {
// Load window state
this.windowState = this.restoreWindowState(config.state);
// in case we are maximized or fullscreen, only show later after the call to maximize/fullscreen (see below)
const isFullscreenOrMaximized = (this.windowState.mode === WindowMode.Maximized || this.windowState.mode === WindowMode.Fullscreen);
let backgroundColor = this.getBackgroundColor();
if (isMacintosh && backgroundColor.toUpperCase() === CodeWindow.DEFAULT_BG_DARK) {
backgroundColor = '#171717'; // https://github.com/electron/electron/issues/5150
}
const options: Electron.BrowserWindowConstructorOptions = {
width: this.windowState.width,
height: this.windowState.height,
x: this.windowState.x,
y: this.windowState.y,
backgroundColor,
minWidth: CodeWindow.MIN_WIDTH,
minHeight: CodeWindow.MIN_HEIGHT,
show: !isFullscreenOrMaximized,
title: product.nameLong,
webPreferences: {
'backgroundThrottling': false, // by default if Code is in the background, intervals and timeouts get throttled,
disableBlinkFeatures: 'Auxclick' // disable auxclick events (see https://developers.google.com/web/updates/2016/10/auxclick)
}
};
if (isLinux) {
options.icon = path.join(this.environmentService.appRoot, 'resources/linux/code.png'); // Windows and Mac are better off using the embedded icon(s)
}
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
if (isMacintosh) {
options.acceptFirstMouse = true; // enabled by default
if (windowConfig && windowConfig.clickThroughInactive === false) {
options.acceptFirstMouse = false;
}
}
let useNativeTabs = false;
if (isMacintosh && windowConfig && windowConfig.nativeTabs === true) {
options.tabbingIdentifier = product.nameShort; // this opts in to sierra tabs
useNativeTabs = true;
}
let useCustomTitleStyle = false;
// {{SQL CARBON EDIT}}
// if (isMacintosh) {
// turn-off custom menus to avoid bug calculating size of SQL editor
//
// if (isMacintosh && (!windowConfig || !windowConfig.titleBarStyle || windowConfig.titleBarStyle === 'custom')) {
// const isDev = !this.environmentService.isBuilt || !!config.extensionDevelopmentPath;
// if (!isDev) {
// useCustomTitleStyle = true; // not enabled when developing due to https://github.com/electron/electron/issues/3647
// }
// }
if (useNativeTabs) {
useCustomTitleStyle = false; // native tabs on sierra do not work with custom title style
}
if (useCustomTitleStyle) {
options.titleBarStyle = 'hidden';
this.hiddenTitleBarStyle = true;
if (!isMacintosh) {
options.frame = false;
}
}
// Create the browser window.
this._win = new BrowserWindow(options);
this._id = this._win.id;
if (useCustomTitleStyle) {
this._win.setSheetOffset(22); // offset dialogs by the height of the custom title bar if we have any
}
if (isFullscreenOrMaximized) {
this._win.maximize();
if (this.windowState.mode === WindowMode.Fullscreen) {
this._win.setFullScreen(true);
}
if (!this._win.isVisible()) {
this._win.show(); // to reduce flicker from the default window size to maximize, we only show after maximize
}
}
this._lastFocusTime = Date.now(); // since we show directly, we need to set the last focus time too
}
hasHiddenTitleBarStyle(): boolean {
return this.hiddenTitleBarStyle;
}
get isExtensionDevelopmentHost(): boolean {
return !!this.config.extensionDevelopmentPath;
}
get isExtensionTestHost(): boolean {
return !!this.config.extensionTestsPath;
}
get extensionDevelopmentPath(): string {
return this.config.extensionDevelopmentPath;
}
get config(): IWindowConfiguration {
return this.currentConfig;
}
get id(): number {
return this._id;
}
get win(): Electron.BrowserWindow {
return this._win;
}
setRepresentedFilename(filename: string): void {
if (isMacintosh) {
this.win.setRepresentedFilename(filename);
} else {
this.representedFilename = filename;
}
}
getRepresentedFilename(): string {
if (isMacintosh) {
return this.win.getRepresentedFilename();
}
return this.representedFilename;
}
focus(): void {
if (!this._win) {
return;
}
if (this._win.isMinimized()) {
this._win.restore();
}
this._win.focus();
}
get lastFocusTime(): number {
return this._lastFocusTime;
}
get backupPath(): string {
return this.currentConfig ? this.currentConfig.backupPath : void 0;
}
get openedWorkspace(): IWorkspaceIdentifier {
return this.currentConfig ? this.currentConfig.workspace : void 0;
}
get openedFolderUri(): URI {
return this.currentConfig ? this.currentConfig.folderUri : void 0;
}
setReady(): void {
this._readyState = ReadyState.READY;
// inform all waiting promises that we are ready now
while (this.whenReadyCallbacks.length) {
this.whenReadyCallbacks.pop()(this);
}
}
ready(): TPromise<ICodeWindow> {
return new TPromise<ICodeWindow>((c) => {
if (this._readyState === ReadyState.READY) {
return c(this);
}
// otherwise keep and call later when we are ready
this.whenReadyCallbacks.push(c);
});
}
get readyState(): ReadyState {
return this._readyState;
}
private handleMarketplaceRequests(): void {
// Resolve marketplace headers
this.marketplaceHeadersPromise = resolveMarketplaceHeaders(this.environmentService);
// Inject headers when requests are incoming
const urls = ['https://marketplace.visualstudio.com/*', 'https://*.vsassets.io/*'];
this._win.webContents.session.webRequest.onBeforeSendHeaders({ urls }, (details: any, cb: any) => {
this.marketplaceHeadersPromise.done(headers => {
cb({ cancel: false, requestHeaders: objects.assign(details.requestHeaders, headers) });
});
});
}
private registerListeners(): void {
// Prevent loading of svgs
this._win.webContents.session.webRequest.onBeforeRequest(null, (details, callback) => {
if (details.url.indexOf('.svg') > 0) {
const uri = URI.parse(details.url);
if (uri && !uri.scheme.match(/file/i) && (uri.path as any).endsWith('.svg')) {
return callback({ cancel: true });
}
}
return callback({});
});
this._win.webContents.session.webRequest.onHeadersReceived(null, (details: any, callback: any) => {
const contentType: string[] = (details.responseHeaders['content-type'] || details.responseHeaders['Content-Type']) as any;
if (contentType && Array.isArray(contentType) && contentType.some(x => x.toLowerCase().indexOf('image/svg') >= 0)) {
return callback({ cancel: true });
}
return callback({ cancel: false, responseHeaders: details.responseHeaders });
});
// Remember that we loaded
this._win.webContents.on('did-finish-load', () => {
this._readyState = ReadyState.LOADING;
// Associate properties from the load request if provided
if (this.pendingLoadConfig) {
this.currentConfig = this.pendingLoadConfig;
this.pendingLoadConfig = null;
}
// To prevent flashing, we set the window visible after the page has finished to load but before Code is loaded
if (this._win && !this._win.isVisible()) {
if (this.windowState.mode === WindowMode.Maximized) {
this._win.maximize();
}
if (!this._win.isVisible()) { // maximize also makes visible
this._win.show();
}
}
});
// App commands support
this.registerNavigationListenerOn('app-command', 'browser-backward', 'browser-forward', false);
// Handle code that wants to open links
this._win.webContents.on('new-window', (event: Event, url: string) => {
event.preventDefault();
shell.openExternal(url);
});
// Window Focus
this._win.on('focus', () => {
this._lastFocusTime = Date.now();
});
// Window (Un)Maximize
this._win.on('maximize', (e) => {
if (this.currentConfig) {
this.currentConfig.maximized = true;
}
app.emit('browser-window-maximize', e, this._win);
});
this._win.on('unmaximize', (e) => {
if (this.currentConfig) {
this.currentConfig.maximized = false;
}
app.emit('browser-window-unmaximize', e, this._win);
});
// Window Fullscreen
this._win.on('enter-full-screen', () => {
this.sendWhenReady('vscode:enterFullScreen');
});
this._win.on('leave-full-screen', () => {
this.sendWhenReady('vscode:leaveFullScreen');
});
// Window Failed to load
this._win.webContents.on('did-fail-load', (event: Electron.Event, errorCode: number, errorDescription: string, validatedURL: string, isMainFrame: boolean) => {
this.logService.warn('[electron event]: fail to load, ', errorDescription);
});
// Prevent any kind of navigation triggered by the user!
// But do not touch this in dev version because it will prevent "Reload" from dev tools
if (this.environmentService.isBuilt) {
this._win.webContents.on('will-navigate', (event: Event) => {
if (event) {
event.preventDefault();
}
});
}
// Handle configuration changes
this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationUpdated()));
// Handle Workspace events
this.toDispose.push(this.workspacesMainService.onUntitledWorkspaceDeleted(e => this.onUntitledWorkspaceDeleted(e)));
// TODO@Ben workaround for https://github.com/Microsoft/vscode/issues/13612
// It looks like smooth scrolling disappears as soon as the window is minimized
// and maximized again. Touching some window properties "fixes" it, like toggling
// the visibility of the menu.
if (isWindows) {
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
if (windowConfig && windowConfig.smoothScrollingWorkaround === true) {
let minimized = false;
const restoreSmoothScrolling = () => {
if (minimized) {
const visibility = this.getMenuBarVisibility();
const temporaryVisibility: MenuBarVisibility = (visibility === 'hidden' || visibility === 'toggle') ? 'default' : 'hidden';
setTimeout(() => {
this.doSetMenuBarVisibility(temporaryVisibility);
this.doSetMenuBarVisibility(visibility);
}, 0);
}
minimized = false;
};
this._win.on('minimize', () => minimized = true);
this._win.on('restore', () => restoreSmoothScrolling());
this._win.on('maximize', () => restoreSmoothScrolling());
}
}
}
private onUntitledWorkspaceDeleted(workspace: IWorkspaceIdentifier): void {
// Make sure to update our workspace config if we detect that it
// was deleted
if (this.openedWorkspace && this.openedWorkspace.id === workspace.id) {
this.currentConfig.workspace = void 0;
}
}
private onConfigurationUpdated(): void {
const newMenuBarVisibility = this.getMenuBarVisibility();
if (newMenuBarVisibility !== this.currentMenuBarVisibility) {
this.currentMenuBarVisibility = newMenuBarVisibility;
this.setMenuBarVisibility(newMenuBarVisibility);
}
// Swipe command support (macOS)
if (isMacintosh) {
const config = this.configurationService.getValue<IWorkbenchEditorConfiguration>();
if (config && config.workbench && config.workbench.editor && config.workbench.editor.swipeToNavigate) {
this.registerNavigationListenerOn('swipe', 'left', 'right', true);
} else {
this._win.removeAllListeners('swipe');
}
}
}
private registerNavigationListenerOn(command: 'swipe' | 'app-command', back: 'left' | 'browser-backward', forward: 'right' | 'browser-forward', acrossEditors: boolean) {
this._win.on(command as 'swipe' /* | 'app-command' */, (e: Electron.Event, cmd: string) => {
if (this.readyState !== ReadyState.READY) {
return; // window must be ready
}
if (cmd === back) {
this.send('vscode:runAction', { id: acrossEditors ? 'workbench.action.openPreviousRecentlyUsedEditor' : 'workbench.action.navigateBack', from: 'mouse' } as IRunActionInWindowRequest);
} else if (cmd === forward) {
this.send('vscode:runAction', { id: acrossEditors ? 'workbench.action.openNextRecentlyUsedEditor' : 'workbench.action.navigateForward', from: 'mouse' } as IRunActionInWindowRequest);
}
});
}
load(config: IWindowConfiguration, isReload?: boolean, disableExtensions?: boolean): void {
// If this is the first time the window is loaded, we associate the paths
// directly with the window because we assume the loading will just work
if (this.readyState === ReadyState.NONE) {
this.currentConfig = config;
}
// Otherwise, the window is currently showing a folder and if there is an
// unload handler preventing the load, we cannot just associate the paths
// because the loading might be vetoed. Instead we associate it later when
// the window load event has fired.
else {
this.pendingLoadConfig = config;
this._readyState = ReadyState.NAVIGATING;
}
// Add disable-extensions to the config, but do not preserve it on currentConfig or
// pendingLoadConfig so that it is applied only on this load
const configuration = objects.assign({}, config);
if (disableExtensions !== undefined) {
configuration['disable-extensions'] = disableExtensions;
}
// Clear Document Edited if needed
if (isMacintosh && this._win.isDocumentEdited()) {
if (!isReload || !this.backupMainService.isHotExitEnabled()) {
this._win.setDocumentEdited(false);
}
}
// Clear Title and Filename if needed
if (!isReload) {
if (this.getRepresentedFilename()) {
this.setRepresentedFilename('');
}
this._win.setTitle(product.nameLong);
}
// Load URL
mark('main:loadWindow');
this._win.loadURL(this.getUrl(configuration));
// Make window visible if it did not open in N seconds because this indicates an error
// Only do this when running out of sources and not when running tests
if (!this.environmentService.isBuilt && !this.environmentService.extensionTestsPath) {
this.showTimeoutHandle = setTimeout(() => {
if (this._win && !this._win.isVisible() && !this._win.isMinimized()) {
this._win.show();
this._win.focus();
this._win.webContents.openDevTools();
}
}, 10000);
}
}
reload(configuration?: IWindowConfiguration, cli?: ParsedArgs): void {
// If config is not provided, copy our current one
if (!configuration) {
configuration = objects.mixin({}, this.currentConfig);
}
// Delete some properties we do not want during reload
delete configuration.filesToOpen;
delete configuration.filesToCreate;
delete configuration.filesToDiff;
delete configuration.filesToWait;
// Some configuration things get inherited if the window is being reloaded and we are
// in extension development mode. These options are all development related.
if (this.isExtensionDevelopmentHost && cli) {
configuration.verbose = cli.verbose;
configuration.debugPluginHost = cli.debugPluginHost;
configuration.debugBrkPluginHost = cli.debugBrkPluginHost;
configuration.debugId = cli.debugId;
configuration['extensions-dir'] = cli['extensions-dir'];
}
configuration.isInitialStartup = false; // since this is a reload
// Load config
const disableExtensions = cli ? cli['disable-extensions'] : undefined;
this.load(configuration, true, disableExtensions);
}
private getUrl(windowConfiguration: IWindowConfiguration): string {
// Set window ID
windowConfiguration.windowId = this._win.id;
windowConfiguration.logLevel = this.logService.getLevel();
// Set zoomlevel
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
const zoomLevel = windowConfig && windowConfig.zoomLevel;
if (typeof zoomLevel === 'number') {
windowConfiguration.zoomLevel = zoomLevel;
}
// Set fullscreen state
windowConfiguration.fullscreen = this._win.isFullScreen();
// Set Accessibility Config
let autoDetectHighContrast = true;
if (windowConfig && windowConfig.autoDetectHighContrast === false) {
autoDetectHighContrast = false;
}
windowConfiguration.highContrast = isWindows && autoDetectHighContrast && systemPreferences.isInvertedColorScheme();
windowConfiguration.accessibilitySupport = app.isAccessibilitySupportEnabled();
// Theme
windowConfiguration.baseTheme = this.getBaseTheme();
windowConfiguration.backgroundColor = this.getBackgroundColor();
// Title style related
windowConfiguration.maximized = this._win.isMaximized();
windowConfiguration.frameless = this.hasHiddenTitleBarStyle() && !isMacintosh;
// Perf Counters
windowConfiguration.perfEntries = exportEntries();
windowConfiguration.perfStartTime = (<any>global).perfStartTime;
windowConfiguration.perfWindowLoadTime = Date.now();
// Config (combination of process.argv and window configuration)
const environment = parseArgs(process.argv);
const config = objects.assign(environment, windowConfiguration);
for (let key in config) {
if (config[key] === void 0 || config[key] === null || config[key] === '') {
delete config[key]; // only send over properties that have a true value
}
}
return `${require.toUrl('vs/workbench/electron-browser/bootstrap/index.html')}?config=${encodeURIComponent(JSON.stringify(config))}`;
}
private getBaseTheme(): string {
if (isWindows && systemPreferences.isInvertedColorScheme()) {
return 'hc-black';
}
const theme = this.stateService.getItem<string>(CodeWindow.themeStorageKey, 'vs-dark');
return theme.split(' ')[0];
}
private getBackgroundColor(): string {
if (isWindows && systemPreferences.isInvertedColorScheme()) {
return CodeWindow.DEFAULT_BG_HC_BLACK;
}
const background = this.stateService.getItem<string>(CodeWindow.themeBackgroundStorageKey, null);
if (!background) {
const baseTheme = this.getBaseTheme();
return baseTheme === 'hc-black' ? CodeWindow.DEFAULT_BG_HC_BLACK : (baseTheme === 'vs' ? CodeWindow.DEFAULT_BG_LIGHT : CodeWindow.DEFAULT_BG_DARK);
}
return background;
}
serializeWindowState(): IWindowState {
if (!this._win) {
return defaultWindowState();
}
// fullscreen gets special treatment
if (this._win.isFullScreen()) {
const display = screen.getDisplayMatching(this.getBounds());
return {
mode: WindowMode.Fullscreen,
display: display ? display.id : void 0,
// still carry over window dimensions from previous sessions!
width: this.windowState.width,
height: this.windowState.height,
x: this.windowState.x,
y: this.windowState.y
};
}
const state: IWindowState = Object.create(null);
let mode: WindowMode;
// get window mode
if (!isMacintosh && this._win.isMaximized()) {
mode = WindowMode.Maximized;
} else {
mode = WindowMode.Normal;
}
// we don't want to save minimized state, only maximized or normal
if (mode === WindowMode.Maximized) {
state.mode = WindowMode.Maximized;
} else {
state.mode = WindowMode.Normal;
}
// only consider non-minimized window states
if (mode === WindowMode.Normal || mode === WindowMode.Maximized) {
const bounds = this.getBounds();
state.x = bounds.x;
state.y = bounds.y;
state.width = bounds.width;
state.height = bounds.height;
}
return state;
}
private restoreWindowState(state?: IWindowState): IWindowState {
if (state) {
try {
state = this.validateWindowState(state);
} catch (err) {
this.logService.warn(`Unexpected error validating window state: ${err}\n${err.stack}`); // somehow display API can be picky about the state to validate
}
}
if (!state) {
state = defaultWindowState();
}
return state;
}
private validateWindowState(state: IWindowState): IWindowState {
if (!state) {
return null;
}
if ([state.x, state.y, state.width, state.height].some(n => typeof n !== 'number')) {
return null;
}
if (state.width <= 0 || state.height <= 0) {
return null;
}
const displays = screen.getAllDisplays();
// Single Monitor: be strict about x/y positioning
if (displays.length === 1) {
const displayBounds = displays[0].bounds;
// Careful with maximized: in that mode x/y can well be negative!
if (state.mode !== WindowMode.Maximized && displayBounds.width > 0 && displayBounds.height > 0 /* Linux X11 sessions sometimes report wrong display bounds */) {
if (state.x < displayBounds.x) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the left
}
if (state.y < displayBounds.y) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the top
}
if (state.x > (displayBounds.x + displayBounds.width)) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the right
}
if (state.y > (displayBounds.y + displayBounds.height)) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the bottom
}
if (state.width > displayBounds.width) {
state.width = displayBounds.width; // prevent window from exceeding display bounds width
}
if (state.height > displayBounds.height) {
state.height = displayBounds.height; // prevent window from exceeding display bounds height
}
}
if (state.mode === WindowMode.Maximized) {
return defaultWindowState(WindowMode.Maximized); // when maximized, make sure we have good values when the user restores the window
}
return state;
}
// Multi Montior (fullscreen): try to find the previously used display
if (state.display && state.mode === WindowMode.Fullscreen) {
const display = displays.filter(d => d.id === state.display)[0];
if (display && display.bounds && typeof display.bounds.x === 'number' && typeof display.bounds.y === 'number') {
const defaults = defaultWindowState(WindowMode.Fullscreen); // make sure we have good values when the user restores the window
defaults.x = display.bounds.x; // carefull to use displays x/y position so that the window ends up on the correct monitor
defaults.y = display.bounds.y;
return defaults;
}
}
// Multi Monitor (non-fullscreen): be less strict because metrics can be crazy
const bounds = { x: state.x, y: state.y, width: state.width, height: state.height };
const display = screen.getDisplayMatching(bounds);
if (
display && // we have a display matching the desired bounds
bounds.x < display.bounds.x + display.bounds.width && // prevent window from falling out of the screen to the right
bounds.y < display.bounds.y + display.bounds.height && // prevent window from falling out of the screen to the bottom
bounds.x + bounds.width > display.bounds.x && // prevent window from falling out of the screen to the left
bounds.y + bounds.height > display.bounds.y // prevent window from falling out of the scree nto the top
) {
if (state.mode === WindowMode.Maximized) {
const defaults = defaultWindowState(WindowMode.Maximized); // when maximized, make sure we have good values when the user restores the window
defaults.x = state.x; // carefull to keep x/y position so that the window ends up on the correct monitor
defaults.y = state.y;
return defaults;
}
return state;
}
return null;
}
getBounds(): Electron.Rectangle {
const pos = this._win.getPosition();
const dimension = this._win.getSize();
return { x: pos[0], y: pos[1], width: dimension[0], height: dimension[1] };
}
toggleFullScreen(): void {
const willBeFullScreen = !this._win.isFullScreen();
// set fullscreen flag on window
this._win.setFullScreen(willBeFullScreen);
// respect configured menu bar visibility or default to toggle if not set
this.setMenuBarVisibility(this.currentMenuBarVisibility, false);
}
private getMenuBarVisibility(): MenuBarVisibility {
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
if (!windowConfig || !windowConfig.menuBarVisibility) {
return 'default';
}
let menuBarVisibility = windowConfig.menuBarVisibility;
if (['visible', 'toggle', 'hidden'].indexOf(menuBarVisibility) < 0) {
menuBarVisibility = 'default';
}
return menuBarVisibility;
}
private setMenuBarVisibility(visibility: MenuBarVisibility, notify: boolean = true): void {
if (isMacintosh) {
return; // ignore for macOS platform
}
if (visibility === 'toggle') {
if (notify) {
this.send('vscode:showInfoMessage', nls.localize('hiddenMenuBar', "You can still access the menu bar by pressing the Alt-key."));
}
}
if (visibility === 'hidden') {
// for some weird reason that I have no explanation for, the menu bar is not hiding when calling
// this without timeout (see https://github.com/Microsoft/vscode/issues/19777). there seems to be
// a timing issue with us opening the first window and the menu bar getting created. somehow the
// fact that we want to hide the menu without being able to bring it back via Alt key makes Electron
// still show the menu. Unable to reproduce from a simple Hello World application though...
setTimeout(() => {
this.doSetMenuBarVisibility(visibility);
});
} else {
this.doSetMenuBarVisibility(visibility);
}
}
private doSetMenuBarVisibility(visibility: MenuBarVisibility): void {
const isFullscreen = this._win.isFullScreen();
switch (visibility) {
case ('default'):
this._win.setMenuBarVisibility(!isFullscreen);
this._win.setAutoHideMenuBar(isFullscreen);
break;
case ('visible'):
this._win.setMenuBarVisibility(true);
this._win.setAutoHideMenuBar(false);
break;
case ('toggle'):
this._win.setMenuBarVisibility(false);
this._win.setAutoHideMenuBar(true);
break;
case ('hidden'):
this._win.setMenuBarVisibility(false);
this._win.setAutoHideMenuBar(false);
break;
}
}
onWindowTitleDoubleClick(): void {
// Respect system settings on mac with regards to title click on windows title
if (isMacintosh) {
const action = systemPreferences.getUserDefault('AppleActionOnDoubleClick', 'string');
switch (action) {
case 'Minimize':
this.win.minimize();
break;
case 'None':
break;
case 'Maximize':
default:
this.win.maximize();
}
}
// Linux/Windows: just toggle maximize/minimized state
else {
if (this.win.isMaximized()) {
this.win.unmaximize();
} else {
this.win.maximize();
}
}
}
close(): void {
if (this._win) {
this._win.close();
}
}
sendWhenReady(channel: string, ...args: any[]): void {
this.ready().then(() => {
this.send(channel, ...args);
});
}
send(channel: string, ...args: any[]): void {
if (this._win) {
this._win.webContents.send(channel, ...args);
}
}
updateTouchBar(groups: ISerializableCommandAction[][]): void {
if (!isMacintosh) {
return; // only supported on macOS
}
// Update segments for all groups. Setting the segments property
// of the group directly prevents ugly flickering from happening
this.touchBarGroups.forEach((touchBarGroup, index) => {
const commands = groups[index];
touchBarGroup.segments = this.createTouchBarGroupSegments(commands);
});
}
private createTouchBar(): void {
if (!isMacintosh) {
return; // only supported on macOS
}
// To avoid flickering, we try to reuse the touch bar group
// as much as possible by creating a large number of groups
// for reusing later.
for (let i = 0; i < 10; i++) {
const groupTouchBar = this.createTouchBarGroup();
this.touchBarGroups.push(groupTouchBar);
}
this._win.setTouchBar(new TouchBar({ items: this.touchBarGroups }));
}
private createTouchBarGroup(items: ISerializableCommandAction[] = []): Electron.TouchBarSegmentedControl {
// Group Segments
const segments = this.createTouchBarGroupSegments(items);
// Group Control
const control = new TouchBar.TouchBarSegmentedControl({
segments,
mode: 'buttons',
segmentStyle: 'automatic',
change: (selectedIndex) => {
this.sendWhenReady('vscode:runAction', { id: (control.segments[selectedIndex] as ITouchBarSegment).id, from: 'touchbar' });
}
});
return control;
}
private createTouchBarGroupSegments(items: ISerializableCommandAction[] = []): ITouchBarSegment[] {
const segments: ITouchBarSegment[] = items.map(item => {
let icon: Electron.NativeImage;
if (item.iconLocation && item.iconLocation.dark.scheme === 'file') {
icon = nativeImage.createFromPath(URI.revive(item.iconLocation.dark).fsPath);
if (icon.isEmpty()) {
icon = void 0;
}
}
return {
id: item.id,
label: !icon ? item.title as string : void 0,
icon
};
});
return segments;
}
dispose(): void {
if (this.showTimeoutHandle) {
clearTimeout(this.showTimeoutHandle);
}
this.toDispose = dispose(this.toDispose);
this._win = null; // Important to dereference the window object to allow for GC
}
}