Skip to content

Commit

Permalink
Preferences: Support Window.zoomLevel Preference
Browse files Browse the repository at this point in the history
Fixes eclipse-theia#8751

What it does

- allows user to specify window zoom level as a preference

How to test

Signed-off-by: seantan22 <[email protected]>
  • Loading branch information
seantan22 committed Feb 17, 2021
1 parent f3a15db commit 3802aad
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { KeybindingContribution, KeybindingRegistry } from '../../browser';
import { FrontendApplication, FrontendApplicationContribution, CommonMenus } from '../../browser';
import { ElectronMainMenuFactory } from './electron-main-menu-factory';
import { FrontendApplicationStateService, FrontendApplicationState } from '../../browser/frontend-application-state';
import { WindowPreferences } from '../window/window-preferences';

export namespace ElectronCommands {
export const TOGGLE_DEVELOPER_TOOLS: Command = {
Expand Down Expand Up @@ -71,6 +72,9 @@ export class ElectronMenuContribution implements FrontendApplicationContribution
@inject(FrontendApplicationStateService)
protected readonly stateService: FrontendApplicationStateService;

@inject(WindowPreferences)
protected readonly windowPreferences: WindowPreferences;

constructor(
@inject(ElectronMainMenuFactory) protected readonly factory: ElectronMainMenuFactory
) { }
Expand Down Expand Up @@ -155,16 +159,22 @@ export class ElectronMenuContribution implements FrontendApplicationContribution
execute: () => {
const webContents = currentWindow.webContents;
webContents.setZoomLevel(webContents.zoomLevel + 0.5);
console.log(`ZOOM LEVEL = ${webContents.zoomLevel}`);
}
});
registry.registerCommand(ElectronCommands.ZOOM_OUT, {
execute: () => {
const webContents = currentWindow.webContents;
webContents.setZoomLevel(webContents.zoomLevel - 0.5);
console.log(`ZOOM LEVEL = ${webContents.zoomLevel}`);
}
});
registry.registerCommand(ElectronCommands.RESET_ZOOM, {
execute: () => currentWindow.webContents.setZoomLevel(0)
execute: () => {
const webContents = currentWindow.webContents;
webContents.setZoomLevel(0);
console.log(`ZOOM LEVEL = ${webContents.zoomLevel}`);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import { ElectronClipboardService } from '../electron-clipboard-service';
import { ClipboardService } from '../../browser/clipboard-service';
import { ElectronMainWindowService, electronMainWindowServicePath } from '../../electron-common/electron-main-window-service';
import { ElectronIpcConnectionProvider } from '../messaging/electron-ipc-connection-provider';
import { bindWindowPreferences } from './window-preferences';

export default new ContainerModule(bind => {
bind(ElectronMainWindowService).toDynamicValue(context =>
ElectronIpcConnectionProvider.createProxy(context.container, electronMainWindowServicePath)
).inSingletonScope();
bindWindowPreferences(bind);
bind(WindowService).to(ElectronWindowService).inSingletonScope();
bind(FrontendApplicationContribution).toService(WindowService);
bind(ClipboardService).to(ElectronClipboardService).inSingletonScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as electron from 'electron';
import { injectable, inject } from 'inversify';
import { remote } from 'electron';
import { NewWindowOptions } from '../../browser/window/window-service';
import { DefaultWindowService } from '../../browser/window/default-window-service';
import { ElectronMainWindowService } from '../../electron-common/electron-main-window-service';
import { WindowConfiguration, WindowPreferences } from './window-preferences';
import { PreferenceChangeEvent } from '../../browser';

@injectable()
export class ElectronWindowService extends DefaultWindowService {
Expand All @@ -33,9 +36,17 @@ export class ElectronWindowService extends DefaultWindowService {
*/
protected closeOnUnload: boolean = false;

/**
* Do not update window zoom level if preference is unchanged.
*/
protected prevPreferredZoomLevel: number | undefined;

@inject(ElectronMainWindowService)
protected readonly delegate: ElectronMainWindowService;

@inject(WindowPreferences)
protected readonly windowPreferences: WindowPreferences;

openNewWindow(url: string, { external }: NewWindowOptions = {}): undefined {
this.delegate.openNewWindow(url, { external });
return undefined;
Expand Down Expand Up @@ -67,6 +78,14 @@ export class ElectronWindowService extends DefaultWindowService {
return this.preventUnload(event);
}
});

// Update changes to zoom level.
this.updateWindowZoomLevel();
this.windowPreferences.onPreferenceChanged((e: PreferenceChangeEvent<WindowConfiguration>) => {
if (e.affects('window.zoomLevel')) {
this.updateWindowZoomLevel();
}
});
}

/**
Expand All @@ -85,4 +104,22 @@ export class ElectronWindowService extends DefaultWindowService {
});
return response === 0; // 'Yes', close the window.
}

/**
* Updates the window zoom level based on the amount set in `Preferences`.
*/
protected updateWindowZoomLevel(): void {
const preferredZoomLevel = this.windowPreferences['window.zoomLevel'];
if (this.prevPreferredZoomLevel === preferredZoomLevel) {
return;
}
this.prevPreferredZoomLevel = preferredZoomLevel;

const currentWindow = electron.remote.getCurrentWindow();
const webContents = currentWindow.webContents;

if (webContents.getZoomLevel() !== preferredZoomLevel) {
webContents.setZoomLevel(preferredZoomLevel);
}
}
}
50 changes: 50 additions & 0 deletions packages/core/src/electron-browser/window/window-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/********************************************************************************
* Copyright (C) 2021 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { interfaces } from 'inversify';
import { createPreferenceProxy, PreferenceContribution, PreferenceProxy, PreferenceSchema, PreferenceService } from '../../browser/preferences';

export const windowPreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
'window.zoomLevel': {
'type': 'number',
'default': 0,
'minimum': -8,
'maximum': 9,
'description': 'Adjust the zoom level of the window. The original size is 0. Each increment above (e.g. 1) or below (e.g. -1) represents zooming 20% larger or smaller.'
},
}
};

export class WindowConfiguration {
'window.zoomLevel': number;
}

export const WindowPreferences = Symbol('WindowPreferences');
export type WindowPreferences = PreferenceProxy<WindowConfiguration>;

export function createWindowPreferences(preferences: PreferenceService): WindowPreferences {
return createPreferenceProxy(preferences, windowPreferencesSchema);
}

export function bindWindowPreferences(bind: interfaces.Bind): void {
bind(WindowPreferences).toDynamicValue(ctx => {
const preferences = ctx.container.get<PreferenceService>(PreferenceService);
return createWindowPreferences(preferences);
}).inSingletonScope();

bind(PreferenceContribution).toConstantValue({ schema: windowPreferencesSchema });
}

0 comments on commit 3802aad

Please sign in to comment.