Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aux window - apply zoom via webFrame on preload slim #195289

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/vs/base/parts/sandbox/electron-sandbox/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,32 @@ export const ipcMessagePort: IpcMessagePort = globals.vscode.ipcMessagePort;
export const webFrame: WebFrame = globals.vscode.webFrame;
export const process: ISandboxNodeProcess = globals.vscode.process;
export const context: ISandboxContext = globals.vscode.context;

export interface IGlobalsSlim {
readonly ipcRenderer: Pick<import('vs/base/parts/sandbox/electron-sandbox/electronTypes').IpcRenderer, 'send'>;
readonly webFrame: import('vs/base/parts/sandbox/electron-sandbox/electronTypes').WebFrame;
}

/**
* Get the globals that are available in the given window. Since
* this method supports auxiliary windows, only a subset of globals
* is returned.
*/
export function getGlobals(win: Window): IGlobalsSlim | undefined {
if (win === window) {
return { ipcRenderer, webFrame };
}

const auxiliaryWindowCandidate = win as unknown as {
vscode: {
ipcRenderer: Pick<import('vs/base/parts/sandbox/electron-sandbox/electronTypes').IpcRenderer, 'send'>;
webFrame: import('vs/base/parts/sandbox/electron-sandbox/electronTypes').WebFrame;
};
};

if (auxiliaryWindowCandidate?.vscode?.ipcRenderer && auxiliaryWindowCandidate?.vscode?.webFrame) {
return auxiliaryWindowCandidate.vscode;
}

return undefined;
}
20 changes: 18 additions & 2 deletions src/vs/base/parts/sandbox/electron-sandbox/preload-slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(function () {
'use strict';

const { ipcRenderer, contextBridge } = require('electron');
const { ipcRenderer, webFrame, contextBridge } = require('electron');

/**
* @param {string} channel
Expand All @@ -31,7 +31,6 @@
*
* @type {IpcRenderer}
*/

ipcRenderer: {

/**
Expand All @@ -43,6 +42,23 @@
ipcRenderer.send(channel, ...args);
}
}
},

/**
* Support for subset of methods of Electron's `webFrame` type.
*
* @type {import('./electronTypes').WebFrame}
*/
webFrame: {

/**
* @param {number} level
*/
setZoomLevel(level) {
if (typeof level === 'number') {
webFrame.setZoomLevel(level);
}
}
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/vs/platform/window/electron-sandbox/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
*--------------------------------------------------------------------------------------------*/

import { getZoomLevel, setZoomFactor, setZoomLevel } from 'vs/base/browser/browser';
import { webFrame } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { getWindows } from 'vs/base/browser/dom';
import { getGlobals } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { zoomLevelToZoomFactor } from 'vs/platform/window/common/window';

/**
* 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);
for (const window of getWindows()) {
getGlobals(window)?.webFrame?.setZoomLevel(zoomLevel);
}
setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
setZoomLevel(zoomLevel);
}
Expand Down
11 changes: 0 additions & 11 deletions src/vs/platform/windows/electron-main/auxiliaryWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

import { BrowserWindow, BrowserWindowConstructorOptions, WebContents } from 'electron';
import { FileAccess } from 'vs/base/common/network';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWindowSettings, zoomLevelToZoomFactor } from 'vs/platform/window/common/window';
import { defaultBrowserWindowOptions } from 'vs/platform/windows/electron-main/windows';

export class AuxiliaryWindow {
Expand All @@ -23,7 +21,6 @@ export class AuxiliaryWindow {

constructor(
private readonly contents: WebContents,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService
) {

Expand All @@ -33,14 +30,6 @@ export class AuxiliaryWindow {

private create(): void {

// Apply zoom level when DOM is ready
this.contents.on('dom-ready', () => {
const windowZoomLevel = this.configurationService.getValue<IWindowSettings | undefined>('window')?.zoomLevel ?? 0;

this.contents.setZoomLevel(windowZoomLevel);
this.contents.setZoomFactor(zoomLevelToZoomFactor(windowZoomLevel));
});

// Handle devtools argument
if (this.environmentMainService.args['open-devtools'] === true) {
this.contents.openDevTools({ mode: 'bottom' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IAuxiliaryWindow extends IDisposable {
layout(): void;
}

type AuxiliaryWindow = Window & typeof globalThis;
export type AuxiliaryWindow = Window & typeof globalThis;

export class BrowserAuxiliaryWindowService implements IAuxiliaryWindowService {

Expand Down Expand Up @@ -85,7 +85,7 @@ export class BrowserAuxiliaryWindowService implements IAuxiliaryWindowService {
}
}

private applyCSS(auxiliaryWindow: AuxiliaryWindow, disposables: DisposableStore): void {
protected applyCSS(auxiliaryWindow: AuxiliaryWindow, disposables: DisposableStore): void {

// Clone all style elements and stylesheet links from the window to the child window
for (const element of document.head.querySelectorAll('link[rel="stylesheet"], style')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,41 @@ import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { BrowserAuxiliaryWindowService, IAuxiliaryWindowService } from 'vs/workbench/services/auxiliaryWindow/browser/auxiliaryWindowService';
import { getGlobals } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWindowsConfiguration } from 'vs/platform/window/common/window';
import { DisposableStore } from 'vs/base/common/lifecycle';

type AuxiliaryWindow = Window & typeof globalThis & {
vscode: {
ipcRenderer: Pick<import('vs/base/parts/sandbox/electron-sandbox/electronTypes').IpcRenderer, 'send'>;
};
moveTop: () => void;
};

export function isAuxiliaryWindow(obj: unknown): obj is AuxiliaryWindow {
const candidate = obj as AuxiliaryWindow | undefined;

return typeof candidate?.vscode?.ipcRenderer?.send === 'function';
return typeof candidate?.moveTop === 'function';
}

export class NativeAuxiliaryWindowService extends BrowserAuxiliaryWindowService {

constructor(
@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
@IConfigurationService private readonly configurationService: IConfigurationService
) {
super(layoutService, environmentService);
}


protected override applyCSS(auxiliaryWindow: AuxiliaryWindow, disposables: DisposableStore): void {
super.applyCSS(auxiliaryWindow, disposables);

// Zoom level
const windowConfig = this.configurationService.getValue<IWindowsConfiguration>();
const windowZoomLevel = typeof windowConfig.window?.zoomLevel === 'number' ? windowConfig.window.zoomLevel : 0;
getGlobals(auxiliaryWindow)?.webFrame?.setZoomLevel(windowZoomLevel);
}

protected override patchMethods(auxiliaryWindow: AuxiliaryWindow): void {
super.patchMethods(auxiliaryWindow);

Expand All @@ -39,13 +51,13 @@ export class NativeAuxiliaryWindowService extends BrowserAuxiliaryWindowService
auxiliaryWindow.focus = function () {
originalWindowFocus();

auxiliaryWindow.vscode.ipcRenderer.send('vscode:focusAuxiliaryWindow');
getGlobals(auxiliaryWindow)?.ipcRenderer.send('vscode:focusAuxiliaryWindow');
};

// Add a method to move window to the top (TODO@bpasero better to go entirely through native host service)
Object.defineProperty(auxiliaryWindow, 'moveTop', {
value: () => {
auxiliaryWindow.vscode.ipcRenderer.send('vscode:moveAuxiliaryWindowTop');
getGlobals(auxiliaryWindow)?.ipcRenderer.send('vscode:moveAuxiliaryWindowTop');
},
writable: false,
enumerable: false,
Expand Down