From b4df3add54303fd60d199c0f64e8b7d65868db1c Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 29 Jun 2016 15:19:23 +0200 Subject: [PATCH] reuse action and update resource since editor title area also reuses the actions, fixes #8317 --- .../platform/actions/browser/menuService.ts | 21 +++++++++---------- src/vs/platform/actions/common/actions.ts | 19 ++++++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/vs/platform/actions/browser/menuService.ts b/src/vs/platform/actions/browser/menuService.ts index ea6eacf367c..3e08735b50b 100644 --- a/src/vs/platform/actions/browser/menuService.ts +++ b/src/vs/platform/actions/browser/menuService.ts @@ -93,7 +93,7 @@ export class MenuService implements IMenuService { } } -type MenuItemGroup = [string, IMenuItem[]]; +type MenuItemGroup = [string, MenuItemAction[]]; class Menu implements IMenu { @@ -121,7 +121,7 @@ class Menu implements IMenu { group = [groupName, []]; this._menuGroups.push(group); } - group[1].push(item); + group[1].push(new MenuItemAction(item, this._keybindingService)); // keep keys for eventing Menu._fillInKbExprKeys(item.when, keysFilter); @@ -151,19 +151,18 @@ class Menu implements IMenu { } getActions(): [string, IAction[]][] { - const result: [string, IAction[]][] = []; + const result: MenuItemGroup[] = []; for (let group of this._menuGroups) { - const [id, items] = group; - const actions: IAction[] = []; - for (let item of items) { - if (this._keybindingService.contextMatchesRules(item.when)) { - actions.push(new MenuItemAction(item, - this._keybindingService.getContextValue(ResourceContextKey.Resource), - this._keybindingService)); + const [id, actions] = group; + const activeActions: MenuItemAction[] = []; + for (let action of actions) { + if (this._keybindingService.contextMatchesRules(action.item.when)) { + action.resource = this._keybindingService.getContextValue(ResourceContextKey.Resource); + activeActions.push(action); } } if (actions.length > 0) { - result.push([id, actions]); + result.push([id, activeActions]); } } return result; diff --git a/src/vs/platform/actions/common/actions.ts b/src/vs/platform/actions/common/actions.ts index fc8bd27ca11..4879ce0559f 100644 --- a/src/vs/platform/actions/common/actions.ts +++ b/src/vs/platform/actions/common/actions.ts @@ -61,9 +61,10 @@ export class MenuItemAction extends Actions.Action { return result; } + private _resource: URI; + constructor( private _item: IMenuItem, - private _resource: URI, @IKeybindingService private _keybindingService: IKeybindingService ) { super(MenuItemAction._getMenuItemId(_item), _item.command.title); @@ -71,6 +72,18 @@ export class MenuItemAction extends Actions.Action { this.order = 100000; //TODO@Ben order is menu item property, not an action property } + set resource(value: URI) { + this._resource = value; + } + + get resource() { + return this._resource; + } + + get item(): IMenuItem { + return this._item; + } + get command() { return this._item.command; } @@ -79,10 +92,6 @@ export class MenuItemAction extends Actions.Action { return this._item.alt; } - get selectedCommand() { - return this.command; - } - run(alt: boolean) { const {id} = alt === true && this._item.alt || this._item.command; return this._keybindingService.executeCommand(id, this._resource);