Browse Source

Merge pull request #8114 from blink1073/fix-nbtools

Fix property inspector and widget tracker load behavior
Steven Silvester 5 years ago
parent
commit
04d2bfa738

+ 9 - 1
packages/application-extension/src/index.tsx

@@ -822,13 +822,21 @@ const propertyInspector: JupyterFrontEndPlugin<IPropertyInspectorProvider> = {
   id: '@jupyterlab/application-extension:property-inspector',
   autoStart: true,
   requires: [ILabShell],
+  optional: [ILayoutRestorer],
   provides: IPropertyInspectorProvider,
-  activate: (app: JupyterFrontEnd, labshell: ILabShell) => {
+  activate: (
+    app: JupyterFrontEnd,
+    labshell: ILabShell,
+    restorer: ILayoutRestorer | null
+  ) => {
     const widget = new SideBarPropertyInspectorProvider(labshell);
     widget.title.icon = buildIcon;
     widget.title.caption = 'Property Inspector';
     widget.id = 'jp-property-inspector';
     labshell.add(widget, 'left');
+    if (restorer) {
+      restorer.add(widget, 'jp-property-inspector');
+    }
     return widget;
   }
 };

+ 7 - 1
packages/apputils/src/widgettracker.ts

@@ -142,6 +142,7 @@ export class WidgetTracker<T extends Widget = Widget>
         pool.current = focus.currentWidget;
         return;
       }
+
       this.onCurrentChanged(widget);
       this._currentChanged.emit(widget);
     }, this);
@@ -216,11 +217,16 @@ export class WidgetTracker<T extends Widget = Widget>
    * the tracker can be checked with the `has()` method. The promise this method
    * returns resolves after the widget has been added and saved to an underlying
    * restoration connector, if one is available.
+   *
+   * The newly added widget becomes the current widget unless the focus tracker
+   * already had a focused widget.
    */
   async add(widget: T): Promise<void> {
     this._focusTracker.add(widget);
     await this._pool.add(widget);
-    this._pool.current = widget;
+    if (!this._focusTracker.activeWidget) {
+      this._pool.current = widget;
+    }
   }
 
   /**

+ 1 - 0
packages/property-inspector/src/index.ts

@@ -164,6 +164,7 @@ export class SideBarPropertyInspectorProvider extends PropertyInspectorProvider
     }
     layout.widget = this._placeholder;
     labshell.currentChanged.connect(this._onShellCurrentChanged, this);
+    this._onShellCurrentChanged();
   }
 
   /**

+ 41 - 2
tests/test-apputils/src/widgettracker.spec.ts

@@ -55,15 +55,54 @@ describe('@jupyterlab/apputils', () => {
     });
 
     describe('#currentChanged', () => {
-      it('should emit when the current widget has been updated', async () => {
+      it('should emit for the first added widget', async () => {
         const widget = createWidget();
         let promise = signalToPromise(tracker.currentChanged);
+        void tracker.add(widget);
+        await promise;
+        widget.dispose();
+      });
 
+      it('should emit when a widget is added and there is another widget that does not have focus', async () => {
+        const widget = createWidget();
+        const widget2 = createWidget();
+        await tracker.add(widget);
+        let promise = signalToPromise(tracker.currentChanged);
+        await tracker.add(widget2);
+        await promise;
+        widget.dispose();
+        widget2.dispose();
+      });
+
+      it('should not emit when a widget is added and there is another widget that has focus', async () => {
+        const widget = createWidget();
+        const widget2 = createWidget();
         Widget.attach(widget, document.body);
         focus(widget);
-        void tracker.add(widget);
+        tracker.add(widget);
+        let called = false;
+        tracker.currentChanged.connect(() => {
+          called = true;
+        });
+        await tracker.add(widget2);
+        expect(called).to.equal(false);
+        widget.dispose();
+        widget2.dispose();
+      });
+
+      it('should emit when the focus changes', async () => {
+        const widget = createWidget();
+        const widget2 = createWidget();
+        Widget.attach(widget, document.body);
+        Widget.attach(widget2, document.body);
+        focus(widget);
+        tracker.add(widget);
+        await tracker.add(widget2);
+        let promise = signalToPromise(tracker.currentChanged);
+        focus(widget2);
         await promise;
         widget.dispose();
+        widget2.dispose();
       });
     });