提交 684af006 编写于 作者: B Benjamin Pasero

debt - use dialog service over bare windows service

上级 8d0228f5
......@@ -39,7 +39,7 @@ import { OS } from 'vs/base/common/platform';
import { IRange } from 'vs/editor/common/core/range';
import { ITextModel } from 'vs/editor/common/model';
import { INotificationService, INotification, INotificationHandle, NoOpNotification, PromptOption } from 'vs/platform/notification/common/notification';
import { IConfirmation, IConfirmationResult, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IConfirmation, IConfirmationResult, IDialogService, IDialogOptions } from 'vs/platform/dialogs/common/dialogs';
import { IPosition, Position as Pos } from 'vs/editor/common/core/position';
export class SimpleEditor implements IEditor {
......@@ -258,7 +258,7 @@ export class SimpleDialogService implements IDialogService {
return TPromise.wrap(window.confirm(messageText));
}
public show(severity: Severity, message: string, buttons: string[], cancelId?: number): TPromise<number> {
public show(severity: Severity, message: string, buttons: string[], options?: IDialogOptions): TPromise<number> {
return TPromise.as(0);
}
}
......
......@@ -41,6 +41,11 @@ export interface IConfirmationResult {
export const IDialogService = createDecorator<IDialogService>('dialogService');
export interface IDialogOptions {
cancelId?: number;
detail?: string;
}
/**
* A service to bring up modal dialogs.
*
......@@ -63,7 +68,7 @@ export interface IDialogService {
* then a promise with index of `cancelId` option is returned. If there is no such
* option then promise with index `0` is returned.
*/
show(severity: Severity, message: string, buttons: string[], cancelId?: number): TPromise<number>;
show(severity: Severity, message: string, buttons: string[], options?: IDialogOptions): TPromise<number>;
}
const MAX_CONFIRM_FILES = 10;
......
......@@ -194,7 +194,7 @@ export class ExtensionManagementService extends Disposable implements IExtension
nls.localize('override', "Override"),
nls.localize('cancel', "Cancel")
];
return this.dialogService.show(Severity.Info, message, buttons, 1)
return this.dialogService.show(Severity.Info, message, buttons, { cancelId: 1 })
.then<boolean>(value => {
if (value === 0) {
return this.uninstall(newer, true).then(() => true);
......@@ -545,7 +545,7 @@ export class ExtensionManagementService extends Disposable implements IExtension
nls.localize('cancel', "Cancel")
];
this.logService.info('Requesting for confirmation to uninstall extension with dependencies', extension.identifier.id);
return this.dialogService.show(Severity.Info, message, buttons, 2)
return this.dialogService.show(Severity.Info, message, buttons, { cancelId: 2 })
.then<void>(value => {
if (value === 0) {
return this.uninstallWithDependencies(extension, [], installed);
......@@ -570,7 +570,7 @@ export class ExtensionManagementService extends Disposable implements IExtension
nls.localize('cancel', "Cancel")
];
this.logService.info('Requesting for confirmation to uninstall extension', extension.identifier.id);
return this.dialogService.show(Severity.Info, message, buttons, 1)
return this.dialogService.show(Severity.Info, message, buttons, { cancelId: 1 })
.then<void>(value => {
if (value === 0) {
return this.uninstallWithDependencies(extension, [], installed);
......
......@@ -119,7 +119,7 @@ export class MainThreadMessageService implements MainThreadMessageServiceShape {
cancelId = buttons.length - 1;
}
return this._dialogService.show(severity, message, buttons, cancelId)
return this._dialogService.show(severity, message, buttons, { cancelId })
.then(result => result === commands.length ? undefined : commands[result].handle);
}
}
......@@ -105,7 +105,7 @@ class InstallAction extends Action {
return new TPromise<void>((c, e) => {
const buttons = [nls.localize('ok', "OK"), nls.localize('cancel2', "Cancel")];
this.dialogService.show(Severity.Info, nls.localize('warnEscalation', "Code will now prompt with 'osascript' for Administrator privileges to install the shell command."), buttons, 1).then(choice => {
this.dialogService.show(Severity.Info, nls.localize('warnEscalation', "Code will now prompt with 'osascript' for Administrator privileges to install the shell command."), buttons, { cancelId: 1 }).then(choice => {
switch (choice) {
case 0 /* OK */:
const command = 'osascript -e "do shell script \\"mkdir -p /usr/local/bin && chown \\" & (do shell script (\\"whoami\\")) & \\" /usr/local/bin\\" with administrator privileges"';
......
......@@ -986,7 +986,7 @@ export class DebugService implements debug.IDebugService {
private showError(message: string, actions: IAction[] = []): TPromise<any> {
const configureAction = this.instantiationService.createInstance(debugactions.ConfigureAction, debugactions.ConfigureAction.ID, debugactions.ConfigureAction.LABEL);
actions.push(configureAction);
return this.dialogService.show(severity.Error, message, actions.map(a => a.label).concat(nls.localize('cancel', "Cancel")), actions.length).then(choice => {
return this.dialogService.show(severity.Error, message, actions.map(a => a.label).concat(nls.localize('cancel', "Cancel")), { cancelId: actions.length }).then(choice => {
if (choice < actions.length) {
return actions[choice].run();
}
......
......@@ -691,7 +691,7 @@ export class ExtensionsWorkbenchService implements IExtensionsWorkbenchService {
nls.localize('enable', "Yes"),
nls.localize('doNotEnable', "No")
];
return this.dialogService.show(Severity.Info, message, buttons, 1)
return this.dialogService.show(Severity.Info, message, buttons, { cancelId: 1 })
.then<void>(value => {
if (value === 0) {
return this.checkAndSetEnablement(extension, dependencies, enablementState, enable);
......@@ -707,7 +707,7 @@ export class ExtensionsWorkbenchService implements IExtensionsWorkbenchService {
nls.localize('disableAll', "All"),
nls.localize('cancel', "Cancel")
];
return this.dialogService.show(Severity.Info, message, buttons, 2)
return this.dialogService.show(Severity.Info, message, buttons, { cancelId: 2 })
.then<void>(value => {
if (value === 0) {
return this.checkAndSetEnablement(extension, [], enablementState, enable);
......
......@@ -316,8 +316,7 @@ export class UpdateContribution implements IGlobalActivity {
this.dialogService.show(
severity.Info,
nls.localize('noUpdatesAvailable', "There are currently no updates available."),
[nls.localize('ok', "OK")],
0
[nls.localize('ok', "OK")]
);
}
......
......@@ -12,7 +12,7 @@ import Severity from 'vs/base/common/severity';
import { isLinux, isMacintosh } from 'vs/base/common/platform';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { IDialogService, IConfirmation, IConfirmationResult } from 'vs/platform/dialogs/common/dialogs';
import { IDialogService, IConfirmation, IConfirmationResult, IDialogOptions } from 'vs/platform/dialogs/common/dialogs';
interface IMassagedMessageBoxOptions {
......@@ -87,10 +87,16 @@ export class DialogService implements IDialogService {
return opts;
}
public show(severity: Severity, message: string, buttons: string[], cancelId?: number): TPromise<number> {
public show(severity: Severity, message: string, buttons: string[], dialogOptions?: IDialogOptions): TPromise<number> {
const type: 'none' | 'info' | 'error' | 'question' | 'warning' = severity === Severity.Info ? 'question' : severity === Severity.Error ? 'error' : severity === Severity.Warning ? 'warning' : 'none';
const { options, buttonIndexMap } = this.massageMessageBoxOptions({ message, buttons, type, cancelId });
const { options, buttonIndexMap } = this.massageMessageBoxOptions({
message,
buttons,
type,
cancelId: dialogOptions ? dialogOptions.cancelId : void 0,
detail: dialogOptions ? dialogOptions.detail : void 0
});
return this.windowService.showMessageBox(options).then(result => buttonIndexMap[result.button]);
}
......
......@@ -9,7 +9,7 @@ import nls = require('vs/nls');
import { TPromise } from 'vs/base/common/winjs.base';
import paths = require('vs/base/common/paths');
import strings = require('vs/base/common/strings');
import { isWindows, isLinux } from 'vs/base/common/platform';
import { isWindows } from 'vs/base/common/platform';
import URI from 'vs/base/common/uri';
import { ConfirmResult } from 'vs/workbench/common/editor';
import { TextFileService as AbstractTextFileService } from 'vs/workbench/services/textfile/common/textFileService';
......@@ -21,7 +21,6 @@ import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IModeService } from 'vs/editor/common/services/modeService';
import { createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel';
import product from 'vs/platform/node/product';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
......@@ -31,7 +30,8 @@ import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IModelService } from 'vs/editor/common/services/modelService';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { getConfirmMessage } from 'vs/platform/dialogs/common/dialogs';
import { getConfirmMessage, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { Severity } from 'vs/editor/common/standalone/standaloneBase';
export class TextFileService extends AbstractTextFileService {
......@@ -50,7 +50,8 @@ export class TextFileService extends AbstractTextFileService {
@IBackupFileService backupFileService: IBackupFileService,
@IWindowsService windowsService: IWindowsService,
@IHistoryService historyService: IHistoryService,
@IContextKeyService contextKeyService: IContextKeyService
@IContextKeyService contextKeyService: IContextKeyService,
@IDialogService private dialogService: IDialogService
) {
super(lifecycleService, contextService, configurationService, fileService, untitledEditorService, instantiationService, notificationService, environmentService, backupFileService, windowsService, historyService, contextKeyService, modelService);
}
......@@ -84,39 +85,22 @@ export class TextFileService extends AbstractTextFileService {
const message = resourcesToConfirm.length === 1 ? nls.localize('saveChangesMessage', "Do you want to save the changes you made to {0}?", paths.basename(resourcesToConfirm[0].fsPath))
: getConfirmMessage(nls.localize('saveChangesMessages', "Do you want to save the changes to the following {0} files?", resourcesToConfirm.length), resourcesToConfirm);
// Button order
// Windows: Save | Don't Save | Cancel
// Mac: Save | Cancel | Don't Save
// Linux: Don't Save | Cancel | Save
const save = { label: resourcesToConfirm.length > 1 ? mnemonicButtonLabel(nls.localize({ key: 'saveAll', comment: ['&& denotes a mnemonic'] }, "&&Save All")) : mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")), result: ConfirmResult.SAVE };
const dontSave = { label: mnemonicButtonLabel(nls.localize({ key: 'dontSave', comment: ['&& denotes a mnemonic'] }, "Do&&n't Save")), result: ConfirmResult.DONT_SAVE };
const cancel = { label: nls.localize('cancel', "Cancel"), result: ConfirmResult.CANCEL };
const buttons: { label: string; result: ConfirmResult; }[] = [];
if (isWindows) {
buttons.push(save, dontSave, cancel);
} else if (isLinux) {
buttons.push(dontSave, cancel, save);
} else {
buttons.push(save, cancel, dontSave);
}
const opts: Electron.MessageBoxOptions = {
title: product.nameLong,
message,
type: 'warning',
detail: nls.localize('saveChangesDetail', "Your changes will be lost if you don't save them."),
buttons: buttons.map(b => b.label),
noLink: true,
cancelId: buttons.indexOf(cancel)
};
if (isLinux) {
opts.defaultId = 2;
}
return this.windowService.showMessageBox(opts).then(result => buttons[result.button].result);
const buttons: string[] = [
resourcesToConfirm.length > 1 ? mnemonicButtonLabel(nls.localize({ key: 'saveAll', comment: ['&& denotes a mnemonic'] }, "&&Save All")) : mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")),
mnemonicButtonLabel(nls.localize({ key: 'dontSave', comment: ['&& denotes a mnemonic'] }, "Do&&n't Save")),
nls.localize('cancel', "Cancel")
];
return this.dialogService.show(Severity.Warning, message, buttons, {
cancelId: 2,
detail: nls.localize('saveChangesDetail', "Your changes will be lost if you don't save them.")
}).then(index => {
switch (index) {
case 0: return ConfirmResult.SAVE;
case 1: return ConfirmResult.DONT_SAVE;
default: return ConfirmResult.CANCEL;
}
});
}
public promptForPath(defaultPath: string): TPromise<string> {
......
......@@ -14,7 +14,7 @@ import { ICommandService } from 'vs/platform/commands/common/commands';
const emptyDialogService = new class implements IDialogService {
_serviceBrand: 'dialogService';
show(severity, message, options): never {
show(severity, message, buttons): never {
throw new Error('not implemented');
}
......@@ -93,11 +93,11 @@ suite('ExtHostMessageService', function () {
suite('modal', () => {
test('calls dialog service', () => {
const service = new MainThreadMessageService(null, emptyNotificationService, emptyCommandService, {
show(severity, message, options) {
show(severity, message, buttons) {
assert.equal(severity, 1);
assert.equal(message, 'h');
assert.equal(options.length, 2);
assert.equal(options[1], 'Cancel');
assert.equal(buttons.length, 2);
assert.equal(buttons[1], 'Cancel');
return Promise.as(0);
}
} as IDialogService, null);
......@@ -109,7 +109,7 @@ suite('ExtHostMessageService', function () {
test('returns undefined when cancelled', () => {
const service = new MainThreadMessageService(null, emptyNotificationService, emptyCommandService, {
show(severity, message, options) {
show(severity, message, buttons) {
return Promise.as(1);
}
} as IDialogService, null);
......@@ -121,8 +121,8 @@ suite('ExtHostMessageService', function () {
test('hides Cancel button when not needed', () => {
const service = new MainThreadMessageService(null, emptyNotificationService, emptyCommandService, {
show(severity, message, options) {
assert.equal(options.length, 1);
show(severity, message, buttons) {
assert.equal(buttons.length, 1);
return Promise.as(0);
}
} as IDialogService, null);
......
......@@ -62,7 +62,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService';
import { ITextBufferFactory, DefaultEndOfLine, EndOfLinePreference } from 'vs/editor/common/model';
import { Range } from 'vs/editor/common/core/range';
import { IConfirmation, IConfirmationResult, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IConfirmation, IConfirmationResult, IDialogService, IDialogOptions } from 'vs/platform/dialogs/common/dialogs';
import { INotificationService, INotificationHandle, INotification, NoOpNotification, PromptOption } from 'vs/platform/notification/common/notification';
export function createFileInput(instantiationService: IInstantiationService, resource: URI): FileEditorInput {
......@@ -340,7 +340,7 @@ export class TestDialogService implements IDialogService {
return TPromise.as({ confirmed: false });
}
public show(severity: Severity, message: string, buttons: string[], cancelId?: number): Promise<number, any> {
public show(severity: Severity, message: string, buttons: string[], options?: IDialogOptions): Promise<number, any> {
return TPromise.as(0);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册