提交 c7fe3b41 编写于 作者: B Benjamin Pasero

perf - set zoom level from electron-main and stop using webFrame in renderer unless changing zoom

//cc @deepak1556
上级 348e2b4c
......@@ -22,7 +22,6 @@
}
}(this, function () {
const path = require.__$__nodeRequire('path');
const webFrame = require.__$__nodeRequire('electron').webFrame;
const ipc = require.__$__nodeRequire('electron').ipcRenderer;
const bootstrap = globalThis.MonacoBootstrap;
......@@ -36,7 +35,6 @@
/**
* // configuration: INativeWindowConfiguration
* @type {{
* zoomLevel?: number,
* extensionDevelopmentPath?: string[],
* extensionTestsPath?: string,
* userEnv?: { [key: string]: string | undefined },
......@@ -45,12 +43,6 @@
* }} */
const configuration = JSON.parse(args['config'] || '{}') || {};
// Apply zoom level early to avoid glitches
const zoomLevel = configuration.zoomLevel;
if (typeof zoomLevel === 'number' && zoomLevel !== 0) {
webFrame.setZoomLevel(zoomLevel);
}
// Error handler
process.on('uncaughtException', function (error) {
onUnexpectedError(error, enableDeveloperTools);
......
......@@ -68,14 +68,6 @@
*/
webFrame: {
getZoomFactor() {
return webFrame.getZoomFactor();
},
getZoomLevel() {
return webFrame.getZoomLevel();
},
/**
* @param {number} level
*/
......
......@@ -43,16 +43,6 @@ export const ipcRenderer = (window as any).vscode.ipcRenderer as {
export const webFrame = (window as any).vscode.webFrame as {
/**
* The current zoom factor.
*/
getZoomFactor(): number;
/**
* The current zoom level.
*/
getZoomLevel(): number;
/**
* Changes the zoom level to the specified level. The original size is 0 and each
* increment above or below represents zooming 20% larger or smaller to default
......
......@@ -4,13 +4,13 @@
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/issueReporter';
import { ElectronService, IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
import { ipcRenderer, webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import 'vs/base/browser/ui/codicons/codiconStyles'; // make sure codicon css is loaded
import * as os from 'os';
import * as browser from 'vs/base/browser/browser';
import { ElectronService, IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { applyZoom, zoomIn, zoomOut } from 'vs/platform/windows/electron-sandbox/window';
import { $, windowOpenNoOpener, addClass } from 'vs/base/browser/dom';
import { Button } from 'vs/base/browser/ui/button/button';
import 'vs/base/browser/ui/codicons/codiconStyles'; // make sure codicon css is loaded
import { CodiconLabel } from 'vs/base/browser/ui/codicons/codiconLabel';
import * as collections from 'vs/base/common/collections';
import { debounce } from 'vs/base/common/decorators';
......@@ -152,7 +152,7 @@ export class IssueReporter extends Disposable {
this.setUpTypes();
this.setEventHandlers();
this.applyZoom(configuration.data.zoomLevel);
applyZoom(configuration.data.zoomLevel);
this.applyStyles(configuration.data.styles);
this.handleExtensionData(configuration.data.enabledExtensions);
......@@ -180,15 +180,6 @@ export class IssueReporter extends Disposable {
}
}
private applyZoom(zoomLevel: number) {
webFrame.setZoomLevel(zoomLevel);
browser.setZoomFactor(webFrame.getZoomFactor());
// See https://github.com/Microsoft/vscode/issues/26151
// Cannot be trusted because the webFrame might take some time
// until it really applies the new zoom level
browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
}
private applyStyles(styles: IssueReporterStyles) {
const styleTag = document.createElement('style');
const content: string[] = [];
......@@ -505,12 +496,12 @@ export class IssueReporter extends Disposable {
// Cmd/Ctrl + zooms in
if (cmdOrCtrlKey && e.keyCode === 187) {
this.applyZoom(webFrame.getZoomLevel() + 1);
zoomIn();
}
// Cmd/Ctrl - zooms out
if (cmdOrCtrlKey && e.keyCode === 189) {
this.applyZoom(webFrame.getZoomLevel() - 1);
zoomOut();
}
// With latest electron upgrade, cmd+a is no longer propagating correctly for inputs in this window on mac
......
......@@ -5,13 +5,12 @@
import 'vs/css!./media/processExplorer';
import { clipboard } from 'electron';
import { webFrame, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { repeat } from 'vs/base/common/strings';
import { totalmem } from 'os';
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import product from 'vs/platform/product/common/product';
import { localize } from 'vs/nls';
import { ProcessExplorerStyles, ProcessExplorerData } from 'vs/platform/issue/common/issue';
import * as browser from 'vs/base/browser/browser';
import { applyZoom, zoomIn, zoomOut } from 'vs/platform/windows/electron-sandbox/window';
import * as platform from 'vs/base/common/platform';
import { IContextMenuItem } from 'vs/base/parts/contextmenu/common/contextmenu';
import { popup } from 'vs/base/parts/contextmenu/electron-sandbox/contextmenu';
......@@ -63,7 +62,7 @@ function getProcessItem(processes: FormattedProcessItem[], item: ProcessItem, in
}
// Format name with indent
const formattedName = isRoot ? name : `${repeat(' ', indent)} ${name}`;
const formattedName = isRoot ? name : `${' '.repeat(indent)} ${name}`;
const memory = process.platform === 'win32' ? item.mem : (totalmem() * (item.mem / 100));
processes.push({
cpu: item.load,
......@@ -291,15 +290,6 @@ function applyStyles(styles: ProcessExplorerStyles): void {
}
}
function applyZoom(zoomLevel: number): void {
webFrame.setZoomLevel(zoomLevel);
browser.setZoomFactor(webFrame.getZoomFactor());
// See https://github.com/Microsoft/vscode/issues/26151
// Cannot be trusted because the webFrame might take some time
// until it really applies the new zoom level
browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
}
function showContextMenu(e: MouseEvent, item: FormattedProcessItem, isLocal: boolean) {
e.preventDefault();
......@@ -416,12 +406,12 @@ export function startup(data: ProcessExplorerData): void {
// Cmd/Ctrl + zooms in
if (cmdOrCtrlKey && e.keyCode === 187) {
applyZoom(webFrame.getZoomLevel() + 1);
zoomIn();
}
// Cmd/Ctrl - zooms out
if (cmdOrCtrlKey && e.keyCode === 189) {
applyZoom(webFrame.getZoomLevel() - 1);
zoomOut();
}
};
}
......@@ -15,7 +15,7 @@ import { ILogService } from 'vs/platform/log/common/log';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { parseArgs, OPTIONS, ParsedArgs } from 'vs/platform/environment/node/argv';
import product from 'vs/platform/product/common/product';
import { IWindowSettings, MenuBarVisibility, getTitleBarStyle, getMenuBarVisibility } from 'vs/platform/windows/common/windows';
import { IWindowSettings, MenuBarVisibility, getTitleBarStyle, getMenuBarVisibility, zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
import { Disposable, toDisposable } 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';
......@@ -153,6 +153,8 @@ export class CodeWindow extends Disposable implements ICodeWindow {
// 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);
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
const options: BrowserWindowConstructorOptions = {
width: this.windowState.width,
height: this.windowState.height,
......@@ -169,7 +171,8 @@ export class CodeWindow extends Disposable implements ICodeWindow {
webviewTag: true,
enableWebSQL: false,
enableRemoteModule: false,
nativeWindowOpen: true
nativeWindowOpen: true,
zoomFactor: zoomLevelToZoomFactor(windowConfig?.zoomLevel)
}
};
......@@ -182,8 +185,6 @@ export class CodeWindow extends Disposable implements ICodeWindow {
options.icon = path.join(this.environmentService.appRoot, 'resources/win32/code_150x150.png');
}
const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
if (isMacintosh && !this.useNativeFullScreen()) {
options.fullscreenable = false; // enables simple fullscreen mode
}
......
......@@ -20,6 +20,7 @@ import { listProcesses } from 'vs/base/node/ps';
import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogs';
import { URI } from 'vs/base/common/uri';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
const DEFAULT_BACKGROUND_COLOR = '#1E1E1E';
......@@ -198,7 +199,8 @@ export class IssueMainService implements ICommonIssueService {
nodeIntegration: true,
enableWebSQL: false,
enableRemoteModule: false,
nativeWindowOpen: true
nativeWindowOpen: true,
zoomFactor: zoomLevelToZoomFactor(data.zoomLevel)
}
});
......@@ -251,7 +253,8 @@ export class IssueMainService implements ICommonIssueService {
nodeIntegration: true,
enableWebSQL: false,
enableRemoteModule: false,
nativeWindowOpen: true
nativeWindowOpen: true,
zoomFactor: zoomLevelToZoomFactor(data.zoomLevel)
}
});
......
......@@ -187,3 +187,11 @@ export interface IWindowConfiguration {
filesToOpenOrCreate?: IPath[];
filesToDiff?: IPath[];
}
/**
* According to Electron docs: `scale := 1.2 ^ level`.
* https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentssetzoomlevellevel
*/
export function zoomLevelToZoomFactor(zoomLevel = 0): number {
return Math.pow(1.2, zoomLevel);
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
import { setZoomFactor, setZoomLevel, getZoomLevel } from 'vs/base/browser/browser';
/**
* Apply a zoom level to the window. Also sets it in our in-memory
* browser helper so that it can be accessed in non-electron layers.
*/
export function applyZoom(zoomLevel: number): void {
webFrame.setZoomLevel(zoomLevel);
setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
// Cannot be trusted because the webFrame might take some time
// until it really applies the new zoom level
// See https://github.com/Microsoft/vscode/issues/26151
setZoomLevel(zoomLevel, false /* isTrusted */);
}
export function zoomIn(): void {
applyZoom(getZoomLevel() + 1);
}
export function zoomOut(): void {
applyZoom(getZoomLevel() - 1);
}
......@@ -10,7 +10,7 @@ import { textLinkForeground, inputBackground, inputBorder, inputForeground, butt
import { SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
import { IExtensionManagementService } from 'vs/platform/extensionManagement/common/extensionManagement';
import { IWorkbenchExtensionEnablementService } from 'vs/workbench/services/extensionManagement/common/extensionManagement';
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { getZoomLevel } from 'vs/base/browser/browser';
import { IWorkbenchIssueService } from 'vs/workbench/contrib/issue/electron-browser/issue';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-browser/environmentService';
......@@ -50,7 +50,7 @@ export class WorkbenchIssueService implements IWorkbenchIssueService {
const theme = this.themeService.getColorTheme();
const issueReporterData: IssueReporterData = Object.assign({
styles: getIssueReporterStyles(theme),
zoomLevel: webFrame.getZoomLevel(),
zoomLevel: getZoomLevel(),
enabledExtensions: extensionData,
}, dataOverrides);
return this.issueService.openReporter(issueReporterData);
......@@ -60,7 +60,7 @@ export class WorkbenchIssueService implements IWorkbenchIssueService {
const theme = this.themeService.getColorTheme();
const data: ProcessExplorerData = {
pid: this.environmentService.configuration.mainPid,
zoomLevel: webFrame.getZoomLevel(),
zoomLevel: getZoomLevel(),
styles: {
backgroundColor: getColor(theme, editorBackground),
color: getColor(theme, editorForeground),
......
......@@ -5,7 +5,7 @@
import * as fs from 'fs';
import * as gracefulFs from 'graceful-fs';
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
import { importEntries, mark } from 'vs/base/common/performance';
import { Workbench } from 'vs/workbench/browser/workbench';
import { NativeWindow } from 'vs/workbench/electron-browser/window';
......@@ -73,8 +73,9 @@ class DesktopMain extends Disposable {
importEntries(this.environmentService.configuration.perfEntries);
// Browser config
setZoomFactor(webFrame.getZoomFactor()); // Ensure others can listen to zoom level changes
setZoomLevel(webFrame.getZoomLevel(), true /* isTrusted */); // Can be trusted because we are not setting it ourselves (https://github.com/Microsoft/vscode/issues/26151)
const zoomLevel = this.configuration.zoomLevel || 0;
setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
setZoomLevel(zoomLevel, true /* isTrusted */);
setFullscreen(!!this.environmentService.configuration.fullscreen);
// Keyboard support
......
......@@ -18,12 +18,13 @@ import { IWindowSettings, IOpenFileRequest, IWindowsConfiguration, getTitleBarSt
import { IRunActionInWindowRequest, IRunKeybindingInWindowRequest, INativeOpenFileRequest } from 'vs/platform/windows/node/window';
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
import { IWorkbenchThemeService, VS_HC_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
import * as browser from 'vs/base/browser/browser';
import { applyZoom } from 'vs/platform/windows/electron-sandbox/window';
import { setFullscreen, getZoomLevel } from 'vs/base/browser/browser';
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/nativeKeymapService';
import { CrashReporterStartOptions } from 'vs/base/parts/sandbox/common/electronTypes';
import { crashReporter, ipcRenderer, webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { crashReporter, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspaces/common/workspaceEditing';
import { IMenuService, MenuId, IMenu, MenuItemAction, ICommandAction, SubmenuItemAction, MenuRegistry } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
......@@ -194,12 +195,12 @@ export class NativeWindow extends Disposable {
// Fullscreen Events
ipcRenderer.on('vscode:enterFullScreen', async () => {
await this.lifecycleService.when(LifecyclePhase.Ready);
browser.setFullscreen(true);
setFullscreen(true);
});
ipcRenderer.on('vscode:leaveFullScreen', async () => {
await this.lifecycleService.when(LifecyclePhase.Ready);
browser.setFullscreen(false);
setFullscreen(false);
});
// High Contrast Events
......@@ -230,10 +231,10 @@ export class NativeWindow extends Disposable {
});
// Zoom level changes
this.updateWindowZoomLevel(false);
this.updateWindowZoomLevel();
this._register(this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('window.zoomLevel')) {
this.updateWindowZoomLevel(true);
this.updateWindowZoomLevel();
} else if (e.affectsConfiguration('keyboard.touchbar.enabled') || e.affectsConfiguration('keyboard.touchbar.ignored')) {
this.updateTouchbarMenu();
}
......@@ -326,7 +327,7 @@ export class NativeWindow extends Disposable {
}
}
private updateWindowZoomLevel(fromEvent: boolean): void {
private updateWindowZoomLevel(): void {
const windowConfig = this.configurationService.getValue<IWindowsConfiguration>();
let configuredZoomLevel = 0;
......@@ -341,13 +342,8 @@ export class NativeWindow extends Disposable {
this.previousConfiguredZoomLevel = configuredZoomLevel;
}
if (fromEvent && webFrame.getZoomLevel() !== configuredZoomLevel) {
webFrame.setZoomLevel(configuredZoomLevel);
browser.setZoomFactor(webFrame.getZoomFactor());
// Cannot be trusted because the webFrame might take some time
// until it really applies the new zoom level
// See https://github.com/Microsoft/vscode/issues/26151
browser.setZoomLevel(webFrame.getZoomLevel(), false /* isTrusted */);
if (getZoomLevel() !== configuredZoomLevel) {
applyZoom(configuredZoomLevel);
}
}
......
......@@ -8,9 +8,9 @@ import 'vs/css!./media/actions';
import { URI } from 'vs/base/common/uri';
import { Action } from 'vs/base/common/actions';
import * as nls from 'vs/nls';
import * as browser from 'vs/base/browser/browser';
import { applyZoom } from 'vs/platform/windows/electron-sandbox/window';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { getZoomLevel } from 'vs/base/browser/browser';
import { FileKind } from 'vs/platform/files/common/files';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
......@@ -62,18 +62,9 @@ export abstract class BaseZoomAction extends Action {
return; // https://github.com/microsoft/vscode/issues/48357
}
const applyZoom = () => {
webFrame.setZoomLevel(level);
browser.setZoomFactor(webFrame.getZoomFactor());
// Cannot be trusted because the webFrame might take some time
// until it really applies the new zoom level
// See https://github.com/Microsoft/vscode/issues/26151
browser.setZoomLevel(webFrame.getZoomLevel(), false /* isTrusted */);
};
await this.configurationService.updateValue(BaseZoomAction.SETTING_KEY, level);
applyZoom();
applyZoom(level);
}
}
......@@ -91,7 +82,7 @@ export class ZoomInAction extends BaseZoomAction {
}
async run(): Promise<void> {
this.setConfiguredZoomLevel(webFrame.getZoomLevel() + 1);
this.setConfiguredZoomLevel(getZoomLevel() + 1);
}
}
......@@ -109,7 +100,7 @@ export class ZoomOutAction extends BaseZoomAction {
}
async run(): Promise<void> {
this.setConfiguredZoomLevel(webFrame.getZoomLevel() - 1);
this.setConfiguredZoomLevel(getZoomLevel() - 1);
}
}
......
......@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as browser from 'vs/base/browser/browser';
import { getZoomFactor } from 'vs/base/browser/browser';
import * as DOM from 'vs/base/browser/dom';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
......@@ -209,14 +209,14 @@ export class TitlebarPart extends BrowserTitleBarPart {
if (getTitleBarStyle(this.configurationService, this.environmentService) === 'custom') {
// Only prevent zooming behavior on macOS or when the menubar is not visible
if (isMacintosh || this.currentMenubarVisibility === 'hidden') {
this.title.style.zoom = `${1 / browser.getZoomFactor()}`;
this.title.style.zoom = `${1 / getZoomFactor()}`;
if (isWindows || isLinux) {
if (this.appIcon) {
this.appIcon.style.zoom = `${1 / browser.getZoomFactor()}`;
this.appIcon.style.zoom = `${1 / getZoomFactor()}`;
}
if (this.windowControls) {
this.windowControls.style.zoom = `${1 / browser.getZoomFactor()}`;
this.windowControls.style.zoom = `${1 / getZoomFactor()}`;
}
}
} else {
......
......@@ -9,7 +9,7 @@ import * as dom from 'vs/base/browser/dom';
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { getZoomFactor } from 'vs/base/browser/browser';
import { unmnemonicLabel } from 'vs/base/common/labels';
import { Event, Emitter } from 'vs/base/common/event';
import { INotificationService } from 'vs/platform/notification/common/notification';
......@@ -94,9 +94,9 @@ class NativeContextMenuService extends Disposable implements IContextMenuService
let x: number;
let y: number;
const zoom = webFrame.getZoomFactor();
const zoom = getZoomFactor();
if (dom.isHTMLElement(anchor)) {
let elementPosition = dom.getDomNodePagePosition(anchor);
const elementPosition = dom.getDomNodePagePosition(anchor);
x = elementPosition.left;
y = elementPosition.top + elementPosition.height;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册