diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index ec8aa7ffa9920c8d6d694c71aebfae12bea9584d..f65ac67814763197630bfc857337e731b84ce1ce 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -1212,6 +1212,12 @@ declare namespace vscode { * A short title like 'Retry', 'Open Log' etc. */ title: string; + + /** + * Indicates that this item replaces the default + * 'Close' action. + */ + isCloseAffordance?: boolean; } /** diff --git a/src/vs/workbench/api/node/extHostMessageService.ts b/src/vs/workbench/api/node/extHostMessageService.ts index 66fefacbca03f9f0cc0ff210449f791a80607bfa..d7440a3a6c074066ea658729a973d4e3c0d03b42 100644 --- a/src/vs/workbench/api/node/extHostMessageService.ts +++ b/src/vs/workbench/api/node/extHostMessageService.ts @@ -22,14 +22,15 @@ export class ExtHostMessageService { showMessage(severity: Severity, message: string, commands: (string|vscode.MessageItem)[]): Thenable { - const items: { title: string; handle: number; }[] = []; + const items: { title: string; isCloseAffordance: boolean; handle: number; }[] = []; for (let handle = 0; handle < commands.length; handle++) { let command = commands[handle]; if (typeof command === 'string') { - items.push({ title: command, handle }); + items.push({ title: command, handle, isCloseAffordance: false }); } else { - items.push({ title: command.title, handle }); + let {title, isCloseAffordance} = command; + items.push({ title, isCloseAffordance, handle }); } } @@ -50,23 +51,29 @@ export class MainThreadMessageService { this._messageService = messageService; } - $showMessage(severity: Severity, message: string, commands: { title: string; handle: number;}[]): Thenable { + $showMessage(severity: Severity, message: string, commands: { title: string; isCloseAffordance: boolean; handle: number;}[]): Thenable { let hide: (handle?: number) => void; let actions: Action[] = []; - - actions.push(new Action('__close', nls.localize('close', "Close"), undefined, true, () => { - hide(); - return Promise.as(undefined); - })); + let hasCloseAffordance = false; commands.forEach(command => { + if (command.isCloseAffordance === true) { + hasCloseAffordance = true; + } actions.push(new Action('_extension_message_handle_' + command.handle, command.title, undefined, true, () => { hide(command.handle); return Promise.as(undefined); })); }); + if (!hasCloseAffordance) { + actions.unshift(new Action('__close', nls.localize('close', "Close"), undefined, true, () => { + hide(); + return Promise.as(undefined); + })); + } + return new Promise((c) => { let messageHide: Function; diff --git a/src/vs/workbench/test/node/api/extHostMessagerService.test.ts b/src/vs/workbench/test/node/api/extHostMessagerService.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..50ff62358ae70e0b921d6906157a38d35200a0c6 --- /dev/null +++ b/src/vs/workbench/test/node/api/extHostMessagerService.test.ts @@ -0,0 +1,72 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import * as assert from 'assert'; +import {Action} from 'vs/base/common/actions'; +import {MainThreadMessageService} from 'vs/workbench/api/node/extHostMessageService'; + +suite('ExtHostMessageService', function () { + + test('propagte handle on select', function () { + + let service = new MainThreadMessageService({ + show(sev: number, m: { message; actions: Action[] }) { + assert.equal(m.actions.length, 1); + setImmediate(() => m.actions[0].run()); + return () => { }; + } + }); + + return service.$showMessage(1, 'h', [{ handle: 42, title: 'a thing', isCloseAffordance: true }]).then(handle => { + assert.equal(handle, 42); + }); + }); + + test('isCloseAffordance', function () { + + let actions: Action[]; + let service = new MainThreadMessageService({ + show(sev: number, m: { message; actions: Action[] }) { + actions = m.actions; + } + }); + + // default close action + service.$showMessage(1, '', [{ title: 'a thing', isCloseAffordance: false, handle: 0 }]); + assert.equal(actions.length, 2); + let [first, second] = actions; + assert.equal(first.label, 'Close'); + assert.equal(second.label, 'a thing'); + + // override close action + service.$showMessage(1, '', [{ title: 'a thing', isCloseAffordance: true, handle: 0 }]); + assert.equal(actions.length, 1); + first = actions[0]; + assert.equal(first.label, 'a thing'); + }); + + test('hide on select', function () { + + let actions: Action[]; + let c: number; + let service = new MainThreadMessageService({ + show(sev: number, m: { message; actions: Action[] }) { + c = 0; + actions = m.actions; + return () => { + c += 1; + }; + } + }); + + service.$showMessage(1, '', [{ title: 'a thing', isCloseAffordance: true, handle: 0 }]); + assert.equal(actions.length, 1); + + actions[0].run(); + assert.equal(c, 1); + }); +});