Skip to content

Commit

Permalink
add auto save toggle (#11974)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 16, 2016
1 parent bf316a5 commit 906a01d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/vs/code/electron-main/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { IEnvService } from 'vs/code/electron-main/env';
import { ipcMain as ipc, app, shell, dialog, Menu, MenuItem } from 'electron';
import { IWindowsService } from 'vs/code/electron-main/windows';
import { IPath, VSCodeWindow } from 'vs/code/electron-main/window';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IStorageService } from 'vs/code/electron-main/storage';
import { IFilesConfiguration, AutoSaveConfiguration } from 'vs/platform/files/common/files';
import { IUpdateService, State as UpdateState } from 'vs/code/electron-main/update-manager';
import { Keybinding } from 'vs/base/common/keybinding';
import product from 'vs/platform/product';
Expand Down Expand Up @@ -46,6 +48,8 @@ export class VSCodeMenu {

private static MAX_MENU_RECENT_ENTRIES = 10;

private currentAutoSaveSetting: string;

private isQuitting: boolean;
private appMenuInstalled: boolean;

Expand All @@ -57,13 +61,17 @@ export class VSCodeMenu {
constructor(
@IStorageService private storageService: IStorageService,
@IUpdateService private updateService: IUpdateService,
@IConfigurationService private configurationService: IConfigurationService,
@IWindowsService private windowsService: IWindowsService,
@IEnvService private envService: IEnvService
) {
this.actionIdKeybindingRequests = [];

this.mapResolvedKeybindingToActionId = Object.create(null);
this.mapLastKnownKeybindingToActionId = this.storageService.getItem<{ [id: string]: string; }>(VSCodeMenu.lastKnownKeybindingsMapStorageKey) || Object.create(null);

const config = configurationService.getConfiguration<IFilesConfiguration>();
this.currentAutoSaveSetting = config && config.files && config.files.autoSave;
}

public ready(): void {
Expand Down Expand Up @@ -119,10 +127,21 @@ export class VSCodeMenu {
}
});

// Update when auto save config changes
this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config));

// Listen to update service
this.updateService.on('change', () => this.updateMenu());
}

private onConfigurationUpdated(config: IFilesConfiguration): void {
const newAutoSaveSetting = config && config.files && config.files.autoSave;
if (newAutoSaveSetting !== this.currentAutoSaveSetting) {
this.currentAutoSaveSetting = newAutoSaveSetting;
this.updateMenu();
}
}

private resolveKeybindings(win: VSCodeWindow): void {
if (this.keybindingsResolved) {
return; // only resolve once
Expand Down Expand Up @@ -290,6 +309,8 @@ export class VSCodeMenu {
const saveFileAs = this.createMenuItem(nls.localize({ key: 'miSaveAs', comment: ['&& denotes a mnemonic'] }, "Save &&As..."), 'workbench.action.files.saveAs', this.windowsService.getWindowCount() > 0);
const saveAllFiles = this.createMenuItem(nls.localize({ key: 'miSaveAll', comment: ['&& denotes a mnemonic'] }, "Save A&&ll"), 'workbench.action.files.saveAll', this.windowsService.getWindowCount() > 0);

const autoSave = new MenuItem({ label: mnemonicLabel(nls.localize('miAutoSave', "Auto Save")), type: 'checkbox', checked: this.currentAutoSaveSetting !== AutoSaveConfiguration.OFF, enabled: this.windowsService.getWindowCount() > 0, click: () => this.windowsService.sendToFocused('vscode.toggleAutoSave') });

const preferences = this.getPreferencesMenu();

const newWindow = new MenuItem({ label: mnemonicLabel(nls.localize({ key: 'miNewWindow', comment: ['&& denotes a mnemonic'] }, "&&New Window")), accelerator: this.getAccelerator('workbench.action.newWindow'), click: () => this.windowsService.openNewWindow() });
Expand All @@ -314,6 +335,8 @@ export class VSCodeMenu {
saveFileAs,
saveAllFiles,
__separator__(),
autoSave,
__separator__(),
!platform.isMacintosh ? preferences : null,
!platform.isMacintosh ? __separator__() : null,
revertFile,
Expand Down
29 changes: 26 additions & 3 deletions src/vs/workbench/electron-browser/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {ICommandService} from 'vs/platform/commands/common/commands';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {IWorkspaceContextService}from 'vs/platform/workspace/common/workspace';
import {IWindowService} from 'vs/workbench/services/window/electron-browser/windowService';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {AutoSaveConfiguration} from 'vs/platform/files/common/files';
import {IConfigurationEditingService, ConfigurationTarget} from 'vs/workbench/services/configuration/common/configurationEditing';
import {IWorkspaceConfigurationService} from 'vs/workbench/services/configuration/common/configuration';
import {ElectronWindow} from 'vs/workbench/electron-browser/window';
import * as browser from 'vs/base/browser/browser';
import {DiffEditorInput, toDiffLabel} from 'vs/workbench/common/editor/diffEditorInput';
Expand Down Expand Up @@ -50,14 +52,17 @@ const TextInputActions: IAction[] = [

export class ElectronIntegration {

private static AUTO_SAVE_SETTING = 'files.autoSave';

constructor(
@IInstantiationService private instantiationService: IInstantiationService,
@IWindowService private windowService: IWindowService,
@IPartService private partService: IPartService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@ITelemetryService private telemetryService: ITelemetryService,
@IConfigurationService private configurationService: IConfigurationService,
@IWorkspaceConfigurationService private configurationService: IWorkspaceConfigurationService,
@ICommandService private commandService: ICommandService,
@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
@IKeybindingService private keybindingService: IKeybindingService,
@IMessageService private messageService: IMessageService,
@IContextMenuService private contextMenuService: IContextMenuService,
Expand Down Expand Up @@ -119,6 +124,11 @@ export class ElectronIntegration {
this.messageService.show(Severity.Info, message);
});

// Support toggling auto save
ipc.on('vscode.toggleAutoSave', (event) => {
this.toggleAutoSave();
});

// Ensure others can listen to zoom level changes
browser.setZoomLevel(webFrame.getZoomLevel());

Expand Down Expand Up @@ -258,4 +268,17 @@ export class ElectronIntegration {
return input;
});
}
}

private toggleAutoSave(): void {
const userAutoSaveConfig = this.configurationService.lookup(ElectronIntegration.AUTO_SAVE_SETTING).user;

let newAutoSaveValue: string;
if (userAutoSaveConfig === AutoSaveConfiguration.OFF) {
newAutoSaveValue = AutoSaveConfiguration.AFTER_DELAY;
} else {
newAutoSaveValue = AutoSaveConfiguration.OFF;
}

this.configurationEditingService.writeConfiguration(ConfigurationTarget.USER, { key: ElectronIntegration.AUTO_SAVE_SETTING, value: newAutoSaveValue }).done(null, (error) => this.messageService.show(Severity.Error, error));
}
}

0 comments on commit 906a01d

Please sign in to comment.