UNPKG

8.7 kBTypeScriptView Raw
1import { Disposable } from './disposable';
2import { CommandRegistry, Command } from './command';
3import { ContributionProvider } from './contribution-provider';
4/**
5 * A menu entry representing an action, e.g. "New File".
6 */
7export interface MenuAction {
8 /**
9 * The command to execute.
10 */
11 commandId: string;
12 /**
13 * In addition to the mandatory command property, an alternative command can be defined.
14 * It will be shown and invoked when pressing Alt while opening a menu.
15 */
16 alt?: string;
17 /**
18 * A specific label for this action. If not specified the command label or command id will be used.
19 */
20 label?: string;
21 /**
22 * Icon class(es). If not specified the icon class associated with the specified command
23 * (i.e. `command.iconClass`) will be used if it exists.
24 */
25 icon?: string;
26 /**
27 * Menu entries are sorted in ascending order based on their `order` strings. If omitted the determined
28 * label will be used instead.
29 */
30 order?: string;
31 /**
32 * Optional expression which will be evaluated by the {@link ContextKeyService} to determine visibility
33 * of the action, e.g. `resourceLangId == markdown`.
34 */
35 when?: string;
36}
37export declare namespace MenuAction {
38 function is(arg: MenuAction | any): arg is MenuAction;
39}
40/**
41 * Additional options when creating a new submenu.
42 */
43export interface SubMenuOptions {
44 /**
45 * The class to use for the submenu icon.
46 */
47 iconClass?: string;
48 /**
49 * Menu entries are sorted in ascending order based on their `order` strings. If omitted the determined
50 * label will be used instead.
51 */
52 order?: string;
53}
54export declare type MenuPath = string[];
55export declare const MAIN_MENU_BAR: MenuPath;
56export declare const SETTINGS_MENU: MenuPath;
57export declare const ACCOUNTS_MENU: MenuPath;
58export declare const ACCOUNTS_SUBMENU: string[];
59export declare const MenuContribution: unique symbol;
60/**
61 * Representation of a menu contribution.
62 *
63 * Note that there are also convenience classes which combine multiple contributions into one.
64 * For example to register a view together with a menu and keybinding you could use
65 * {@link AbstractViewContribution} instead.
66 *
67 * ### Example usage
68 *
69 * ```ts
70 * import { MenuContribution, MenuModelRegistry, MAIN_MENU_BAR } from '@theia/core';
71 *
72 * @injectable()
73 * export class NewMenuContribution implements MenuContribution {
74 * registerMenus(menus: MenuModelRegistry): void {
75 * const menuPath = [...MAIN_MENU_BAR, '99_mymenu'];
76 * menus.registerSubmenu(menuPath, 'My Menu');
77 *
78 * menus.registerMenuAction(menuPath, {
79 * commandId: MyCommand.id,
80 * label: 'My Action'
81 * });
82 * }
83 * }
84 * ```
85 */
86export interface MenuContribution {
87 /**
88 * Registers menus.
89 * @param menus the menu model registry.
90 */
91 registerMenus(menus: MenuModelRegistry): void;
92}
93/**
94 * The MenuModelRegistry allows to register and unregister menus, submenus and actions
95 * via strings and {@link MenuAction}s without the need to access the underlying UI
96 * representation.
97 */
98export declare class MenuModelRegistry {
99 protected readonly contributions: ContributionProvider<MenuContribution>;
100 protected readonly commands: CommandRegistry;
101 protected readonly root: CompositeMenuNode;
102 constructor(contributions: ContributionProvider<MenuContribution>, commands: CommandRegistry);
103 onStart(): void;
104 /**
105 * Adds the given menu action to the menu denoted by the given path.
106 *
107 * @returns a disposable which, when called, will remove the menu action again.
108 */
109 registerMenuAction(menuPath: MenuPath, item: MenuAction): Disposable;
110 /**
111 * Adds the given menu node to the menu denoted by the given path.
112 *
113 * @returns a disposable which, when called, will remove the menu node again.
114 */
115 registerMenuNode(menuPath: MenuPath, menuNode: MenuNode): Disposable;
116 /**
117 * Register a new menu at the given path with the given label.
118 * (If the menu already exists without a label, iconClass or order this method can be used to set them.)
119 *
120 * @param menuPath the path for which a new submenu shall be registered.
121 * @param label the label to be used for the new submenu.
122 * @param options optionally allows to set an icon class and specify the order of the new menu.
123 *
124 * @returns if the menu was successfully created a disposable will be returned which,
125 * when called, will remove the menu again. If the menu already existed a no-op disposable
126 * will be returned.
127 *
128 * Note that if the menu already existed and was registered with a different label an error
129 * will be thrown.
130 */
131 registerSubmenu(menuPath: MenuPath, label: string, options?: SubMenuOptions): Disposable;
132 /**
133 * Unregister all menu nodes with the same id as the given menu action.
134 *
135 * @param item the item whose id will be used.
136 * @param menuPath if specified only nodes within the path will be unregistered.
137 */
138 unregisterMenuAction(item: MenuAction, menuPath?: MenuPath): void;
139 /**
140 * Unregister all menu nodes with the same id as the given command.
141 *
142 * @param command the command whose id will be used.
143 * @param menuPath if specified only nodes within the path will be unregistered.
144 */
145 unregisterMenuAction(command: Command, menuPath?: MenuPath): void;
146 /**
147 * Unregister all menu nodes with the given id.
148 *
149 * @param id the id which shall be removed.
150 * @param menuPath if specified only nodes within the path will be unregistered.
151 */
152 unregisterMenuAction(id: string, menuPath?: MenuPath): void;
153 /**
154 * Recurse all menus, removing any menus matching the `id`.
155 *
156 * @param id technical identifier of the `MenuNode`.
157 */
158 unregisterMenuNode(id: string): void;
159 protected findGroup(menuPath: MenuPath, options?: SubMenuOptions): CompositeMenuNode;
160 protected findSubMenu(current: CompositeMenuNode, menuId: string, options?: SubMenuOptions): CompositeMenuNode;
161 /**
162 * Returns the menu at the given path.
163 *
164 * @param menuPath the path specifying the menu to return. If not given the empty path will be used.
165 *
166 * @returns the root menu when `menuPath` is empty. If `menuPath` is not empty the specified menu is
167 * returned if it exists, otherwise an error is thrown.
168 */
169 getMenu(menuPath?: MenuPath): CompositeMenuNode;
170}
171/**
172 * Base interface of the nodes used in the menu tree structure.
173 */
174export interface MenuNode {
175 /**
176 * the optional label for this specific node.
177 */
178 readonly label?: string;
179 /**
180 * technical identifier.
181 */
182 readonly id: string;
183 /**
184 * Menu nodes are sorted in ascending order based on their `sortString`.
185 */
186 readonly sortString: string;
187}
188/**
189 * Node representing a (sub)menu in the menu tree structure.
190 */
191export declare class CompositeMenuNode implements MenuNode {
192 readonly id: string;
193 label?: string | undefined;
194 protected readonly _children: MenuNode[];
195 iconClass?: string;
196 order?: string;
197 constructor(id: string, label?: string | undefined, options?: SubMenuOptions);
198 get children(): ReadonlyArray<MenuNode>;
199 /**
200 * Inserts the given node at the position indicated by `sortString`.
201 *
202 * @returns a disposable which, when called, will remove the given node again.
203 */
204 addNode(node: MenuNode): Disposable;
205 /**
206 * Removes the first node with the given id.
207 *
208 * @param id node id.
209 */
210 removeNode(id: string): void;
211 get sortString(): string;
212 get isSubmenu(): boolean;
213 /**
214 * Indicates whether the given node is the special `navigation` menu.
215 *
216 * @param node the menu node to check.
217 * @returns `true` when the given node is a {@link CompositeMenuNode} with id `navigation`,
218 * `false` otherwise.
219 */
220 static isNavigationGroup(node: MenuNode): node is CompositeMenuNode;
221}
222/**
223 * Node representing an action in the menu tree structure.
224 * It's based on {@link MenuAction} for which it tries to determine the
225 * best label, icon and sortString with the given data.
226 */
227export declare class ActionMenuNode implements MenuNode {
228 readonly action: MenuAction;
229 protected readonly commands: CommandRegistry;
230 readonly altNode: ActionMenuNode | undefined;
231 constructor(action: MenuAction, commands: CommandRegistry);
232 get id(): string;
233 get label(): string;
234 get icon(): string | undefined;
235 get sortString(): string;
236}
237//# sourceMappingURL=menu.d.ts.map
\No newline at end of file