file.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IRankedMenu, RankedMenu } from '@jupyterlab/ui-components';
  4. import { find } from '@lumino/algorithm';
  5. import { Widget } from '@lumino/widgets';
  6. import { IMenuExtender } from './tokens';
  7. /**
  8. * An interface for a File menu.
  9. */
  10. export interface IFileMenu extends IRankedMenu {
  11. /**
  12. * Option to add a `Quit` entry in the File menu
  13. */
  14. quitEntry: boolean;
  15. /**
  16. * A submenu for creating new files/launching new activities.
  17. */
  18. readonly newMenu: IRankedMenu;
  19. /**
  20. * The close and cleanup extension point.
  21. */
  22. readonly closeAndCleaners: Set<IFileMenu.ICloseAndCleaner<Widget>>;
  23. /**
  24. * A set storing IConsoleCreators for the File menu.
  25. */
  26. readonly consoleCreators: Set<IFileMenu.IConsoleCreator<Widget>>;
  27. }
  28. /**
  29. * An extensible FileMenu for the application.
  30. */
  31. export class FileMenu extends RankedMenu implements IFileMenu {
  32. constructor(options: IRankedMenu.IOptions) {
  33. super(options);
  34. this.quitEntry = false;
  35. // Create the "New" submenu.
  36. this.closeAndCleaners = new Set<IFileMenu.ICloseAndCleaner<Widget>>();
  37. this.consoleCreators = new Set<IFileMenu.IConsoleCreator<Widget>>();
  38. }
  39. /**
  40. * The New submenu.
  41. */
  42. get newMenu(): RankedMenu {
  43. if (!this._newMenu) {
  44. this._newMenu =
  45. (find(this.items, menu => menu.submenu?.id === 'jp-mainmenu-file-new')
  46. ?.submenu as RankedMenu) ??
  47. new RankedMenu({
  48. commands: this.commands
  49. });
  50. }
  51. return this._newMenu;
  52. }
  53. /**
  54. * The close and cleanup extension point.
  55. */
  56. readonly closeAndCleaners: Set<IFileMenu.ICloseAndCleaner<Widget>>;
  57. /**
  58. * A set storing IConsoleCreators for the Kernel menu.
  59. */
  60. readonly consoleCreators: Set<IFileMenu.IConsoleCreator<Widget>>;
  61. /**
  62. * Dispose of the resources held by the file menu.
  63. */
  64. dispose(): void {
  65. this._newMenu?.dispose();
  66. this.consoleCreators.clear();
  67. super.dispose();
  68. }
  69. /**
  70. * Option to add a `Quit` entry in File menu
  71. */
  72. public quitEntry: boolean;
  73. private _newMenu: RankedMenu;
  74. }
  75. /**
  76. * Namespace for IFileMenu
  77. */
  78. export namespace IFileMenu {
  79. /**
  80. * Interface for an activity that has some cleanup action associated
  81. * with it in addition to merely closing its widget in the main area.
  82. */
  83. export interface ICloseAndCleaner<T extends Widget> extends IMenuExtender<T> {
  84. /**
  85. * A function to create the label for the `closeAndCleanup`action.
  86. *
  87. * This function receives the number of items `n` to be able to provided
  88. * correct pluralized forms of translations.
  89. */
  90. closeAndCleanupLabel?: (n: number) => string;
  91. /**
  92. * A function to perform the close and cleanup action.
  93. */
  94. closeAndCleanup: (widget: T) => Promise<void>;
  95. }
  96. /**
  97. * Interface for a command to create a console for an activity.
  98. */
  99. export interface IConsoleCreator<T extends Widget> extends IMenuExtender<T> {
  100. /**
  101. * A function to create the label for the `createConsole`action.
  102. *
  103. * This function receives the number of items `n` to be able to provided
  104. * correct pluralized forms of translations.
  105. */
  106. createConsoleLabel?: (n: number) => string;
  107. /**
  108. * The function to create the console.
  109. */
  110. createConsole: (widget: T) => Promise<void>;
  111. }
  112. }