UNPKG

17.9 kBTypeScriptView Raw
1import { CommandRegistry } from '@lumino/commands';
2import { ReadonlyJSONObject } from '@lumino/coreutils';
3import { Message } from '@lumino/messaging';
4import { ISignal } from '@lumino/signaling';
5import { ElementARIAAttrs, ElementDataset, h, VirtualElement } from '@lumino/virtualdom';
6import { Widget } from './widget';
7/**
8 * A widget which displays items as a canonical menu.
9 */
10export declare class Menu extends Widget {
11 /**
12 * Construct a new menu.
13 *
14 * @param options - The options for initializing the menu.
15 */
16 constructor(options: Menu.IOptions);
17 /**
18 * Dispose of the resources held by the menu.
19 */
20 dispose(): void;
21 /**
22 * A signal emitted just before the menu is closed.
23 *
24 * #### Notes
25 * This signal is emitted when the menu receives a `'close-request'`
26 * message, just before it removes itself from the DOM.
27 *
28 * This signal is not emitted if the menu is already detached from
29 * the DOM when it receives the `'close-request'` message.
30 */
31 readonly aboutToClose: ISignal<this, void>;
32 /**
33 * A signal emitted when a new menu is requested by the user.
34 *
35 * #### Notes
36 * This signal is emitted whenever the user presses the right or left
37 * arrow keys, and a submenu cannot be opened or closed in response.
38 *
39 * This signal is useful when implementing menu bars in order to open
40 * the next or previous menu in response to a user key press.
41 *
42 * This signal is only emitted for the root menu in a hierarchy.
43 */
44 readonly menuRequested: ISignal<this, 'next' | 'previous'>;
45 /**
46 * The command registry used by the menu.
47 */
48 readonly commands: CommandRegistry;
49 /**
50 * The renderer used by the menu.
51 */
52 readonly renderer: Menu.IRenderer;
53 /**
54 * The parent menu of the menu.
55 *
56 * #### Notes
57 * This is `null` unless the menu is an open submenu.
58 */
59 readonly parentMenu: Menu | null;
60 /**
61 * The child menu of the menu.
62 *
63 * #### Notes
64 * This is `null` unless the menu has an open submenu.
65 */
66 readonly childMenu: Menu | null;
67 /**
68 * The root menu of the menu hierarchy.
69 */
70 readonly rootMenu: Menu;
71 /**
72 * The leaf menu of the menu hierarchy.
73 */
74 readonly leafMenu: Menu;
75 /**
76 * The menu content node.
77 *
78 * #### Notes
79 * This is the node which holds the menu item nodes.
80 *
81 * Modifying this node directly can lead to undefined behavior.
82 */
83 readonly contentNode: HTMLUListElement;
84 /**
85 * Get the currently active menu item.
86 */
87 /**
88 * Set the currently active menu item.
89 *
90 * #### Notes
91 * If the item cannot be activated, the item will be set to `null`.
92 */
93 activeItem: Menu.IItem | null;
94 /**
95 * Get the index of the currently active menu item.
96 *
97 * #### Notes
98 * This will be `-1` if no menu item is active.
99 */
100 /**
101 * Set the index of the currently active menu item.
102 *
103 * #### Notes
104 * If the item cannot be activated, the index will be set to `-1`.
105 */
106 activeIndex: number;
107 /**
108 * A read-only array of the menu items in the menu.
109 */
110 readonly items: ReadonlyArray<Menu.IItem>;
111 /**
112 * Activate the next selectable item in the menu.
113 *
114 * #### Notes
115 * If no item is selectable, the index will be set to `-1`.
116 */
117 activateNextItem(): void;
118 /**
119 * Activate the previous selectable item in the menu.
120 *
121 * #### Notes
122 * If no item is selectable, the index will be set to `-1`.
123 */
124 activatePreviousItem(): void;
125 /**
126 * Trigger the active menu item.
127 *
128 * #### Notes
129 * If the active item is a submenu, it will be opened and the first
130 * item will be activated.
131 *
132 * If the active item is a command, the command will be executed.
133 *
134 * If the menu is not attached, this is a no-op.
135 *
136 * If there is no active item, this is a no-op.
137 */
138 triggerActiveItem(): void;
139 /**
140 * Add a menu item to the end of the menu.
141 *
142 * @param options - The options for creating the menu item.
143 *
144 * @returns The menu item added to the menu.
145 */
146 addItem(options: Menu.IItemOptions): Menu.IItem;
147 /**
148 * Insert a menu item into the menu at the specified index.
149 *
150 * @param index - The index at which to insert the item.
151 *
152 * @param options - The options for creating the menu item.
153 *
154 * @returns The menu item added to the menu.
155 *
156 * #### Notes
157 * The index will be clamped to the bounds of the items.
158 */
159 insertItem(index: number, options: Menu.IItemOptions): Menu.IItem;
160 /**
161 * Remove an item from the menu.
162 *
163 * @param item - The item to remove from the menu.
164 *
165 * #### Notes
166 * This is a no-op if the item is not in the menu.
167 */
168 removeItem(item: Menu.IItem): void;
169 /**
170 * Remove the item at a given index from the menu.
171 *
172 * @param index - The index of the item to remove.
173 *
174 * #### Notes
175 * This is a no-op if the index is out of range.
176 */
177 removeItemAt(index: number): void;
178 /**
179 * Remove all menu items from the menu.
180 */
181 clearItems(): void;
182 /**
183 * Open the menu at the specified location.
184 *
185 * @param x - The client X coordinate of the menu location.
186 *
187 * @param y - The client Y coordinate of the menu location.
188 *
189 * @param options - The additional options for opening the menu.
190 *
191 * #### Notes
192 * The menu will be opened at the given location unless it will not
193 * fully fit on the screen. If it will not fit, it will be adjusted
194 * to fit naturally on the screen.
195 *
196 * This is a no-op if the menu is already attached to the DOM.
197 */
198 open(x: number, y: number, options?: Menu.IOpenOptions): void;
199 /**
200 * Handle the DOM events for the menu.
201 *
202 * @param event - The DOM event sent to the menu.
203 *
204 * #### Notes
205 * This method implements the DOM `EventListener` interface and is
206 * called in response to events on the menu's DOM nodes. It should
207 * not be called directly by user code.
208 */
209 handleEvent(event: Event): void;
210 /**
211 * A message handler invoked on a `'before-attach'` message.
212 */
213 protected onBeforeAttach(msg: Message): void;
214 /**
215 * A message handler invoked on an `'after-detach'` message.
216 */
217 protected onAfterDetach(msg: Message): void;
218 /**
219 * A message handler invoked on an `'activate-request'` message.
220 */
221 protected onActivateRequest(msg: Message): void;
222 /**
223 * A message handler invoked on an `'update-request'` message.
224 */
225 protected onUpdateRequest(msg: Message): void;
226 /**
227 * A message handler invoked on a `'close-request'` message.
228 */
229 protected onCloseRequest(msg: Message): void;
230 /**
231 * Handle the `'keydown'` event for the menu.
232 *
233 * #### Notes
234 * This listener is attached to the menu node.
235 */
236 private _evtKeyDown;
237 /**
238 * Handle the `'mouseup'` event for the menu.
239 *
240 * #### Notes
241 * This listener is attached to the menu node.
242 */
243 private _evtMouseUp;
244 /**
245 * Handle the `'mousemove'` event for the menu.
246 *
247 * #### Notes
248 * This listener is attached to the menu node.
249 */
250 private _evtMouseMove;
251 /**
252 * Handle the `'mouseenter'` event for the menu.
253 *
254 * #### Notes
255 * This listener is attached to the menu node.
256 */
257 private _evtMouseEnter;
258 /**
259 * Handle the `'mouseleave'` event for the menu.
260 *
261 * #### Notes
262 * This listener is attached to the menu node.
263 */
264 private _evtMouseLeave;
265 /**
266 * Handle the `'mousedown'` event for the menu.
267 *
268 * #### Notes
269 * This listener is attached to the document node.
270 */
271 private _evtMouseDown;
272 /**
273 * Open the child menu at the active index immediately.
274 *
275 * If a different child menu is already open, it will be closed,
276 * even if the active item is not a valid submenu.
277 */
278 private _openChildMenu;
279 /**
280 * Close the child menu immediately.
281 *
282 * This is a no-op if a child menu is not open.
283 */
284 private _closeChildMenu;
285 /**
286 * Start the open timer, unless it is already pending.
287 */
288 private _startOpenTimer;
289 /**
290 * Start the close timer, unless it is already pending.
291 */
292 private _startCloseTimer;
293 /**
294 * Cancel the open timer, if the timer is pending.
295 */
296 private _cancelOpenTimer;
297 /**
298 * Cancel the close timer, if the timer is pending.
299 */
300 private _cancelCloseTimer;
301 /**
302 * Save window data used for menu positioning in transient cache.
303 *
304 * In order to avoid layout trashing it is recommended to invoke this
305 * method immediately prior to opening the menu and any DOM modifications
306 * (like closing previously visible menu, or adding a class to menu widget).
307 *
308 * The transient cache will be released upon `open()` call.
309 */
310 static saveWindowData(): void;
311 private _childIndex;
312 private _activeIndex;
313 private _openTimerID;
314 private _closeTimerID;
315 private _items;
316 private _childMenu;
317 private _parentMenu;
318 private _aboutToClose;
319 private _menuRequested;
320}
321/**
322 * The namespace for the `Menu` class statics.
323 */
324export declare namespace Menu {
325 /**
326 * An options object for creating a menu.
327 */
328 interface IOptions {
329 /**
330 * The command registry for use with the menu.
331 */
332 commands: CommandRegistry;
333 /**
334 * A custom renderer for use with the menu.
335 *
336 * The default is a shared renderer instance.
337 */
338 renderer?: IRenderer;
339 }
340 /**
341 * An options object for the `open` method on a menu.
342 */
343 interface IOpenOptions {
344 /**
345 * Whether to force the X position of the menu.
346 *
347 * Setting to `true` will disable the logic which repositions the
348 * X coordinate of the menu if it will not fit entirely on screen.
349 *
350 * The default is `false`.
351 */
352 forceX?: boolean;
353 /**
354 * Whether to force the Y position of the menu.
355 *
356 * Setting to `true` will disable the logic which repositions the
357 * Y coordinate of the menu if it will not fit entirely on screen.
358 *
359 * The default is `false`.
360 */
361 forceY?: boolean;
362 }
363 /**
364 * A type alias for a menu item type.
365 */
366 type ItemType = 'command' | 'submenu' | 'separator';
367 /**
368 * An options object for creating a menu item.
369 */
370 interface IItemOptions {
371 /**
372 * The type of the menu item.
373 *
374 * The default value is `'command'`.
375 */
376 type?: ItemType;
377 /**
378 * The command to execute when the item is triggered.
379 *
380 * The default value is an empty string.
381 */
382 command?: string;
383 /**
384 * The arguments for the command.
385 *
386 * The default value is an empty object.
387 */
388 args?: ReadonlyJSONObject;
389 /**
390 * The submenu for a `'submenu'` type item.
391 *
392 * The default value is `null`.
393 */
394 submenu?: Menu | null;
395 }
396 /**
397 * An object which represents a menu item.
398 *
399 * #### Notes
400 * Item objects are created automatically by a menu.
401 */
402 interface IItem {
403 /**
404 * The type of the menu item.
405 */
406 readonly type: ItemType;
407 /**
408 * The command to execute when the item is triggered.
409 */
410 readonly command: string;
411 /**
412 * The arguments for the command.
413 */
414 readonly args: ReadonlyJSONObject;
415 /**
416 * The submenu for a `'submenu'` type item.
417 */
418 readonly submenu: Menu | null;
419 /**
420 * The display label for the menu item.
421 */
422 readonly label: string;
423 /**
424 * The mnemonic index for the menu item.
425 */
426 readonly mnemonic: number;
427 /**
428 * The icon renderer for the menu item.
429 */
430 readonly icon: VirtualElement.IRenderer | undefined | string;
431 /**
432 * The icon class for the menu item.
433 */
434 readonly iconClass: string;
435 /**
436 * The icon label for the menu item.
437 */
438 readonly iconLabel: string;
439 /**
440 * The display caption for the menu item.
441 */
442 readonly caption: string;
443 /**
444 * The extra class name for the menu item.
445 */
446 readonly className: string;
447 /**
448 * The dataset for the menu item.
449 */
450 readonly dataset: CommandRegistry.Dataset;
451 /**
452 * Whether the menu item is enabled.
453 */
454 readonly isEnabled: boolean;
455 /**
456 * Whether the menu item is toggled.
457 */
458 readonly isToggled: boolean;
459 /**
460 * Whether the menu item is visible.
461 */
462 readonly isVisible: boolean;
463 /**
464 * The key binding for the menu item.
465 */
466 readonly keyBinding: CommandRegistry.IKeyBinding | null;
467 }
468 /**
469 * An object which holds the data to render a menu item.
470 */
471 interface IRenderData {
472 /**
473 * The item to be rendered.
474 */
475 readonly item: IItem;
476 /**
477 * Whether the item is the active item.
478 */
479 readonly active: boolean;
480 /**
481 * Whether the item should be collapsed.
482 */
483 readonly collapsed: boolean;
484 /**
485 * Handler for when element is in focus.
486 */
487 readonly onfocus?: () => void;
488 }
489 /**
490 * A renderer for use with a menu.
491 */
492 interface IRenderer {
493 /**
494 * Render the virtual element for a menu item.
495 *
496 * @param data - The data to use for rendering the item.
497 *
498 * @returns A virtual element representing the item.
499 */
500 renderItem(data: IRenderData): VirtualElement;
501 }
502 /**
503 * The default implementation of `IRenderer`.
504 *
505 * #### Notes
506 * Subclasses are free to reimplement rendering methods as needed.
507 */
508 class Renderer implements IRenderer {
509 /**
510 * Render the virtual element for a menu item.
511 *
512 * @param data - The data to use for rendering the item.
513 *
514 * @returns A virtual element representing the item.
515 */
516 renderItem(data: IRenderData): VirtualElement;
517 /**
518 * Render the icon element for a menu item.
519 *
520 * @param data - The data to use for rendering the icon.
521 *
522 * @returns A virtual element representing the item icon.
523 */
524 renderIcon(data: IRenderData): VirtualElement;
525 /**
526 * Render the label element for a menu item.
527 *
528 * @param data - The data to use for rendering the label.
529 *
530 * @returns A virtual element representing the item label.
531 */
532 renderLabel(data: IRenderData): VirtualElement;
533 /**
534 * Render the shortcut element for a menu item.
535 *
536 * @param data - The data to use for rendering the shortcut.
537 *
538 * @returns A virtual element representing the item shortcut.
539 */
540 renderShortcut(data: IRenderData): VirtualElement;
541 /**
542 * Render the submenu icon element for a menu item.
543 *
544 * @param data - The data to use for rendering the submenu icon.
545 *
546 * @returns A virtual element representing the submenu icon.
547 */
548 renderSubmenu(data: IRenderData): VirtualElement;
549 /**
550 * Create the class name for the menu item.
551 *
552 * @param data - The data to use for the class name.
553 *
554 * @returns The full class name for the menu item.
555 */
556 createItemClass(data: IRenderData): string;
557 /**
558 * Create the dataset for the menu item.
559 *
560 * @param data - The data to use for creating the dataset.
561 *
562 * @returns The dataset for the menu item.
563 */
564 createItemDataset(data: IRenderData): ElementDataset;
565 /**
566 * Create the class name for the menu item icon.
567 *
568 * @param data - The data to use for the class name.
569 *
570 * @returns The full class name for the item icon.
571 */
572 createIconClass(data: IRenderData): string;
573 /**
574 * Create the aria attributes for menu item.
575 *
576 * @param data - The data to use for the aria attributes.
577 *
578 * @returns The aria attributes object for the item.
579 */
580 createItemARIA(data: IRenderData): ElementARIAAttrs;
581 /**
582 * Create the render content for the label node.
583 *
584 * @param data - The data to use for the label content.
585 *
586 * @returns The content to add to the label node.
587 */
588 formatLabel(data: IRenderData): h.Child;
589 /**
590 * Create the render content for the shortcut node.
591 *
592 * @param data - The data to use for the shortcut content.
593 *
594 * @returns The content to add to the shortcut node.
595 */
596 formatShortcut(data: IRenderData): h.Child;
597 }
598 /**
599 * The default `Renderer` instance.
600 */
601 const defaultRenderer: Renderer;
602}
603//# sourceMappingURL=menu.d.ts.map
\No newline at end of file