Skip to content

Commit

Permalink
Merge pull request #7705 from KsavinN/7648-issue
Browse files Browse the repository at this point in the history
Change Tracker from MainAreaWidget back to ConsolePanel
  • Loading branch information
Steven Silvester committed Jan 3, 2020
2 parents b88ea48 + 8cb150e commit 736503e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 72 deletions.
2 changes: 1 addition & 1 deletion packages/completer-extension/src/index.ts
Expand Up @@ -136,7 +136,7 @@ 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
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: 37 additions & 41 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 @@ -155,26 +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 => {
const {
path,
name,
kernelPreference
} = widget.content.console.sessionContext;
const { path, name, kernelPreference } = widget.console.sessionContext;
return {
path,
name,
kernelPreference: { ...kernelPreference }
};
},
name: widget => widget.content.console.sessionContext.path ?? UUID.uuid4(),
name: widget => widget.console.sessionContext.path ?? UUID.uuid4(),
when: manager.ready
});

Expand Down Expand Up @@ -226,6 +221,11 @@ async function activateConsole(
* The options used to create a widget.
*/
interface ICreateOptions extends Partial<ConsolePanel.IOptions> {
/**
* Whether to activate the widget. Defaults to `true`.
*/
activate?: boolean;

/**
* The reference widget id for the insert location.
*
Expand All @@ -240,11 +240,6 @@ async function activateConsole(
* to the main area relative to a reference widget.
*/
insertMode?: DockLayout.InsertMode;

/**
* Whether to activate the widget. Defaults to `true`.
*/
activate?: boolean;
}

/**
Expand All @@ -261,7 +256,6 @@ async function activateConsole(
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 @@ -273,13 +267,15 @@ 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(() => {
void tracker.save(panel);
});

shell.add(panel, 'main', {
ref: options.ref,
mode: options.insertMode,
activate: options.activate
activate: options.activate !== false
});
return panel;
}
Expand All @@ -290,7 +286,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 @@ -306,7 +302,7 @@ async function activateConsole(
function isEnabled(): boolean {
return (
tracker.currentWidget !== null &&
tracker.currentWidget.content === shell.currentWidget
tracker.currentWidget === shell.currentWidget
);
}

Expand All @@ -325,10 +321,10 @@ async function activateConsole(
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.activate !== false) {
shell.activateById(widget.id);
}
return widget;
Expand Down Expand Up @@ -380,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 @@ -487,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 @@ -538,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 @@ -651,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
23 changes: 12 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({ content: new Panel() });
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 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> {}
8 changes: 3 additions & 5 deletions packages/fileeditor-extension/src/commands.ts
Expand Up @@ -877,19 +877,17 @@ export namespace Commands {
noun: 'Code',
isEnabled: current =>
!!consoleTracker.find(
widget =>
widget.content.sessionContext.session?.path === current.context.path
widget => widget.sessionContext.session?.path === current.context.path
),
run: () => commands.execute(CommandIDs.runCode),
runAll: () => commands.execute(CommandIDs.runAllCode),
restartAndRunAll: current => {
const widget = consoleTracker.find(
widget =>
widget.content.sessionContext.session?.path === current.context.path
widget => widget.sessionContext.session?.path === current.context.path
);
if (widget) {
return (sessionDialogs || sessionContextDialogs)
.restart(widget.content.sessionContext)
.restart(widget.sessionContext)
.then(restarted => {
if (restarted) {
void commands.execute(CommandIDs.runAllCode);
Expand Down

0 comments on commit 736503e

Please sign in to comment.