Skip to content

Commit c7fe3b4

Browse files
committed
perf - set zoom level from electron-main and stop using webFrame in renderer unless changing zoom
//cc @deepak1556
1 parent 348e2b4 commit c7fe3b4

File tree

15 files changed

+88
-104
lines changed

15 files changed

+88
-104
lines changed

src/bootstrap-window.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
}
2323
}(this, function () {
2424
const path = require.__$__nodeRequire('path');
25-
const webFrame = require.__$__nodeRequire('electron').webFrame;
2625
const ipc = require.__$__nodeRequire('electron').ipcRenderer;
2726
const bootstrap = globalThis.MonacoBootstrap;
2827

@@ -36,7 +35,6 @@
3635
/**
3736
* // configuration: INativeWindowConfiguration
3837
* @type {{
39-
* zoomLevel?: number,
4038
* extensionDevelopmentPath?: string[],
4139
* extensionTestsPath?: string,
4240
* userEnv?: { [key: string]: string | undefined },
@@ -45,12 +43,6 @@
4543
* }} */
4644
const configuration = JSON.parse(args['config'] || '{}') || {};
4745

48-
// Apply zoom level early to avoid glitches
49-
const zoomLevel = configuration.zoomLevel;
50-
if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
51-
webFrame.setZoomLevel(zoomLevel);
52-
}
53-
5446
// Error handler
5547
process.on('uncaughtException', function (error) {
5648
onUnexpectedError(error, enableDeveloperTools);

src/vs/base/parts/sandbox/electron-browser/preload.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@
6868
*/
6969
webFrame: {
7070

71-
getZoomFactor() {
72-
return webFrame.getZoomFactor();
73-
},
74-
75-
getZoomLevel() {
76-
return webFrame.getZoomLevel();
77-
},
78-
7971
/**
8072
* @param {number} level
8173
*/

src/vs/base/parts/sandbox/electron-sandbox/globals.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ export const ipcRenderer = (window as any).vscode.ipcRenderer as {
4343

4444
export const webFrame = (window as any).vscode.webFrame as {
4545

46-
/**
47-
* The current zoom factor.
48-
*/
49-
getZoomFactor(): number;
50-
51-
/**
52-
* The current zoom level.
53-
*/
54-
getZoomLevel(): number;
55-
5646
/**
5747
* Changes the zoom level to the specified level. The original size is 0 and each
5848
* increment above or below represents zooming 20% larger or smaller to default

src/vs/code/electron-browser/issue/issueReporterMain.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import 'vs/css!./media/issueReporter';
7-
import { ElectronService, IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
8-
import { ipcRenderer, webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
7+
import 'vs/base/browser/ui/codicons/codiconStyles'; // make sure codicon css is loaded
98
import * as os from 'os';
10-
import * as browser from 'vs/base/browser/browser';
9+
import { ElectronService, IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
10+
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
11+
import { applyZoom, zoomIn, zoomOut } from 'vs/platform/windows/electron-sandbox/window';
1112
import { $, windowOpenNoOpener, addClass } from 'vs/base/browser/dom';
1213
import { Button } from 'vs/base/browser/ui/button/button';
13-
import 'vs/base/browser/ui/codicons/codiconStyles'; // make sure codicon css is loaded
1414
import { CodiconLabel } from 'vs/base/browser/ui/codicons/codiconLabel';
1515
import * as collections from 'vs/base/common/collections';
1616
import { debounce } from 'vs/base/common/decorators';
@@ -152,7 +152,7 @@ export class IssueReporter extends Disposable {
152152

153153
this.setUpTypes();
154154
this.setEventHandlers();
155-
this.applyZoom(configuration.data.zoomLevel);
155+
applyZoom(configuration.data.zoomLevel);
156156
this.applyStyles(configuration.data.styles);
157157
this.handleExtensionData(configuration.data.enabledExtensions);
158158

@@ -180,15 +180,6 @@ export class IssueReporter extends Disposable {
180180
}
181181
}
182182

183-
private applyZoom(zoomLevel: number) {
184-
webFrame.setZoomLevel(zoomLevel);
185-
browser.setZoomFactor(webFrame.getZoomFactor());
186-
// See https://github.com/Microsoft/vscode/issues/26151
187-
// Cannot be trusted because the webFrame might take some time
188-
// until it really applies the new zoom level
189-
browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
190-
}
191-
192183
private applyStyles(styles: IssueReporterStyles) {
193184
const styleTag = document.createElement('style');
194185
const content: string[] = [];
@@ -505,12 +496,12 @@ export class IssueReporter extends Disposable {
505496

506497
// Cmd/Ctrl + zooms in
507498
if (cmdOrCtrlKey && e.keyCode === 187) {
508-
this.applyZoom(webFrame.getZoomLevel() + 1);
499+
zoomIn();
509500
}
510501

511502
// Cmd/Ctrl - zooms out
512503
if (cmdOrCtrlKey && e.keyCode === 189) {
513-
this.applyZoom(webFrame.getZoomLevel() - 1);
504+
zoomOut();
514505
}
515506

516507
// With latest electron upgrade, cmd+a is no longer propagating correctly for inputs in this window on mac

src/vs/code/electron-browser/processExplorer/processExplorerMain.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
import 'vs/css!./media/processExplorer';
77
import { clipboard } from 'electron';
8-
import { webFrame, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
9-
import { repeat } from 'vs/base/common/strings';
108
import { totalmem } from 'os';
9+
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
1110
import product from 'vs/platform/product/common/product';
1211
import { localize } from 'vs/nls';
1312
import { ProcessExplorerStyles, ProcessExplorerData } from 'vs/platform/issue/common/issue';
14-
import * as browser from 'vs/base/browser/browser';
13+
import { applyZoom, zoomIn, zoomOut } from 'vs/platform/windows/electron-sandbox/window';
1514
import * as platform from 'vs/base/common/platform';
1615
import { IContextMenuItem } from 'vs/base/parts/contextmenu/common/contextmenu';
1716
import { popup } from 'vs/base/parts/contextmenu/electron-sandbox/contextmenu';
@@ -63,7 +62,7 @@ function getProcessItem(processes: FormattedProcessItem[], item: ProcessItem, in
6362
}
6463

6564
// Format name with indent
66-
const formattedName = isRoot ? name : `${repeat(' ', indent)} ${name}`;
65+
const formattedName = isRoot ? name : `${' '.repeat(indent)} ${name}`;
6766
const memory = process.platform === 'win32' ? item.mem : (totalmem() * (item.mem / 100));
6867
processes.push({
6968
cpu: item.load,
@@ -291,15 +290,6 @@ function applyStyles(styles: ProcessExplorerStyles): void {
291290
}
292291
}
293292

294-
function applyZoom(zoomLevel: number): void {
295-
webFrame.setZoomLevel(zoomLevel);
296-
browser.setZoomFactor(webFrame.getZoomFactor());
297-
// See https://github.com/Microsoft/vscode/issues/26151
298-
// Cannot be trusted because the webFrame might take some time
299-
// until it really applies the new zoom level
300-
browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
301-
}
302-
303293
function showContextMenu(e: MouseEvent, item: FormattedProcessItem, isLocal: boolean) {
304294
e.preventDefault();
305295

@@ -416,12 +406,12 @@ export function startup(data: ProcessExplorerData): void {
416406

417407
// Cmd/Ctrl + zooms in
418408
if (cmdOrCtrlKey && e.keyCode === 187) {
419-
applyZoom(webFrame.getZoomLevel() + 1);
409+
zoomIn();
420410
}
421411

422412
// Cmd/Ctrl - zooms out
423413
if (cmdOrCtrlKey && e.keyCode === 189) {
424-
applyZoom(webFrame.getZoomLevel() - 1);
414+
zoomOut();
425415
}
426416
};
427417
}

src/vs/code/electron-main/window.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ILogService } from 'vs/platform/log/common/log';
1515
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1616
import { parseArgs, OPTIONS, ParsedArgs } from 'vs/platform/environment/node/argv';
1717
import product from 'vs/platform/product/common/product';
18-
import { IWindowSettings, MenuBarVisibility, getTitleBarStyle, getMenuBarVisibility } from 'vs/platform/windows/common/windows';
18+
import { IWindowSettings, MenuBarVisibility, getTitleBarStyle, getMenuBarVisibility, zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
1919
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
2020
import { isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
2121
import { ICodeWindow, IWindowState, WindowMode } from 'vs/platform/windows/electron-main/windows';
@@ -153,6 +153,8 @@ export class CodeWindow extends Disposable implements ICodeWindow {
153153
// in case we are maximized or fullscreen, only show later after the call to maximize/fullscreen (see below)
154154
const isFullscreenOrMaximized = (this.windowState.mode === WindowMode.Maximized || this.windowState.mode === WindowMode.Fullscreen);
155155

156+
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
157+
156158
const options: BrowserWindowConstructorOptions = {
157159
width: this.windowState.width,
158160
height: this.windowState.height,
@@ -169,7 +171,8 @@ export class CodeWindow extends Disposable implements ICodeWindow {
169171
webviewTag: true,
170172
enableWebSQL: false,
171173
enableRemoteModule: false,
172-
nativeWindowOpen: true
174+
nativeWindowOpen: true,
175+
zoomFactor: zoomLevelToZoomFactor(windowConfig?.zoomLevel)
173176
}
174177
};
175178

@@ -182,8 +185,6 @@ export class CodeWindow extends Disposable implements ICodeWindow {
182185
options.icon = path.join(this.environmentService.appRoot, 'resources/win32/code_150x150.png');
183186
}
184187

185-
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
186-
187188
if (isMacintosh && !this.useNativeFullScreen()) {
188189
options.fullscreenable = false; // enables simple fullscreen mode
189190
}

src/vs/platform/issue/electron-main/issueMainService.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { listProcesses } from 'vs/base/node/ps';
2020
import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogs';
2121
import { URI } from 'vs/base/common/uri';
2222
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
23+
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
2324

2425
const DEFAULT_BACKGROUND_COLOR = '#1E1E1E';
2526

@@ -198,7 +199,8 @@ export class IssueMainService implements ICommonIssueService {
198199
nodeIntegration: true,
199200
enableWebSQL: false,
200201
enableRemoteModule: false,
201-
nativeWindowOpen: true
202+
nativeWindowOpen: true,
203+
zoomFactor: zoomLevelToZoomFactor(data.zoomLevel)
202204
}
203205
});
204206

@@ -251,7 +253,8 @@ export class IssueMainService implements ICommonIssueService {
251253
nodeIntegration: true,
252254
enableWebSQL: false,
253255
enableRemoteModule: false,
254-
nativeWindowOpen: true
256+
nativeWindowOpen: true,
257+
zoomFactor: zoomLevelToZoomFactor(data.zoomLevel)
255258
}
256259
});
257260

src/vs/platform/windows/common/windows.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,11 @@ export interface IWindowConfiguration {
187187
filesToOpenOrCreate?: IPath[];
188188
filesToDiff?: IPath[];
189189
}
190+
191+
/**
192+
* According to Electron docs: `scale := 1.2 ^ level`.
193+
* https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentssetzoomlevellevel
194+
*/
195+
export function zoomLevelToZoomFactor(zoomLevel = 0): number {
196+
return Math.pow(1.2, zoomLevel);
197+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
7+
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
8+
import { setZoomFactor, setZoomLevel, getZoomLevel } from 'vs/base/browser/browser';
9+
10+
/**
11+
* Apply a zoom level to the window. Also sets it in our in-memory
12+
* browser helper so that it can be accessed in non-electron layers.
13+
*/
14+
export function applyZoom(zoomLevel: number): void {
15+
webFrame.setZoomLevel(zoomLevel);
16+
setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
17+
// Cannot be trusted because the webFrame might take some time
18+
// until it really applies the new zoom level
19+
// See https://github.com/Microsoft/vscode/issues/26151
20+
setZoomLevel(zoomLevel, false /* isTrusted */);
21+
}
22+
23+
export function zoomIn(): void {
24+
applyZoom(getZoomLevel() + 1);
25+
}
26+
27+
export function zoomOut(): void {
28+
applyZoom(getZoomLevel() - 1);
29+
}

src/vs/workbench/contrib/issue/electron-browser/issueService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { textLinkForeground, inputBackground, inputBorder, inputForeground, butt
1010
import { SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
1111
import { IExtensionManagementService } from 'vs/platform/extensionManagement/common/extensionManagement';
1212
import { IWorkbenchExtensionEnablementService } from 'vs/workbench/services/extensionManagement/common/extensionManagement';
13-
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
13+
import { getZoomLevel } from 'vs/base/browser/browser';
1414
import { IWorkbenchIssueService } from 'vs/workbench/contrib/issue/electron-browser/issue';
1515
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
1616
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-browser/environmentService';
@@ -50,7 +50,7 @@ export class WorkbenchIssueService implements IWorkbenchIssueService {
5050
const theme = this.themeService.getColorTheme();
5151
const issueReporterData: IssueReporterData = Object.assign({
5252
styles: getIssueReporterStyles(theme),
53-
zoomLevel: webFrame.getZoomLevel(),
53+
zoomLevel: getZoomLevel(),
5454
enabledExtensions: extensionData,
5555
}, dataOverrides);
5656
return this.issueService.openReporter(issueReporterData);
@@ -60,7 +60,7 @@ export class WorkbenchIssueService implements IWorkbenchIssueService {
6060
const theme = this.themeService.getColorTheme();
6161
const data: ProcessExplorerData = {
6262
pid: this.environmentService.configuration.mainPid,
63-
zoomLevel: webFrame.getZoomLevel(),
63+
zoomLevel: getZoomLevel(),
6464
styles: {
6565
backgroundColor: getColor(theme, editorBackground),
6666
color: getColor(theme, editorForeground),

src/vs/workbench/electron-browser/desktop.main.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as fs from 'fs';
77
import * as gracefulFs from 'graceful-fs';
8-
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
8+
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
99
import { importEntries, mark } from 'vs/base/common/performance';
1010
import { Workbench } from 'vs/workbench/browser/workbench';
1111
import { NativeWindow } from 'vs/workbench/electron-browser/window';
@@ -73,8 +73,9 @@ class DesktopMain extends Disposable {
7373
importEntries(this.environmentService.configuration.perfEntries);
7474

7575
// Browser config
76-
setZoomFactor(webFrame.getZoomFactor()); // Ensure others can listen to zoom level changes
77-
setZoomLevel(webFrame.getZoomLevel(), true /* isTrusted */); // Can be trusted because we are not setting it ourselves (https://github.com/Microsoft/vscode/issues/26151)
76+
const zoomLevel = this.configuration.zoomLevel || 0;
77+
setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
78+
setZoomLevel(zoomLevel, true /* isTrusted */);
7879
setFullscreen(!!this.environmentService.configuration.fullscreen);
7980

8081
// Keyboard support

src/vs/workbench/electron-browser/window.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import { IWindowSettings, IOpenFileRequest, IWindowsConfiguration, getTitleBarSt
1818
import { IRunActionInWindowRequest, IRunKeybindingInWindowRequest, INativeOpenFileRequest } from 'vs/platform/windows/node/window';
1919
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
2020
import { IWorkbenchThemeService, VS_HC_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
21-
import * as browser from 'vs/base/browser/browser';
21+
import { applyZoom } from 'vs/platform/windows/electron-sandbox/window';
22+
import { setFullscreen, getZoomLevel } from 'vs/base/browser/browser';
2223
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
2324
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
2425
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/nativeKeymapService';
2526
import { CrashReporterStartOptions } from 'vs/base/parts/sandbox/common/electronTypes';
26-
import { crashReporter, ipcRenderer, webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
27+
import { crashReporter, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
2728
import { IWorkspaceEditingService } from 'vs/workbench/services/workspaces/common/workspaceEditing';
2829
import { IMenuService, MenuId, IMenu, MenuItemAction, ICommandAction, SubmenuItemAction, MenuRegistry } from 'vs/platform/actions/common/actions';
2930
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
@@ -194,12 +195,12 @@ export class NativeWindow extends Disposable {
194195
// Fullscreen Events
195196
ipcRenderer.on('vscode:enterFullScreen', async () => {
196197
await this.lifecycleService.when(LifecyclePhase.Ready);
197-
browser.setFullscreen(true);
198+
setFullscreen(true);
198199
});
199200

200201
ipcRenderer.on('vscode:leaveFullScreen', async () => {
201202
await this.lifecycleService.when(LifecyclePhase.Ready);
202-
browser.setFullscreen(false);
203+
setFullscreen(false);
203204
});
204205

205206
// High Contrast Events
@@ -230,10 +231,10 @@ export class NativeWindow extends Disposable {
230231
});
231232

232233
// Zoom level changes
233-
this.updateWindowZoomLevel(false);
234+
this.updateWindowZoomLevel();
234235
this._register(this.configurationService.onDidChangeConfiguration(e => {
235236
if (e.affectsConfiguration('window.zoomLevel')) {
236-
this.updateWindowZoomLevel(true);
237+
this.updateWindowZoomLevel();
237238
} else if (e.affectsConfiguration('keyboard.touchbar.enabled') || e.affectsConfiguration('keyboard.touchbar.ignored')) {
238239
this.updateTouchbarMenu();
239240
}
@@ -326,7 +327,7 @@ export class NativeWindow extends Disposable {
326327
}
327328
}
328329

329-
private updateWindowZoomLevel(fromEvent: boolean): void {
330+
private updateWindowZoomLevel(): void {
330331
const windowConfig = this.configurationService.getValue<IWindowsConfiguration>();
331332

332333
let configuredZoomLevel = 0;
@@ -341,13 +342,8 @@ export class NativeWindow extends Disposable {
341342
this.previousConfiguredZoomLevel = configuredZoomLevel;
342343
}
343344

344-
if (fromEvent && webFrame.getZoomLevel() !== configuredZoomLevel) {
345-
webFrame.setZoomLevel(configuredZoomLevel);
346-
browser.setZoomFactor(webFrame.getZoomFactor());
347-
// Cannot be trusted because the webFrame might take some time
348-
// until it really applies the new zoom level
349-
// See https://github.com/Microsoft/vscode/issues/26151
350-
browser.setZoomLevel(webFrame.getZoomLevel(), false /* isTrusted */);
345+
if (getZoomLevel() !== configuredZoomLevel) {
346+
applyZoom(configuredZoomLevel);
351347
}
352348
}
353349

0 commit comments

Comments
 (0)