Skip to content

Commit

Permalink
[menus] Restore the focus for context menu and main menu bar (fixes #481
Browse files Browse the repository at this point in the history
)

Signed-off-by: Sven Efftinge <[email protected]>
  • Loading branch information
svenefftinge committed Sep 21, 2017
1 parent f2ec970 commit 7d9e304
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/core/src/browser/menu/browser-context-menu-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ export class BrowserContextMenuRenderer implements ContextMenuRenderer {
render(path: string, anchor: Anchor, onHide?: () => void): void {
const contextMenu = this.menuFactory.createContextMenu(path);
const { x, y } = anchor instanceof MouseEvent ? { x: anchor.clientX, y: anchor.clientY } : anchor;
const previouslyActive = window.document.activeElement as HTMLElement;
contextMenu.aboutToClose.connect(() => {
previouslyActive.focus();
if (onHide) {
onHide();
}
});
if (onHide) {
contextMenu.aboutToClose.connect(() => onHide());
}
contextMenu.open(x, y);
}

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/browser/menu/browser-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class DynamicMenuBarWidget extends MenuBarWidget {
super['_openChildMenu']();
};
}

}
/**
* A menu widget that would recompute its items on update
Expand All @@ -115,6 +116,17 @@ class DynamicMenuWidget extends MenuWidget {
this.updateSubMenus(this, this.menu, this.options.commands);
}

public open(x: number, y: number, options?: MenuWidget.IOpenOptions): void {
// we want to restore the focus after the menu closes.
const previouslyActive = window.document.activeElement as HTMLElement;
const cb = () => {
previouslyActive.focus();
this.aboutToClose.disconnect(cb);
};
this.aboutToClose.connect(cb);
super.open(x, y, options);
}

private updateSubMenus(parent: MenuWidget, menu: CompositeMenuNode, commands: PhosphorCommandRegistry): void {
for (const item of menu.children) {
if (item instanceof CompositeMenuNode) {
Expand Down

0 comments on commit 7d9e304

Please sign in to comment.