import { JSONObject } from '../algorithm/json'; import { ISequence } from '../algorithm/sequence'; import { Message } from '../core/messaging'; import { ISignal } from '../core/signaling'; import { CommandRegistry } from './commandregistry'; import { Keymap } from './keymap'; import { Widget } from './widget'; /** * A widget which displays menu items as a canonical menu. */ export declare class Menu extends Widget { /** * Construct a new menu. * * @param options - The options for initializing the menu. */ constructor(options: Menu.IOptions); /** * Dispose of the resources held by the menu. */ dispose(): void; /** * A signal emitted just before the menu is closed. * * #### Notes * This signal is emitted when the menu receives a `'close-request'` * message, just before it removes itself from the DOM. * * This signal is not emitted if the menu is already detached from * the DOM when it receives the `'close-request'` message. */ aboutToClose: ISignal; /** * A signal emitted when a new menu is requested by the user. * * #### Notes * This signal is emitted whenever the user presses the right or left * arrow keys, and a submenu cannot be opened or closed in response. * * This signal is useful when implementing menu bars in order to open * the next or previous menu in response to a user key press. * * This signal is only emitted for the root menu in a hierarchy. */ menuRequested: ISignal; /** * Get the parent menu of the menu. * * #### Notes * This will be `null` if the menu is not an open submenu. * * This is a read-only property. */ readonly parentMenu: Menu; /** * Get the child menu of the menu. * * #### Notes * This will be `null` if the menu does not have an open submenu. * * This is a read-only property. */ readonly childMenu: Menu; /** * Find the root menu of this menu hierarchy. * * #### Notes * This is a read-only property. */ readonly rootMenu: Menu; /** * Find the leaf menu of this menu hierarchy. * * #### Notes * This is a read-only property. */ readonly leafMenu: Menu; /** * Get the menu content node. * * #### Notes * This is the node which holds the menu item nodes. * * Modifying this node directly can lead to undefined behavior. * * This is a read-only property. */ readonly contentNode: HTMLUListElement; /** * The command registry used by the menu. * * #### Notes * This is a read-only property. */ readonly commands: CommandRegistry; /** * The keymap used by the menu. * * #### Notes * This is a read-only property. */ readonly keymap: Keymap; /** * The renderer used by the menu. * * #### Notes * This is a read-only property. */ readonly renderer: Menu.IRenderer; /** * A read-only sequence of the menu items in the menu. * * #### Notes * This is a read-only property. */ readonly items: ISequence; /** * Get the currently active menu item. * * #### Notes * This will be `null` if no menu item is active. */ /** * Set the currently active menu item. * * #### Notes * If the item cannot be activated, the item will be set to `null`. */ activeItem: Menu.IItem; /** * Get the index of the currently active menu item. * * #### Notes * This will be `-1` if no menu item is active. */ /** * Set the index of the currently active menu item. * * #### Notes * If the item cannot be activated, the index will be set to `-1`. */ activeIndex: number; /** * Activate the next selectable item in the menu. * * #### Notes * If no item is selectable, the index will be set to `-1`. */ activateNextItem(): void; /** * Activate the previous selectable item in the menu. * * #### Notes * If no item is selectable, the index will be set to `-1`. */ activatePreviousItem(): void; /** * Trigger the active menu item. * * #### Notes * If the active item is a submenu, it will be opened and the first * item will be activated. * * If the active item is a command, the command will be executed. * * If the menu is not attached, this is a no-op. * * If there is no active item, this is a no-op. */ triggerActiveItem(): void; /** * Add a menu item to the end of the menu. * * @param options - The options for creating the menu item. * * @returns The menu item added to the menu. */ addItem(options: Menu.IItemOptions): Menu.IItem; /** * Insert a menu item into the menu at the specified index. * * @param index - The index at which to insert the item. * * @param options - The options for creating the menu item. * * @returns The menu item added to the menu. * * #### Notes * The index will be clamped to the bounds of the items. */ insertItem(index: number, options: Menu.IItemOptions): Menu.IItem; /** * Remove an item from the menu. * * @param item - The item to remove from the menu. * * @returns The index occupied by the item, or `-1` if the item * was not contained in the menu. */ removeItem(item: Menu.IItem): number; /** * Remove the item at a given index from the menu. * * @param index - The index of the item to remove. * * @returns The item occupying the index, or `null` if the index * is out of range. */ removeItemAt(index: number): Menu.IItem; /** * Remove all menu items from the menu. */ clearItems(): void; /** * Open the menu at the specified location. * * @param x - The client X coordinate of the menu location. * * @param y - The client Y coordinate of the menu location. * * @param options - The additional options for opening the menu. * * #### Notes * The menu will be opened at the given location unless it will not * fully fit on the screen. If it will not fit, it will be adjusted * to fit naturally on the screen. * * This is a no-op if the menu is already attached to the DOM. */ open(x: number, y: number, options?: Menu.IOpenOptions): void; /** * Handle the DOM events for the menu. * * @param event - The DOM event sent to the menu. * * #### Notes * This method implements the DOM `EventListener` interface and is * called in response to events on the menu's DOM nodes. It should * not be called directly by user code. */ handleEvent(event: Event): void; /** * A message handler invoked on an `'after-attach'` message. */ protected onAfterAttach(msg: Message): void; /** * A message handler invoked on a `'before-detach'` message. */ protected onBeforeDetach(msg: Message): void; /** * A message handler invoked on an `'activate-request'` message. */ protected onActivateRequest(msg: Message): void; /** * A message handler invoked on an `'update-request'` message. */ protected onUpdateRequest(msg: Message): void; /** * A message handler invoked on a `'close-request'` message. */ protected onCloseRequest(msg: Message): void; /** * Handle the `'keydown'` event for the menu. * * #### Notes * This listener is attached to the menu node. */ private _evtKeyDown(event); /** * Handle the `'mouseup'` event for the menu. * * #### Notes * This listener is attached to the menu node. */ private _evtMouseUp(event); /** * Handle the `'mousemove'` event for the menu. * * #### Notes * This listener is attached to the menu node. */ private _evtMouseMove(event); /** * Handle the `'mouseenter'` event for the menu. * * #### Notes * This listener is attached to the menu node. */ private _evtMouseEnter(event); /** * Handle the `'mouseleave'` event for the menu. * * #### Notes * This listener is attached to the menu node. */ private _evtMouseLeave(event); /** * Handle the `'mousedown'` event for the menu. * * #### Notes * This listener is attached to the document node. */ private _evtMouseDown(event); /** * Open the child menu at the active index immediately. * * If a different child menu is already open, it will be closed, * even if the active item is not a valid submenu. */ private _openChildMenu(activateFirst?); /** * Close the child menu immediately. * * This is a no-op if a child menu is not open. */ private _closeChildMenu(); /** * Start the open timer, unless it is already pending. */ private _startOpenTimer(); /** * Start the close timer, unless it is already pending. */ private _startCloseTimer(); /** * Cancel the open timer, if the timer is pending. */ private _cancelOpenTimer(); /** * Cancel the close timer, if the timer is pending. */ private _cancelCloseTimer(); private _keymap; private _childIndex; private _openTimerID; private _closeTimerID; private _activeIndex; private _childMenu; private _parentMenu; private _renderer; private _commands; private _items; private _nodes; } /** * The namespace for the `Menu` class statics. */ export declare namespace Menu { /** * An options object for creating a menu. */ interface IOptions { /** * The command registry for use with the menu. */ commands: CommandRegistry; /** * The keymap for use with the menu. */ keymap: Keymap; /** * A custom renderer for use with the menu. * * The default is a shared renderer instance. */ renderer?: IRenderer; } /** * A type alias for a menu item type. */ type ItemType = 'command' | 'submenu' | 'separator'; /** * An options object for creating a menu item. */ interface IItemOptions { /** * The type of the menu item. * * The default value is `'command'`. */ type?: ItemType; /** * The command to execute when the item is triggered. * * The default value is an empty string. */ command?: string; /** * The arguments for the command. * * The default value is `null`. */ args?: JSONObject; /** * The menu for a `'submenu'` type item. * * The default value is `null`. */ menu?: Menu; } /** * An object which represents a menu item. * * #### Notes * An item is an immutable object created by a menu. */ interface IItem { /** * The type of the menu item. */ type: ItemType; /** * The command to execute when the item is triggered. */ command: string; /** * The arguments for the command. */ args: JSONObject; /** * The menu for a `'submenu'` type item. */ menu: Menu; /** * The display label for the menu item. */ label: string; /** * The mnemonic index for the menu item. */ mnemonic: number; /** * The icon class for the menu item. */ icon: string; /** * The display caption for the menu item. */ caption: string; /** * The extra class name for the menu item. */ className: string; /** * Whether the menu item is enabled. */ isEnabled: boolean; /** * Whether the menu item is toggled. */ isToggled: boolean; /** * Whether the menu item is visible. */ isVisible: boolean; /** * The key binding for the menu item. */ keyBinding: Keymap.IBinding; } /** * An options object for the `open` method on a menu. */ interface IOpenOptions { /** * Whether to force the X position of the menu. * * Setting to `true` will disable the logic which repositions the * X coordinate of the menu if it will not fit entirely on screen. * * The default is `false`. */ forceX?: boolean; /** * Whether to force the Y position of the menu. * * Setting to `true` will disable the logic which repositions the * Y coordinate of the menu if it will not fit entirely on screen. * * The default is `false`. */ forceY?: boolean; } /** * A renderer for use with a menu. */ interface IRenderer { /** * Create a node for a menu item. * * @returns A new node for a menu item. * * #### Notes * The data in the node should be uninitialized. * * The `updateItemNode` method will be called for initialization. */ createItemNode(): HTMLLIElement; /** * Update an item node to reflect the state of a menu item. * * @param node - A node created by a call to `createItemNode`. * * @param item - The menu item holding the data for the node. * * #### Notes * This method should completely reset the state of the node to * reflect the data for the menu item. */ updateItemNode(node: HTMLLIElement, item: IItem): void; } /** * The default implementation of `IRenderer`. */ class Renderer implements IRenderer { /** * Create a node for a menu item. * * @returns A new node for a menu item. */ createItemNode(): HTMLLIElement; /** * Update an item node to reflect the state of a menu item. * * @param node - A node created by a call to `createItemNode`. * * @param item - The menu item holding the data for the node. */ updateItemNode(node: HTMLLIElement, item: IItem): void; /** * Format a label into HTML for display. * * @param label - The label text of interest. * * @param mnemonic - The index of the mnemonic character. * * @return The formatted label HTML for display. */ formatLabel(label: string, mnemonic: number): string; /** * Format a key binding into shortcut text for display. * * @param binding - The key binding to format. This may be `null`. * * @returns The formatted shortcut text for display. */ formatShortcut(binding: Keymap.IBinding): string; } /** * The default `Renderer` instance. */ const defaultRenderer: Renderer; }