Skip to content

Commit

Permalink
Merge pull request microsoft#868 from Microsoft/ben/persistent-zoom
Browse files Browse the repository at this point in the history
Persist zoom settings
  • Loading branch information
bpasero committed Dec 1, 2015
2 parents d0751f6 + df346e4 commit bbeb32a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 29 deletions.
66 changes: 52 additions & 14 deletions src/vs/workbench/electron-browser/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import {Promise} from 'vs/base/common/winjs.base';
import {Promise, TPromise} from 'vs/base/common/winjs.base';
import timer = require('vs/base/common/timer');
import paths = require('vs/base/common/paths');
import {Action} from 'vs/base/common/actions';
Expand All @@ -15,10 +15,12 @@ import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/edito
import nls = require('vs/nls');
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IThreadService} from 'vs/platform/thread/common/thread';
import {IWindowConfiguration} from 'vs/workbench/electron-browser/window';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOpenService';
import {INullService} from 'vs/platform/instantiation/common/instantiation';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';

import ipc = require('ipc');
import remote = require('remote');
Expand Down Expand Up @@ -155,37 +157,73 @@ export class ZoomInAction extends Action {
}
}

export class ZoomOutAction extends Action {
export abstract class BaseZoomAction extends Action {

constructor(
id: string,
label: string,
@IConfigurationService private configurationService: IConfigurationService
) {
super(id, label);
}

public run(): Promise {
return Promise.as(false); // Subclass to implement
}

protected loadConfiguredZoomLevel(): TPromise<number> {
return this.configurationService.loadConfiguration().then((windowConfig: IWindowConfiguration) => {
if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
return windowConfig.window.zoomLevel;
}

return 0; // default
});
}
}

export class ZoomOutAction extends BaseZoomAction {

public static ID = 'workbench.action.zoomOut';
public static LABEL = nls.localize('zoomOut', "Zoom out");

constructor(id: string, label: string, @INullService ns) {
super(id, label);
constructor(
id: string,
label: string,
@IConfigurationService configurationService: IConfigurationService
) {
super(id, label, configurationService);
}

public run(): Promise {
if (webFrame.getZoomLevel() > 0) {
webFrame.setZoomLevel(webFrame.getZoomLevel() - 1); // prevent zoom out below 0 for now because it results in blurryness
}
return this.loadConfiguredZoomLevel().then(level => {
let newZoomLevelCandiate = webFrame.getZoomLevel() - 1;
if (newZoomLevelCandiate < level) {
newZoomLevelCandiate = level; // do not allow to zoom below the configured level
}

return Promise.as(true);
webFrame.setZoomLevel(newZoomLevelCandiate);
});
}
}

export class ZoomResetAction extends Action {
export class ZoomResetAction extends BaseZoomAction {

public static ID = 'workbench.action.zoomReset';
public static LABEL = nls.localize('zoomReset', "Reset Zoom");

constructor(id: string, label: string, @INullService ns) {
super(id, label);
constructor(
id: string,
label: string,
@IConfigurationService configurationService: IConfigurationService
) {
super(id, label, configurationService);
}

public run(): Promise {
webFrame.setZoomLevel(0);

return Promise.as(true);
return this.loadConfiguredZoomLevel().then(level => {
webFrame.setZoomLevel(level);
});
}
}

Expand Down
37 changes: 22 additions & 15 deletions src/vs/workbench/electron-browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@

<!-- Startup Code -->
<script type="text/javascript">
var webFrame = require('web-frame');

var mainStarted = false;
var args = parseURLQueryArgs();
var configuration = JSON.parse(args['config']);
Expand All @@ -108,9 +110,27 @@

registerListeners(enableDeveloperTools);

// disable pinch zoom
require('web-frame').setZoomLevelLimits(1, 1);
// We get the global settings through a remote call from the browser
// because its value can change dynamically.
var globalSettings;
var globalSettingsValue = remote.getGlobal('globalSettingsValue');
if (globalSettingsValue) {
globalSettings = JSON.parse(globalSettingsValue);
} else {
globalSettings = {
settings: {},
keybindings: []
};
}

// disable pinch zoom & apply zoom level early to avoid glitches
var windowConfiguration = globalSettings.settings && globalSettings.settings.window;
webFrame.setZoomLevelLimits(1, 1);
if (windowConfiguration && typeof windowConfiguration.zoomLevel === 'number' && windowConfiguration.zoomLevel !== 0) {
webFrame.setZoomLevel(windowConfiguration.zoomLevel);
}

// Load the loader and start loading the workbench
var rootUrl = uriFromPath(configuration.appRoot) + '/out';
createScript(rootUrl + '/vs/loader.js', function() {
require.config({
Expand Down Expand Up @@ -153,19 +173,6 @@
], function() {
timers.afterLoad = new Date();

// We get the global settings through a remote call from the browser
// because its value can change dynamically.
var globalSettings;
var globalSettingsValue = remote.getGlobal('globalSettingsValue');
if (globalSettingsValue) {
globalSettings = JSON.parse(globalSettingsValue);
} else {
globalSettings = {
settings: {},
keybindings: []
};
}

var main = require('vs/workbench/electron-browser/main');
main.startup(configuration, globalSettings).then(function() {
mainStarted = true;
Expand Down
18 changes: 18 additions & 0 deletions src/vs/workbench/electron-browser/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {IWorkspaceContextService}from 'vs/workbench/services/workspace/common/contextService';
import {IWindowService}from 'vs/workbench/services/window/electron-browser/windowService';
import {IWindowConfiguration} from 'vs/workbench/electron-browser/window';
import {IConfigurationService, IConfigurationServiceEvent, ConfigurationServiceEventTypes} from 'vs/platform/configuration/common/configuration';

import win = require('vs/workbench/electron-browser/window');

import remote = require('remote');
import ipc = require('ipc');
import webFrame = require('web-frame');

export class ElectronIntegration {

Expand All @@ -31,6 +34,7 @@ export class ElectronIntegration {
@IPartService private partService: IPartService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@ITelemetryService private telemetryService: ITelemetryService,
@IConfigurationService private configurationService: IConfigurationService,
@IKeybindingService private keybindingService: IKeybindingService,
@IStorageService private storageService: IStorageService,
@IMessageService private messageService: IMessageService
Expand Down Expand Up @@ -97,6 +101,20 @@ export class ElectronIntegration {
ipc.on('vscode:changeTheme', (theme:string) => {
this.storageService.store('workbench.theme', theme, StorageScope.GLOBAL);
});

// Configuration changes
this.configurationService.addListener(ConfigurationServiceEventTypes.UPDATED, (e: IConfigurationServiceEvent) => {
let windowConfig: IWindowConfiguration = e.config;

let newZoomLevel = 0;
if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
newZoomLevel = windowConfig.window.zoomLevel;
}

if (webFrame.getZoomLevel() !== newZoomLevel) {
webFrame.setZoomLevel(newZoomLevel);
}
});
}

private resolveKeybindings(actionIds: string[]): TPromise<{ id: string; binding: number; }[]> {
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/electron-browser/main.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ configurationRegistry.registerConfiguration({
'enum': ['none', 'one', 'all'],
'default': 'one',
'description': nls.localize('reopenFolders', "Controls how folders are being reopened after a restart. Select 'none' to never reopen a folder, 'one' to reopen the last folder you worked on or 'all' to reopen all folders of your last session.")
},
'window.zoomLevel': {
'type': 'number',
'default': 0,
'description': nls.localize('zoomLevel', "Adjust the zoom level of the window. The original size is 0 and each increment above or below represents zooming 20% larger or smaller.")
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/electron-browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export interface IPath {
columnNumber?: number;
}

export interface IMainConfiguration {
window: {

}
}

export interface IMainEnvironment extends IEnvironment {
workspacePath?: string;
autoSaveDelay?: number;
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/electron-browser/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import { IServiceCtor, isServiceEvent } from 'vs/base/common/service';
import { connect, Client } from 'vs/base/node/service.net';
import { IExtensionsService } from 'vs/workbench/parts/extensions/common/extensions';
import { ExtensionsService } from 'vs/workbench/parts/extensions/node/extensionsService';
import webFrame = require('web-frame');

/**
* This ugly beast is needed because at the point when we need shared services
Expand Down
8 changes: 8 additions & 0 deletions src/vs/workbench/electron-browser/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import ipc = require('ipc');
const Shell = remote.require('shell');
const Dialog = remote.require('dialog');

export interface IWindowConfiguration {
window: {
openFilesInNewWindow: boolean;
reopenFolders: string;
zoomLevel: number;
}
}

export class ElectronWindow {
private win: remote.BrowserWindow;

Expand Down

0 comments on commit bbeb32a

Please sign in to comment.