Procházet zdrojové kódy

Clean up kernel status item, remove dependency on IFilePath.

Ian Rose před 6 roky
rodič
revize
fcf4cc85d6

+ 0 - 3
packages/statusbar-extension/package.json

@@ -37,17 +37,14 @@
     "@jupyterlab/apputils": "^0.19.1",
     "@jupyterlab/cells": "^0.19.1",
     "@jupyterlab/codeeditor": "^0.19.1",
-    "@jupyterlab/codemirror": "^0.19.1",
     "@jupyterlab/console": "^0.19.1",
     "@jupyterlab/coreutils": "^2.2.1",
     "@jupyterlab/docmanager": "^0.19.1",
     "@jupyterlab/docregistry": "^0.19.1",
-    "@jupyterlab/filebrowser": "^0.19.3",
     "@jupyterlab/fileeditor": "^0.19.1",
     "@jupyterlab/notebook": "^0.19.2",
     "@jupyterlab/services": "^3.2.1",
     "@jupyterlab/statusbar": "^0.7.1",
-    "@phosphor/algorithm": "^1.1.2",
     "@phosphor/commands": "^1.6.1",
     "@phosphor/coreutils": "^1.3.0",
     "@phosphor/disposable": "^1.1.2",

+ 0 - 1
packages/statusbar-extension/src/defaults/index.ts

@@ -1,5 +1,4 @@
 export * from './lineCol';
-export * from './fileUpload';
 export * from './kernelStatus';
 export * from './runningSessions';
 export * from './filePath';

+ 58 - 66
packages/statusbar-extension/src/defaults/kernelStatus.tsx

@@ -5,7 +5,6 @@
  * Part of Jupyterlab status bar defaults.
  */
 import React from 'react';
-import { IStatusBar, TextItem, TextExt } from '@jupyterlab/statusbar';
 
 import {
   JupyterLabPlugin,
@@ -13,20 +12,32 @@ import {
   ApplicationShell
 } from '@jupyterlab/application';
 
-import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
+import { IClientSession, VDomRenderer, VDomModel } from '@jupyterlab/apputils';
 
 import { IConsoleTracker, ConsolePanel } from '@jupyterlab/console';
-import { IClientSession, VDomRenderer, VDomModel } from '@jupyterlab/apputils';
-import { ISignal } from '@phosphor/signaling';
-import { Token } from '@phosphor/coreutils';
-import { IDisposable } from '@phosphor/disposable';
+
+import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
+
 import { Kernel, Session } from '@jupyterlab/services';
-import { Widget } from '@phosphor/widgets';
-import { IStatusContext } from '../contexts';
+
+import {
+  interactiveItem,
+  IStatusBar,
+  TextItem,
+  TextExt
+} from '@jupyterlab/statusbar';
+
 import { CommandRegistry } from '@phosphor/commands';
-import { interactiveItem } from '@jupyterlab/statusbar';
+
+import { IDisposable } from '@phosphor/disposable';
+
 import { Message } from '@phosphor/messaging';
-import { IFilePath } from './filePath';
+
+import { ISignal, Signal } from '@phosphor/signaling';
+
+import { Widget } from '@phosphor/widgets';
+
+import { IStatusContext } from '../contexts';
 
 // tslint:disable-next-line:variable-name
 const KernelStatusComponent = (
@@ -59,22 +70,21 @@ class KernelStatus extends VDomRenderer<KernelStatus.Model>
     this._consoleTracker = opts.consoleTracker;
     this._commands = opts.commands;
     this._shell = opts.shell;
-    this._filePath = opts.filePath;
 
-    this._shell.currentChanged.connect(this._onMainAreaCurrentChange);
-    this._filePath.model!.stateChanged.connect(this._onFilePathChange);
+    this._shell.currentChanged.connect(
+      this._onCurrentChanged,
+      this
+    );
 
     this.model = new KernelStatus.Model(
       this._getFocusedSession(this._shell.currentWidget)
     );
 
-    if (this.model!.type === 'notebook') {
-      this.addClass(interactiveItem);
-    }
-
-    this._onFilePathChange();
+    this.addClass(interactiveItem);
   }
 
+  readonly model: KernelStatus.Model;
+
   render() {
     if (this.model === null) {
       return null;
@@ -91,40 +101,30 @@ class KernelStatus extends VDomRenderer<KernelStatus.Model>
 
   dispose() {
     super.dispose();
-
-    this._shell.currentChanged.disconnect(this._onMainAreaCurrentChange);
+    Signal.disconnectAll(this);
+    this._shell.currentChanged.disconnect(this._onCurrentChanged);
   }
 
   protected onUpdateRequest(msg: Message) {
-    this.model!.session = this._getFocusedSession(this._shell.currentWidget);
-
-    if (this.model!.type === 'notebook') {
-      this.addClass(interactiveItem);
-    } else {
-      this.removeClass(interactiveItem);
-    }
-
+    this.model.session = this._getFocusedSession(this._shell.currentWidget);
     super.onUpdateRequest(msg);
   }
 
   private _handleClick = () => {
-    if (this.model!.type === 'notebook') {
-      this._commands.execute('notebook:change-kernel');
-    }
+    // The kernel menu flavor of change kernel delegates
+    // based on the active widget, so use that.
+    this._commands.execute('kernelmenu:change');
   };
 
-  private _onFilePathChange = () => {
-    if (this.model!.type === 'notebook') {
-      this.node.title = `Change active kernel for ${
-        this._filePath.model!.name
-      }`;
-    } else {
-      this.node.title = `Active kernel type for ${this._filePath.model!.name}`;
-    }
+  private _onTitleChanged = () => {
+    const name = this._shell.currentWidget
+      ? this._shell.currentWidget.title.label
+      : 'activity';
+    this.node.title = `Change active kernel for ${name}`;
   };
 
   private _getFocusedSession(val: Widget | null): IClientSession | null {
-    if (val === null) {
+    if (!val) {
       return null;
     } else {
       if (this._notebookTracker.has(val)) {
@@ -137,25 +137,27 @@ class KernelStatus extends VDomRenderer<KernelStatus.Model>
     }
   }
 
-  private _onMainAreaCurrentChange = (
+  private _onCurrentChanged(
     shell: ApplicationShell,
     change: ApplicationShell.IChangedArgs
-  ) => {
-    const { newValue } = change;
-    const editor = this._getFocusedSession(newValue);
-    this.model!.session = editor;
-    if (this.model!.type === 'notebook') {
-      this.addClass(interactiveItem);
-    } else {
-      this.removeClass(interactiveItem);
+  ): void {
+    if (this._current) {
+      this._current.title.changed.disconnect(this._onTitleChanged, this);
     }
-  };
+    this._current = change.newValue;
+    this._current.title.changed.connect(
+      this._onTitleChanged,
+      this
+    );
+    const session = this._getFocusedSession(this._current);
+    this.model.session = session;
+  }
 
+  private _current: Widget | undefined;
   private _notebookTracker: INotebookTracker;
   private _consoleTracker: IConsoleTracker;
   private _shell: ApplicationShell;
   private _commands: CommandRegistry;
-  private _filePath: IFilePath;
 }
 
 namespace KernelStatus {
@@ -252,7 +254,6 @@ namespace KernelStatus {
     consoleTracker: IConsoleTracker;
     shell: ApplicationShell;
     commands: CommandRegistry;
-    filePath: IFilePath;
   }
 }
 
@@ -270,28 +271,21 @@ export namespace IKernelStatus {
   }
 }
 
-// tslint:disable-next-line:variable-name
-export const IKernelStatus = new Token<IKernelStatus>(
-  '@jupyterlab/statusbar:IKernelStatus'
-);
-
-export const kernelStatusItem: JupyterLabPlugin<IKernelStatus> = {
-  id: '@jupyterlab/statusbar:kernel-status-item',
+export const kernelStatus: JupyterLabPlugin<void> = {
+  id: '@jupyterlab/statusbar:kernel-status',
   autoStart: true,
-  requires: [IStatusBar, INotebookTracker, IConsoleTracker, IFilePath],
+  requires: [IStatusBar, INotebookTracker, IConsoleTracker],
   activate: (
     app: JupyterLab,
     statusBar: IStatusBar,
     notebookTracker: INotebookTracker,
-    consoleTracker: IConsoleTracker,
-    filePath: IFilePath
+    consoleTracker: IConsoleTracker
   ) => {
     const item = new KernelStatus({
       shell: app.shell,
       notebookTracker,
       consoleTracker,
-      commands: app.commands,
-      filePath
+      commands: app.commands
     });
 
     statusBar.registerStatusItem('kernel-status-item', item, {
@@ -302,7 +296,5 @@ export const kernelStatusItem: JupyterLabPlugin<IKernelStatus> = {
         { tracker: consoleTracker }
       ])
     });
-
-    return item;
   }
 };

+ 2 - 2
packages/statusbar-extension/src/index.ts

@@ -9,7 +9,7 @@ import { IStatusBar, StatusBar } from '@jupyterlab/statusbar';
 
 import {
   lineColItem,
-  kernelStatusItem,
+  kernelStatus,
   runningSessionsItem,
   filePathItem,
   tabSpaceItem,
@@ -34,7 +34,7 @@ const statusBar: JupyterLabPlugin<IStatusBar> = {
 const plugins: JupyterLabPlugin<any>[] = [
   statusBar,
   lineColItem,
-  kernelStatusItem,
+  kernelStatus,
   runningSessionsItem,
   filePathItem,
   tabSpaceItem,

+ 0 - 6
packages/statusbar-extension/tsconfig.json

@@ -18,9 +18,6 @@
     {
       "path": "../codeeditor"
     },
-    {
-      "path": "../codemirror"
-    },
     {
       "path": "../console"
     },
@@ -33,9 +30,6 @@
     {
       "path": "../docregistry"
     },
-    {
-      "path": "../filebrowser"
-    },
     {
       "path": "../fileeditor"
     },