Skip to content

Commit

Permalink
change ConsolePanel to MainAreaWidget<ConsolePanel>
Browse files Browse the repository at this point in the history
  • Loading branch information
KsavinN committed Dec 18, 2019
1 parent e16485e commit 7a9f5d6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 54 deletions.
6 changes: 3 additions & 3 deletions packages/completer-extension/src/index.ts
Expand Up @@ -135,12 +135,12 @@ const consoles: JupyterFrontEndPlugin<void> = {
consoles: IConsoleTracker
): void => {
// Create a handler for each console that is created.
consoles.widgetAdded.connect((sender, panel) => {
const anchor = panel.console;
consoles.widgetAdded.connect((sender, widget) => {
const anchor = widget.content.console;
const cell = anchor.promptCell;
const editor = cell && cell.editor;
const session = anchor.session;
const parent = panel;
const parent = widget;
const connector = new CompletionConnector({ session, editor });
const handler = manager.register({ connector, editor, parent });

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

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

commands.addCommand(toggleShowAllActivity, {
Expand All @@ -77,7 +77,8 @@ function activateForeign(
},
isToggled: () =>
tracker.currentWidget !== null &&
Private.foreignHandlerProperty.get(tracker.currentWidget.console).enabled,
Private.foreignHandlerProperty.get(tracker.currentWidget.content.console)
.enabled,
isEnabled: () =>
tracker.currentWidget !== null &&
tracker.currentWidget === shell.currentWidget
Expand Down
75 changes: 40 additions & 35 deletions packages/console-extension/src/index.ts
Expand Up @@ -12,6 +12,7 @@ import {
Dialog,
IClientSession,
ICommandPalette,
MainAreaWidget,
showDialog,
WidgetTracker
} from '@jupyterlab/apputils';
Expand Down Expand Up @@ -142,20 +143,22 @@ async function activateConsole(
const category = 'Console';

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

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

Expand Down Expand Up @@ -236,6 +239,7 @@ async function activateConsole(
setBusy: status && (() => status.setBusy()),
...(options as Partial<ConsolePanel.IOptions>)
});
const widget = new MainAreaWidget<ConsolePanel>({ content: panel });

const interactionMode: string = (
await settingRegistry.get(
Expand All @@ -247,8 +251,8 @@ 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(panel);
panel.session.propertyChanged.connect(() => tracker.save(panel));
await tracker.add(widget);
panel.session.propertyChanged.connect(() => tracker.save(widget));

shell.add(panel, 'main', {
ref: options.ref,
Expand All @@ -263,8 +267,8 @@ async function activateConsole(
async function updateSettings() {
interactionMode = (await settingRegistry.get(pluginId, 'interactionMode'))
.composite as string;
tracker.forEach(panel => {
panel.console.node.dataset.jpInteractionMode = interactionMode;
tracker.forEach(widget => {
widget.content.console.node.dataset.jpInteractionMode = interactionMode;
});
}
settingRegistry.pluginChanged.connect((sender, plugin) => {
Expand All @@ -278,10 +282,11 @@ async function activateConsole(
* Whether there is an active console.
*/
function isEnabled(): boolean {
return (
const enabled =
tracker.currentWidget !== null &&
tracker.currentWidget === shell.currentWidget
);
tracker.currentWidget === shell.currentWidget;
console.log(tracker.currentWidget, shell.currentWidget, enabled);
return enabled;
}

/**
Expand All @@ -299,7 +304,7 @@ async function activateConsole(
execute: (args: IOpenOptions) => {
let path = args['path'];
let widget = tracker.find(value => {
return value.console.session.path === path;
return value.content.console.session.path === path;
});
if (widget) {
if (args['activate'] !== false) {
Expand Down Expand Up @@ -350,7 +355,7 @@ async function activateConsole(
if (activate && widget) {
shell.activateById(widget.id);
}
return widget;
return widget.content;
}

commands.addCommand(CommandIDs.clear, {
Expand Down Expand Up @@ -457,11 +462,11 @@ async function activateConsole(
execute: args => {
let path = args['path'];
tracker.find(widget => {
if (widget.console.session.path === path) {
if (widget.content.console.session.path === path) {
if (args['activate'] !== false) {
shell.activateById(widget.id);
}
void widget.console.inject(
void widget.content.console.inject(
args['code'] as string,
args['metadata'] as JSONObject
);
Expand Down Expand Up @@ -508,63 +513,63 @@ async function activateConsole(
tracker,
action: 'Shutdown',
name: 'Console',
closeAndCleanup: (current: ConsolePanel) => {
closeAndCleanup: (current: MainAreaWidget<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.console.session.shutdown().then(() => {
return current.content.console.session.shutdown().then(() => {
current.dispose();
});
} else {
return void 0;
}
});
}
} as IFileMenu.ICloseAndCleaner<ConsolePanel>);
} as IFileMenu.ICloseAndCleaner<MainAreaWidget<ConsolePanel>>);

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

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

// Add a clearer to the edit menu
mainMenu.editMenu.clearers.add({
tracker,
noun: 'Console Cells',
clearCurrent: (current: ConsolePanel) => {
return current.console.clear();
clearCurrent: (current: MainAreaWidget<ConsolePanel>) => {
return current.content.console.clear();
}
} as IEditMenu.IClearer<ConsolePanel>);
} as IEditMenu.IClearer<MainAreaWidget<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 @@ -617,8 +622,8 @@ async function activateConsole(
// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
tracker,
getKernel: current => current.session.kernel
} as IHelpMenu.IKernelUser<ConsolePanel>);
getKernel: current => current.content.session.kernel
} as IHelpMenu.IKernelUser<MainAreaWidget<ConsolePanel>>);

app.contextMenu.addItem({
command: CommandIDs.clear,
Expand Down
5 changes: 3 additions & 2 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 } from '@jupyterlab/apputils';
import { IWidgetTracker, MainAreaWidget } from '@jupyterlab/apputils';

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

Expand All @@ -19,4 +19,5 @@ export const IConsoleTracker = new Token<IConsoleTracker>(
/**
* A class that tracks console widgets.
*/
export interface IConsoleTracker extends IWidgetTracker<ConsolePanel> {}
export interface IConsoleTracker
extends IWidgetTracker<MainAreaWidget<ConsolePanel>> {}
12 changes: 7 additions & 5 deletions packages/fileeditor-extension/src/commands.ts
Expand Up @@ -865,15 +865,17 @@ export namespace Commands {
tracker,
noun: 'Code',
isEnabled: current =>
!!consoleTracker.find(c => c.session.path === current.context.path),
!!consoleTracker.find(
widget => widget.content.session.path === current.context.path
),
run: () => commands.execute(CommandIDs.runCode),
runAll: () => commands.execute(CommandIDs.runAllCode),
restartAndRunAll: current => {
const console = consoleTracker.find(
console => console.session.path === current.context.path
const widget = consoleTracker.find(
widget => widget.content.session.path === current.context.path
);
if (console) {
return console.session.restart().then(restarted => {
if (widget) {
return widget.content.session.restart().then(restarted => {
if (restarted) {
void commands.execute(CommandIDs.runAllCode);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/inspector-extension/src/index.ts
Expand Up @@ -135,20 +135,21 @@ const consoles: JupyterFrontEndPlugin<void> = {

// Create a handler for each console that is created.
consoles.widgetAdded.connect((sender, parent) => {
const session = parent.console.session;
const rendermime = parent.console.rendermime;
const content = parent.content;
const session = content.console.session;
const rendermime = content.console.rendermime;
const connector = new KernelConnector({ session });
const handler = new InspectionHandler({ connector, rendermime });

// Associate the handler to the widget.
handlers[parent.id] = handler;

// Set the initial editor.
let cell = parent.console.promptCell;
let cell = content.console.promptCell;
handler.editor = cell && cell.editor;

// Listen for prompt creation.
parent.console.promptCellCreated.connect((sender, cell) => {
content.console.promptCellCreated.connect((sender, cell) => {
handler.editor = cell && cell.editor;
});

Expand Down
2 changes: 1 addition & 1 deletion packages/tooltip-extension/src/index.ts
Expand Up @@ -105,7 +105,7 @@ const consoles: JupyterFrontEndPlugin<void> = {
return;
}

const anchor = parent.console;
const anchor = parent.content.console;
const editor = anchor.promptCell.editor;
const kernel = anchor.session.kernel;
const rendermime = anchor.rendermime;
Expand Down

0 comments on commit 7a9f5d6

Please sign in to comment.