Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent opening an empty menu #277

Merged
merged 3 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/example-dockpanel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
7 changes: 6 additions & 1 deletion packages/widgets/src/menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -607,7 +610,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);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/widgets/tests/src/menubar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down