Skip to content

Commit

Permalink
change tracker from MainAreaWidget back to ConsolePanel
Browse files Browse the repository at this point in the history
  • Loading branch information
KsavinN committed Dec 30, 2019
1 parent 37c7a64 commit 7c90ac5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 72 deletions.
3 changes: 2 additions & 1 deletion packages/completer-extension/src/index.ts
Expand Up @@ -136,10 +136,11 @@ const consoles: JupyterFrontEndPlugin<void> = {
): void => {
// Create a handler for each console that is created.
consoles.widgetAdded.connect((sender, widget) => {
const anchor = widget.content.console;
const anchor = widget.console;
const editor = anchor.promptCell?.editor ?? null;
const session = anchor.sessionContext.session;
// TODO: CompletionConnector assumes editor and session are not null

const connector = new CompletionConnector({ session, editor });
const handler = manager.register({ connector, editor, parent: widget });

Expand Down
9 changes: 4 additions & 5 deletions packages/console-extension/src/foreign.ts
Expand Up @@ -39,7 +39,7 @@ function activateForeign(
) {
const { shell } = app;
tracker.widgetAdded.connect((sender, widget) => {
const console = widget.content.console;
const console = widget.console;

const handler = new ForeignHandler({
sessionContext: console.sessionContext,
Expand All @@ -62,7 +62,7 @@ function activateForeign(
if (activate && widget) {
shell.activateById(widget.id);
}
return widget?.content ?? null;
return widget;
}

commands.addCommand(toggleShowAllActivity, {
Expand All @@ -79,9 +79,8 @@ function activateForeign(
},
isToggled: () =>
tracker.currentWidget !== null &&
!!Private.foreignHandlerProperty.get(
tracker.currentWidget.content.console
)?.enabled,
!!Private.foreignHandlerProperty.get(tracker.currentWidget.console)
?.enabled,
isEnabled: () =>
tracker.currentWidget !== null &&
tracker.currentWidget === shell.currentWidget
Expand Down
78 changes: 38 additions & 40 deletions packages/console-extension/src/index.ts
Expand Up @@ -13,7 +13,6 @@ import {
ISessionContext,
ISessionContextDialogs,
ICommandPalette,
MainAreaWidget,
sessionContextDialogs,
showDialog,
WidgetTracker
Expand Down Expand Up @@ -54,7 +53,7 @@ import {

import { DisposableSet } from '@lumino/disposable';

import { DockLayout, Menu } from '@lumino/widgets';
import { DockLayout, Menu, Panel } from '@lumino/widgets';

import foreign from './foreign';

Expand Down Expand Up @@ -155,24 +154,22 @@ async function activateConsole(
sessionDialogs = sessionDialogs ?? sessionContextDialogs;

// Create a widget tracker for all console panels.
const tracker = new WidgetTracker<MainAreaWidget<ConsolePanel>>({
const tracker = new WidgetTracker<ConsolePanel>({
namespace: 'console'
});

// Handle state restoration.
void restorer.restore(tracker, {
command: CommandIDs.create,
args: widget => ({
path: widget.content.console.sessionContext.session?.path,
name: widget.content.console.sessionContext.session?.name,
path: widget.console.sessionContext.session?.path,
name: widget.console.sessionContext.session?.name,
kernelPreference: {
name: widget.content.console.sessionContext.kernelPreference.name,
language:
widget.content.console.sessionContext.kernelPreference.language
name: widget.console.sessionContext.kernelPreference.name,
language: widget.console.sessionContext.kernelPreference.language
}
}),
name: widget =>
widget.content.console.sessionContext.session?.path ?? UUID.uuid4(),
name: widget => widget.console.sessionContext.session?.path ?? UUID.uuid4(),
when: manager.ready
});

Expand Down Expand Up @@ -242,7 +239,7 @@ async function activateConsole(
/**
* Whether to activate the widget. Defaults to `true`.
*/
activate?: boolean;
isActivate?: boolean;
}

/**
Expand All @@ -252,14 +249,14 @@ async function activateConsole(
await manager.ready;

const panel = new ConsolePanel({
content: new Panel(),
manager,
contentFactory,
mimeTypeService: editorServices.mimeTypeService,
rendermime,
setBusy: (status && (() => status.setBusy())) ?? undefined,
...(options as Partial<ConsolePanel.IOptions>)
});
const widget = new MainAreaWidget<ConsolePanel>({ content: panel });

const interactionMode: string = (
await settingRegistry.get(
Expand All @@ -271,13 +268,13 @@ async function activateConsole(

// Add the console panel to the tracker. We want the panel to show up before
// any kernel selection dialog, so we do not await panel.session.ready;
await tracker.add(widget);
panel.sessionContext.propertyChanged.connect(() => tracker.save(widget));
await tracker.add(panel);
panel.sessionContext.propertyChanged.connect(() => tracker.save(panel));

shell.add(panel, 'main', {
ref: options.ref,
mode: options.insertMode,
activate: options.activate
activate: options.isActivate
});
return panel;
}
Expand All @@ -288,7 +285,7 @@ async function activateConsole(
interactionMode = (await settingRegistry.get(pluginId, 'interactionMode'))
.composite as string;
tracker.forEach(widget => {
widget.content.console.node.dataset.jpInteractionMode = interactionMode;
widget.console.node.dataset.jpInteractionMode = interactionMode;
});
}
settingRegistry.pluginChanged.connect((sender, plugin) => {
Expand All @@ -302,9 +299,10 @@ async function activateConsole(
* Whether there is an active console.
*/
function isEnabled(): boolean {
console.log({ tracker, shell });
return (
tracker.currentWidget !== null &&
tracker.currentWidget.content === shell.currentWidget
tracker.currentWidget === shell.currentWidget
);
}

Expand All @@ -315,18 +313,18 @@ async function activateConsole(
/**
* Whether to activate the console. Defaults to `true`.
*/
activate?: boolean;
isActivate?: boolean;
}

let command = CommandIDs.open;
commands.addCommand(command, {
execute: (args: IOpenOptions) => {
let path = args['path'];
let widget = tracker.find(value => {
return value.content.console.sessionContext.session?.path === path;
return value.console.sessionContext.session?.path === path;
});
if (widget) {
if (args['activate'] !== false) {
if (args['isActivate'] !== false) {
shell.activateById(widget.id);
}
return widget;
Expand Down Expand Up @@ -378,7 +376,7 @@ async function activateConsole(
if (activate && widget) {
shell.activateById(widget.id);
}
return widget?.content ?? null;
return widget ?? null;
}

commands.addCommand(CommandIDs.clear, {
Expand Down Expand Up @@ -485,11 +483,11 @@ async function activateConsole(
execute: args => {
let path = args['path'];
tracker.find(widget => {
if (widget.content.console.sessionContext.session?.path === path) {
if (widget.console.sessionContext.session?.path === path) {
if (args['activate'] !== false) {
shell.activateById(widget.id);
}
void widget.content.console.inject(
void widget.console.inject(
args['code'] as string,
args['metadata'] as JSONObject
);
Expand Down Expand Up @@ -536,67 +534,67 @@ async function activateConsole(
tracker,
action: 'Shutdown',
name: 'Console',
closeAndCleanup: (current: MainAreaWidget<ConsolePanel>) => {
closeAndCleanup: (current: ConsolePanel) => {
return showDialog({
title: 'Shut down the console?',
body: `Are you sure you want to close "${current.title.label}"?`,
buttons: [Dialog.cancelButton(), Dialog.warnButton()]
}).then(result => {
if (result.button.accept) {
return current.content.console.sessionContext.shutdown().then(() => {
return current.console.sessionContext.shutdown().then(() => {
current.dispose();
});
} else {
return void 0;
}
});
}
} as IFileMenu.ICloseAndCleaner<MainAreaWidget<ConsolePanel>>);
} as IFileMenu.ICloseAndCleaner<ConsolePanel>);

// Add a kernel user to the Kernel menu
mainMenu.kernelMenu.kernelUsers.add({
tracker,
interruptKernel: current => {
let kernel = current.content.console.sessionContext.session?.kernel;
let kernel = current.console.sessionContext.session?.kernel;
if (kernel) {
return kernel.interrupt();
}
return Promise.resolve(void 0);
},
noun: 'Console',
restartKernel: current =>
sessionDialogs!.restart(current.content.console.sessionContext),
sessionDialogs!.restart(current.console.sessionContext),
restartKernelAndClear: current => {
return sessionDialogs!
.restart(current.content.console.sessionContext)
.restart(current.console.sessionContext)
.then(restarted => {
if (restarted) {
current.content.console.clear();
current.console.clear();
}
return restarted;
});
},
changeKernel: current =>
sessionDialogs!.selectKernel(current.content.console.sessionContext),
shutdownKernel: current => current.content.console.sessionContext.shutdown()
} as IKernelMenu.IKernelUser<MainAreaWidget<ConsolePanel>>);
sessionDialogs!.selectKernel(current.console.sessionContext),
shutdownKernel: current => current.console.sessionContext.shutdown()
} as IKernelMenu.IKernelUser<ConsolePanel>);

// Add a code runner to the Run menu.
mainMenu.runMenu.codeRunners.add({
tracker,
noun: 'Cell',
pluralNoun: 'Cells',
run: current => current.content.console.execute(true)
} as IRunMenu.ICodeRunner<MainAreaWidget<ConsolePanel>>);
run: current => current.console.execute(true)
} as IRunMenu.ICodeRunner<ConsolePanel>);

// Add a clearer to the edit menu
mainMenu.editMenu.clearers.add({
tracker,
noun: 'Console Cells',
clearCurrent: (current: MainAreaWidget<ConsolePanel>) => {
return current.content.console.clear();
clearCurrent: (current: ConsolePanel) => {
return current.console.clear();
}
} as IEditMenu.IClearer<MainAreaWidget<ConsolePanel>>);
} as IEditMenu.IClearer<ConsolePanel>);

// For backwards compatibility and clarity, we explicitly label the run
// keystroke with the actual effected change, rather than the generic
Expand Down Expand Up @@ -649,8 +647,8 @@ async function activateConsole(
// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
tracker,
getKernel: current => current.content.sessionContext.session?.kernel
} as IHelpMenu.IKernelUser<MainAreaWidget<ConsolePanel>>);
getKernel: current => current.sessionContext.session?.kernel
} as IHelpMenu.IKernelUser<ConsolePanel>);

app.contextMenu.addItem({
command: CommandIDs.clear,
Expand Down
24 changes: 13 additions & 11 deletions packages/console/src/panel.ts
Expand Up @@ -4,7 +4,8 @@
import {
ISessionContext,
SessionContext,
sessionContextDialogs
sessionContextDialogs,
MainAreaWidget
} from '@jupyterlab/apputils';

import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
Expand Down Expand Up @@ -39,12 +40,12 @@ const CONSOLE_ICON_CLASS = 'jp-CodeConsoleIcon';
/**
* A panel which contains a console and the ability to add other children.
*/
export class ConsolePanel extends Panel {
export class ConsolePanel extends MainAreaWidget<Panel> {
/**
* Construct a console panel.
*/
constructor(options: ConsolePanel.IOptions) {
super();
super(options);
this.addClass(PANEL_CLASS);
let {
rendermime,
Expand Down Expand Up @@ -85,20 +86,20 @@ export class ConsolePanel extends Panel {
contentFactory,
modelFactory
});
this.addWidget(this.console);
this.content.addWidget(this.console);

void sessionContext.initialize().then(async value => {
if (value) {
await sessionContextDialogs.selectKernel(sessionContext);
}
this._connected = new Date();
this._updateTitle();
this._updateTitlePanel();
});

this.console.executed.connect(this._onExecuted, this);
this._updateTitle();
sessionContext.kernelChanged.connect(this._updateTitle, this);
sessionContext.propertyChanged.connect(this._updateTitle, this);
this._updateTitlePanel();
sessionContext.kernelChanged.connect(this._updateTitlePanel, this);
sessionContext.propertyChanged.connect(this._updateTitlePanel, this);

this.title.icon = CONSOLE_ICON_CLASS;
this.title.closable = true;
Expand All @@ -113,7 +114,7 @@ export class ConsolePanel extends Panel {
/**
* The console widget used by the panel.
*/
readonly console: CodeConsole;
console: CodeConsole;

/**
* The session used by the panel.
Expand Down Expand Up @@ -154,13 +155,13 @@ export class ConsolePanel extends Panel {
*/
private _onExecuted(sender: CodeConsole, args: Date) {
this._executed = args;
this._updateTitle();
this._updateTitlePanel();
}

/**
* Update the console panel title.
*/
private _updateTitle(): void {
private _updateTitlePanel(): void {
Private.updateTitle(this, this._connected, this._executed);
}

Expand All @@ -177,6 +178,7 @@ export namespace ConsolePanel {
* The initialization options for a console panel.
*/
export interface IOptions {
content: Panel;
/**
* The rendermime instance used by the panel.
*/
Expand Down
5 changes: 2 additions & 3 deletions packages/console/src/tokens.ts
@@ -1,7 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { IWidgetTracker, MainAreaWidget } from '@jupyterlab/apputils';
import { IWidgetTracker } from '@jupyterlab/apputils';

import { Token } from '@lumino/coreutils';

Expand All @@ -19,5 +19,4 @@ export const IConsoleTracker = new Token<IConsoleTracker>(
/**
* A class that tracks console widgets.
*/
export interface IConsoleTracker
extends IWidgetTracker<MainAreaWidget<ConsolePanel>> {}
export interface IConsoleTracker extends IWidgetTracker<ConsolePanel> {}

0 comments on commit 7c90ac5

Please sign in to comment.