mainmenu.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ArrayExt
  5. } from '@phosphor/algorithm';
  6. import {
  7. Token
  8. } from '@phosphor/application';
  9. import {
  10. Menu, MenuBar
  11. } from '@phosphor/widgets';
  12. /* tslint:disable */
  13. /**
  14. * The main menu token.
  15. */
  16. export
  17. const IMainMenu = new Token<IMainMenu>('jupyter.services.main-menu');
  18. /* tslint:enable */
  19. /**
  20. * The main menu interface.
  21. */
  22. export
  23. interface IMainMenu {
  24. /**
  25. * Add a new menu to the main menu bar.
  26. */
  27. addMenu(menu: Menu, options?: IMainMenu.IAddOptions): void;
  28. }
  29. /**
  30. * The namespace for IMainMenu attached interfaces.
  31. */
  32. export
  33. namespace IMainMenu {
  34. /**
  35. * The options used to add a menu to the main menu.
  36. */
  37. export
  38. interface IAddOptions {
  39. /**
  40. * The rank order of the menu among its siblings.
  41. */
  42. rank?: number;
  43. }
  44. }
  45. /**
  46. * The main menu class. It is intended to be used as a singleton.
  47. */
  48. export
  49. class MainMenu extends MenuBar implements IMainMenu {
  50. /**
  51. * Add a new menu to the main menu bar.
  52. */
  53. addMenu(menu: Menu, options: IMainMenu.IAddOptions = {}): void {
  54. if (ArrayExt.firstIndexOf(this.menus, menu) > -1) {
  55. return;
  56. }
  57. let rank = 'rank' in options ? options.rank : 100;
  58. let rankItem = { menu, rank };
  59. let index = ArrayExt.upperBound(this._items, rankItem, Private.itemCmp);
  60. // Upon disposal, remove the menu and its rank reference.
  61. menu.disposed.connect(this._onMenuDisposed, this);
  62. ArrayExt.insert(this._items, index, rankItem);
  63. this.insertMenu(index, menu);
  64. }
  65. /**
  66. * Handle the disposal of a menu.
  67. */
  68. private _onMenuDisposed(menu: Menu): void {
  69. this.removeMenu(menu);
  70. let index = ArrayExt.findFirstIndex(this._items, item => item.menu === menu);
  71. if (index !== -1) {
  72. ArrayExt.removeAt(this._items, index);
  73. }
  74. }
  75. private _items: Private.IRankItem[] = [];
  76. }
  77. /**
  78. * A namespace for private data.
  79. */
  80. namespace Private {
  81. /**
  82. * An object which holds a menu and its sort rank.
  83. */
  84. export
  85. interface IRankItem {
  86. /**
  87. * The menu for the item.
  88. */
  89. menu: Menu;
  90. /**
  91. * The sort rank of the menu.
  92. */
  93. rank: number;
  94. }
  95. /**
  96. * A comparator function for menu rank items.
  97. */
  98. export
  99. function itemCmp(first: IRankItem, second: IRankItem): number {
  100. return first.rank - second.rank;
  101. }
  102. }