Selaa lähdekoodia

Only enable commands if the application current widget belongs to them.

Ian Rose 7 vuotta sitten
vanhempi
commit
7703d7f2fa

+ 8 - 7
packages/codemirror-extension/src/index.ts

@@ -153,8 +153,9 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
   /**
    * A test for whether the tracker has an active widget.
    */
-  function hasWidget(): boolean {
-    return tracker.currentWidget !== null;
+  function isEnabled(): boolean {
+    return tracker.currentWidget !== null &&
+           tracker.currentWidget === app.shell.currentWidget;
   }
 
   /**
@@ -184,7 +185,7 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
           console.error(`Failed to set ${id}:${key} - ${reason.message}`);
         });
       },
-      isEnabled: hasWidget,
+      isEnabled,
       isToggled: args => args['theme'] === theme
     });
 
@@ -202,7 +203,7 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
           console.error(`Failed to set ${id}:${key} - ${reason.message}`);
         });
       },
-      isEnabled: hasWidget,
+      isEnabled,
       isToggled: args => args['keyMap'] === keyMap
     });
 
@@ -216,7 +217,7 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
         let editor = widget.editor as CodeMirrorEditor;
         editor.execCommand('find');
       },
-      isEnabled: hasWidget
+      isEnabled
     });
 
     commands.addCommand(CommandIDs.findAndReplace, {
@@ -229,7 +230,7 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
         let editor = widget.editor as CodeMirrorEditor;
         editor.execCommand('replace');
       },
-      isEnabled: hasWidget
+      isEnabled
     });
 
     commands.addCommand(CommandIDs.changeMode, {
@@ -244,7 +245,7 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
           }
         }
       },
-      isEnabled: hasWidget,
+      isEnabled,
       isToggled: args => {
         let widget = tracker.currentWidget;
         if (!widget) {

+ 12 - 11
packages/console-extension/src/index.ts

@@ -204,8 +204,9 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
   /**
    * Whether there is an active console.
    */
-  function hasWidget(): boolean {
-    return tracker.currentWidget !== null;
+  function isEnabled(): boolean {
+    return tracker.currentWidget !== null
+           && tracker.currentWidget === app.shell.currentWidget;
   }
 
   let command = CommandIDs.open;
@@ -260,7 +261,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       current.console.clear();
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -274,7 +275,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       return current.console.execute();
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -288,7 +289,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       current.console.execute(true);
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -302,7 +303,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       current.console.insertLinebreak();
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -319,7 +320,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
         return kernel.interrupt();
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -333,7 +334,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       return current.console.session.restart();
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 
@@ -359,7 +360,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
         }
       });
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 
   command = CommandIDs.inject;
@@ -377,7 +378,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
         return false;
       });
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 
   command = CommandIDs.changeKernel;
@@ -390,7 +391,7 @@ function activateConsole(app: JupyterLab, mainMenu: IMainMenu, palette: ICommand
       }
       return current.console.session.selectKernel();
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   palette.addItem({ command, category });
 

+ 9 - 8
packages/fileeditor-extension/src/index.ts

@@ -109,7 +109,8 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
   });
   const { commands, restored } = app;
   const tracker = new InstanceTracker<FileEditor>({ namespace });
-  const hasWidget = () => !!tracker.currentWidget;
+  const isEnabled = () => tracker.currentWidget !== null &&
+                          tracker.currentWidget === app.shell.currentWidget;
 
   let {
     lineNumbers, lineWrap, matchBrackets, autoClosingBrackets
@@ -192,7 +193,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
         console.error(`Failed to set ${id}:${key} - ${reason.message}`);
       });
     },
-    isEnabled: hasWidget,
+    isEnabled,
     isToggled: () => lineNumbers,
     label: 'Line Numbers'
   });
@@ -207,7 +208,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
         console.error(`Failed to set ${id}:${key} - ${reason.message}`);
       });
     },
-    isEnabled: hasWidget,
+    isEnabled,
     isToggled: () => lineWrap,
     label: 'Word Wrap'
   });
@@ -225,7 +226,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
       editor.setOption('insertSpaces', insertSpaces);
       editor.setOption('tabSize', size);
     },
-    isEnabled: hasWidget,
+    isEnabled,
     isToggled: args => {
       let widget = tracker.currentWidget;
       if (!widget) {
@@ -250,7 +251,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
       return settingRegistry.set(id, 'matchBrackets', matchBrackets);
     },
     label: 'Match Brackets',
-    isEnabled: hasWidget,
+    isEnabled,
     isToggled: () => matchBrackets
   });
 
@@ -264,7 +265,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
         .set(id, 'autoClosingBrackets', autoClosingBrackets);
     },
     label: 'Auto-Closing Brackets',
-    isEnabled: hasWidget,
+    isEnabled,
     isToggled: () => autoClosingBrackets
   });
 
@@ -282,7 +283,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
         preferredLanguage: widget.context.model.defaultKernelLanguage
       });
     },
-    isEnabled: hasWidget,
+    isEnabled,
     label: 'Create Console for Editor'
   });
 
@@ -340,7 +341,7 @@ function activate(app: JupyterLab, editorServices: IEditorServices, browserFacto
         return Promise.resolve(void 0);
       }
     },
-    isEnabled: hasWidget,
+    isEnabled,
     label: 'Run Code'
   });
 

+ 56 - 55
packages/notebook-extension/src/index.ts

@@ -535,8 +535,9 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   /**
    * Whether there is an active notebook.
    */
-  function hasWidget(): boolean {
-    return tracker.currentWidget !== null;
+  function isEnabled(): boolean {
+    return tracker.currentWidget !== null &&
+           tracker.currentWidget === app.shell.currentWidget;
   }
 
   /**
@@ -560,7 +561,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.runAndAdvance(notebook, context.session);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.run, {
     label: 'Run Cell(s)',
@@ -573,7 +574,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.run(notebook, context.session);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.runAndInsert, {
     label: 'Run Cell(s) and Insert Below',
@@ -586,7 +587,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.runAndInsert(notebook, context.session);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.runAll, {
     label: 'Run All Cells',
@@ -599,7 +600,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.runAll(notebook, context.session);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.restart, {
     label: 'Restart Kernel',
@@ -610,7 +611,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return current.session.restart();
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.closeAndShutdown, {
     label: 'Close and Shutdown',
@@ -634,7 +635,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         }
       });
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.trust, {
     label: () => `Trust ${currentName()}`,
@@ -647,7 +648,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.trust(notebook).then(() => context.save());
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.exportToFormat, {
     label: args => {
@@ -682,7 +683,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         resolve(undefined);
       });
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.restartClear, {
     label: 'Restart Kernel & Clear Outputs',
@@ -696,7 +697,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
           .then(() => { NotebookActions.clearAllOutputs(notebook); });
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.restartRunAll, {
     label: 'Restart Kernel & Run All',
@@ -710,7 +711,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
           .then(() => { NotebookActions.runAll(notebook, context.session); });
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.clearAllOutputs, {
     label: 'Clear All Outputs',
@@ -721,7 +722,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.clearAllOutputs(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.clearOutputs, {
     label: 'Clear Output(s)',
@@ -732,7 +733,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.clearOutputs(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.interrupt, {
     label: 'Interrupt Kernel',
@@ -749,7 +750,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return kernel.interrupt();
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.toCode, {
     label: 'Change to Code Cell Type',
@@ -760,7 +761,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.changeCellType(current.notebook, 'code');
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.toMarkdown, {
     label: 'Change to Markdown Cell Type',
@@ -771,7 +772,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.changeCellType(current.notebook, 'markdown');
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.toRaw, {
     label: 'Change to Raw Cell Type',
@@ -782,7 +783,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.changeCellType(current.notebook, 'raw');
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.cut, {
     label: 'Cut Cell(s)',
@@ -793,7 +794,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.cut(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.copy, {
     label: 'Copy Cell(s)',
@@ -804,7 +805,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.copy(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.paste, {
     label: 'Paste Cell(s) Below',
@@ -815,7 +816,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.paste(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.deleteCell, {
     label: 'Delete Cell(s)',
@@ -826,7 +827,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.deleteCells(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.split, {
     label: 'Split Cell',
@@ -837,7 +838,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.splitCell(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.merge, {
     label: 'Merge Selected Cell(s)',
@@ -848,7 +849,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.mergeCells(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.insertAbove, {
     label: 'Insert Cell Above',
@@ -859,7 +860,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.insertAbove(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.insertBelow, {
     label: 'Insert Cell Below',
@@ -870,7 +871,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.insertBelow(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.selectAbove, {
     label: 'Select Cell Above',
@@ -881,7 +882,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.selectAbove(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.selectBelow, {
     label: 'Select Cell Below',
@@ -892,7 +893,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.selectBelow(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.extendAbove, {
     label: 'Extend Selection Above',
@@ -903,7 +904,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.extendSelectionAbove(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.extendBelow, {
     label: 'Extend Selection Below',
@@ -914,7 +915,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.extendSelectionBelow(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.moveUp, {
     label: 'Move Cell(s) Up',
@@ -925,7 +926,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.moveUp(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.moveDown, {
     label: 'Move Cell(s) Down',
@@ -936,7 +937,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.moveDown(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.toggleAllLines, {
     label: 'Toggle All Line Numbers',
@@ -947,7 +948,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.toggleAllLineNumbers(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.commandMode, {
     label: 'Enter Command Mode',
@@ -958,7 +959,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         current.notebook.mode = 'command';
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.editMode, {
     label: 'Enter Edit Mode',
@@ -969,7 +970,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         current.notebook.mode = 'edit';
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.undoCellAction, {
     label: 'Undo Cell Operation',
@@ -980,7 +981,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.undo(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.redoCellAction, {
     label: 'Redo Cell Operation',
@@ -991,7 +992,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.redo(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.changeKernel, {
     label: 'Change Kernel',
@@ -1002,7 +1003,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return current.context.session.selectKernel();
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.reconnectToKernel, {
     label: 'Reconnect To Kernel',
@@ -1019,7 +1020,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return kernel.reconnect();
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.createCellView, {
     label: 'Create New View for Cell',
@@ -1040,7 +1041,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
       p.addWidget(newCell);
       shell.addToMainArea(p);
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.createConsole, {
     label: 'Create Console for Notebook',
@@ -1060,7 +1061,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
 
       return commands.execute('console:create', options);
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown1, {
     label: 'Change to Heading 1',
@@ -1071,7 +1072,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 1);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown2, {
     label: 'Change to Heading 2',
@@ -1082,7 +1083,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 2);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown3, {
     label: 'Change to Heading 3',
@@ -1093,7 +1094,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 3);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown4, {
     label: 'Change to Heading 4',
@@ -1104,7 +1105,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 4);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown5, {
     label: 'Change to Heading 5',
@@ -1115,7 +1116,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 5);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.markdown6, {
     label: 'Change to Heading 6',
@@ -1126,7 +1127,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.setMarkdownHeader(current.notebook, 6);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.hideCode, {
     label: 'Hide Code',
@@ -1137,7 +1138,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.hideCode(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.showCode, {
     label: 'Show Code',
@@ -1148,7 +1149,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.showCode(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.hideAllCode, {
     label: 'Hide All Code',
@@ -1159,7 +1160,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.hideAllCode(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.showAllCode, {
     label: 'Show All Code',
@@ -1170,7 +1171,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.showAllCode(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.hideOutput, {
     label: 'Hide Output',
@@ -1181,7 +1182,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.hideOutput(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.showOutput, {
     label: 'Show Output',
@@ -1192,7 +1193,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.showOutput(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.hideAllOutputs, {
     label: 'Hide All Outputs',
@@ -1203,7 +1204,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.hideAllOutputs(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
   commands.addCommand(CommandIDs.showAllOutputs, {
     label: 'Show All Outputs',
@@ -1214,7 +1215,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return NotebookActions.showAllOutputs(current.notebook);
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 }
 

+ 6 - 5
packages/terminal-extension/src/index.ts

@@ -152,8 +152,9 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Instanc
   /**
    * Whether there is an active terminal.
    */
-  function hasWidget(): boolean {
-    return tracker.currentWidget !== null;
+  function isEnabled(): boolean {
+    return tracker.currentWidget !== null &&
+           tracker.currentWidget === app.shell.currentWidget;
   }
 
   // Add terminal commands.
@@ -226,7 +227,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Instanc
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 
   commands.addCommand('terminal:decrease-font', {
@@ -238,7 +239,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Instanc
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
       }
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 
   let terminalTheme: Terminal.Theme = 'dark';
@@ -254,7 +255,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Instanc
         }
       });
     },
-    isEnabled: hasWidget
+    isEnabled
   });
 }