From f338b68e686b82b4d33581a72457bb189568d021 Mon Sep 17 00:00:00 2001 From: hbcarlos Date: Wed, 29 Dec 2021 11:07:58 +0100 Subject: [PATCH 1/3] Prevent opening an empty menu --- examples/example-dockpanel/src/index.ts | 5 +++++ packages/widgets/src/menubar.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/example-dockpanel/src/index.ts b/examples/example-dockpanel/src/index.ts index 6c884c2e8..d236c7d4c 100644 --- a/examples/example-dockpanel/src/index.ts +++ b/examples/example-dockpanel/src/index.ts @@ -280,10 +280,15 @@ function main(): void { menu3.title.label = 'View'; menu3.title.mnemonic = 0; + let emptyMenu = new Menu({ commands }); + emptyMenu.title.label = 'Empty Menu'; + emptyMenu.title.mnemonic = 0; + let bar = new MenuBar(); bar.addMenu(menu1); bar.addMenu(menu2); bar.addMenu(menu3); + bar.addMenu(emptyMenu); bar.id = 'menuBar'; let palette = new CommandPalette({ commands }); diff --git a/packages/widgets/src/menubar.ts b/packages/widgets/src/menubar.ts index 25cd7eea8..7887cfd40 100644 --- a/packages/widgets/src/menubar.ts +++ b/packages/widgets/src/menubar.ts @@ -607,7 +607,9 @@ export class MenuBar extends Widget { let { left, bottom } = (itemNode as HTMLElement).getBoundingClientRect(); // Open the new menu at the computed location. - newMenu.open(left, bottom, this._forceItemsPosition); + if (newMenu.items.length > 0) { + newMenu.open(left, bottom, this._forceItemsPosition); + } } /** From a58217956a3839b27abfdd892d7499fee9849be8 Mon Sep 17 00:00:00 2001 From: hbcarlos Date: Wed, 29 Dec 2021 11:55:24 +0100 Subject: [PATCH 2/3] Prevent activating a menu if it is empty --- packages/widgets/src/menubar.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/widgets/src/menubar.ts b/packages/widgets/src/menubar.ts index 7887cfd40..0a88bfb10 100644 --- a/packages/widgets/src/menubar.ts +++ b/packages/widgets/src/menubar.ts @@ -405,6 +405,9 @@ export class MenuBar extends Widget { for (let i = 0, n = menus.length; i < n; ++i) { let title = menus[i].title; let active = i === activeIndex; + if (active && menus[i].items.length == 0) { + active = false; + } content[i] = renderer.renderItem({ title, active, From e26281b47d881692b8d08dff24e77a88d218f1a1 Mon Sep 17 00:00:00 2001 From: hbcarlos Date: Fri, 31 Dec 2021 12:52:51 +0100 Subject: [PATCH 3/3] Add tests --- packages/widgets/tests/src/menubar.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/widgets/tests/src/menubar.spec.ts b/packages/widgets/tests/src/menubar.spec.ts index be4d31107..bd81506f3 100644 --- a/packages/widgets/tests/src/menubar.spec.ts +++ b/packages/widgets/tests/src/menubar.spec.ts @@ -288,6 +288,16 @@ describe('@lumino/widgets', () => { expect(menu.isAttached).to.equal(false); bar.dispose(); }); + + it('should be a no-op if the active menu is empty', () => { + let bar = new MenuBar(); + let menu = new Menu({ commands }); + bar.addMenu(menu); + bar.activeMenu = menu; + bar.openActiveMenu(); + expect(menu.isAttached).to.equal(false); + bar.dispose(); + }); }); describe('#addMenu()', () => {