UNPKG

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