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

Move MenuBar index check to an overridable method #733

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions packages/widgets/src/menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,8 @@ export class MenuBar extends Widget {
* If the menu cannot be activated, the index will be set to `-1`.
*/
set activeIndex(value: number) {
// Adjust the value for an out of range index.
if (value < 0 || value >= this._menus.length) {
value = -1;
}

// An empty menu cannot be active
if (value > -1 && this._menus[value].items.length === 0) {
// Adjust the value for an invalid index.
if (!this.isValidIndex(value)) {
value = -1;
}

Expand All @@ -162,6 +157,20 @@ export class MenuBar extends Widget {
this.update();
}

/**
* Before setting a new index, this method is used to validate it.
*
* By default it checks whether the index is within menu range
* and whether the corresponding menu has at least one item.
*/
protected isValidIndex(index: number): boolean {
return (
index >= 0 &&
index < this._menus.length &&
this._menus[index].items.length > 0
);
}

/**
* A read-only array of the menus in the menu bar.
*/
Expand Down
23 changes: 23 additions & 0 deletions packages/widgets/tests/src/menubar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ describe('@lumino/widgets', () => {
expect(bar.activeIndex).to.equal(-1);
bar.dispose();
});

it('should allow adopting the validation check', () => {
class AllowEmptyMenusBar extends MenuBar {
protected override isValidIndex(index: number): boolean {
return index >= 0 && index < this.menus.length;
}
}
const bar = new AllowEmptyMenusBar();
let emptyMenu = new Menu({ commands });
bar.insertMenu(1, emptyMenu);

// check that empty menu can be active
bar.activeIndex = 1;
expect(bar.activeIndex).to.equal(1);

// other indices should still be disallowed
bar.activeIndex = -1;
expect(bar.activeIndex).to.equal(-1);
bar.activeIndex = 2;
expect(bar.activeIndex).to.equal(-1);

bar.dispose();
});
});

describe('#menus', () => {
Expand Down
1 change: 1 addition & 0 deletions review/api/widgets.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ export class MenuBar extends Widget {
dispose(): void;
handleEvent(event: Event): void;
insertMenu(index: number, menu: Menu, update?: boolean): void;
protected isValidIndex(index: number): boolean;
get menus(): ReadonlyArray<Menu>;
protected onActivateRequest(msg: Message): void;
protected onAfterDetach(msg: Message): void;
Expand Down
Loading