Ver Fonte

Don't expose the whole Phosphor menu interface for the toplevel menus.

Ian Rose há 7 anos atrás
pai
commit
3a8147a71e

+ 5 - 5
packages/mainmenu-extension/src/index.ts

@@ -108,7 +108,7 @@ const menuPlugin: JupyterLabPlugin<IMainMenu> = {
  * Create the basic `Edit` menu.
  */
 function createEditMenu(app: JupyterLab, menu: EditMenu): void {
-  const commands = menu.commands;
+  const commands = menu.menu.commands;
 
   // Add the undo/redo commands the the Edit menu.
   commands.addCommand(CommandIDs.undo, {
@@ -169,7 +169,7 @@ function createEditMenu(app: JupyterLab, menu: EditMenu): void {
  * Create the basic `File` menu.
  */
 function createFileMenu(app: JupyterLab, menu: FileMenu): void {
-  const commands = menu.commands;
+  const commands = menu.menu.commands;
 
   // Add a delegator command for closing and cleaning up an activity.
   commands.addCommand(CommandIDs.closeAndCleanup, {
@@ -210,7 +210,7 @@ function createFileMenu(app: JupyterLab, menu: FileMenu): void {
  * Create the basic `Kernel` menu.
  */
 function createKernelMenu(app: JupyterLab, menu: KernelMenu): void {
-  const commands = menu.commands;
+  const commands = menu.menu.commands;
 
   commands.addCommand(CommandIDs.interruptKernel, {
     label: 'Interrupt Kernel',
@@ -261,7 +261,7 @@ function createKernelMenu(app: JupyterLab, menu: KernelMenu): void {
  * Create the basic `View` menu.
  */
 function createViewMenu(app: JupyterLab, menu: ViewMenu): void {
-  const commands = menu.commands;
+  const commands = menu.menu.commands;
 
   commands.addCommand(CommandIDs.lineNumbering, {
     label: 'Line Numbers',
@@ -311,7 +311,7 @@ function createViewMenu(app: JupyterLab, menu: ViewMenu): void {
 }
 
 function createRunMenu(app: JupyterLab, menu: RunMenu): void {
-  const commands = menu.commands;
+  const commands = menu.menu.commands;
 
   commands.addCommand(CommandIDs.run, {
     label: () => {

+ 1 - 0
packages/mainmenu/package.json

@@ -33,6 +33,7 @@
     "@phosphor/algorithm": "^1.1.2",
     "@phosphor/commands": "^1.4.0",
     "@phosphor/coreutils": "^1.3.0",
+    "@phosphor/disposable": "^1.1.2",
     "@phosphor/widgets": "^1.5.0"
   },
   "devDependencies": {

+ 11 - 1
packages/mainmenu/src/edit.ts

@@ -50,7 +50,7 @@ class EditMenu extends JupyterLabMenu implements IEditMenu {
    */
   constructor(options: Menu.IOptions) {
     super(options);
-    this.title.label = 'Edit';
+    this.menu.title.label = 'Edit';
 
     this.undoers =
       new Map<string, IEditMenu.IUndoer<Widget>>();
@@ -85,6 +85,16 @@ class EditMenu extends JupyterLabMenu implements IEditMenu {
    * The key for the map may be used in menu labels.
    */
   readonly findReplacers: Map<string, IEditMenu.IFindReplacer<Widget>>;
+
+  /**
+   * Dispose of the resources held by the edit menu.
+   */
+  dispose(): void {
+    this.undoers.clear();
+    this.clearers.clear();
+    this.findReplacers.clear();
+    super.dispose();
+  }
 }
 
 /**

+ 9 - 1
packages/mainmenu/src/file.ts

@@ -33,7 +33,7 @@ class FileMenu extends JupyterLabMenu implements IFileMenu {
   constructor(options: Menu.IOptions) {
     super(options);
 
-    this.title.label = 'File';
+    this.menu.title.label = 'File';
 
     // Create the "New" submenu.
     this.newMenu = new Menu(options);
@@ -56,6 +56,14 @@ class FileMenu extends JupyterLabMenu implements IFileMenu {
    * The close and cleanup extension point.
    */
   readonly closeAndCleaners: Map<string, IFileMenu.ICloseAndCleaner<Widget>>;
+
+  /**
+   * Dispose of the resources held by the file menu.
+   */
+  dispose(): void {
+    this.newMenu.dispose();
+    super.dispose();
+  }
 }
 
 

+ 2 - 2
packages/mainmenu/src/help.ts

@@ -10,7 +10,7 @@ import {
 } from './labmenu';
 
 /**
- * An interface for a Run menu.
+ * An interface for a Help menu.
  */
 export
 interface IHelpMenu extends IJupyterLabMenu {
@@ -26,6 +26,6 @@ class HelpMenu extends JupyterLabMenu implements IHelpMenu {
    */
   constructor(options: Menu.IOptions) {
     super(options);
-    this.title.label = 'Help';
+    this.menu.title.label = 'Help';
   }
 }

+ 10 - 1
packages/mainmenu/src/kernel.ts

@@ -45,7 +45,7 @@ class KernelMenu extends JupyterLabMenu implements IKernelMenu {
    */
   constructor(options: Menu.IOptions) {
     super(options);
-    this.title.label = 'Kernel';
+    this.menu.title.label = 'Kernel';
 
     this.kernelUsers =
       new Map<string, IKernelMenu.IKernelUser<Widget>>();
@@ -68,6 +68,15 @@ class KernelMenu extends JupyterLabMenu implements IKernelMenu {
    * The key for the map may be used in menu labels.
    */
   readonly consoleCreators: Map<string, IKernelMenu.IConsoleCreator<Widget>>;
+
+  /**
+   * Dispose of the resources held by the kernel menu.
+   */
+  dispose(): void {
+    this.kernelUsers.clear();
+    this.consoleCreators.clear();
+    super.dispose();
+  }
 }
 
 /**

+ 38 - 5
packages/mainmenu/src/labmenu.ts

@@ -9,6 +9,10 @@ import {
   ArrayExt
 } from '@phosphor/algorithm';
 
+import {
+  IDisposable
+} from '@phosphor/disposable';
+
 import {
   Menu, Widget
 } from '@phosphor/widgets';
@@ -23,7 +27,7 @@ import {
  * such as "Edit" and "View"
  */
 export
-interface IJupyterLabMenu extends Menu {
+interface IJupyterLabMenu extends IDisposable {
   /**
    * Add a group of menu items specific to a particular
    * plugin.
@@ -50,7 +54,14 @@ interface IMenuExtender<T extends Widget> {
  * An extensible menu for JupyterLab application menus.
  */
 export
-class JupyterLabMenu extends Menu implements IJupyterLabMenu {
+class JupyterLabMenu implements IJupyterLabMenu {
+  /**
+   * Construct a new menu.
+   */
+  constructor(options: Menu.IOptions) {
+    this.menu = new Menu(options);
+  }
+
   /**
    * Add a group of menu items specific to a particular
    * plugin.
@@ -74,18 +85,40 @@ class JupyterLabMenu extends Menu implements IJupyterLabMenu {
     // Insert a separator before the group.
     // Phosphor takes care of superfluous leading,
     // trailing, and duplicate separators.
-    this.insertItem(insertIndex++, { type: 'separator' });
+    this.menu.insertItem(insertIndex++, { type: 'separator' });
     // Insert the group.
     for (let item of items) {
-      this.insertItem(insertIndex++, item);
+      this.menu.insertItem(insertIndex++, item);
     }
     // Insert a separator after the group.
-    this.insertItem(insertIndex++, { type: 'separator' });
+    this.menu.insertItem(insertIndex++, { type: 'separator' });
 
     ArrayExt.insert(this._groups, groupIndex, rankGroup);
   }
 
+  /**
+   * The underlying Phosphor menu.
+   */
+  readonly menu: Menu;
+
+  /**
+   * Whether the menu has been disposed.
+   */
+  get isDisposed(): boolean {
+    return this._isDisposed;
+  }
+
+  /**
+   * Dispose of the resources held by the menu.
+   */
+  dispose(): void {
+    this._groups.length = 0;
+    this._isDisposed = true;
+    this.menu.dispose();
+  }
+
   private _groups: Private.IRankGroup[] = [];
+  private _isDisposed = false;
 }
 
 

+ 19 - 6
packages/mainmenu/src/mainmenu.ts

@@ -129,12 +129,12 @@ class MainMenu extends MenuBar implements IMainMenu {
     this.runMenu = new RunMenu({ commands });
     this.viewMenu = new ViewMenu({ commands });
 
-    this.addMenu(this.fileMenu, { rank: 0 });
-    this.addMenu(this.editMenu, { rank: 1 });
-    this.addMenu(this.runMenu, { rank: 2 });
-    this.addMenu(this.kernelMenu, { rank: 3 });
-    this.addMenu(this.viewMenu, { rank: 4 });
-    this.addMenu(this.helpMenu, { rank: 1000 });
+    this.addMenu(this.fileMenu.menu, { rank: 0 });
+    this.addMenu(this.editMenu.menu, { rank: 1 });
+    this.addMenu(this.runMenu.menu, { rank: 2 });
+    this.addMenu(this.kernelMenu.menu, { rank: 3 });
+    this.addMenu(this.viewMenu.menu, { rank: 4 });
+    this.addMenu(this.helpMenu.menu, { rank: 1000 });
   }
 
   /**
@@ -190,6 +190,19 @@ class MainMenu extends MenuBar implements IMainMenu {
     this.insertMenu(index, menu);
   }
 
+  /**
+   * Dispose of the resources held by the menu bar.
+   */
+  dispose(): void {
+    this.editMenu.dispose();
+    this.fileMenu.dispose();
+    this.helpMenu.dispose();
+    this.kernelMenu.dispose();
+    this.runMenu.dispose();
+    this.viewMenu.dispose();
+    super.dispose();
+  }
+
   /**
    * Handle the disposal of a menu.
    */

+ 9 - 1
packages/mainmenu/src/run.ts

@@ -33,7 +33,7 @@ class RunMenu extends JupyterLabMenu implements IRunMenu {
    */
   constructor(options: Menu.IOptions) {
     super(options);
-    this.title.label = 'Run';
+    this.menu.title.label = 'Run';
 
     this.codeRunners =
       new Map<string, IRunMenu.ICodeRunner<Widget>>();
@@ -46,6 +46,14 @@ class RunMenu extends JupyterLabMenu implements IRunMenu {
    * The key for the map may be used in menu labels.
    */
   readonly codeRunners: Map<string, IRunMenu.ICodeRunner<Widget>>;
+
+  /**
+   * Dispose of the resources held by the run menu.
+   */
+  dispose(): void {
+    this.codeRunners.clear();
+    super.dispose();
+  }
 }
 
 /**

+ 10 - 2
packages/mainmenu/src/view.ts

@@ -33,19 +33,27 @@ class ViewMenu extends JupyterLabMenu implements IViewMenu {
    */
   constructor(options: Menu.IOptions) {
     super(options);
-    this.title.label = 'View';
+    this.menu.title.label = 'View';
 
     this.editorViewers =
       new Map<string, IViewMenu.IEditorViewer<Widget>>();
   }
 
   /**
-   * A map storing IKernelUsers for the Kernel menu.
+   * A map storing IEditorViewers for the View menu.
    *
    * ### Notes
    * The key for the map may be used in menu labels.
    */
   readonly editorViewers: Map<string, IViewMenu.IEditorViewer<Widget>>;
+
+  /**
+   * Dispose of the resources held by the view menu.
+   */
+  dispose(): void {
+    this.editorViewers.clear();
+    super.dispose();
+  }
 }
 
 /**