0

I'm working on an extension that adds an icon to the sidebar and displays some information about the currently open file when the icon is clicked. I use shell.currentChanged.connect to detect when a file widget becomes active and extract CodeEditorWrapper from the widget. I add this widget to a WidgetTracker<CodeEditorWrapper> editorTracker to keep tabs on the active file. The goal is to use editorTracker.currentWidget anywhere I need a reference to the file widget.

Code snippet:

export const MyPlugin: JupyterFrontEndPlugin<void> = {
  id: 'jupyterlab-myplugin:text',
  autoStart: true,
  requires: [ILabShell, IThemeManager],
  activate: (
    app: JupyterFrontEnd,
    shell: ILabShell,
    manager: IThemeManager
  ) => {
    let editorTracker = new WidgetTracker<CodeEditorWrapper>({
      namespace: 'my-namespace'
    });
    ...
    shell.currentChanged.connect(async (_, changed) => {
      if (!editorTracker.has(editorWidget)) {
        await editorTracker.add(editorWidget);
      }
    });
    ...
  }
};

This is mostly working, but sometimes editorTracker.add(editorWidget) doesn't update the WidgetTracker's current widget.

After some extensive debugging, it's because the WidgetTracker's FocusTracker's active widget gets set if the widget has focus. And if that is set, the WidgetTracker's current widget does not get set.

WidgetTracker - https://github.com/jupyterlab/jupyterlab/blob/4.0.x/packages/apputils/src/widgettracker.ts#L226-L232

FocusTracker - https://github.com/jupyterlab/lumino/blob/main/packages/widgets/src/focustracker.ts#L161-L191

The weird thing is that the shell.currentChanged signal is triggered as the page loads sometimes, in which case the widget tracker's current widget is successfully set. However, other times, the signal is triggered post load by me manually clicking something on the page, in which case the current widget is not set.

Is there a way to have the WidgetTracker's currentWidget always set to the open file's widget? Am I not using the WidgetTracker properly? Any help would be greatly appreciated. Thank you.

I expected WidgetTracker::add to update the current widget, so I can have a reference to the currently active file.

0 Answers0