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

Support ordering of sub menus #7963

Merged
merged 1 commit into from
Jun 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { ContainerModule } from 'inversify';
import { bindDynamicLabelProvider } from './label/sample-dynamic-label-provider-command-contribution';
import { bindSampleUnclosableView } from './view/sample-unclosable-view-contribution';
import { bindSampleOutputChannelWithSeverity } from './output/sample-output-channel-with-severity';
import { bindSampleMenu } from './menu/sample-menu-contribution';

export default new ContainerModule(bind => {
bindDynamicLabelProvider(bind);
bindSampleUnclosableView(bind);
bindSampleOutputChannelWithSeverity(bind);
bindSampleMenu(bind);
});
53 changes: 53 additions & 0 deletions examples/api-samples/src/browser/menu/sample-menu-contribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/********************************************************************************
* Copyright (C) 2020 TORO Limited and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Command, CommandContribution, CommandRegistry, MAIN_MENU_BAR, MenuContribution, MenuModelRegistry } from '@theia/core/lib/common';
import { injectable, interfaces } from 'inversify';

const SampleCommand: Command = {
id: 'sample-command',
label: 'Sample Command'
};

@injectable()
export class SampleCommandContribution implements CommandContribution {
registerCommands(commands: CommandRegistry): void {
commands.registerCommand(SampleCommand, {
execute: () => {
alert('This is a sample command!');
}
});
}

}

@injectable()
export class SampleMenuContribution implements MenuContribution {
registerMenus(menus: MenuModelRegistry): void {
const subMenuPath = [...MAIN_MENU_BAR, 'sample-menu'];
menus.registerSubmenu(subMenuPath, 'Sample Menu', {
order: '2' // that should put the menu right next to the File menu
});
menus.registerMenuAction(subMenuPath, {
commandId: SampleCommand.id
});
}
}

export const bindSampleMenu = (bind: interfaces.Bind) => {
bind(CommandContribution).to(SampleCommandContribution).inSingletonScope();
bind(MenuContribution).to(SampleMenuContribution).inSingletonScope();
};
28 changes: 21 additions & 7 deletions packages/core/src/common/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export namespace MenuAction {
}

export interface SubMenuOptions {
iconClass: string
iconClass?: string
order?: string
}

export type MenuPath = string[];
Expand Down Expand Up @@ -93,16 +94,21 @@ export class MenuModelRegistry {
const parent = this.findGroup(groupPath);
let groupNode = this.findSubMenu(parent, menuId);
if (!groupNode) {
groupNode = new CompositeMenuNode(menuId, label, options ? options.iconClass : undefined);
groupNode = new CompositeMenuNode(menuId, label, options);
return parent.addNode(groupNode);
} else {
if (!groupNode.label) {
groupNode.label = label;
} else if (groupNode.label !== label) {
throw new Error("The group '" + menuPath.join('/') + "' already has a different label.");
}
if (!groupNode.iconClass && options) {
groupNode.iconClass = options.iconClass;
if (options) {
if (!groupNode.iconClass) {
groupNode.iconClass = options.iconClass;
}
if (!groupNode.order) {
groupNode.order = options.order;
}
}
return { dispose: () => { } };
}
Expand Down Expand Up @@ -187,11 +193,19 @@ export interface MenuNode {

export class CompositeMenuNode implements MenuNode {
protected readonly _children: MenuNode[] = [];
public iconClass?: string;
public order?: string;

constructor(
public readonly id: string,
public label?: string,
public iconClass?: string
) { }
options?: SubMenuOptions
) {
if (options) {
this.iconClass = options.iconClass;
this.order = options.order;
}
}

get children(): ReadonlyArray<MenuNode> {
return this._children;
Expand Down Expand Up @@ -236,7 +250,7 @@ export class CompositeMenuNode implements MenuNode {
}

get sortString(): string {
return this.id;
return this.order || this.id;
}

get isSubmenu(): boolean {
Expand Down