This repository was archived by the owner on Nov 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathdocklayout.ts
More file actions
2120 lines (1815 loc) · 57.3 KB
/
docklayout.ts
File metadata and controls
2120 lines (1815 loc) · 57.3 KB
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) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import {
ArrayExt, ChainIterator, IIterator, chain, each, empty, map, once, reduce
} from '@phosphor/algorithm';
import {
ElementExt
} from '@phosphor/domutils';
import {
Message, MessageLoop
} from '@phosphor/messaging';
import {
BoxEngine, BoxSizer
} from './boxengine';
import {
Layout, LayoutItem
} from './layout';
import {
TabBar
} from './tabbar';
import {
Widget
} from './widget';
/**
* A layout which provides a flexible docking arrangement.
*
* #### Notes
* The consumer of this layout is repsonsible for handling all signals
* from the generated tab bars and managing the visibility of widgets
* and tab bars as needed.
*/
export
class DockLayout extends Layout {
/**
* Construct a new dock layout.
*
* @param options - The options for initializing the layout.
*/
constructor(options: DockLayout.IOptions) {
super();
this.renderer = options.renderer;
if (options.spacing !== undefined) {
this._spacing = Private.clampSpacing(options.spacing);
}
}
/**
* Dispose of the resources held by the layout.
*
* #### Notes
* This will clear and dispose all widgets in the layout.
*/
dispose(): void {
// Get an iterator over the widgets in the layout.
let widgets = this.iter();
// Dispose of the layout items.
this._items.forEach(item => { item.dispose(); });
// Clear the layout state before disposing the widgets.
this._box = null;
this._root = null;
this._items.clear();
// Dispose of the widgets contained in the old layout root.
each(widgets, widget => { widget.dispose(); });
// Dispose of the base class.
super.dispose();
}
/**
* The renderer used by the dock layout.
*/
readonly renderer: DockLayout.IRenderer;
/**
* Get the inter-element spacing for the dock layout.
*/
get spacing(): number {
return this._spacing;
}
/**
* Set the inter-element spacing for the dock layout.
*/
set spacing(value: number) {
value = Private.clampSpacing(value);
if (this._spacing === value) {
return;
}
this._spacing = value;
if (!this.parent) {
return;
}
this.parent.fit();
}
/**
* Whether the dock layout is empty.
*/
get isEmpty(): boolean {
return this._root === null;
}
/**
* Create an iterator over all widgets in the layout.
*
* @returns A new iterator over the widgets in the layout.
*
* #### Notes
* This iterator includes the generated tab bars.
*/
iter(): IIterator<Widget> {
return this._root ? this._root.iterAllWidgets() : empty<Widget>();
}
/**
* Create an iterator over the user widgets in the layout.
*
* @returns A new iterator over the user widgets in the layout.
*
* #### Notes
* This iterator does not include the generated tab bars.
*/
widgets(): IIterator<Widget> {
return this._root ? this._root.iterUserWidgets() : empty<Widget>();
}
/**
* Create an iterator over the selected widgets in the layout.
*
* @returns A new iterator over the selected user widgets.
*
* #### Notes
* This iterator yields the widgets corresponding to the current tab
* of each tab bar in the layout.
*/
selectedWidgets(): IIterator<Widget> {
return this._root ? this._root.iterSelectedWidgets() : empty<Widget>();
}
/**
* Create an iterator over the tab bars in the layout.
*
* @returns A new iterator over the tab bars in the layout.
*
* #### Notes
* This iterator does not include the user widgets.
*/
tabBars(): IIterator<TabBar<Widget>> {
return this._root ? this._root.iterTabBars() : empty<TabBar<Widget>>();
}
/**
* Create an iterator over the handles in the layout.
*
* @returns A new iterator over the handles in the layout.
*/
handles(): IIterator<HTMLDivElement> {
return this._root ? this._root.iterHandles() : empty<HTMLDivElement>();
}
/**
* Move a handle to the given offset position.
*
* @param handle - The handle to move.
*
* @param offsetX - The desired offset X position of the handle.
*
* @param offsetY - The desired offset Y position of the handle.
*
* #### Notes
* If the given handle is not contained in the layout, this is no-op.
*
* The handle will be moved as close as possible to the desired
* position without violating any of the layout constraints.
*
* Only one of the coordinates is used depending on the orientation
* of the handle. This method accepts both coordinates to make it
* easy to invoke from a mouse move event without needing to know
* the handle orientation.
*/
moveHandle(handle: HTMLDivElement, offsetX: number, offsetY: number): void {
// Bail early if there is no root or if the handle is hidden.
if (!this._root || handle.classList.contains('p-mod-hidden')) {
return;
}
// Lookup the split node for the handle.
let data = this._root.findSplitNode(handle);
if (!data) {
return;
}
// Compute the desired delta movement for the handle.
let delta: number;
if (data.node.orientation === 'horizontal') {
delta = offsetX - handle.offsetLeft;
} else {
delta = offsetY - handle.offsetTop;
}
// Bail if there is no handle movement.
if (delta === 0) {
return;
}
// Prevent sibling resizing unless needed.
data.node.holdSizes();
// Adjust the sizers to reflect the handle movement.
BoxEngine.adjust(data.node.sizers, data.index, delta);
// Update the layout of the widgets.
if (this.parent) {
this.parent.update();
}
}
/**
* Save the current configuration of the dock layout.
*
* @returns A new config object for the current layout state.
*
* #### Notes
* The return value can be provided to the `restoreLayout` method
* in order to restore the layout to its current configuration.
*/
saveLayout(): DockLayout.ILayoutConfig {
// Bail early if there is no root.
if (!this._root) {
return { main: null };
}
// Hold the current sizes in the layout tree.
this._root.holdAllSizes();
// Return the layout config.
return { main: this._root.createConfig() };
}
/**
* Restore the layout to a previously saved configuration.
*
* @param config - The layout configuration to restore.
*
* #### Notes
* Widgets which currently belong to the layout but which are not
* contained in the config will be unparented.
*/
restoreLayout(config: DockLayout.ILayoutConfig): void {
// Create the widget set for validating the config.
let widgetSet = new Set<Widget>();
// Normalize the main area config and collect the widgets.
let mainConfig: DockLayout.AreaConfig | null;
if (config.main) {
mainConfig = Private.normalizeAreaConfig(config.main, widgetSet);
} else {
mainConfig = null;
}
// Create iterators over the old content.
let oldWidgets = this.widgets();
let oldTabBars = this.tabBars();
let oldHandles = this.handles();
// Clear the root before removing the old content.
this._root = null;
// Unparent the old widgets which are not in the new config.
each(oldWidgets, widget => {
if (!widgetSet.has(widget)) {
widget.parent = null;
}
});
// Dispose of the old tab bars.
each(oldTabBars, tabBar => {
tabBar.dispose();
});
// Remove the old handles.
each(oldHandles, handle => {
if (handle.parentNode) {
handle.parentNode.removeChild(handle);
}
});
// Reparent the new widgets to the current parent.
widgetSet.forEach(widget => {
widget.parent = this.parent;
});
// Create the root node for the new config.
if (mainConfig) {
this._root = Private.realizeAreaConfig(mainConfig, {
createTabBar: () => this._createTabBar(),
createHandle: () => this._createHandle()
});
} else {
this._root = null;
}
// If there is no parent, there is nothing more to do.
if (!this.parent) {
return;
}
// Attach the new widgets to the parent.
widgetSet.forEach(widget => {
this.attachWidget(widget);
});
// Post a fit request to the parent.
this.parent.fit();
}
/**
* Add a widget to the dock layout.
*
* @param widget - The widget to add to the dock layout.
*
* @param options - The additional options for adding the widget.
*
* #### Notes
* The widget will be moved if it is already contained in the layout.
*
* An error will be thrown if the reference widget is invalid.
*/
addWidget(widget: Widget, options: DockLayout.IAddOptions = {}): void {
// Parse the options.
let ref = options.ref || null;
let mode = options.mode || 'tab-after';
let proportion = options.proportion || 0.5;
// Find the tab node which holds the reference widget.
let refNode: Private.TabLayoutNode | null = null;
if (this._root && ref) {
refNode = this._root.findTabNode(ref);
}
// Throw an error if the reference widget is invalid.
if (ref && !refNode) {
throw new Error('Reference widget is not in the layout.');
}
// Reparent the widget to the current layout parent.
widget.parent = this.parent;
// Insert the widget according to the insert mode.
switch (mode) {
case 'tab-after':
this._insertTab(widget, ref, refNode, true);
break;
case 'tab-before':
this._insertTab(widget, ref, refNode, false);
break;
case 'split-top':
this._insertSplit(widget, ref, refNode, 'vertical', false, proportion);
break;
case 'split-left':
this._insertSplit(widget, ref, refNode, 'horizontal', false, proportion);
break;
case 'split-right':
this._insertSplit(widget, ref, refNode, 'horizontal', true, proportion);
break;
case 'split-bottom':
this._insertSplit(widget, ref, refNode, 'vertical', true, proportion);
break;
}
// Do nothing else if there is no parent widget.
if (!this.parent) {
return;
}
// Ensure the widget is attached to the parent widget.
this.attachWidget(widget);
// Post a fit request for the parent widget.
this.parent.fit();
}
/**
* Remove a widget from the layout.
*
* @param widget - The widget to remove from the layout.
*
* #### Notes
* A widget is automatically removed from the layout when its `parent`
* is set to `null`. This method should only be invoked directly when
* removing a widget from a layout which has yet to be installed on a
* parent widget.
*
* This method does *not* modify the widget's `parent`.
*/
removeWidget(widget: Widget): void {
// Remove the widget from its current layout location.
this._removeWidget(widget);
// Do nothing else if there is no parent widget.
if (!this.parent) {
return;
}
// Detach the widget from the parent widget.
this.detachWidget(widget);
// Post a fit request for the parent widget.
this.parent.fit();
}
/**
* Find the tab area which contains the given client position.
*
* @param clientX - The client X position of interest.
*
* @param clientY - The client Y position of interest.
*
* @returns The geometry of the tab area at the given position, or
* `null` if there is no tab area at the given position.
*/
hitTestTabAreas(clientX: number, clientY: number): DockLayout.ITabAreaGeometry | null {
// Bail early if hit testing cannot produce valid results.
if (!this._root || !this.parent || !this.parent.isVisible) {
return null;
}
// Ensure the parent box sizing data is computed.
if (!this._box) {
this._box = ElementExt.boxSizing(this.parent.node);
}
// Convert from client to local coordinates.
let rect = this.parent.node.getBoundingClientRect();
let x = clientX - rect.left - this._box.borderLeft;
let y = clientY - rect.top - this._box.borderTop;
// Find the tab layout node at the local position.
let tabNode = this._root.hitTestTabNodes(x, y);
// Bail if a tab layout node was not found.
if (!tabNode) {
return null;
}
// Extract the data from the tab node.
let { tabBar, top, left, width, height } = tabNode;
// Compute the right and bottom edges of the tab area.
let borderWidth = this._box.borderLeft + this._box.borderRight;
let borderHeight = this._box.borderTop + this._box.borderBottom;
let right = rect.width - borderWidth - (left + width);
let bottom = rect.height - borderHeight - (top + height);
// Return the hit test results.
return { tabBar, x, y, top, left, right, bottom, width, height };
}
/**
* Perform layout initialization which requires the parent widget.
*/
protected init(): void {
// Perform superclass initialization.
super.init();
// Attach each widget to the parent.
each(this, widget => { this.attachWidget(widget); });
// Attach each handle to the parent.
each(this.handles(), handle => { this.parent!.node.appendChild(handle); });
// Post a fit request for the parent widget.
this.parent!.fit();
}
/**
* Attach the widget to the layout parent widget.
*
* @param widget - The widget to attach to the parent.
*
* #### Notes
* This is a no-op if the widget is already attached.
*/
protected attachWidget(widget: Widget): void {
// Do nothing if the widget is already attached.
if (this.parent!.node === widget.node.parentNode) {
return;
}
// Create the layout item for the widget.
this._items.set(widget, new LayoutItem(widget));
// Send a `'before-attach'` message if the parent is attached.
if (this.parent!.isAttached) {
MessageLoop.sendMessage(widget, Widget.Msg.BeforeAttach);
}
// Add the widget's node to the parent.
this.parent!.node.appendChild(widget.node);
// Send an `'after-attach'` message if the parent is attached.
if (this.parent!.isAttached) {
MessageLoop.sendMessage(widget, Widget.Msg.AfterAttach);
}
}
/**
* Detach the widget from the layout parent widget.
*
* @param widget - The widget to detach from the parent.
*
* #### Notes
* This is a no-op if the widget is not attached.
*/
protected detachWidget(widget: Widget): void {
// Do nothing if the widget is not attached.
if (this.parent!.node !== widget.node.parentNode) {
return;
}
// Send a `'before-detach'` message if the parent is attached.
if (this.parent!.isAttached) {
MessageLoop.sendMessage(widget, Widget.Msg.BeforeDetach);
}
// Remove the widget's node from the parent.
this.parent!.node.removeChild(widget.node);
// Send an `'after-detach'` message if the parent is attached.
if (this.parent!.isAttached) {
MessageLoop.sendMessage(widget, Widget.Msg.AfterDetach);
}
// Delete the layout item for the widget.
let item = this._items.get(widget);
if (item) {
this._items.delete(widget);
item.dispose();
}
}
/**
* A message handler invoked on a `'before-show'` message.
*/
protected onBeforeShow(msg: Message): void {
super.onBeforeShow(msg);
this.parent!.update();
}
/**
* A message handler invoked on a `'before-attach'` message.
*/
protected onBeforeAttach(msg: Message): void {
super.onBeforeAttach(msg);
this.parent!.fit();
}
/**
* A message handler invoked on a `'child-shown'` message.
*/
protected onChildShown(msg: Widget.ChildMessage): void {
this.parent!.fit();
}
/**
* A message handler invoked on a `'child-hidden'` message.
*/
protected onChildHidden(msg: Widget.ChildMessage): void {
this.parent!.fit();
}
/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(msg: Widget.ResizeMessage): void {
if (this.parent!.isVisible) {
this._update(msg.width, msg.height);
}
}
/**
* A message handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
if (this.parent!.isVisible) {
this._update(-1, -1);
}
}
/**
* A message handler invoked on a `'fit-request'` message.
*/
protected onFitRequest(msg: Message): void {
if (this.parent!.isAttached) {
this._fit();
}
}
/**
* Remove the specified widget from the layout structure.
*
* #### Notes
* This is a no-op if the widget is not in the layout tree.
*
* This does not detach the widget from the parent node.
*/
private _removeWidget(widget: Widget): void {
// Bail early if there is no layout root.
if (!this._root) {
return;
}
// Find the tab node which contains the given widget.
let tabNode = this._root.findTabNode(widget);
// Bail early if the tab node is not found.
if (!tabNode) {
return;
}
// If there are multiple tabs, just remove the widget's tab.
if (tabNode.tabBar.titles.length > 1) {
tabNode.tabBar.removeTab(widget.title);
return;
}
// Otherwise, the tab node needs to be removed...
// Dispose the tab bar.
tabNode.tabBar.dispose();
// Handle the case where the tab node is the root.
if (this._root === tabNode) {
this._root = null;
return;
}
// Otherwise, remove the tab node from its parent...
// Prevent widget resizing unless needed.
this._root.holdAllSizes();
// Clear the parent reference on the tab node.
let splitNode = tabNode.parent!;
tabNode.parent = null;
// Remove the tab node from its parent split node.
let i = ArrayExt.removeFirstOf(splitNode.children, tabNode);
let handle = ArrayExt.removeAt(splitNode.handles, i)!;
ArrayExt.removeAt(splitNode.sizers, i);
// Remove the handle from its parent DOM node.
if (handle.parentNode) {
handle.parentNode.removeChild(handle);
}
// If there are multiple children, just update the handles.
if (splitNode.children.length > 1) {
splitNode.syncHandles();
return;
}
// Otherwise, the split node also needs to be removed...
// Clear the parent reference on the split node.
let maybeParent = splitNode.parent;
splitNode.parent = null;
// Lookup the remaining child node and handle.
let childNode = splitNode.children[0];
let childHandle = splitNode.handles[0];
// Clear the split node data.
splitNode.children.length = 0;
splitNode.handles.length = 0;
splitNode.sizers.length = 0;
// Remove the child handle from its parent node.
if (childHandle.parentNode) {
childHandle.parentNode.removeChild(childHandle);
}
// Handle the case where the split node is the root.
if (this._root === splitNode) {
childNode.parent = null;
this._root = childNode;
return;
}
// Otherwise, move the child node to the parent node...
let parentNode = maybeParent!;
// Lookup the index of the split node.
let j = parentNode.children.indexOf(splitNode);
// Handle the case where the child node is a tab node.
if (childNode instanceof Private.TabLayoutNode) {
childNode.parent = parentNode;
parentNode.children[j] = childNode;
return;
}
// Remove the split data from the parent.
let splitHandle = ArrayExt.removeAt(parentNode.handles, j)!;
ArrayExt.removeAt(parentNode.children, j);
ArrayExt.removeAt(parentNode.sizers, j);
// Remove the handle from its parent node.
if (splitHandle.parentNode) {
splitHandle.parentNode.removeChild(splitHandle);
}
// The child node and the split parent node will have the same
// orientation. Merge the grand-children with the parent node.
for (let i = 0, n = childNode.children.length; i < n; ++i) {
let gChild = childNode.children[i];
let gHandle = childNode.handles[i];
let gSizer = childNode.sizers[i];
ArrayExt.insert(parentNode.children, j + i, gChild);
ArrayExt.insert(parentNode.handles, j + i, gHandle);
ArrayExt.insert(parentNode.sizers, j + i, gSizer);
gChild.parent = parentNode;
}
// Clear the child node.
childNode.children.length = 0;
childNode.handles.length = 0;
childNode.sizers.length = 0;
childNode.parent = null;
// Sync the handles on the parent node.
parentNode.syncHandles();
}
/**
* Insert a widget next to an existing tab.
*
* #### Notes
* This does not attach the widget to the parent widget.
*/
private _insertTab(widget: Widget, ref: Widget | null, refNode: Private.TabLayoutNode | null, after: boolean): void {
// Do nothing if the tab is inserted next to itself.
if (widget === ref) {
return;
}
// Create the root if it does not exist.
if (!this._root) {
let tabNode = new Private.TabLayoutNode(this._createTabBar());
tabNode.tabBar.addTab(widget.title);
this._root = tabNode;
return;
}
// Use the first tab node as the ref node if needed.
if (!refNode) {
refNode = this._root.findFirstTabNode()!;
}
// If the widget is not contained in the ref node, ensure it is
// removed from the layout and hidden before being added again.
if (refNode.tabBar.titles.indexOf(widget.title) === -1) {
this._removeWidget(widget);
widget.hide();
}
// Lookup the target index for inserting the tab.
let index: number;
if (ref) {
index = refNode.tabBar.titles.indexOf(ref.title);
} else {
index = refNode.tabBar.currentIndex;
}
// Insert the widget's tab relative to the target index.
refNode.tabBar.insertTab(index + (after ? 1 : 0), widget.title);
}
/**
* Insert a widget as a new split area.
*
* #### Notes
* This does not attach the widget to the parent widget.
*/
private _insertSplit(widget: Widget, ref: Widget | null, refNode: Private.TabLayoutNode | null, orientation: Private.Orientation, after: boolean, proportion: number): void {
// Do nothing if there is no effective split.
if (widget === ref && refNode && refNode.tabBar.titles.length === 1) {
return;
}
// Ensure the widget is removed from the current layout.
this._removeWidget(widget);
// Create the tab layout node to hold the widget.
let tabNode = new Private.TabLayoutNode(this._createTabBar());
tabNode.tabBar.addTab(widget.title);
// Set the root if it does not exist.
if (!this._root) {
this._root = tabNode;
return;
}
// If the ref node parent is null, split the root.
if (!refNode || !refNode.parent) {
// Ensure the root is split with the correct orientation.
let root = this._splitRoot(orientation);
// Determine the insert index for the new tab node.
let i = after ? root.children.length : 0;
// Normalize the split node.
root.normalizeSizes();
// Create the sizer for new tab node.
let sizer = Private.createSizer(refNode ? 1 : Private.GOLDEN_RATIO);
// Insert the tab node sized to the golden ratio.
ArrayExt.insert(root.children, i, tabNode);
ArrayExt.insert(root.sizers, i, sizer);
ArrayExt.insert(root.handles, i, this._createHandle());
tabNode.parent = root;
// Re-normalize the split node to maintain the ratios.
root.normalizeSizes();
// Finally, synchronize the visibility of the handles.
root.syncHandles();
return;
}
// Lookup the split node for the ref widget.
let splitNode = refNode.parent;
// If the split node already had the correct orientation,
// the widget can be inserted into the split node directly.
if (splitNode.orientation === orientation) {
// Find the index of the ref node.
let i = splitNode.children.indexOf(refNode);
// Normalize the split node.
splitNode.normalizeSizes();
// Consume half the space for the insert location.
if (proportion >= 1 || proportion <= 0) {
proportion = 0.5;
}
splitNode.sizers[i].sizeHint = (1 - proportion);
// Insert the tab node sized to the other half.
let j = i + (after ? 1 : 0);
ArrayExt.insert(splitNode.children, j, tabNode);
ArrayExt.insert(splitNode.sizers, j, Private.createSizer(proportion));
ArrayExt.insert(splitNode.handles, j, this._createHandle());
tabNode.parent = splitNode;
// Finally, synchronize the visibility of the handles.
splitNode.syncHandles();
return;
}
// Remove the ref node from the split node.
let i = ArrayExt.removeFirstOf(splitNode.children, refNode);
// Create a new normalized split node for the children.
let childNode = new Private.SplitLayoutNode(orientation);
childNode.normalized = true;
// Add the ref node sized to half the space.
childNode.children.push(refNode);
childNode.sizers.push(Private.createSizer(0.5));
childNode.handles.push(this._createHandle());
refNode.parent = childNode;
// Add the tab node sized to the other half.
let j = after ? 1 : 0;
ArrayExt.insert(childNode.children, j, tabNode);
ArrayExt.insert(childNode.sizers, j, Private.createSizer(0.5));
ArrayExt.insert(childNode.handles, j, this._createHandle());
tabNode.parent = childNode;
// Synchronize the visibility of the handles.
childNode.syncHandles();
// Finally, add the new child node to the original split node.
ArrayExt.insert(splitNode.children, i, childNode);
childNode.parent = splitNode;
}
/**
* Ensure the root is a split node with the given orientation.
*/
private _splitRoot(orientation: Private.Orientation): Private.SplitLayoutNode {
// Bail early if the root already meets the requirements.
let oldRoot = this._root;
if (oldRoot instanceof Private.SplitLayoutNode) {
if (oldRoot.orientation === orientation) {
return oldRoot;
}
}
// Create a new root node with the specified orientation.
let newRoot = this._root = new Private.SplitLayoutNode(orientation);
// Add the old root to the new root.
if (oldRoot) {
newRoot.children.push(oldRoot);
newRoot.sizers.push(Private.createSizer(0));
newRoot.handles.push(this._createHandle());
oldRoot.parent = newRoot;
}
// Return the new root as a convenience.
return newRoot;
}
/**
* Fit the layout to the total size required by the widgets.
*/
private _fit(): void {
// Set up the computed minimum size.
let minW = 0;
let minH = 0;
// Update the size limits for the layout tree.
if (this._root) {
let limits = this._root.fit(this._spacing, this._items);
minW = limits.minWidth;
minH = limits.minHeight;
}
// Update the box sizing and add it to the computed min size.
let box = this._box = ElementExt.boxSizing(this.parent!.node);
minW += box.horizontalSum;
minH += box.verticalSum;
// Update the parent's min size constraints.
let style = this.parent!.node.style;
style.minWidth = `${minW}px`;
style.minHeight = `${minH}px`;
// Set the dirty flag to ensure only a single update occurs.
this._dirty = true;
// Notify the ancestor that it should fit immediately. This may
// cause a resize of the parent, fulfilling the required update.
if (this.parent!.parent) {
MessageLoop.sendMessage(this.parent!.parent!, Widget.Msg.FitRequest);
}
// If the dirty flag is still set, the parent was not resized.
// Trigger the required update on the parent widget immediately.
if (this._dirty) {
MessageLoop.sendMessage(this.parent!, Widget.Msg.UpdateRequest);
}
}
/**
* Update the layout position and size of the widgets.
*
* The parent offset dimensions should be `-1` if unknown.
*/
private _update(offsetWidth: number, offsetHeight: number): void {
// Clear the dirty flag to indicate the update occurred.
this._dirty = false;
// Bail early if there is no root layout node.
if (!this._root) {
return;
}
// Measure the parent if the offset dimensions are unknown.
if (offsetWidth < 0) {
offsetWidth = this.parent!.node.offsetWidth;
}
if (offsetHeight < 0) {
offsetHeight = this.parent!.node.offsetHeight;
}
// Ensure the parent box sizing data is computed.
if (!this._box) {
this._box = ElementExt.boxSizing(this.parent!.node);
}