diff --git a/src/vs/platform/actions/common/actions.ts b/src/vs/platform/actions/common/actions.ts index ad304c341b84c4cbf7cc6200751715c0c274a3ba..e2a4e3f4848cd3e4c8f3e8d2ad802c55219d5824 100644 --- a/src/vs/platform/actions/common/actions.ts +++ b/src/vs/platform/actions/common/actions.ts @@ -11,7 +11,7 @@ import { IConstructorSignature2, createDecorator } from 'vs/platform/instantiati import { IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { ICommandService } from 'vs/platform/commands/common/commands'; -import { IDisposable } from 'vs/base/common/lifecycle'; +import { IDisposable, dispose } from 'vs/base/common/lifecycle'; import { Event } from 'vs/base/common/event'; import { URI, UriComponents } from 'vs/base/common/uri'; @@ -233,11 +233,12 @@ export class SubmenuItemAction extends Action { export class MenuItemAction extends ExecuteCommandAction { - private _options: IMenuActionOptions; - readonly item: ICommandAction; readonly alt: MenuItemAction; + private _options: IMenuActionOptions; + private _contextListener: IDisposable; + constructor( item: ICommandAction, alt: ICommandAction, @@ -252,6 +253,19 @@ export class MenuItemAction extends ExecuteCommandAction { this.item = item; this.alt = alt ? new MenuItemAction(alt, undefined, this._options, contextKeyService, commandService) : undefined; + + if (item.precondition) { + const preconditionKeysSet = new Set(); + for (let key of item.precondition.keys()) { + preconditionKeysSet.add(key); + } + + this._contextListener = contextKeyService.onDidChangeContext(event => { + if (event.affectsSome(preconditionKeysSet)) { + this._setEnabled(contextKeyService.contextMatchesRules(item.precondition)); + } + }); + } } run(...args: any[]): TPromise { @@ -267,6 +281,12 @@ export class MenuItemAction extends ExecuteCommandAction { return super.run(...runArgs); } + + dispose(): void { + this._contextListener = dispose(this._contextListener); + + super.dispose(); + } } export class SyncActionDescriptor {