From 19556f4d90c1b661ba53caea9b6a035a714e112d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=A4der?= Date: Thu, 5 Sep 2024 14:58:15 +0200 Subject: [PATCH] Fix selection of contributed menu action argument adapters (#14132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #14072 Contributed on behalf of STMicroelectronics Signed-off-by: Thomas Mäder om> --- .../tab-bar-toolbar-menu-adapters.ts | 2 +- .../tab-bar-toolbar-registry.ts | 5 +++-- .../tab-bar-toolbar/tab-bar-toolbar-types.ts | 4 ++++ .../shell/tab-bar-toolbar/tab-bar-toolbar.tsx | 4 ++-- .../menus/plugin-menu-command-adapter.ts | 22 ++++++++++++++++++- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-menu-adapters.ts b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-menu-adapters.ts index 76edc12e0a1f9..261fbd4bbf9f5 100644 --- a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-menu-adapters.ts +++ b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-menu-adapters.ts @@ -20,7 +20,7 @@ import { NAVIGATION, RenderedToolbarItem } from './tab-bar-toolbar-types'; export const TOOLBAR_WRAPPER_ID_SUFFIX = '-as-tabbar-toolbar-item'; export class ToolbarMenuNodeWrapper implements RenderedToolbarItem { - constructor(protected readonly menuNode: MenuNode, readonly group?: string, readonly menuPath?: MenuPath) { } + constructor(protected readonly menuNode: MenuNode, readonly group: string | undefined, readonly delegateMenuPath: MenuPath, readonly menuPath?: MenuPath) { } get id(): string { return this.menuNode.id + TOOLBAR_WRAPPER_ID_SUFFIX; } get command(): string { return this.menuNode.command ?? ''; }; get icon(): string | undefined { return this.menuNode.icon; } diff --git a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-registry.ts b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-registry.ts index 5851830895b23..e10afb4a0c09e 100644 --- a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-registry.ts +++ b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-registry.ts @@ -116,11 +116,12 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution { for (const grandchild of child.children) { if (!grandchild.when || this.contextKeyService.match(grandchild.when, widget.node)) { const menuPath = this.menuRegistry.getPath(grandchild); - result.push(new ToolbarMenuNodeWrapper(grandchild, child.id, menuPath)); + result.push(new ToolbarMenuNodeWrapper(grandchild, child.id, delegate.menuPath, menuPath)); } } } else if (child.command) { - result.push(new ToolbarMenuNodeWrapper(child, '')); + const menuPath = this.menuRegistry.getPath(child); + result.push(new ToolbarMenuNodeWrapper(child, undefined, delegate.menuPath, menuPath)); } } } diff --git a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-types.ts b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-types.ts index e59f9f63c2384..c9db6e3b18027 100644 --- a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-types.ts +++ b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar-types.ts @@ -73,6 +73,10 @@ export interface TabBarToolbarItemBase { * If no command is present, this menu will be opened. */ menuPath?: MenuPath; + /** + * The path of the menu delegate that contributed this toolbar item + */ + delegateMenuPath?: MenuPath; contextKeyOverlays?: Record; /** * Optional ordering string for placing the item within its group diff --git a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar.tsx b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar.tsx index 6d4f21b3d3264..e5c65095477b0 100644 --- a/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar.tsx +++ b/packages/core/src/browser/shell/tab-bar-toolbar/tab-bar-toolbar.tsx @@ -405,8 +405,8 @@ export class TabBarToolbar extends ReactWidget { return; } - if (item.command && item.menuPath) { - this.menuCommandExecutor.executeCommand(item.menuPath, item.command, this.current); + if (item.command && item.delegateMenuPath) { + this.menuCommandExecutor.executeCommand(item.delegateMenuPath, item.command, this.current); } else if (item.command) { this.commands.executeCommand(item.command, this.current); } else if (item.menuPath) { diff --git a/packages/plugin-ext/src/main/browser/menus/plugin-menu-command-adapter.ts b/packages/plugin-ext/src/main/browser/menus/plugin-menu-command-adapter.ts index ec91bc9ef7c95..9e79ae892cd96 100644 --- a/packages/plugin-ext/src/main/browser/menus/plugin-menu-command-adapter.ts +++ b/packages/plugin-ext/src/main/browser/menus/plugin-menu-command-adapter.ts @@ -169,7 +169,27 @@ export class PluginMenuCommandAdapter implements MenuCommandAdapter { } protected getArgumentAdapterForMenu(menuPath: MenuPath): ArgumentAdapter | undefined { - return this.argumentAdapters.get(menuPath.join(this.separator)); + let result; + let length = 0; + for (const [key, value] of this.argumentAdapters.entries()) { + const candidate = key.split(this.separator); + if (this.isPrefixOf(candidate, menuPath) && candidate.length > length) { + result = value; + length = candidate.length; + } + } + return result; + } + isPrefixOf(candidate: string[], menuPath: MenuPath): boolean { + if (candidate.length > menuPath.length) { + return false; + } + for (let i = 0; i < candidate.length; i++) { + if (candidate[i] !== menuPath[i]) { + return false; + } + } + return true; } protected addArgumentAdapter(menuPath: MenuPath, adapter: ArgumentAdapter): void {