-
Notifications
You must be signed in to change notification settings - Fork 5k
/
shell.ts
292 lines (253 loc) · 7.13 KB
/
shell.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { JupyterFrontEnd } from '@jupyterlab/application';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { ArrayExt, find, IIterator, iter } from '@lumino/algorithm';
import { Token } from '@lumino/coreutils';
import { Message, MessageLoop, IMessageHandler } from '@lumino/messaging';
import { ISignal, Signal } from '@lumino/signaling';
import { Panel, Widget, BoxLayout } from '@lumino/widgets';
/**
* The Jupyter Notebook application shell token.
*/
export const INotebookShell = new Token<INotebookShell>(
'@jupyter-notebook/application:INotebookShell'
);
/**
* The Jupyter Notebook application shell interface.
*/
export interface INotebookShell extends NotebookShell {}
/**
* The default rank for ranked panels.
*/
const DEFAULT_RANK = 900;
/**
* The application shell.
*/
export class NotebookShell extends Widget implements JupyterFrontEnd.IShell {
constructor() {
super();
this.id = 'main';
const rootLayout = new BoxLayout();
this._topHandler = new Private.PanelHandler();
this._menuHandler = new Private.PanelHandler();
this._main = new Panel();
this._topHandler.panel.id = 'top-panel';
this._menuHandler.panel.id = 'menu-panel';
this._main.id = 'main-panel';
// create wrappers around the top and menu areas
const topWrapper = (this._topWrapper = new Panel());
topWrapper.id = 'top-panel-wrapper';
topWrapper.addWidget(this._topHandler.panel);
const menuWrapper = (this._menuWrapper = new Panel());
menuWrapper.id = 'menu-panel-wrapper';
menuWrapper.addWidget(this._menuHandler.panel);
BoxLayout.setStretch(topWrapper, 0);
BoxLayout.setStretch(menuWrapper, 0);
BoxLayout.setStretch(this._main, 1);
this._spacer = new Widget();
this._spacer.id = 'spacer-widget';
rootLayout.spacing = 0;
rootLayout.addWidget(topWrapper);
rootLayout.addWidget(menuWrapper);
rootLayout.addWidget(this._spacer);
rootLayout.addWidget(this._main);
this.layout = rootLayout;
}
/**
* A signal emitted when the current widget changes.
*/
get currentChanged(): ISignal<NotebookShell, void> {
return this._currentChanged;
}
/**
* The current widget in the shell's main area.
*/
get currentWidget(): Widget | null {
return this._main.widgets[0] ?? null;
}
/**
* Get the top area wrapper panel
*/
get top(): Widget {
return this._topWrapper;
}
/**
* Get the menu area wrapper panel
*/
get menu(): Widget {
return this._menuWrapper;
}
/**
* Activate a widget in its area.
*/
activateById(id: string): void {
const widget = find(this.widgets('main'), w => w.id === id);
if (widget) {
widget.activate();
}
}
/**
* Add a widget to the application shell.
*
* @param widget - The widget being added.
*
* @param area - Optional region in the shell into which the widget should
* be added.
*
* @param options - Optional open options.
*
*/
add(
widget: Widget,
area?: Shell.Area,
options?: DocumentRegistry.IOpenOptions
): void {
const rank = options?.rank ?? DEFAULT_RANK;
if (area === 'top') {
return this._topHandler.addWidget(widget, rank);
}
if (area === 'menu') {
return this._menuHandler.addWidget(widget, rank);
}
if (area === 'main' || area === undefined) {
if (this._main.widgets.length > 0) {
// do not add the widget if there is already one
return;
}
this._main.addWidget(widget);
this._main.update();
this._currentChanged.emit(void 0);
}
}
/**
* Collapse the top area and the spacer to make the view more compact.
*/
collapseTop(): void {
this._topWrapper.setHidden(true);
this._spacer.setHidden(true);
}
/**
* Expand the top area to show the header and the spacer.
*/
expandTop(): void {
this._topWrapper.setHidden(false);
this._spacer.setHidden(false);
}
/**
* Return the list of widgets for the given area.
*
* @param area The area
*/
widgets(area: Shell.Area): IIterator<Widget> {
switch (area ?? 'main') {
case 'top':
return iter(this._topHandler.panel.widgets);
case 'menu':
return iter(this._menuHandler.panel.widgets);
case 'main':
return iter(this._main.widgets);
default:
throw new Error(`Invalid area: ${area}`);
}
}
private _topWrapper: Panel;
private _topHandler: Private.PanelHandler;
private _menuWrapper: Panel;
private _menuHandler: Private.PanelHandler;
private _spacer: Widget;
private _main: Panel;
private _currentChanged = new Signal<this, void>(this);
}
/**
* A namespace for Shell statics
*/
export namespace Shell {
/**
* The areas of the application shell where widgets can reside.
*/
export type Area = 'main' | 'top' | 'menu';
}
/**
* A namespace for private module data.
*/
namespace Private {
/**
* An object which holds a widget and its sort rank.
*/
export interface IRankItem {
/**
* The widget for the item.
*/
widget: Widget;
/**
* The sort rank of the widget.
*/
rank: number;
}
/**
* A less-than comparison function for side bar rank items.
*/
export function itemCmp(first: IRankItem, second: IRankItem): number {
return first.rank - second.rank;
}
/**
* A class which manages a panel and sorts its widgets by rank.
*/
export class PanelHandler {
constructor() {
MessageLoop.installMessageHook(this._panel, this._panelChildHook);
}
/**
* Get the panel managed by the handler.
*/
get panel(): Panel {
return this._panel;
}
/**
* Add a widget to the panel.
*
* If the widget is already added, it will be moved.
*/
addWidget(widget: Widget, rank: number): void {
widget.parent = null;
const item = { widget, rank };
const index = ArrayExt.upperBound(this._items, item, Private.itemCmp);
ArrayExt.insert(this._items, index, item);
this._panel.insertWidget(index, widget);
}
/**
* A message hook for child add/remove messages on the main area dock panel.
*/
private _panelChildHook = (
handler: IMessageHandler,
msg: Message
): boolean => {
switch (msg.type) {
case 'child-added':
{
const widget = (msg as Widget.ChildMessage).child;
// If we already know about this widget, we're done
if (this._items.find(v => v.widget === widget)) {
break;
}
// Otherwise, add to the end by default
const rank = this._items[this._items.length - 1].rank;
this._items.push({ widget, rank });
}
break;
case 'child-removed':
{
const widget = (msg as Widget.ChildMessage).child;
ArrayExt.removeFirstWhere(this._items, v => v.widget === widget);
}
break;
default:
break;
}
return true;
};
private _items = new Array<Private.IRankItem>();
private _panel = new Panel();
}
}