/
foreign.ts
117 lines (102 loc) · 2.83 KB
/
foreign.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICommandPalette } from '@jupyterlab/apputils';
import {
CodeConsole,
ConsolePanel,
IConsoleTracker,
ForeignHandler
} from '@jupyterlab/console';
import { AttachedProperty } from '@lumino/properties';
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
/**
* The console widget tracker provider.
*/
export const foreign: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/console-extension:foreign',
requires: [IConsoleTracker],
optional: [ICommandPalette],
activate: activateForeign,
autoStart: true
};
export default foreign;
function activateForeign(
app: JupyterFrontEnd,
tracker: IConsoleTracker,
palette: ICommandPalette | null
) {
const { shell } = app;
tracker.widgetAdded.connect((sender, widget) => {
const console = widget.console;
const handler = new ForeignHandler({
sessionContext: console.sessionContext,
parent: console
});
Private.foreignHandlerProperty.set(console, handler);
console.disposed.connect(() => {
handler.dispose();
});
});
const { commands } = app;
const category = 'Console';
const toggleShowAllActivity = 'console:toggle-show-all-kernel-activity';
// Get the current widget and activate unless the args specify otherwise.
function getCurrent(args: ReadonlyPartialJSONObject): ConsolePanel | null {
let widget = tracker.currentWidget;
let activate = args['activate'] !== false;
if (activate && widget) {
shell.activateById(widget.id);
}
return widget;
}
commands.addCommand(toggleShowAllActivity, {
label: args => 'Show All Kernel Activity',
execute: args => {
let current = getCurrent(args);
if (!current) {
return;
}
const handler = Private.foreignHandlerProperty.get(current.console);
if (handler) {
handler.enabled = !handler.enabled;
}
},
isToggled: () =>
tracker.currentWidget !== null &&
!!Private.foreignHandlerProperty.get(tracker.currentWidget.console)
?.enabled,
isEnabled: () =>
tracker.currentWidget !== null &&
tracker.currentWidget === shell.currentWidget
});
if (palette) {
palette.addItem({
command: toggleShowAllActivity,
category,
args: { isPalette: true }
});
}
app.contextMenu.addItem({
command: toggleShowAllActivity,
selector: '.jp-CodeConsole'
});
}
/*
* A namespace for private data.
*/
namespace Private {
/**
* An attached property for a console's foreign handler.
*/
export const foreignHandlerProperty = new AttachedProperty<
CodeConsole,
ForeignHandler | undefined
>({
name: 'foreignHandler',
create: () => undefined
});
}