Skip to content

Commit

Permalink
Add option to remove anchor arg from context menus
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Grant committed Jan 12, 2022
1 parent dd900be commit 88fb763
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
id: mainMenuId,
iconClass: 'codicon codicon-menu',
title: nls.localizeByDefault('Application Menu'),
menuPath: ['menubar'],
menuPath: MAIN_MENU_BAR,
order: 0,
});
} else {
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/browser/context-menu-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ export abstract class ContextMenuRenderer {
}

export interface RenderContextMenuOptions {
menuPath: MenuPath
anchor: Anchor
args?: any[]
onHide?: () => void
menuPath: MenuPath;
anchor: Anchor;
args?: any[];
/**
* Whether the anchor should be passed as an argument to the handlers of commands for this context menu.
* If true, the anchor will be appended to the list of arguments or passed as the only argument if no other
* arguments are supplied.
* Default is `true`.
*/
includeAnchorArg?: boolean;
onHide?: () => void;
}
export namespace RenderContextMenuOptions {
export function resolve(menuPathOrOptions: MenuPath | RenderContextMenuOptions, anchor?: Anchor, onHide?: () => void): RenderContextMenuOptions {
Expand All @@ -110,7 +117,10 @@ export namespace RenderContextMenuOptions {
menuPath = menuPathOrOptions.menuPath;
anchor = menuPathOrOptions.anchor;
onHide = menuPathOrOptions.onHide;
args = menuPathOrOptions.args ? [...menuPathOrOptions.args, anchor] : [anchor];
args = menuPathOrOptions.args ? menuPathOrOptions.args.slice() : [];
if (menuPathOrOptions.includeAnchorArg !== false) {
args.push(anchor);
}
}
return {
menuPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class SidebarBottomMenuWidget extends SidebarMenuWidget {
const button = e.currentTarget.getBoundingClientRect();
this.contextMenuRenderer.render({
menuPath,
includeAnchorArg: false,
anchor: {
x: button.left + button.width,
y: button.top + button.height,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/browser/shell/sidebar-menu-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class SidebarMenuWidget extends ReactWidget {
return;
}
this.menus.push(menu);
this.menus.sort((a, b) => a.order - b.order);
this.update();
}

Expand All @@ -73,6 +74,7 @@ export class SidebarMenuWidget extends ReactWidget {
const button = e.currentTarget.getBoundingClientRect();
this.contextMenuRenderer.render({
menuPath,
includeAnchorArg: false,
anchor: {
x: button.left + button.width,
y: button.top,
Expand All @@ -82,7 +84,7 @@ export class SidebarMenuWidget extends ReactWidget {

protected render(): React.ReactNode {
return <React.Fragment>
{this.menus.sort((a, b) => a.order - b.order).map(menu => <i
{this.menus.map(menu => <i
key={menu.id}
className={menu.iconClass}
title={menu.title}
Expand Down

0 comments on commit 88fb763

Please sign in to comment.