mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-26 03:29:00 +01:00
implement modal message API
This commit is contained in:
@@ -255,6 +255,18 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
|
||||
}
|
||||
};
|
||||
|
||||
const emptyMessageOptions: vscode.MessageOptions = Object.create(null);
|
||||
|
||||
function parseMessageArguments(args: any[]): { options: vscode.MessageOptions; items: any[]; } {
|
||||
const [first, ...rest] = args;
|
||||
|
||||
if (first && (typeof first === 'string' || first.title)) {
|
||||
return { options: emptyMessageOptions, items: args };
|
||||
} else {
|
||||
return { options: first || emptyMessageOptions, items: rest };
|
||||
}
|
||||
}
|
||||
|
||||
// namespace: window
|
||||
const window: typeof vscode.window = {
|
||||
get activeTextEditor() {
|
||||
@@ -287,14 +299,17 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
|
||||
onDidCloseTerminal(listener, thisArg?, disposables?) {
|
||||
return extHostTerminalService.onDidCloseTerminal(listener, thisArg, disposables);
|
||||
},
|
||||
showInformationMessage(message, ...items) {
|
||||
return extHostMessageService.showMessage(Severity.Info, message, items);
|
||||
showInformationMessage(message, ...args) {
|
||||
const { options, items } = parseMessageArguments(args);
|
||||
return extHostMessageService.showMessage(Severity.Info, message, options, items);
|
||||
},
|
||||
showWarningMessage(message, ...items) {
|
||||
return extHostMessageService.showMessage(Severity.Warning, message, items);
|
||||
showWarningMessage(message, ...args) {
|
||||
const { options, items } = parseMessageArguments(args);
|
||||
return extHostMessageService.showMessage(Severity.Warning, message, options, items);
|
||||
},
|
||||
showErrorMessage(message, ...items) {
|
||||
return extHostMessageService.showMessage(Severity.Error, message, items);
|
||||
showErrorMessage(message, ...args) {
|
||||
const { options, items } = parseMessageArguments(args);
|
||||
return extHostMessageService.showMessage(Severity.Error, message, options, items);
|
||||
},
|
||||
showQuickPick(items: any, options: vscode.QuickPickOptions, token?: vscode.CancellationToken) {
|
||||
return extHostQuickOpen.showQuickPick(items, options, token);
|
||||
@@ -325,7 +340,7 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
|
||||
},
|
||||
// proposed API
|
||||
sampleFunction: proposedApiFunction(extension, () => {
|
||||
return extHostMessageService.showMessage(Severity.Info, 'Hello Proposed Api!', []);
|
||||
return extHostMessageService.showMessage(Severity.Info, 'Hello Proposed Api!', {}, []);
|
||||
}),
|
||||
registerTreeExplorerNodeProvider: proposedApiFunction(extension, (providerId: string, provider: vscode.TreeExplorerNodeProvider<any>) => {
|
||||
return extHostExplorers.registerTreeExplorerNodeProvider(providerId, provider);
|
||||
|
||||
@@ -176,7 +176,7 @@ export abstract class MainThreadLanguagesShape {
|
||||
}
|
||||
|
||||
export abstract class MainThreadMessageServiceShape {
|
||||
$showMessage(severity: Severity, message: string, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> { throw ni(); }
|
||||
$showMessage(severity: Severity, message: string, options: vscode.MessageOptions, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> { throw ni(); }
|
||||
}
|
||||
|
||||
export abstract class MainThreadOutputServiceShape {
|
||||
|
||||
@@ -17,7 +17,7 @@ export class ExtHostMessageService {
|
||||
this._proxy = threadService.get(MainContext.MainThreadMessageService);
|
||||
}
|
||||
|
||||
showMessage(severity: Severity, message: string, commands: (string | vscode.MessageItem)[]): Thenable<string | vscode.MessageItem> {
|
||||
showMessage(severity: Severity, message: string, options: vscode.MessageOptions, commands: (string | vscode.MessageItem)[]): Thenable<string | vscode.MessageItem> {
|
||||
|
||||
const items: { title: string; isCloseAffordance: boolean; handle: number; }[] = [];
|
||||
|
||||
@@ -33,7 +33,7 @@ export class ExtHostMessageService {
|
||||
}
|
||||
}
|
||||
|
||||
return this._proxy.$showMessage(severity, message, items).then(handle => {
|
||||
return this._proxy.$showMessage(severity, message, options, items).then(handle => {
|
||||
if (typeof handle === 'number') {
|
||||
return commands[handle];
|
||||
}
|
||||
|
||||
@@ -5,22 +5,31 @@
|
||||
'use strict';
|
||||
|
||||
import nls = require('vs/nls');
|
||||
import { IMessageService } from 'vs/platform/message/common/message';
|
||||
import { IMessageService, IChoiceService } from 'vs/platform/message/common/message';
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import { Action } from 'vs/base/common/actions';
|
||||
import { TPromise as Promise } from 'vs/base/common/winjs.base';
|
||||
import { MainThreadMessageServiceShape } from './extHost.protocol';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export class MainThreadMessageService extends MainThreadMessageServiceShape {
|
||||
|
||||
private _messageService: IMessageService;
|
||||
|
||||
constructor( @IMessageService messageService: IMessageService) {
|
||||
constructor(
|
||||
@IMessageService private _messageService: IMessageService,
|
||||
@IChoiceService private _choiceService: IChoiceService
|
||||
) {
|
||||
super();
|
||||
this._messageService = messageService;
|
||||
}
|
||||
|
||||
$showMessage(severity: Severity, message: string, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> {
|
||||
$showMessage(severity: Severity, message: string, options: vscode.MessageOptions, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> {
|
||||
if (options.modal) {
|
||||
return this.showModalMessage(severity, message, commands);
|
||||
} else {
|
||||
return this.showMessage(severity, message, commands);
|
||||
}
|
||||
}
|
||||
|
||||
private showMessage(severity: Severity, message: string, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> {
|
||||
|
||||
return new Promise<number>(resolve => {
|
||||
|
||||
@@ -58,4 +67,27 @@ export class MainThreadMessageService extends MainThreadMessageServiceShape {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private showModalMessage(severity: Severity, message: string, commands: { title: string; isCloseAffordance: boolean; handle: number; }[]): Thenable<number> {
|
||||
let closeAffordanceIndex = -1;
|
||||
|
||||
const options = commands.map((command, index) => {
|
||||
if (command.isCloseAffordance === true) {
|
||||
closeAffordanceIndex = index;
|
||||
}
|
||||
|
||||
return command.title;
|
||||
});
|
||||
|
||||
if (closeAffordanceIndex === -1) {
|
||||
if (options.length > 0) {
|
||||
options.push(nls.localize('cancel', "Cancel"));
|
||||
} else {
|
||||
options.push(nls.localize('ok', "OK"));
|
||||
}
|
||||
}
|
||||
|
||||
return this._choiceService.choose(severity, message, options, true)
|
||||
.then(result => result === commands.length ? undefined : commands[result].handle);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user