Browse Source

Merge pull request #2135 from blink1073/console-commands

Handle command enabling
Afshin Darian 8 years ago
parent
commit
366fd534c6

+ 27 - 10
packages/console-extension/src/index.ts

@@ -81,6 +81,7 @@ namespace CommandIDs {
   const switchKernel = 'console:switch-kernel';
   const switchKernel = 'console:switch-kernel';
 };
 };
 
 
+
 /**
 /**
  * The console widget tracker provider.
  * The console widget tracker provider.
  */
  */
@@ -184,6 +185,13 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
     });
     });
   }
   }
 
 
+  /**
+   * Whether there is an active console.
+   */
+  function hasWidget(): boolean {
+    return tracker.currentWidget !== null;
+  }
+
   command = CommandIDs.open;
   command = CommandIDs.open;
   commands.addCommand(command, {
   commands.addCommand(command, {
     execute: (args: Partial<ConsolePanel.IOptions>) => {
     execute: (args: Partial<ConsolePanel.IOptions>) => {
@@ -205,7 +213,7 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
           }
           }
         });
         });
       }
       }
-    }
+    },
   });
   });
 
 
   command = CommandIDs.create;
   command = CommandIDs.create;
@@ -237,7 +245,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       current.console.clear();
       current.console.clear();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -250,7 +259,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       return current.console.execute();
       return current.console.execute();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -263,7 +273,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       current.console.execute(true);
       current.console.execute(true);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -276,7 +287,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       current.console.insertLinebreak();
       current.console.insertLinebreak();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -292,7 +304,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
       if (kernel) {
       if (kernel) {
         return kernel.interrupt();
         return kernel.interrupt();
       }
       }
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -305,7 +318,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       return current.console.session.restart();
       return current.console.session.restart();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 
@@ -330,7 +344,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
           return false;
           return false;
         }
         }
     });
     });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
 
 
   command = CommandIDs.inject;
   command = CommandIDs.inject;
@@ -346,7 +361,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
           return true;
           return true;
         }
         }
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
 
 
   command = CommandIDs.switchKernel;
   command = CommandIDs.switchKernel;
@@ -358,7 +374,8 @@ function activateConsole(app: JupyterLab, manager: IServiceManager, rendermime:
         return;
         return;
       }
       }
       return current.console.session.selectKernel();
       return current.console.session.selectKernel();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   palette.addItem({ command, category });
   palette.addItem({ command, category });
 
 

+ 95 - 40
packages/notebook-extension/src/index.ts

@@ -415,6 +415,13 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
     return widget;
     return widget;
   }
   }
 
 
+  /**
+   * Whether there is an active notebook.
+   */
+  function hasWidget(): boolean {
+    return tracker.currentWidget !== null;
+  }
+
   commands.addCommand(CommandIDs.runAndAdvance, {
   commands.addCommand(CommandIDs.runAndAdvance, {
     label: 'Run Cell(s) and Advance',
     label: 'Run Cell(s) and Advance',
     execute: args => {
     execute: args => {
@@ -424,7 +431,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       }
       }
       let content = current.notebook;
       let content = current.notebook;
       return NotebookActions.runAndAdvance(content, current.context.session);
       return NotebookActions.runAndAdvance(content, current.context.session);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.run, {
   commands.addCommand(CommandIDs.run, {
     label: 'Run Cell(s)',
     label: 'Run Cell(s)',
@@ -434,7 +442,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.run(current.notebook, current.context.session);
       return NotebookActions.run(current.notebook, current.context.session);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.runAndInsert, {
   commands.addCommand(CommandIDs.runAndInsert, {
     label: 'Run Cell(s) and Insert',
     label: 'Run Cell(s) and Insert',
@@ -446,7 +455,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       return NotebookActions.runAndInsert(
       return NotebookActions.runAndInsert(
         current.notebook, current.context.session
         current.notebook, current.context.session
       );
       );
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.runAll, {
   commands.addCommand(CommandIDs.runAll, {
     label: 'Run All Cells',
     label: 'Run All Cells',
@@ -456,7 +466,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.runAll(current.notebook, current.context.session);
       return NotebookActions.runAll(current.notebook, current.context.session);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.restart, {
   commands.addCommand(CommandIDs.restart, {
     label: 'Restart Kernel',
     label: 'Restart Kernel',
@@ -466,7 +477,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       current.session.restart();
       current.session.restart();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.closeAndShutdown, {
   commands.addCommand(CommandIDs.closeAndShutdown, {
     label: 'Close and Shutdown',
     label: 'Close and Shutdown',
@@ -489,7 +501,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
           return;
           return;
         }
         }
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.trust, {
   commands.addCommand(CommandIDs.trust, {
     label: 'Trust Notebook',
     label: 'Trust Notebook',
@@ -501,7 +514,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       return trustNotebook(current.context.model).then(() => {
       return trustNotebook(current.context.model).then(() => {
         return current.context.save();
         return current.context.save();
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.restartClear, {
   commands.addCommand(CommandIDs.restartClear, {
     label: 'Restart Kernel & Clear Outputs',
     label: 'Restart Kernel & Clear Outputs',
@@ -513,7 +527,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       return current.session.restart().then(() => {
       return current.session.restart().then(() => {
         NotebookActions.clearAllOutputs(current.notebook);
         NotebookActions.clearAllOutputs(current.notebook);
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.restartRunAll, {
   commands.addCommand(CommandIDs.restartRunAll, {
     label: 'Restart Kernel & Run All',
     label: 'Restart Kernel & Run All',
@@ -525,7 +540,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       return current.session.restart().then(() => {
       return current.session.restart().then(() => {
         NotebookActions.runAll(current.notebook, current.context.session);
         NotebookActions.runAll(current.notebook, current.context.session);
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.clearAllOutputs, {
   commands.addCommand(CommandIDs.clearAllOutputs, {
     label: 'Clear All Outputs',
     label: 'Clear All Outputs',
@@ -535,7 +551,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.clearAllOutputs(current.notebook);
       return NotebookActions.clearAllOutputs(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.clearOutputs, {
   commands.addCommand(CommandIDs.clearOutputs, {
     label: 'Clear Output(s)',
     label: 'Clear Output(s)',
@@ -545,7 +562,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.clearOutputs(current.notebook);
       return NotebookActions.clearOutputs(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.interrupt, {
   commands.addCommand(CommandIDs.interrupt, {
     label: 'Interrupt Kernel',
     label: 'Interrupt Kernel',
@@ -558,7 +576,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
       if (kernel) {
       if (kernel) {
         return kernel.interrupt();
         return kernel.interrupt();
       }
       }
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.toCode, {
   commands.addCommand(CommandIDs.toCode, {
     label: 'Convert to Code',
     label: 'Convert to Code',
@@ -568,7 +587,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.changeCellType(current.notebook, 'code');
       return NotebookActions.changeCellType(current.notebook, 'code');
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.toMarkdown, {
   commands.addCommand(CommandIDs.toMarkdown, {
     label: 'Convert to Markdown',
     label: 'Convert to Markdown',
@@ -578,7 +598,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.changeCellType(current.notebook, 'markdown');
       return NotebookActions.changeCellType(current.notebook, 'markdown');
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.toRaw, {
   commands.addCommand(CommandIDs.toRaw, {
     label: 'Convert to Raw',
     label: 'Convert to Raw',
@@ -588,7 +609,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.changeCellType(current.notebook, 'raw');
       return NotebookActions.changeCellType(current.notebook, 'raw');
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.cut, {
   commands.addCommand(CommandIDs.cut, {
     label: 'Cut Cell(s)',
     label: 'Cut Cell(s)',
@@ -598,7 +620,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.cut(current.notebook);
       return NotebookActions.cut(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.copy, {
   commands.addCommand(CommandIDs.copy, {
     label: 'Copy Cell(s)',
     label: 'Copy Cell(s)',
@@ -608,7 +631,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.copy(current.notebook);
       return NotebookActions.copy(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.paste, {
   commands.addCommand(CommandIDs.paste, {
     label: 'Paste Cell(s)',
     label: 'Paste Cell(s)',
@@ -618,7 +642,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.paste(current.notebook);
       return NotebookActions.paste(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.deleteCell, {
   commands.addCommand(CommandIDs.deleteCell, {
     label: 'Delete Cell(s)',
     label: 'Delete Cell(s)',
@@ -628,7 +653,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.deleteCells(current.notebook);
       return NotebookActions.deleteCells(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.split, {
   commands.addCommand(CommandIDs.split, {
     label: 'Split Cell',
     label: 'Split Cell',
@@ -638,7 +664,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.splitCell(current.notebook);
       return NotebookActions.splitCell(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.merge, {
   commands.addCommand(CommandIDs.merge, {
     label: 'Merge Selected Cell(s)',
     label: 'Merge Selected Cell(s)',
@@ -648,7 +675,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.mergeCells(current.notebook);
       return NotebookActions.mergeCells(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.insertAbove, {
   commands.addCommand(CommandIDs.insertAbove, {
     label: 'Insert Cell Above',
     label: 'Insert Cell Above',
@@ -658,7 +686,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.insertAbove(current.notebook);
       return NotebookActions.insertAbove(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.insertBelow, {
   commands.addCommand(CommandIDs.insertBelow, {
     label: 'Insert Cell Below',
     label: 'Insert Cell Below',
@@ -668,7 +697,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.insertBelow(current.notebook);
       return NotebookActions.insertBelow(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.selectAbove, {
   commands.addCommand(CommandIDs.selectAbove, {
     label: 'Select Cell Above',
     label: 'Select Cell Above',
@@ -678,7 +708,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.selectAbove(current.notebook);
       return NotebookActions.selectAbove(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.selectBelow, {
   commands.addCommand(CommandIDs.selectBelow, {
     label: 'Select Cell Below',
     label: 'Select Cell Below',
@@ -688,7 +719,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.selectBelow(current.notebook);
       return NotebookActions.selectBelow(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.extendAbove, {
   commands.addCommand(CommandIDs.extendAbove, {
     label: 'Extend Selection Above',
     label: 'Extend Selection Above',
@@ -698,7 +730,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.extendSelectionAbove(current.notebook);
       return NotebookActions.extendSelectionAbove(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.extendBelow, {
   commands.addCommand(CommandIDs.extendBelow, {
     label: 'Extend Selection Below',
     label: 'Extend Selection Below',
@@ -708,7 +741,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.extendSelectionBelow(current.notebook);
       return NotebookActions.extendSelectionBelow(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.moveUp, {
   commands.addCommand(CommandIDs.moveUp, {
     label: 'Move Cell(s) Up',
     label: 'Move Cell(s) Up',
@@ -718,7 +752,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.moveUp(current.notebook);
       return NotebookActions.moveUp(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.moveDown, {
   commands.addCommand(CommandIDs.moveDown, {
     label: 'Move Cell(s) Down',
     label: 'Move Cell(s) Down',
@@ -728,7 +763,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.moveDown(current.notebook);
       return NotebookActions.moveDown(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.toggleLines, {
   commands.addCommand(CommandIDs.toggleLines, {
     label: 'Toggle Line Numbers',
     label: 'Toggle Line Numbers',
@@ -738,7 +774,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.toggleLineNumbers(current.notebook);
       return NotebookActions.toggleLineNumbers(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.toggleAllLines, {
   commands.addCommand(CommandIDs.toggleAllLines, {
     label: 'Toggle All Line Numbers',
     label: 'Toggle All Line Numbers',
@@ -748,7 +785,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.toggleAllLineNumbers(current.notebook);
       return NotebookActions.toggleAllLineNumbers(current.notebook);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.commandMode, {
   commands.addCommand(CommandIDs.commandMode, {
     label: 'To Command Mode',
     label: 'To Command Mode',
@@ -758,7 +796,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return current.notebook.mode = 'command';
       return current.notebook.mode = 'command';
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.editMode, {
   commands.addCommand(CommandIDs.editMode, {
     label: 'To Edit Mode',
     label: 'To Edit Mode',
@@ -768,7 +807,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       current.notebook.mode = 'edit';
       current.notebook.mode = 'edit';
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.undo, {
   commands.addCommand(CommandIDs.undo, {
     label: 'Undo Cell Operation',
     label: 'Undo Cell Operation',
@@ -778,6 +818,10 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.undo(current.notebook);
       return NotebookActions.undo(current.notebook);
+    },
+    isEnabled: () => {
+      let current = tracker.currentWidget;
+      return current && current.notebook.model.cells.canUndo;
     }
     }
   });
   });
   commands.addCommand(CommandIDs.redo, {
   commands.addCommand(CommandIDs.redo, {
@@ -788,6 +832,10 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.redo(current.notebook);
       return NotebookActions.redo(current.notebook);
+    },
+    isEnabled: () => {
+      let current = tracker.currentWidget;
+      return current && current.notebook.model.cells.canRedo;
     }
     }
   });
   });
   commands.addCommand(CommandIDs.switchKernel, {
   commands.addCommand(CommandIDs.switchKernel, {
@@ -798,7 +846,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return current.context.session.selectKernel();
       return current.context.session.selectKernel();
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown1, {
   commands.addCommand(CommandIDs.markdown1, {
     label: 'Markdown Header 1',
     label: 'Markdown Header 1',
@@ -808,7 +857,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 1);
       return NotebookActions.setMarkdownHeader(current.notebook, 1);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown2, {
   commands.addCommand(CommandIDs.markdown2, {
     label: 'Markdown Header 2',
     label: 'Markdown Header 2',
@@ -818,7 +868,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 2);
       return NotebookActions.setMarkdownHeader(current.notebook, 2);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown3, {
   commands.addCommand(CommandIDs.markdown3, {
     label: 'Markdown Header 3',
     label: 'Markdown Header 3',
@@ -828,7 +879,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 3);
       return NotebookActions.setMarkdownHeader(current.notebook, 3);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown4, {
   commands.addCommand(CommandIDs.markdown4, {
     label: 'Markdown Header 4',
     label: 'Markdown Header 4',
@@ -838,7 +890,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 4);
       return NotebookActions.setMarkdownHeader(current.notebook, 4);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown5, {
   commands.addCommand(CommandIDs.markdown5, {
     label: 'Markdown Header 5',
     label: 'Markdown Header 5',
@@ -848,7 +901,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 5);
       return NotebookActions.setMarkdownHeader(current.notebook, 5);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   commands.addCommand(CommandIDs.markdown6, {
   commands.addCommand(CommandIDs.markdown6, {
     label: 'Markdown Header 6',
     label: 'Markdown Header 6',
@@ -858,7 +912,8 @@ function addCommands(app: JupyterLab, services: IServiceManager, tracker: Notebo
         return;
         return;
       }
       }
       return NotebookActions.setMarkdownHeader(current.notebook, 6);
       return NotebookActions.setMarkdownHeader(current.notebook, 6);
-    }
+    },
+    isEnabled: hasWidget
   });
   });
   }
   }
 
 

+ 2 - 1
packages/terminal-extension/src/index.ts

@@ -153,7 +153,8 @@ function activate(app: JupyterLab, services: IServiceManager, mainMenu: IMainMen
       return current.refresh().then(() => {
       return current.refresh().then(() => {
         current.activate();
         current.activate();
       });
       });
-    }
+    },
+    isEnabled: () => { return tracker.currentWidget !== null; }
   });
   });
 
 
   // Add command palette and menu items.
   // Add command palette and menu items.

+ 13 - 3
packages/terminal/src/index.ts

@@ -41,6 +41,13 @@ const ITerminalTracker = new Token<ITerminalTracker>('jupyter.services.terminal-
 export
 export
 function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry) {
 function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry) {
 
 
+  /**
+   * Whether there is an active terminal.
+   */
+  function hasWidget(): boolean {
+    return tracker.currentWidget !== null;
+  }
+
   commands.addCommand('terminal:increase-font', {
   commands.addCommand('terminal:increase-font', {
     label: 'Increase Terminal Font Size',
     label: 'Increase Terminal Font Size',
     execute: () => {
     execute: () => {
@@ -49,7 +56,8 @@ function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry
         options.fontSize++;
         options.fontSize++;
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
       }
       }
-    }
+    },
+    isEnabled: hasWidget
   });
   });
 
 
   commands.addCommand('terminal:decrease-font', {
   commands.addCommand('terminal:decrease-font', {
@@ -60,7 +68,8 @@ function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry
         options.fontSize--;
         options.fontSize--;
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
         tracker.forEach(widget => { widget.fontSize = options.fontSize; });
       }
       }
-    }
+    },
+    isEnabled: hasWidget
   });
   });
 
 
   commands.addCommand('terminal:toggle-theme', {
   commands.addCommand('terminal:toggle-theme', {
@@ -79,6 +88,7 @@ function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry
         widget.background = options.background;
         widget.background = options.background;
         widget.color = options.color;
         widget.color = options.color;
       });
       });
-    }
+    },
+    isEnabled: hasWidget
   });
   });
 }
 }