-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathwindows.ts
373 lines (301 loc) · 14.8 KB
/
windows.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import electron from 'electron';
import { Color } from 'vs/base/common/color';
import { Event } from 'vs/base/common/event';
import { join } from 'vs/base/common/path';
import { IProcessEnvironment, isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { IAuxiliaryWindow } from 'vs/platform/auxiliaryWindow/electron-main/auxiliaryWindow';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { NativeParsedArgs } from 'vs/platform/environment/common/argv';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { ServicesAccessor, createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { ILogService } from 'vs/platform/log/common/log';
import { IProductService } from 'vs/platform/product/common/productService';
import { IThemeMainService } from 'vs/platform/theme/electron-main/themeMainService';
import { IOpenEmptyWindowOptions, IWindowOpenable, IWindowSettings, TitlebarStyle, WindowMinimumSize, hasNativeTitlebar, useNativeFullScreen, useWindowControlsOverlay, zoomLevelToZoomFactor } from 'vs/platform/window/common/window';
import { ICodeWindow, IWindowState, WindowMode, defaultWindowState } from 'vs/platform/window/electron-main/window';
export const IWindowsMainService = createDecorator<IWindowsMainService>('windowsMainService');
export interface IWindowsMainService {
readonly _serviceBrand: undefined;
readonly onDidChangeWindowsCount: Event<IWindowsCountChangedEvent>;
readonly onDidOpenWindow: Event<ICodeWindow>;
readonly onDidSignalReadyWindow: Event<ICodeWindow>;
readonly onDidMaximizeWindow: Event<ICodeWindow>;
readonly onDidUnmaximizeWindow: Event<ICodeWindow>;
readonly onDidChangeFullScreen: Event<{ window: ICodeWindow; fullscreen: boolean }>;
readonly onDidTriggerSystemContextMenu: Event<{ readonly window: ICodeWindow; readonly x: number; readonly y: number }>;
readonly onDidDestroyWindow: Event<ICodeWindow>;
open(openConfig: IOpenConfiguration): Promise<ICodeWindow[]>;
openEmptyWindow(openConfig: IOpenEmptyConfiguration, options?: IOpenEmptyWindowOptions): Promise<ICodeWindow[]>;
openExtensionDevelopmentHostWindow(extensionDevelopmentPath: string[], openConfig: IOpenConfiguration): Promise<ICodeWindow[]>;
openExistingWindow(window: ICodeWindow, openConfig: IOpenConfiguration): void;
sendToFocused(channel: string, ...args: any[]): void;
sendToOpeningWindow(channel: string, ...args: any[]): void;
sendToAll(channel: string, payload?: any, windowIdsToIgnore?: number[]): void;
getWindows(): ICodeWindow[];
getWindowCount(): number;
getFocusedWindow(): ICodeWindow | undefined;
getLastActiveWindow(): ICodeWindow | undefined;
getWindowById(windowId: number): ICodeWindow | undefined;
getWindowByWebContents(webContents: electron.WebContents): ICodeWindow | undefined;
}
export interface IWindowsCountChangedEvent {
readonly oldCount: number;
readonly newCount: number;
}
export const enum OpenContext {
// opening when running from the command line
CLI,
// macOS only: opening from the dock (also when opening files to a running instance from desktop)
DOCK,
// opening from the main application window
MENU,
// opening from a file or folder dialog
DIALOG,
// opening from the OS's UI
DESKTOP,
// opening through the API
API,
// opening from a protocol link
LINK
}
export interface IBaseOpenConfiguration {
readonly context: OpenContext;
readonly contextWindowId?: number;
}
export interface IOpenConfiguration extends IBaseOpenConfiguration {
readonly cli: NativeParsedArgs;
readonly userEnv?: IProcessEnvironment;
readonly urisToOpen?: IWindowOpenable[];
readonly waitMarkerFileURI?: URI;
readonly preferNewWindow?: boolean;
readonly forceNewWindow?: boolean;
readonly forceNewTabbedWindow?: boolean;
readonly forceReuseWindow?: boolean;
readonly forceEmpty?: boolean;
readonly diffMode?: boolean;
readonly mergeMode?: boolean;
addMode?: boolean;
readonly gotoLineMode?: boolean;
readonly initialStartup?: boolean;
readonly noRecentEntry?: boolean;
/**
* The remote authority to use when windows are opened with either
* - no workspace (empty window)
* - a workspace that is neither `file://` nor `vscode-remote://`
*/
readonly remoteAuthority?: string;
readonly forceProfile?: string;
readonly forceTempProfile?: boolean;
}
export interface IOpenEmptyConfiguration extends IBaseOpenConfiguration { }
export interface IDefaultBrowserWindowOptionsOverrides {
forceNativeTitlebar?: boolean;
disableFullscreen?: boolean;
}
export function defaultBrowserWindowOptions(accessor: ServicesAccessor, windowState: IWindowState, overrides?: IDefaultBrowserWindowOptionsOverrides, webPreferences?: electron.WebPreferences): electron.BrowserWindowConstructorOptions & { experimentalDarkMode: boolean } {
const themeMainService = accessor.get(IThemeMainService);
const productService = accessor.get(IProductService);
const configurationService = accessor.get(IConfigurationService);
const environmentMainService = accessor.get(IEnvironmentMainService);
const windowSettings = configurationService.getValue<IWindowSettings | undefined>('window');
const options: electron.BrowserWindowConstructorOptions & { experimentalDarkMode: boolean } = {
backgroundColor: themeMainService.getBackgroundColor(),
minWidth: WindowMinimumSize.WIDTH,
minHeight: WindowMinimumSize.HEIGHT,
title: productService.nameLong,
show: windowState.mode !== WindowMode.Maximized && windowState.mode !== WindowMode.Fullscreen, // reduce flicker by showing later
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
webPreferences: {
...webPreferences,
enableWebSQL: false,
spellcheck: false,
zoomFactor: zoomLevelToZoomFactor(windowState.zoomLevel ?? windowSettings?.zoomLevel),
autoplayPolicy: 'user-gesture-required',
// Enable experimental css highlight api https://chromestatus.com/feature/5436441440026624
// Refs https://github.com/microsoft/vscode/issues/140098
enableBlinkFeatures: 'HighlightAPI',
sandbox: true
},
experimentalDarkMode: true
};
if (isLinux) {
options.icon = join(environmentMainService.appRoot, 'resources/linux/code.png'); // always on Linux
} else if (isWindows && !environmentMainService.isBuilt) {
options.icon = join(environmentMainService.appRoot, 'resources/win32/code_150x150.png'); // only when running out of sources on Windows
}
if (isMacintosh) {
options.acceptFirstMouse = true; // enabled by default
if (windowSettings?.clickThroughInactive === false) {
options.acceptFirstMouse = false;
}
}
if (overrides?.disableFullscreen) {
options.fullscreen = false;
} else if (isMacintosh && !useNativeFullScreen(configurationService)) {
options.fullscreenable = false; // enables simple fullscreen mode
}
const useNativeTabs = isMacintosh && windowSettings?.nativeTabs === true;
if (useNativeTabs) {
options.tabbingIdentifier = productService.nameShort; // this opts in to sierra tabs
}
const hideNativeTitleBar = !hasNativeTitlebar(configurationService, overrides?.forceNativeTitlebar ? TitlebarStyle.NATIVE : undefined);
if (hideNativeTitleBar) {
options.titleBarStyle = 'hidden';
if (!isMacintosh) {
options.frame = false;
}
if (useWindowControlsOverlay(configurationService)) {
// This logic will not perfectly guess the right colors
// to use on initialization, but prefer to keep things
// simple as it is temporary and not noticeable
const titleBarColor = themeMainService.getWindowSplash()?.colorInfo.titleBarBackground ?? themeMainService.getBackgroundColor();
const symbolColor = Color.fromHex(titleBarColor).isDarker() ? '#FFFFFF' : '#000000';
options.titleBarOverlay = {
height: 29, // the smallest size of the title bar on windows accounting for the border on windows 11
color: titleBarColor,
symbolColor
};
}
}
return options;
}
export function getLastFocused(windows: ICodeWindow[]): ICodeWindow | undefined;
export function getLastFocused(windows: IAuxiliaryWindow[]): IAuxiliaryWindow | undefined;
export function getLastFocused(windows: ICodeWindow[] | IAuxiliaryWindow[]): ICodeWindow | IAuxiliaryWindow | undefined {
let lastFocusedWindow: ICodeWindow | IAuxiliaryWindow | undefined = undefined;
let maxLastFocusTime = Number.MIN_VALUE;
for (const window of windows) {
if (window.lastFocusTime > maxLastFocusTime) {
maxLastFocusTime = window.lastFocusTime;
lastFocusedWindow = window;
}
}
return lastFocusedWindow;
}
export namespace WindowStateValidator {
export function validateWindowState(logService: ILogService, state: IWindowState, displays = electron.screen.getAllDisplays()): IWindowState | undefined {
logService.trace(`window#validateWindowState: validating window state on ${displays.length} display(s)`, state);
if (
typeof state.x !== 'number' ||
typeof state.y !== 'number' ||
typeof state.width !== 'number' ||
typeof state.height !== 'number'
) {
logService.trace('window#validateWindowState: unexpected type of state values');
return undefined;
}
if (state.width <= 0 || state.height <= 0) {
logService.trace('window#validateWindowState: unexpected negative values');
return undefined;
}
// Single Monitor: be strict about x/y positioning
// macOS & Linux: these OS seem to be pretty good in ensuring that a window is never outside of it's bounds.
// Windows: it is possible to have a window with a size that makes it fall out of the window. our strategy
// is to try as much as possible to keep the window in the monitor bounds. we are not as strict as
// macOS and Linux and allow the window to exceed the monitor bounds as long as the window is still
// some pixels (128) visible on the screen for the user to drag it back.
if (displays.length === 1) {
const displayWorkingArea = getWorkingArea(displays[0]);
logService.trace('window#validateWindowState: single monitor working area', displayWorkingArea);
if (displayWorkingArea) {
function ensureStateInDisplayWorkingArea(): void {
if (!state || typeof state.x !== 'number' || typeof state.y !== 'number' || !displayWorkingArea) {
return;
}
if (state.x < displayWorkingArea.x) {
// prevent window from falling out of the screen to the left
state.x = displayWorkingArea.x;
}
if (state.y < displayWorkingArea.y) {
// prevent window from falling out of the screen to the top
state.y = displayWorkingArea.y;
}
}
// ensure state is not outside display working area (top, left)
ensureStateInDisplayWorkingArea();
if (state.width > displayWorkingArea.width) {
// prevent window from exceeding display bounds width
state.width = displayWorkingArea.width;
}
if (state.height > displayWorkingArea.height) {
// prevent window from exceeding display bounds height
state.height = displayWorkingArea.height;
}
if (state.x > (displayWorkingArea.x + displayWorkingArea.width - 128)) {
// prevent window from falling out of the screen to the right with
// 128px margin by positioning the window to the far right edge of
// the screen
state.x = displayWorkingArea.x + displayWorkingArea.width - state.width;
}
if (state.y > (displayWorkingArea.y + displayWorkingArea.height - 128)) {
// prevent window from falling out of the screen to the bottom with
// 128px margin by positioning the window to the far bottom edge of
// the screen
state.y = displayWorkingArea.y + displayWorkingArea.height - state.height;
}
// again ensure state is not outside display working area
// (it may have changed from the previous validation step)
ensureStateInDisplayWorkingArea();
}
return state;
}
// Multi Montior (fullscreen): try to find the previously used display
if (state.display && state.mode === WindowMode.Fullscreen) {
const display = displays.find(d => d.id === state.display);
if (display && typeof display.bounds?.x === 'number' && typeof display.bounds?.y === 'number') {
logService.trace('window#validateWindowState: restoring fullscreen to previous display');
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): ensure window is within display bounds
let display: electron.Display | undefined;
let displayWorkingArea: electron.Rectangle | undefined;
try {
display = electron.screen.getDisplayMatching({ x: state.x, y: state.y, width: state.width, height: state.height });
displayWorkingArea = getWorkingArea(display);
logService.trace('window#validateWindowState: multi-monitor working area', displayWorkingArea);
} catch (error) {
// Electron has weird conditions under which it throws errors
// e.g. https://github.com/microsoft/vscode/issues/100334 when
// large numbers are passed in
logService.error('window#validateWindowState: error finding display for window state', error);
}
if (
display && // we have a display matching the desired bounds
displayWorkingArea && // we have valid working area bounds
state.x + state.width > displayWorkingArea.x && // prevent window from falling out of the screen to the left
state.y + state.height > displayWorkingArea.y && // prevent window from falling out of the screen to the top
state.x < displayWorkingArea.x + displayWorkingArea.width && // prevent window from falling out of the screen to the right
state.y < displayWorkingArea.y + displayWorkingArea.height // prevent window from falling out of the screen to the bottom
) {
return state;
}
logService.trace('window#validateWindowState: state is outside of the multi-monitor working area');
return undefined;
}
function getWorkingArea(display: electron.Display): electron.Rectangle | undefined {
// Prefer the working area of the display to account for taskbars on the
// desktop being positioned somewhere (https://github.com/microsoft/vscode/issues/50830).
//
// Linux X11 sessions sometimes report wrong display bounds, so we validate
// the reported sizes are positive.
if (display.workArea.width > 0 && display.workArea.height > 0) {
return display.workArea;
}
if (display.bounds.width > 0 && display.bounds.height > 0) {
return display.bounds;
}
return undefined;
}
}