UNPKG

6.05 kBTypeScriptView Raw
1import { Disposable } from '../disposable';
2import { CommandRegistry, Command } from '../command';
3import { ContributionProvider } from '../contribution-provider';
4import { CompositeMenuNode } from './composite-menu-node';
5import { MenuAction, MenuNode, MenuPath, MutableCompoundMenuNode, SubMenuOptions } from './menu-types';
6export declare const MenuContribution: unique symbol;
7/**
8 * Representation of a menu contribution.
9 *
10 * Note that there are also convenience classes which combine multiple contributions into one.
11 * For example to register a view together with a menu and keybinding you could use
12 * {@link AbstractViewContribution} instead.
13 *
14 * ### Example usage
15 *
16 * ```ts
17 * import { MenuContribution, MenuModelRegistry, MAIN_MENU_BAR } from '@theia/core';
18 *
19 * @injectable()
20 * export class NewMenuContribution implements MenuContribution {
21 * registerMenus(menus: MenuModelRegistry): void {
22 * const menuPath = [...MAIN_MENU_BAR, '99_mymenu'];
23 * menus.registerSubmenu(menuPath, 'My Menu');
24 *
25 * menus.registerMenuAction(menuPath, {
26 * commandId: MyCommand.id,
27 * label: 'My Action'
28 * });
29 * }
30 * }
31 * ```
32 */
33export interface MenuContribution {
34 /**
35 * Registers menus.
36 * @param menus the menu model registry.
37 */
38 registerMenus(menus: MenuModelRegistry): void;
39}
40/**
41 * The MenuModelRegistry allows to register and unregister menus, submenus and actions
42 * via strings and {@link MenuAction}s without the need to access the underlying UI
43 * representation.
44 */
45export declare class MenuModelRegistry {
46 protected readonly contributions: ContributionProvider<MenuContribution>;
47 protected readonly commands: CommandRegistry;
48 protected readonly root: CompositeMenuNode;
49 protected readonly independentSubmenus: Map<string, MutableCompoundMenuNode>;
50 constructor(contributions: ContributionProvider<MenuContribution>, commands: CommandRegistry);
51 onStart(): void;
52 /**
53 * Adds the given menu action to the menu denoted by the given path.
54 *
55 * @returns a disposable which, when called, will remove the menu action again.
56 */
57 registerMenuAction(menuPath: MenuPath, item: MenuAction): Disposable;
58 /**
59 * Adds the given menu node to the menu denoted by the given path.
60 *
61 * @returns a disposable which, when called, will remove the menu node again.
62 */
63 registerMenuNode(menuPath: MenuPath | string, menuNode: MenuNode, group?: string): Disposable;
64 getMenuNode(menuPath: MenuPath | string, group?: string): MutableCompoundMenuNode;
65 /**
66 * Register a new menu at the given path with the given label.
67 * (If the menu already exists without a label, iconClass or order this method can be used to set them.)
68 *
69 * @param menuPath the path for which a new submenu shall be registered.
70 * @param label the label to be used for the new submenu.
71 * @param options optionally allows to set an icon class and specify the order of the new menu.
72 *
73 * @returns if the menu was successfully created a disposable will be returned which,
74 * when called, will remove the menu again. If the menu already existed a no-op disposable
75 * will be returned.
76 *
77 * Note that if the menu already existed and was registered with a different label an error
78 * will be thrown.
79 */
80 registerSubmenu(menuPath: MenuPath, label: string, options?: SubMenuOptions): Disposable;
81 registerIndependentSubmenu(id: string, label: string, options?: SubMenuOptions): Disposable;
82 linkSubmenu(parentPath: MenuPath | string, childId: string | MenuPath, options?: SubMenuOptions, group?: string): Disposable;
83 /**
84 * Unregister all menu nodes with the same id as the given menu action.
85 *
86 * @param item the item whose id will be used.
87 * @param menuPath if specified only nodes within the path will be unregistered.
88 */
89 unregisterMenuAction(item: MenuAction, menuPath?: MenuPath): void;
90 /**
91 * Unregister all menu nodes with the same id as the given command.
92 *
93 * @param command the command whose id will be used.
94 * @param menuPath if specified only nodes within the path will be unregistered.
95 */
96 unregisterMenuAction(command: Command, menuPath?: MenuPath): void;
97 /**
98 * Unregister all menu nodes with the given id.
99 *
100 * @param id the id which shall be removed.
101 * @param menuPath if specified only nodes within the path will be unregistered.
102 */
103 unregisterMenuAction(id: string, menuPath?: MenuPath): void;
104 /**
105 * Recurse all menus, removing any menus matching the `id`.
106 *
107 * @param id technical identifier of the `MenuNode`.
108 */
109 unregisterMenuNode(id: string): void;
110 /**
111 * Finds a submenu as a descendant of the `root` node.
112 * See {@link MenuModelRegistry.findSubMenu findSubMenu}.
113 */
114 protected findGroup(menuPath: MenuPath, options?: SubMenuOptions): MutableCompoundMenuNode;
115 /**
116 * Finds or creates a submenu as an immediate child of `current`.
117 * @throws if a node with the given `menuId` exists but is not a {@link MutableCompoundMenuNode}.
118 */
119 protected findSubMenu(current: MutableCompoundMenuNode, menuId: string, options?: SubMenuOptions): MutableCompoundMenuNode;
120 /**
121 * Returns the menu at the given path.
122 *
123 * @param menuPath the path specifying the menu to return. If not given the empty path will be used.
124 *
125 * @returns the root menu when `menuPath` is empty. If `menuPath` is not empty the specified menu is
126 * returned if it exists, otherwise an error is thrown.
127 */
128 getMenu(menuPath?: MenuPath): MutableCompoundMenuNode;
129 /**
130 * Returns the {@link MenuPath path} at which a given menu node can be accessed from this registry, if it can be determined.
131 * Returns `undefined` if the `parent` of any node in the chain is unknown.
132 */
133 getPath(node: MenuNode): MenuPath | undefined;
134}
135//# sourceMappingURL=menu-model-registry.d.ts.map
\No newline at end of file