Browse Source

More activation handling

Steven Silvester 8 years ago
parent
commit
e8d6ef3344
2 changed files with 64 additions and 49 deletions
  1. 53 43
      src/console/plugin.ts
  2. 11 6
      src/editorwidget/plugin.ts

+ 53 - 43
src/console/plugin.ts

@@ -199,15 +199,24 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   });
   palette.addItem({ command, category });
 
+  // Get the current widget and activate unless the args specify otherwise.
+  function getCurrent(args: JSONObject): ConsolePanel | null {
+    let widget = tracker.currentWidget;
+    if (widget && (args['activate'] !== false)) {
+      widget.activate();
+    }
+    return widget;
+  }
+
   command = CommandIDs.clear;
   commands.addCommand(command, {
     label: 'Clear Cells',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        current.console.clear();
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
       }
+      current.console.clear();
     }
   });
   palette.addItem({ command, category });
@@ -215,12 +224,12 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.run;
   commands.addCommand(command, {
     label: 'Run Cell',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        current.console.execute();
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
       }
+      current.console.execute();
     }
   });
   palette.addItem({ command, category });
@@ -228,12 +237,12 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.runForced;
   commands.addCommand(command, {
     label: 'Run Cell (forced)',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        current.console.execute(true);
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
       }
+      current.console.execute(true);
     }
   });
   palette.addItem({ command, category });
@@ -241,12 +250,12 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.linebreak;
   commands.addCommand(command, {
     label: 'Insert Line Break',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        current.console.insertLinebreak();
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
       }
+      current.console.insertLinebreak();
     }
   });
   palette.addItem({ command, category });
@@ -254,14 +263,14 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.interrupt;
   commands.addCommand(command, {
     label: 'Interrupt Kernel',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        let kernel = current.console.session.kernel;
-        if (kernel) {
-          return kernel.interrupt();
-        }
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
+      }
+      let kernel = current.console.session.kernel;
+      if (kernel) {
+        return kernel.interrupt();
       }
     }
   });
@@ -270,14 +279,14 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.restart;
   commands.addCommand(command, {
     label: 'Restart Kernel',
-    execute: () => {
-      let current = tracker.currentWidget;
-      if (current) {
-        app.shell.activateMain(current.id);
-        let kernel = current.console.session.kernel;
-        if (kernel) {
-          return kernel.restart();
-        }
+    execute: args => {
+      let current = getCurrent(args);
+      if (!current) {
+        return;
+      }
+      let kernel = current.console.session.kernel;
+      if (kernel) {
+        return kernel.restart();
       }
     }
   });
@@ -286,13 +295,12 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.closeAndShutdown;
   commands.addCommand(command, {
     label: 'Close and Shutdown',
-    execute: () => {
-      let current = tracker.currentWidget;
+    execute: args => {
+      let current = getCurrent(args);
       if (!current) {
         return;
       }
-      app.shell.activateMain(current.id);
-      showDialog({
+      return showDialog({
         title: 'Shutdown the console?',
         body: `Are you sure you want to close "${current.title.label}"?`,
         buttons: [cancelButton, warnButton]
@@ -314,6 +322,9 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
       let id = args['id'];
       tracker.find(widget => {
         if (widget.console.session.id === id) {
+          if (args['activate'] !== false) {
+            widget.activate();
+          }
           widget.console.inject(args['code'] as string);
           return true;
         }
@@ -416,12 +427,11 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   command = CommandIDs.switchKernel;
   commands.addCommand(command, {
     label: 'Switch Kernel',
-    execute: () => {
-      let current = tracker.currentWidget;
+    execute: args => {
+      let current = getCurrent(args);
       if (!current) {
         return;
       }
-      app.shell.activateMain(current.id);
       let widget = current.console;
       let session = widget.session;
       let lang = '';

+ 11 - 6
src/editorwidget/plugin.ts

@@ -154,14 +154,15 @@ function activate(app: JupyterLab, registry: IDocumentRegistry, restorer: IInsta
   });
 
   commands.addCommand(CommandIDs.createConsole, {
-    execute: () => {
+    execute: args => {
       let widget = tracker.currentWidget;
       if (!widget) {
         return;
       }
-      let options: any = {
+      let options: JSONObject = {
         path: widget.context.path,
-        preferredLanguage: widget.context.model.defaultKernelLanguage
+        preferredLanguage: widget.context.model.defaultKernelLanguage,
+        activate: args['activate']
       };
       return commands.execute(ConsoleCommandIDs.create, options)
         .then(id => { sessionIdProperty.set(widget, id); });
@@ -170,7 +171,7 @@ function activate(app: JupyterLab, registry: IDocumentRegistry, restorer: IInsta
   });
 
   commands.addCommand(CommandIDs.runCode, {
-    execute: () => {
+    execute: args => {
       let widget = tracker.currentWidget;
       if (!widget) {
         return;
@@ -185,8 +186,12 @@ function activate(app: JupyterLab, registry: IDocumentRegistry, restorer: IInsta
       const selection = editor.getSelection();
       const start = editor.getOffsetAt(selection.start);
       const end = editor.getOffsetAt(selection.end);
-      const code = editor.model.value.text.substring(start, end);
-      return commands.execute(ConsoleCommandIDs.inject, { id, code });
+      const options: JSONObject = {
+        id,
+        code: editor.model.value.text.substring(start, end),
+        activate: args['activate']
+      };
+      return commands.execute(ConsoleCommandIDs.inject, options);
     },
     label: 'Run Code'
   });