/
shell.ts
2415 lines (2164 loc) · 65.9 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { DocumentRegistry, DocumentWidget } from '@jupyterlab/docregistry';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import {
classes,
DockPanelSvg,
LabIcon,
SidePanel,
TabBarSvg,
tabIcon,
TabPanelSvg
} from '@jupyterlab/ui-components';
import { ArrayExt, find, map } from '@lumino/algorithm';
import { JSONExt, PromiseDelegate, Token } from '@lumino/coreutils';
import { IMessageHandler, Message, MessageLoop } from '@lumino/messaging';
import { Debouncer } from '@lumino/polling';
import { ISignal, Signal } from '@lumino/signaling';
import {
AccordionPanel,
BoxLayout,
BoxPanel,
DockLayout,
DockPanel,
FocusTracker,
Panel,
SplitPanel,
StackedPanel,
TabBar,
TabPanel,
Title,
Widget
} from '@lumino/widgets';
import { JupyterFrontEnd } from './frontend';
import { LayoutRestorer } from './layoutrestorer';
/**
* The class name added to AppShell instances.
*/
const APPLICATION_SHELL_CLASS = 'jp-LabShell';
/**
* The class name added to side bar instances.
*/
const SIDEBAR_CLASS = 'jp-SideBar';
/**
* The class name added to the current widget's title.
*/
const CURRENT_CLASS = 'jp-mod-current';
/**
* The class name added to the active widget's title.
*/
const ACTIVE_CLASS = 'jp-mod-active';
/**
* The default rank of items added to a sidebar.
*/
const DEFAULT_RANK = 900;
const ACTIVITY_CLASS = 'jp-Activity';
/**
* The JupyterLab application shell token.
*/
export const ILabShell = new Token<ILabShell>(
'@jupyterlab/application:ILabShell',
'A service for interacting with the JupyterLab shell. The top-level ``application`` object also has a reference to the shell, but it has a restricted interface in order to be agnostic to different shell implementations on the application. Use this to get more detailed information about currently active widgets and layout state.'
);
/**
* The JupyterLab application shell interface.
*/
export interface ILabShell extends LabShell {}
/**
* The namespace for `ILabShell` type information.
*/
export namespace ILabShell {
/**
* The areas of the application shell where widgets can reside.
*/
export type Area =
| 'main'
| 'header'
| 'top'
| 'menu'
| 'left'
| 'right'
| 'bottom'
| 'down';
/**
* The restorable description of an area within the main dock panel.
*/
export type AreaConfig = DockLayout.AreaConfig;
/**
* An options object for creating a lab shell object.
*/
export type IOptions = {
/**
* Whether the layout should wait to be restored before adding widgets or not.
*
* #### Notes
* It defaults to true
*/
waitForRestore?: boolean;
};
/**
* An arguments object for the changed signals.
*/
export type IChangedArgs = FocusTracker.IChangedArgs<Widget>;
export interface IConfig {
/**
* The method for hiding widgets in the dock panel.
*
* The default is `display`.
*
* Using `scale` will often increase performance as most browsers will not trigger style computation
* for the transform action.
*
* `contentVisibility` is only available in Chromium-based browsers.
*/
hiddenMode: 'display' | 'scale' | 'contentVisibility';
}
/**
* Widget position
*/
export interface IWidgetPosition {
/**
* Widget area
*/
area?: Area;
/**
* Widget opening options
*/
options?: DocumentRegistry.IOpenOptions;
}
/**
* To-be-added widget and associated position
*/
export interface IDelayedWidget extends IWidgetPosition {
widget: Widget;
}
/**
* Mapping of widget type identifier and their user customized position
*/
export interface IUserLayout {
/**
* Widget customized position
*/
[k: string]: IWidgetPosition;
}
/**
* The args for the current path change signal.
*/
export interface ICurrentPathChangedArgs {
/**
* The new value of the tree path, not including '/tree'.
*/
oldValue: string;
/**
* The old value of the tree path, not including '/tree'.
*/
newValue: string;
}
/**
* A description of the application's user interface layout.
*/
export interface ILayout {
/**
* Indicates whether fetched session restore data was actually retrieved
* from the state database or whether it is a fresh blank slate.
*
* #### Notes
* This attribute is only relevant when the layout data is retrieved via a
* `fetch` call. If it is set when being passed into `save`, it will be
* ignored.
*/
readonly fresh?: boolean;
/**
* The main area of the user interface.
*/
readonly mainArea: IMainArea | null;
/**
* The down area of the user interface.
*/
readonly downArea: IDownArea | null;
/**
* The left area of the user interface.
*/
readonly leftArea: ISideArea | null;
/**
* The right area of the user interface.
*/
readonly rightArea: ISideArea | null;
/**
* The top area of the user interface.
*/
readonly topArea: ITopArea | null;
/**
* The relatives sizes of the areas of the user interface.
*/
readonly relativeSizes: number[] | null;
}
/**
* The restorable description of the main application area.
*/
export interface IMainArea {
/**
* The current widget that has application focus.
*/
readonly currentWidget: Widget | null;
/**
* The contents of the main application dock panel.
*/
readonly dock: DockLayout.ILayoutConfig | null;
}
export interface IDownArea {
/**
* The current widget that has down area focus.
*/
readonly currentWidget: Widget | null;
/**
* The collection of widgets held by the panel.
*/
readonly widgets: Array<Widget> | null;
/**
* Vertical relative size of the down area
*
* The main area will take the rest of the height
*/
readonly size: number | null;
}
/**
* The restorable description of a sidebar in the user interface.
*/
export interface ISideArea {
/**
* A flag denoting whether the sidebar has been collapsed.
*/
readonly collapsed: boolean;
/**
* The current widget that has side area focus.
*/
readonly currentWidget: Widget | null;
/**
* A flag denoting whether the side tab bar is visible.
*/
readonly visible: boolean;
/**
* The collection of widgets held by the sidebar.
*/
readonly widgets: Array<Widget> | null;
/**
* The collection of widgets states held by the sidebar.
*/
readonly widgetStates: {
[id: string]: {
/**
* Vertical sizes of the widgets.
*/
readonly sizes: Array<number> | null;
/**
* Expansion states of the widgets.
*/
readonly expansionStates: Array<boolean> | null;
};
};
}
/**
* The restorable description of the top area in the user interface.
*/
export interface ITopArea {
/**
* Top area visibility in simple mode.
*/
readonly simpleVisibility: boolean;
}
}
/**
* The restorable description of the top area in the user interface.
*
* @deprecated It has been moved to {@link ILabShell.ITopArea} for consistency.
*/
export interface ITopArea extends ILabShell.ITopArea {}
/**
* The application shell for JupyterLab.
*/
export class LabShell extends Widget implements JupyterFrontEnd.IShell {
/**
* Construct a new application shell.
*/
constructor(options?: ILabShell.IOptions) {
super();
this.addClass(APPLICATION_SHELL_CLASS);
this.id = 'main';
if (options?.waitForRestore === false) {
this._userLayout = { 'multiple-document': {}, 'single-document': {} };
}
// Skip Links
const skipLinkWidget = (this._skipLinkWidget = new Private.SkipLinkWidget(
this
));
this._skipLinkWidget.show();
// Wrap the skip widget to customize its position and size
const skipLinkWrapper = new Panel();
skipLinkWrapper.addClass('jp-skiplink-wrapper');
skipLinkWrapper.addWidget(skipLinkWidget);
const headerPanel = (this._headerPanel = new BoxPanel());
const menuHandler = (this._menuHandler = new Private.PanelHandler());
menuHandler.panel.node.setAttribute('role', 'navigation');
const topHandler = (this._topHandler = new Private.PanelHandler());
topHandler.panel.node.setAttribute('role', 'banner');
const bottomPanel = (this._bottomPanel = new BoxPanel());
bottomPanel.node.setAttribute('role', 'contentinfo');
const hboxPanel = new BoxPanel();
const vsplitPanel = (this._vsplitPanel =
new Private.RestorableSplitPanel());
const dockPanel = (this._dockPanel = new DockPanelSvg({
hiddenMode: Widget.HiddenMode.Display
}));
MessageLoop.installMessageHook(dockPanel, this._dockChildHook);
const hsplitPanel = (this._hsplitPanel =
new Private.RestorableSplitPanel());
const downPanel = (this._downPanel = new TabPanelSvg({
tabsMovable: true
}));
const leftHandler = (this._leftHandler = new Private.SideBarHandler());
const rightHandler = (this._rightHandler = new Private.SideBarHandler());
const rootLayout = new BoxLayout();
headerPanel.id = 'jp-header-panel';
menuHandler.panel.id = 'jp-menu-panel';
topHandler.panel.id = 'jp-top-panel';
bottomPanel.id = 'jp-bottom-panel';
hboxPanel.id = 'jp-main-content-panel';
vsplitPanel.id = 'jp-main-vsplit-panel';
dockPanel.id = 'jp-main-dock-panel';
hsplitPanel.id = 'jp-main-split-panel';
downPanel.id = 'jp-down-stack';
leftHandler.sideBar.addClass(SIDEBAR_CLASS);
leftHandler.sideBar.addClass('jp-mod-left');
leftHandler.sideBar.node.setAttribute('role', 'complementary');
leftHandler.stackedPanel.id = 'jp-left-stack';
rightHandler.sideBar.addClass(SIDEBAR_CLASS);
rightHandler.sideBar.addClass('jp-mod-right');
rightHandler.sideBar.node.setAttribute('role', 'complementary');
rightHandler.stackedPanel.id = 'jp-right-stack';
dockPanel.node.setAttribute('role', 'main');
hboxPanel.spacing = 0;
vsplitPanel.spacing = 1;
dockPanel.spacing = 5;
hsplitPanel.spacing = 1;
headerPanel.direction = 'top-to-bottom';
vsplitPanel.orientation = 'vertical';
hboxPanel.direction = 'left-to-right';
hsplitPanel.orientation = 'horizontal';
bottomPanel.direction = 'bottom-to-top';
SplitPanel.setStretch(leftHandler.stackedPanel, 0);
SplitPanel.setStretch(downPanel, 0);
SplitPanel.setStretch(dockPanel, 1);
SplitPanel.setStretch(rightHandler.stackedPanel, 0);
BoxPanel.setStretch(leftHandler.sideBar, 0);
BoxPanel.setStretch(hsplitPanel, 1);
BoxPanel.setStretch(rightHandler.sideBar, 0);
SplitPanel.setStretch(vsplitPanel, 1);
hsplitPanel.addWidget(leftHandler.stackedPanel);
hsplitPanel.addWidget(dockPanel);
hsplitPanel.addWidget(rightHandler.stackedPanel);
vsplitPanel.addWidget(hsplitPanel);
vsplitPanel.addWidget(downPanel);
hboxPanel.addWidget(leftHandler.sideBar);
hboxPanel.addWidget(vsplitPanel);
hboxPanel.addWidget(rightHandler.sideBar);
rootLayout.direction = 'top-to-bottom';
rootLayout.spacing = 0; // TODO make this configurable?
// Use relative sizing to set the width of the side panels.
// This will still respect the min-size of children widget in the stacked
// panel. The default sizes will be overwritten during layout restoration.
vsplitPanel.setRelativeSizes([3, 1]);
hsplitPanel.setRelativeSizes([1, 2.5, 1]);
BoxLayout.setStretch(headerPanel, 0);
BoxLayout.setStretch(menuHandler.panel, 0);
BoxLayout.setStretch(topHandler.panel, 0);
BoxLayout.setStretch(hboxPanel, 1);
BoxLayout.setStretch(bottomPanel, 0);
rootLayout.addWidget(skipLinkWrapper);
rootLayout.addWidget(headerPanel);
rootLayout.addWidget(topHandler.panel);
rootLayout.addWidget(hboxPanel);
rootLayout.addWidget(bottomPanel);
// initially hiding header and bottom panel when no elements inside,
this._headerPanel.hide();
this._bottomPanel.hide();
this._downPanel.hide();
this.layout = rootLayout;
// Connect change listeners.
this._tracker.currentChanged.connect(this._onCurrentChanged, this);
this._tracker.activeChanged.connect(this._onActiveChanged, this);
// Connect main layout change listener.
this._dockPanel.layoutModified.connect(this._onLayoutModified, this);
// Connect vsplit layout change listener
this._vsplitPanel.updated.connect(this._onLayoutModified, this);
// Connect down panel change listeners
this._downPanel.currentChanged.connect(this._onLayoutModified, this);
this._downPanel.tabBar.tabMoved.connect(this._onTabPanelChanged, this);
this._downPanel.stackedPanel.widgetRemoved.connect(
this._onTabPanelChanged,
this
);
// Catch current changed events on the side handlers.
this._leftHandler.updated.connect(this._onLayoutModified, this);
this._rightHandler.updated.connect(this._onLayoutModified, this);
// Catch update events on the horizontal split panel
this._hsplitPanel.updated.connect(this._onLayoutModified, this);
// Setup single-document-mode title bar
const titleHandler = (this._titleHandler = new Private.TitleHandler(this));
this.add(titleHandler, 'top', { rank: 100 });
if (this._dockPanel.mode === 'multiple-document') {
this._topHandler.addWidget(this._menuHandler.panel, 100);
titleHandler.hide();
} else {
rootLayout.insertWidget(3, this._menuHandler.panel);
}
this.translator = nullTranslator;
// Wire up signals to update the title panel of the simple interface mode to
// follow the title of this.currentWidget
this.currentChanged.connect((sender, args) => {
let newValue = args.newValue;
let oldValue = args.oldValue;
// Stop watching the title of the previously current widget
if (oldValue) {
oldValue.title.changed.disconnect(this._updateTitlePanelTitle, this);
if (oldValue instanceof DocumentWidget) {
oldValue.context.pathChanged.disconnect(
this._updateCurrentPath,
this
);
}
}
// Start watching the title of the new current widget
if (newValue) {
newValue.title.changed.connect(this._updateTitlePanelTitle, this);
this._updateTitlePanelTitle();
if (newValue instanceof DocumentWidget) {
newValue.context.pathChanged.connect(this._updateCurrentPath, this);
}
}
this._updateCurrentPath();
});
}
/**
* A signal emitted when main area's active focus changes.
*/
get activeChanged(): ISignal<this, ILabShell.IChangedArgs> {
return this._activeChanged;
}
/**
* The active widget in the shell's main area.
*/
get activeWidget(): Widget | null {
return this._tracker.activeWidget;
}
/**
* Whether the add buttons for each main area tab bar are enabled.
*/
get addButtonEnabled(): boolean {
return this._dockPanel.addButtonEnabled;
}
set addButtonEnabled(value: boolean) {
this._dockPanel.addButtonEnabled = value;
}
/**
* A signal emitted when the add button on a main area tab bar is clicked.
*/
get addRequested(): ISignal<DockPanel, TabBar<Widget>> {
return this._dockPanel.addRequested;
}
/**
* A signal emitted when main area's current focus changes.
*/
get currentChanged(): ISignal<this, ILabShell.IChangedArgs> {
return this._currentChanged;
}
/**
* Current document path.
*/
// FIXME deprecation `undefined` is to ensure backward compatibility in 4.x
get currentPath(): string | null | undefined {
return this._currentPath;
}
/**
* A signal emitted when the path of the current document changes.
*
* This also fires when the current document itself changes.
*/
get currentPathChanged(): ISignal<this, ILabShell.ICurrentPathChangedArgs> {
return this._currentPathChanged;
}
/**
* The current widget in the shell's main area.
*/
get currentWidget(): Widget | null {
return this._tracker.currentWidget;
}
/**
* A signal emitted when the main area's layout is modified.
*/
get layoutModified(): ISignal<this, void> {
return this._layoutModified;
}
/**
* Whether the left area is collapsed.
*/
get leftCollapsed(): boolean {
return !this._leftHandler.sideBar.currentTitle;
}
/**
* Whether the left area is collapsed.
*/
get rightCollapsed(): boolean {
return !this._rightHandler.sideBar.currentTitle;
}
/**
* Whether JupyterLab is in presentation mode with the
* `jp-mod-presentationMode` CSS class.
*/
get presentationMode(): boolean {
return this.hasClass('jp-mod-presentationMode');
}
set presentationMode(value: boolean) {
this.toggleClass('jp-mod-presentationMode', value);
}
/**
* The main dock area's user interface mode.
*/
get mode(): DockPanel.Mode {
return this._dockPanel.mode;
}
set mode(mode: DockPanel.Mode) {
const dock = this._dockPanel;
if (mode === dock.mode) {
return;
}
const applicationCurrentWidget = this.currentWidget;
if (mode === 'single-document') {
// Cache the current multi-document layout before changing the mode.
this._cachedLayout = dock.saveLayout();
dock.mode = mode;
// In case the active widget in the dock panel is *not* the active widget
// of the application, defer to the application.
if (this.currentWidget) {
dock.activateWidget(this.currentWidget);
}
// Adjust menu and title
(this.layout as BoxLayout).insertWidget(3, this._menuHandler.panel);
this._titleHandler.show();
this._updateTitlePanelTitle();
if (this._topHandlerHiddenByUser) {
this._topHandler.panel.hide();
}
} else {
// Cache a reference to every widget currently in the dock panel before
// changing its mode.
const widgets = Array.from(dock.widgets());
dock.mode = mode;
// Restore cached layout if possible.
if (this._cachedLayout) {
// Remove any disposed widgets in the cached layout and restore.
Private.normalizeAreaConfig(dock, this._cachedLayout.main);
dock.restoreLayout(this._cachedLayout);
this._cachedLayout = null;
}
// If layout restoration has been deferred, restore layout now.
if (this._layoutRestorer.isDeferred) {
this._layoutRestorer
.restoreDeferred()
.then(mainArea => {
if (mainArea) {
const { currentWidget, dock } = mainArea;
if (dock) {
this._dockPanel.restoreLayout(dock);
}
if (currentWidget) {
this.activateById(currentWidget.id);
}
}
})
.catch(reason => {
console.error('Failed to restore the deferred layout.');
console.error(reason);
});
}
// Add any widgets created during single document mode, which have
// subsequently been removed from the dock panel after the multiple document
// layout has been restored. If the widget has add options cached for
// the widget (i.e., if it has been placed with respect to another widget),
// then take that into account.
widgets.forEach(widget => {
if (!widget.parent) {
this._addToMainArea(widget, {
...this._mainOptionsCache.get(widget),
activate: false
});
}
});
this._mainOptionsCache.clear();
// In case the active widget in the dock panel is *not* the active widget
// of the application, defer to the application.
if (applicationCurrentWidget) {
dock.activateWidget(applicationCurrentWidget);
}
// Adjust menu and title
this.add(this._menuHandler.panel, 'top', { rank: 100 });
this._titleHandler.hide();
}
// Set the mode data attribute on the applications shell node.
this.node.dataset.shellMode = mode;
this._downPanel.fit();
// Emit the mode changed signal
this._modeChanged.emit(mode);
}
/**
* A signal emitted when the shell/dock panel change modes (single/multiple document).
*/
get modeChanged(): ISignal<this, DockPanel.Mode> {
return this._modeChanged;
}
/**
* Promise that resolves when state is first restored, returning layout
* description.
*/
get restored(): Promise<ILabShell.ILayout> {
return this._restored.promise;
}
get translator(): ITranslator {
return this._translator ?? nullTranslator;
}
set translator(value: ITranslator) {
if (value !== this._translator) {
this._translator = value;
// Set translator for tab bars
TabBarSvg.translator = value;
const trans = value.load('jupyterlab');
this._menuHandler.panel.node.setAttribute(
'aria-label',
trans.__('main menu')
);
this._leftHandler.sideBar.node.setAttribute(
'aria-label',
trans.__('main sidebar')
);
this._leftHandler.sideBar.contentNode.setAttribute(
'aria-label',
trans.__('main sidebar')
);
this._rightHandler.sideBar.node.setAttribute(
'aria-label',
trans.__('alternate sidebar')
);
this._rightHandler.sideBar.contentNode.setAttribute(
'aria-label',
trans.__('alternate sidebar')
);
}
}
/**
* User customized shell layout.
*/
get userLayout(): {
'single-document': ILabShell.IUserLayout;
'multiple-document': ILabShell.IUserLayout;
} {
return JSONExt.deepCopy(this._userLayout as any);
}
/**
* Activate a widget in its area.
*/
activateById(id: string): void {
if (this._leftHandler.has(id)) {
this._leftHandler.activate(id);
return;
}
if (this._rightHandler.has(id)) {
this._rightHandler.activate(id);
return;
}
const tabIndex = this._downPanel.tabBar.titles.findIndex(
title => title.owner.id === id
);
if (tabIndex >= 0) {
this._downPanel.currentIndex = tabIndex;
return;
}
const dock = this._dockPanel;
const widget = find(dock.widgets(), value => value.id === id);
if (widget) {
dock.activateWidget(widget);
}
}
/**
* Activate widget in specified area.
*
* ### Notes
* The alpha version of this method only supports activating the "main" area.
*
* @alpha
* @param area Name of area to activate
*/
activateArea(area: ILabShell.Area = 'main'): void {
switch (area) {
case 'main':
{
const current = this._currentTabBar();
if (!current) {
return;
}
if (current.currentTitle) {
current.currentTitle.owner.activate();
}
}
return;
case 'left':
case 'right':
case 'header':
case 'top':
case 'menu':
case 'bottom':
console.debug(`Area: ${area} activation not yet implemented`);
break;
default:
throw new Error(`Invalid area: ${area}`);
}
}
/**
* Activate the next Tab in the active TabBar.
*/
activateNextTab(): void {
const current = this._currentTabBar();
if (!current) {
return;
}
const ci = current.currentIndex;
if (ci === -1) {
return;
}
if (ci < current.titles.length - 1) {
current.currentIndex += 1;
if (current.currentTitle) {
current.currentTitle.owner.activate();
}
return;
}
if (ci === current.titles.length - 1) {
const nextBar = this._adjacentBar('next');
if (nextBar) {
nextBar.currentIndex = 0;
if (nextBar.currentTitle) {
nextBar.currentTitle.owner.activate();
}
}
}
}
/**
* Activate the previous Tab in the active TabBar.
*/
activatePreviousTab(): void {
const current = this._currentTabBar();
if (!current) {
return;
}
const ci = current.currentIndex;
if (ci === -1) {
return;
}
if (ci > 0) {
current.currentIndex -= 1;
if (current.currentTitle) {
current.currentTitle.owner.activate();
}
return;
}
if (ci === 0) {
const prevBar = this._adjacentBar('previous');
if (prevBar) {
const len = prevBar.titles.length;
prevBar.currentIndex = len - 1;
if (prevBar.currentTitle) {
prevBar.currentTitle.owner.activate();
}
}
}
}
/**
* Activate the next TabBar.
*/
activateNextTabBar(): void {
const nextBar = this._adjacentBar('next');
if (nextBar) {
if (nextBar.currentTitle) {
nextBar.currentTitle.owner.activate();
}
}
}
/**
* Activate the next TabBar.
*/
activatePreviousTabBar(): void {
const nextBar = this._adjacentBar('previous');
if (nextBar) {
if (nextBar.currentTitle) {
nextBar.currentTitle.owner.activate();
}
}
}
/**
* Add a widget to the JupyterLab shell
*
* @param widget Widget
* @param area Area
* @param options Options
*/
add(
widget: Widget,
area: ILabShell.Area = 'main',
options?: DocumentRegistry.IOpenOptions
): void {
if (!this._userLayout) {
this._delayedWidget.push({ widget, area, options });
return;
}
let userPosition: ILabShell.IWidgetPosition | undefined;
if (options?.type && this._userLayout[this.mode][options.type]) {
userPosition = this._userLayout[this.mode][options.type];
this._idTypeMap.set(widget.id, options.type);
} else {
userPosition = this._userLayout[this.mode][widget.id];
}
if (options?.type) {
this._idTypeMap.set(widget.id, options.type);
widget.disposed.connect(() => {
this._idTypeMap.delete(widget.id);
});
}
area = userPosition?.area ?? area;
options =
options || userPosition?.options
? {
...options,
...userPosition?.options
}
: undefined;
switch (area || 'main') {
case 'bottom':
return this._addToBottomArea(widget, options);
case 'down':
return this._addToDownArea(widget, options);
case 'header':
return this._addToHeaderArea(widget, options);
case 'left':
return this._addToLeftArea(widget, options);
case 'main':
return this._addToMainArea(widget, options);
case 'menu':
return this._addToMenuArea(widget, options);
case 'right':
return this._addToRightArea(widget, options);
case 'top':
return this._addToTopArea(widget, options);
default:
throw new Error(`Invalid area: ${area}`);
}
}
/**
* Move a widget type to a new area.
*
* The type is determined from the `widget.id` and fallback to `widget.id`.
*
* #### Notes
* If `mode` is undefined, both mode are updated.
* The new layout is now persisted.
*
* @param widget Widget to move
* @param area New area
* @param mode Mode to change