123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- findIndex, indexOf, upperBound
- } from 'phosphor/lib/algorithm/searching';
- import {
- Vector
- } from 'phosphor/lib/collections/vector';
- import {
- Token
- } from 'phosphor/lib/core/token';
- import {
- Menu
- } from 'phosphor/lib/ui/menu';
- import {
- MenuBar
- } from 'phosphor/lib/ui/menubar';
- /* tslint:disable */
- /**
- * The main menu token.
- */
- export
- const IMainMenu = new Token<IMainMenu>('jupyter.services.main-menu');
- /* tslint:enable */
- /**
- * The main menu interface.
- */
- export
- interface IMainMenu {
- /**
- * Add a new menu to the main menu bar.
- */
- addMenu(menu: Menu, options: IAddMenuOptions): void;
- }
- /**
- * The options used to add a menu to the main menu.
- */
- export
- interface IAddMenuOptions {
- /**
- * The rank order of the menu among its siblings.
- */
- rank?: number;
- }
- /**
- * The main menu class. It is intended to be used as a singleton.
- */
- export
- class MainMenu extends MenuBar implements IMainMenu {
- /**
- * Add a new menu to the main menu bar.
- */
- addMenu(menu: Menu, options: IAddMenuOptions = {}): void {
- if (indexOf(this.menus, menu) > -1) {
- return;
- }
- let rank = 'rank' in options ? options.rank : 100;
- let rankItem = { menu, rank };
- let index = upperBound(this._items, rankItem, Private.itemCmp);
- // Upon disposal, remove the menu and its rank reference.
- menu.disposed.connect(this._onMenuDisposed, this);
- this._items.insert(index, rankItem);
- this.insertMenu(index, menu);
- }
- /**
- * Handle the disposal of a menu.
- */
- private _onMenuDisposed(menu: Menu): void {
- this.removeMenu(menu);
- let index = findIndex(this._items, item => item.menu === menu);
- if (index !== -1) {
- this._items.removeAt(index);
- }
- }
- private _items = new Vector<Private.IRankItem>();
- }
- /**
- * A namespace for private data.
- */
- namespace Private {
- /**
- * An object which holds a menu and its sort rank.
- */
- export
- interface IRankItem {
- /**
- * The menu for the item.
- */
- menu: Menu;
- /**
- * The sort rank of the menu.
- */
- rank: number;
- }
- /**
- * A comparator function for menu rank items.
- */
- export
- function itemCmp(first: IRankItem, second: IRankItem): number {
- return first.rank - second.rank;
- }
- }
|