Browse Source

Put widget id into args instead of separate command.

Ian Rose 6 years ago
parent
commit
b39b15d740
1 changed files with 24 additions and 24 deletions
  1. 24 24
      packages/mainmenu-extension/src/index.ts

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

@@ -1,7 +1,7 @@
 // Copyright (c) Jupyter Development Team.
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 // Distributed under the terms of the Modified BSD License.
 
 
-import { each } from '@phosphor/algorithm';
+import { each, find } from '@phosphor/algorithm';
 
 
 import { IDisposable } from '@phosphor/disposable';
 import { IDisposable } from '@phosphor/disposable';
 
 
@@ -32,9 +32,6 @@ import { ServerConnection } from '@jupyterlab/services';
  * A namespace for command IDs of semantic extension points.
  * A namespace for command IDs of semantic extension points.
  */
  */
 export namespace CommandIDs {
 export namespace CommandIDs {
-  export const activatePreviouslyUsedTab =
-    'tabmenu:activate-previously-used-tab';
-
   export const undo = 'editmenu:undo';
   export const undo = 'editmenu:undo';
 
 
   export const redo = 'editmenu:redo';
   export const redo = 'editmenu:redo';
@@ -84,6 +81,11 @@ export namespace CommandIDs {
   export const runAbove = 'runmenu:run-above';
   export const runAbove = 'runmenu:run-above';
 
 
   export const runBelow = 'runmenu:run-below';
   export const runBelow = 'runmenu:run-below';
+
+  export const activateById = 'tabmenu:activate-by-id';
+
+  export const activatePreviouslyUsedTab =
+    'tabmenu:activate-previously-used-tab';
 }
 }
 
 
 /**
 /**
@@ -636,32 +638,27 @@ export function createTabsMenu(app: JupyterLab, menu: TabsMenu): void {
   // A disposable for getting rid of the out-of-date tabs list.
   // A disposable for getting rid of the out-of-date tabs list.
   let disposable: IDisposable;
   let disposable: IDisposable;
 
 
-  // Utility function to create a command to activate
-  // a given tab, or get it if it already exists.
-  const createMenuItem = (widget: Widget): Menu.IItemOptions => {
-    const commandID = `tabmenu:activate-${widget.id}`;
-    if (!commands.hasCommand(commandID)) {
-      const disposeCommand = commands.addCommand(commandID, {
-        label: () => widget.title.label,
-        isVisible: () => !widget.isDisposed,
-        isEnabled: () => !widget.isDisposed,
-        isToggled: () => app.shell.currentWidget === widget,
-        execute: () => app.shell.activateById(widget.id)
-      });
-      widget.disposed.connect(() => disposeCommand.dispose());
-    }
-    return { command: commandID };
-  };
+  // Command to activate a widget by id.
+  commands.addCommand(CommandIDs.activateById, {
+    label: args => {
+      const id = args['id'] || '';
+      const widget = find(app.shell.widgets('main'), w => w.id === id);
+      return (widget && widget.title.label) || '';
+    },
+    isToggled: args => {
+      const id = args['id'] || '';
+      return app.shell.currentWidget && app.shell.currentWidget.id === id;
+    },
+    execute: args => app.shell.activateById((args['id'] as string) || '')
+  });
 
 
   let previousId = '';
   let previousId = '';
-
   // Command to toggle between the current
   // Command to toggle between the current
   // tab and the last modified tab.
   // tab and the last modified tab.
   commands.addCommand(CommandIDs.activatePreviouslyUsedTab, {
   commands.addCommand(CommandIDs.activatePreviouslyUsedTab, {
     label: 'Activate Previously Used Tab',
     label: 'Activate Previously Used Tab',
     isEnabled: () => !!previousId,
     isEnabled: () => !!previousId,
-    execute: () =>
-      previousId && app.commands.execute(`tabmenu:activate-${previousId}`)
+    execute: () => commands.execute(CommandIDs.activateById, { id: previousId })
   });
   });
 
 
   app.restored.then(() => {
   app.restored.then(() => {
@@ -680,7 +677,10 @@ export function createTabsMenu(app: JupyterLab, menu: TabsMenu): void {
         if (widget.id === previousId) {
         if (widget.id === previousId) {
           isPreviouslyUsedTabAttached = true;
           isPreviouslyUsedTabAttached = true;
         }
         }
-        tabGroup.push(createMenuItem(widget));
+        tabGroup.push({
+          command: CommandIDs.activateById,
+          args: { id: widget.id }
+        });
       });
       });
       disposable = menu.addGroup(tabGroup, 1);
       disposable = menu.addGroup(tabGroup, 1);
       previousId = isPreviouslyUsedTabAttached ? previousId : '';
       previousId = isPreviouslyUsedTabAttached ? previousId : '';