-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathsplit-view.ts
1225 lines (996 loc) · 31.9 KB
/
split-view.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
import EventEmitter from "eventemitter3";
import clamp from "lodash.clamp";
import styles from "../allotment.module.css";
import { pushToEnd, pushToStart, range } from "../helpers/array";
import { Disposable } from "../helpers/disposable";
import {
Orientation,
Sash,
SashEvent as BaseSashEvent,
SashState,
} from "../sash";
interface SashEvent {
readonly sash: Sash;
readonly start: number;
readonly current: number;
}
/**
* When adding or removing views, distribute the delta space among
* all other views.
*/
export type DistributeSizing = { type: "distribute" };
/**
* When adding or removing views, split the delta space with another
* specific view, indexed by the provided `index`.
*/
export type SplitSizing = { type: "split"; index: number };
/**
* When adding or removing views, assume the view is invisible.
*/
export type InvisibleSizing = { type: "invisible"; cachedVisibleSize: number };
/**
* When adding or removing views, the sizing provides fine grained
* control over how other views get resized.
*/
export type Sizing = DistributeSizing | SplitSizing | InvisibleSizing;
export namespace Sizing {
/**
* When adding or removing views, distribute the delta space among
* all other views.
*/
export const Distribute: DistributeSizing = { type: "distribute" };
/**
* When adding or removing views, split the delta space with another
* specific view, indexed by the provided `index`.
*/
export function Split(index: number): SplitSizing {
return { type: "split", index };
}
/**
* When adding or removing views, assume the view is invisible.
*/
export function Invisible(cachedVisibleSize: number): InvisibleSizing {
return { type: "invisible", cachedVisibleSize };
}
}
/** A descriptor for a {@link SplitView} instance. */
export interface SplitViewDescriptor {
/** The layout size of the {@link SplitView}. */
size: number;
/**
* Descriptors for each {@link View view}.
*/
views: {
/** Whether the {@link View view} is visible. */
visible?: boolean;
/** The size of the {@link View view}. */
size: number;
container: HTMLElement;
view: View;
}[];
}
export interface SplitViewOptions {
/** Which axis the views align on. */
readonly orientation?: Orientation;
/** Resize each view proportionally when resizing the SplitView. */
readonly proportionalLayout?: boolean;
/**
* An initial description of this {@link SplitView} instance, allowing
* to initialze all views within the ctor.
*/
readonly descriptor?: SplitViewDescriptor;
/** Override the orthogonal size of sashes. */
readonly getSashOrthogonalSize?: () => number;
}
export enum LayoutPriority {
Normal = "NORMAL",
Low = "LOW",
High = "HIGH",
}
/**
* The interface to implement for views within a {@link SplitView}.
*/
export interface View {
/** The DOM element for this view. */
readonly element: HTMLElement;
/**
* A minimum size for this view.
*
* @remarks If none, set it to `0`.
*/
readonly minimumSize: number;
/**
* A minimum size for this view.
*
* @remarks If none, set it to `Number.POSITIVE_INFINITY`.
*/
readonly maximumSize: number;
/**
* The priority of the view when the {@link SplitView.resize layout} algorithm
* runs. Views with higher priority will be resized first.
*
* @remarks Only used when `proportionalLayout` is false.
*/
readonly priority?: LayoutPriority;
/**
* Whether the view will snap whenever the user reaches its minimum size or
* attempts to grow it beyond the minimum size.
*/
readonly snap?: boolean;
/**
* This will be called by the {@link SplitView} during layout. A view meant to
* pass along the layout information down to its descendants.
*
* @param size The size of this view, in pixels.
* @param offset The offset of this view, relative to the start of the {@link SplitView}.
*/
layout(size: number, offset: number): void;
/**
* This will be called by the {@link SplitView} whenever this view is made
* visible or hidden.
*
* @param visible Whether the view becomes visible.
*/
setVisible?(visible: boolean): void;
}
type ViewItemSize = number | { cachedVisibleSize: number };
abstract class ViewItem {
protected container: HTMLElement;
public view: View;
private _size: number;
constructor(container: HTMLElement, view: View, size: ViewItemSize) {
this.container = container;
this.view = view;
this.container.classList.add("split-view-view", styles.splitViewView);
this.container.dataset.testid = "split-view-view";
if (typeof size === "number") {
this._size = size;
this._cachedVisibleSize = undefined;
container.classList.add("split-view-view-visible");
} else {
this._size = 0;
this._cachedVisibleSize = size.cachedVisibleSize;
}
}
set size(size: number) {
this._size = size;
}
get size(): number {
return this._size;
}
get priority(): LayoutPriority | undefined {
return this.view.priority;
}
get snap(): boolean {
return !!this.view.snap;
}
private _cachedVisibleSize: number | undefined = undefined;
get cachedVisibleSize(): number | undefined {
return this._cachedVisibleSize;
}
get visible(): boolean {
return typeof this._cachedVisibleSize === "undefined";
}
public setVisible(visible: boolean, size?: number): void {
if (visible === this.visible) {
return;
}
if (visible) {
this.size = clamp(
this._cachedVisibleSize!,
this.viewMinimumSize,
this.viewMaximumSize
);
this._cachedVisibleSize = undefined;
} else {
this._cachedVisibleSize = typeof size === "number" ? size : this.size;
this.size = 0;
}
this.container.classList.toggle("split-view-view-visible", visible);
if (this.view.setVisible) {
this.view.setVisible(visible);
}
}
get minimumSize(): number {
return this.visible ? this.view.minimumSize : 0;
}
get viewMinimumSize(): number {
return this.view.minimumSize;
}
get maximumSize(): number {
return this.visible ? this.view.maximumSize : 0;
}
get viewMaximumSize(): number {
return this.view.maximumSize;
}
set enabled(enabled: boolean) {
this.container.style.pointerEvents = enabled ? "" : "none";
}
layout(offset: number): void {
this.layoutContainer(offset);
this.view.layout(this.size, offset);
}
abstract layoutContainer(offset: number): void;
}
class HorizontalViewItem extends ViewItem {
layoutContainer(offset: number): void {
this.container.style.left = `${offset}px`;
this.container.style.width = `${this.size}px`;
}
}
class VerticalViewItem extends ViewItem {
layoutContainer(offset: number): void {
this.container.style.top = `${offset}px`;
this.container.style.height = `${this.size}px`;
}
}
interface SashItem {
sash: Sash;
}
interface SashDragSnapState {
readonly index: number;
readonly limitDelta: number;
readonly size: number;
}
interface SashDragState {
index: number;
start: number;
current: number;
sizes: number[];
minDelta: number;
maxDelta: number;
snapBefore: SashDragSnapState | undefined;
snapAfter: SashDragSnapState | undefined;
}
/**
* The {@link SplitView} is the UI component which implements a one dimensional
* flex-like layout algorithm for a collection of {@link View} instances, which
* are essentially HTMLElement instances with the following size constraints:
*
* - {@link View.minimumSize}
* - {@link View.maximumSize}
* - {@link View.snap}
*
* In case the SplitView doesn't have enough size to fit all views, it will overflow
* its content with a scrollbar.
*
* In between each pair of views there will be a {@link Sash} allowing the user
* to resize the views, making sure the constraints are respected.
*/
export class SplitView extends EventEmitter implements Disposable {
public onDidChange: ((sizes: number[]) => void) | undefined;
public onDidDragStart: ((sizes: number[]) => void) | undefined;
public onDidDragEnd: ((sizes: number[]) => void) | undefined;
/** This {@link SplitView}'s orientation. */
readonly orientation: Orientation;
private sashContainer: HTMLElement;
private size = 0;
private contentSize = 0;
private proportions: undefined | number[] = undefined;
private viewItems: ViewItem[] = [];
private sashItems: SashItem[] = [];
private sashDragState: SashDragState | undefined;
private proportionalLayout: boolean;
private readonly getSashOrthogonalSize: { (): number } | undefined;
private _startSnappingEnabled = true;
get startSnappingEnabled(): boolean {
return this._startSnappingEnabled;
}
/**
* Enable/disable snapping at the beginning of this {@link SplitView}.
*/
set startSnappingEnabled(startSnappingEnabled: boolean) {
if (this._startSnappingEnabled === startSnappingEnabled) {
return;
}
this._startSnappingEnabled = startSnappingEnabled;
this.updateSashEnablement();
}
private _endSnappingEnabled = true;
get endSnappingEnabled(): boolean {
return this._endSnappingEnabled;
}
/**
* Enable/disable snapping at the end of this {@link SplitView}.
*/
set endSnappingEnabled(endSnappingEnabled: boolean) {
if (this._endSnappingEnabled === endSnappingEnabled) {
return;
}
this._endSnappingEnabled = endSnappingEnabled;
this.updateSashEnablement();
}
/** Create a new {@link SplitView} instance. */
constructor(
container: HTMLElement,
options: SplitViewOptions = {},
onDidChange?: (sizes: number[]) => void,
onDidDragStart?: (sizes: number[]) => void,
onDidDragEnd?: (sizes: number[]) => void
) {
super();
this.orientation = options.orientation ?? Orientation.Vertical;
this.proportionalLayout = options.proportionalLayout ?? true;
this.getSashOrthogonalSize = options.getSashOrthogonalSize;
if (onDidChange) {
this.onDidChange = onDidChange;
}
if(onDidDragStart){
this.onDidDragStart = onDidDragStart;
}
if(onDidDragEnd){
this.onDidDragEnd = onDidDragEnd;
}
this.sashContainer = document.createElement("div");
this.sashContainer.classList.add("sash-container", styles.sashContainer);
container.prepend(this.sashContainer); // Should always be first child
// We have an existing set of view, add them now
if (options.descriptor) {
this.size = options.descriptor.size;
for (const [
index,
viewDescriptor,
] of options.descriptor.views.entries()) {
const size = viewDescriptor.size;
const container = viewDescriptor.container;
const view = viewDescriptor.view;
this.addView(container, view, size, index, true);
}
// Initialize content size and proportions for first layout
this.contentSize = this.viewItems.reduce((r, i) => r + i.size, 0);
this.saveProportions();
}
}
/**
* Add a {@link View view} to this {@link SplitView}.
*
* @param container The container element.
* @param view The view to add.
* @param size Either a fixed size, or a dynamic {@link Sizing} strategy.
* @param index The index to insert the view on.
* @param skipLayout Whether layout should be skipped.
*/
public addView(
container: HTMLElement,
view: View,
size: number | Sizing,
index = this.viewItems.length,
skipLayout?: boolean
) {
let viewSize: ViewItemSize;
if (typeof size === "number") {
viewSize = size;
} else if (size.type === "split") {
viewSize = this.getViewSize(size.index) / 2;
} else if (size.type === "invisible") {
viewSize = { cachedVisibleSize: size.cachedVisibleSize };
} else {
viewSize = view.minimumSize;
}
const item =
this.orientation === Orientation.Vertical
? new VerticalViewItem(container, view, viewSize)
: new HorizontalViewItem(container, view, viewSize);
this.viewItems.splice(index, 0, item);
if (this.viewItems.length > 1) {
const sash =
this.orientation === Orientation.Vertical
? new Sash(
this.sashContainer,
{
getHorizontalSashTop: (s) => this.getSashPosition(s),
getHorizontalSashWidth: this.getSashOrthogonalSize,
},
{ orientation: Orientation.Horizontal }
)
: new Sash(
this.sashContainer,
{
getVerticalSashLeft: (s) => this.getSashPosition(s),
getVerticalSashHeight: this.getSashOrthogonalSize,
},
{ orientation: Orientation.Vertical }
);
const sashEventMapper =
this.orientation === Orientation.Vertical
? (e: BaseSashEvent) => ({
sash,
start: e.startY,
current: e.currentY,
})
: (e: BaseSashEvent) => ({
sash,
start: e.startX,
current: e.currentX,
});
sash.on("start", (event: BaseSashEvent) => {
this.emit("sashDragStart");
this.onSashStart(sashEventMapper(event));
const sizes = this.viewItems.map((i) => i.size);
this.onDidDragStart?.(sizes)
});
sash.on("change", (event: BaseSashEvent) =>
this.onSashChange(sashEventMapper(event))
);
sash.on("end", () => {
this.emit("sashDragEnd");
this.onSashEnd(this.sashItems.findIndex((item) => item.sash === sash));
const sizes = this.viewItems.map((i) => i.size);
this.onDidDragEnd?.(sizes)
});
sash.on("reset", () => {
const index = this.sashItems.findIndex((item) => item.sash === sash);
const upIndexes = range(index, -1, -1);
const downIndexes = range(index + 1, this.viewItems.length);
const snapBeforeIndex = this.findFirstSnapIndex(upIndexes);
const snapAfterIndex = this.findFirstSnapIndex(downIndexes);
if (
typeof snapBeforeIndex === "number" &&
!this.viewItems[snapBeforeIndex].visible
) {
return;
}
if (
typeof snapAfterIndex === "number" &&
!this.viewItems[snapAfterIndex].visible
) {
return;
}
this.emit("sashreset", index);
});
const sashItem: SashItem = { sash };
this.sashItems.splice(index - 1, 0, sashItem);
}
if (!skipLayout) {
this.relayout();
}
if (!skipLayout && typeof size !== "number" && size.type === "distribute") {
this.distributeViewSizes();
}
}
/**
* Remove a {@link View view} from this {@link SplitView}.
*
* @param index The index where the {@link View view} is located.
* @param sizing Whether to distribute other {@link View view}'s sizes.
*/
public removeView(index: number, sizing?: Sizing): View {
if (index < 0 || index >= this.viewItems.length) {
throw new Error("Index out of bounds");
}
// Remove view
const viewItem = this.viewItems.splice(index, 1)[0];
const view = viewItem.view;
// Remove sash
if (this.viewItems.length >= 1) {
const sashIndex = Math.max(index - 1, 0);
const sashItem = this.sashItems.splice(sashIndex, 1)[0];
sashItem.sash.dispose();
}
this.relayout();
if (sizing && sizing.type === "distribute") {
this.distributeViewSizes();
}
return view;
}
/**
* Move a {@link View view} to a different index.
*
* @param from The source index.
* @param to The target index.
*/
public moveView(container: HTMLElement, from: number, to: number): void {
const cachedVisibleSize = this.getViewCachedVisibleSize(from);
const sizing =
typeof cachedVisibleSize === "undefined"
? this.getViewSize(from)
: Sizing.Invisible(cachedVisibleSize);
const view = this.removeView(from);
this.addView(container, view, sizing, to);
}
/**
* Returns the {@link View view}'s size previously to being hidden.
*
* @param index The {@link View view} index.
*/
private getViewCachedVisibleSize(index: number): number | undefined {
if (index < 0 || index >= this.viewItems.length) {
throw new Error("Index out of bounds");
}
const viewItem = this.viewItems[index];
return viewItem.cachedVisibleSize;
}
/**
* Layout the {@link SplitView}.
*
* @param size The entire size of the {@link SplitView}.
*/
public layout(size: number = this.size): void {
const previousSize = Math.max(this.size, this.contentSize);
this.size = size;
if (!this.proportions) {
const indexes = range(0, this.viewItems.length);
const lowPriorityIndexes = indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.Low
);
const highPriorityIndexes = indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.High
);
this.resize(
this.viewItems.length - 1,
size - previousSize,
undefined,
lowPriorityIndexes,
highPriorityIndexes
);
} else {
for (let i = 0; i < this.viewItems.length; i++) {
const item = this.viewItems[i];
item.size = clamp(
Math.round(this.proportions[i] * size),
item.minimumSize,
item.maximumSize
);
}
}
this.distributeEmptySpace();
this.layoutViews();
}
/**
* Resize a {@link View view} within the {@link SplitView}.
*
* @param index The {@link View view} index.
* @param size The {@link View view} size.
*/
public resizeView(index: number, size: number): void {
if (index < 0 || index >= this.viewItems.length) {
return;
}
const indexes = range(0, this.viewItems.length).filter((i) => i !== index);
const lowPriorityIndexes = [
...indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.Low
),
index,
];
const highPriorityIndexes = indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.High
);
const item = this.viewItems[index];
size = Math.round(size);
size = clamp(size, item.minimumSize, Math.min(item.maximumSize, this.size));
item.size = size;
this.relayout(lowPriorityIndexes, highPriorityIndexes);
}
public resizeViews(sizes: number[]): void {
for (let index = 0; index < sizes.length; index++) {
const item = this.viewItems[index];
let size = sizes[index];
size = Math.round(size);
size = clamp(
size,
item.minimumSize,
Math.min(item.maximumSize, this.size)
);
item.size = size;
}
this.contentSize = this.viewItems.reduce((r, i) => r + i.size, 0);
this.saveProportions();
this.layout(this.size);
}
/**
* Returns the size of a {@link View view}.
*/
public getViewSize(index: number): number {
if (index < 0 || index >= this.viewItems.length) {
return -1;
}
return this.viewItems[index].size;
}
/**
* Returns whether the {@link View view} is visible.
*
* @param index The {@link View view} index.
*/
isViewVisible(index: number): boolean {
if (index < 0 || index >= this.viewItems.length) {
throw new Error("Index out of bounds");
}
const viewItem = this.viewItems[index];
return viewItem.visible;
}
/**
* Set a {@link View view}'s visibility.
*
* @param index The {@link View view} index.
* @param visible Whether the {@link View view} should be visible.
*/
setViewVisible(index: number, visible: boolean): void {
if (index < 0 || index >= this.viewItems.length) {
throw new Error("Index out of bounds");
}
const viewItem = this.viewItems[index];
viewItem.setVisible(visible);
this.distributeEmptySpace(index);
this.layoutViews();
this.saveProportions();
}
/**
* Distribute the entire {@link SplitView} size among all {@link View views}.
*/
public distributeViewSizes(): void {
const flexibleViewItems: ViewItem[] = [];
let flexibleSize = 0;
for (const item of this.viewItems) {
if (item.maximumSize - item.minimumSize > 0) {
flexibleViewItems.push(item);
flexibleSize += item.size;
}
}
const size = Math.floor(flexibleSize / flexibleViewItems.length);
for (const item of flexibleViewItems) {
item.size = clamp(size, item.minimumSize, item.maximumSize);
}
const indexes = range(0, this.viewItems.length);
const lowPriorityIndexes = indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.Low
);
const highPriorityIndexes = indexes.filter(
(i) => this.viewItems[i].priority === LayoutPriority.High
);
this.relayout(lowPriorityIndexes, highPriorityIndexes);
}
public dispose(): void {
this.sashItems.forEach((sashItem) => sashItem.sash.dispose());
this.sashItems = [];
this.sashContainer.remove();
}
private relayout(
lowPriorityIndexes?: number[],
highPriorityIndexes?: number[]
): void {
const contentSize = this.viewItems.reduce((r, i) => r + i.size, 0);
this.resize(
this.viewItems.length - 1,
this.size - contentSize,
undefined,
lowPriorityIndexes,
highPriorityIndexes
);
this.distributeEmptySpace();
this.layoutViews();
this.saveProportions();
}
private onSashStart({ sash, start }: SashEvent): void {
const index = this.sashItems.findIndex((item) => item.sash === sash);
const resetSashDragState = (start: number) => {
const sizes = this.viewItems.map((i) => i.size);
let minDelta = Number.NEGATIVE_INFINITY;
let maxDelta = Number.POSITIVE_INFINITY;
let snapBefore: SashDragSnapState | undefined;
let snapAfter: SashDragSnapState | undefined;
const upIndexes = range(index, -1, -1);
const downIndexes = range(index + 1, this.viewItems.length);
const minDeltaUp = upIndexes.reduce(
(r, i) => r + (this.viewItems[i].minimumSize - sizes[i]),
0
);
const maxDeltaUp = upIndexes.reduce(
(r, i) => r + (this.viewItems[i].viewMaximumSize - sizes[i]),
0
);
const maxDeltaDown =
downIndexes.length === 0
? Number.POSITIVE_INFINITY
: downIndexes.reduce(
(r, i) => r + (sizes[i] - this.viewItems[i].minimumSize),
0
);
const minDeltaDown =
downIndexes.length === 0
? Number.NEGATIVE_INFINITY
: downIndexes.reduce(
(r, i) => r + (sizes[i] - this.viewItems[i].viewMaximumSize),
0
);
minDelta = Math.max(minDeltaUp, minDeltaDown);
maxDelta = Math.min(maxDeltaDown, maxDeltaUp);
const snapBeforeIndex = this.findFirstSnapIndex(upIndexes);
const snapAfterIndex = this.findFirstSnapIndex(downIndexes);
if (typeof snapBeforeIndex === "number") {
const viewItem = this.viewItems[snapBeforeIndex];
const halfSize = Math.floor(viewItem.viewMinimumSize / 2);
snapBefore = {
index: snapBeforeIndex,
limitDelta: viewItem.visible
? minDelta - halfSize
: minDelta + halfSize,
size: viewItem.size,
};
}
if (typeof snapAfterIndex === "number") {
const viewItem = this.viewItems[snapAfterIndex];
const halfSize = Math.floor(viewItem.viewMinimumSize / 2);
snapAfter = {
index: snapAfterIndex,
limitDelta: viewItem.visible
? maxDelta + halfSize
: maxDelta - halfSize,
size: viewItem.size,
};
}
this.sashDragState = {
start,
current: start,
index,
sizes,
minDelta,
maxDelta,
snapBefore,
snapAfter,
};
};
resetSashDragState(start);
}
private onSashChange({ current }: SashEvent): void {
const { index, start, sizes, minDelta, maxDelta, snapBefore, snapAfter } =
this.sashDragState!;
this.sashDragState!.current = current;
const delta = current - start;
// TODO: Should this be conditional on alt?
this.resize(
index,
delta,
sizes,
undefined,
undefined,
minDelta,
maxDelta,
snapBefore,
snapAfter
);
this.distributeEmptySpace();
this.layoutViews();
}
private onSashEnd = (index: number): void => {
this.emit("sashchange", index);
this.saveProportions();
for (const item of this.viewItems) {
item.enabled = true;
}
};
private getSashPosition(sash: Sash): number {
let position = 0;
for (let i = 0; i < this.sashItems.length; i++) {
position += this.viewItems[i].size;
if (this.sashItems[i].sash === sash) {
return position;
}
}
return 0;
}
private resize(
index: number,
delta: number,
sizes = this.viewItems.map((i) => i.size),
lowPriorityIndexes?: number[],
highPriorityIndexes?: number[],
overloadMinDelta: number = Number.NEGATIVE_INFINITY,
overloadMaxDelta: number = Number.POSITIVE_INFINITY,
snapBefore?: SashDragSnapState,
snapAfter?: SashDragSnapState
): number {
if (index < 0 || index >= this.viewItems.length) {
return 0;
}
const upIndexes = range(index, -1, -1);
const downIndexes = range(index + 1, this.viewItems.length);
if (highPriorityIndexes) {
for (const index of highPriorityIndexes) {
pushToStart(upIndexes, index);
pushToStart(downIndexes, index);
}
}
if (lowPriorityIndexes) {
for (const index of lowPriorityIndexes) {
pushToEnd(upIndexes, index);
pushToEnd(downIndexes, index);
}
}
const upItems = upIndexes.map((i) => this.viewItems[i]);
const upSizes = upIndexes.map((i) => sizes[i]);
const downItems = downIndexes.map((i) => this.viewItems[i]);
const downSizes = downIndexes.map((i) => sizes[i]);
const minDeltaUp = upIndexes.reduce(
(r, i) => r + (this.viewItems[i].minimumSize - sizes[i]),
0
);
const maxDeltaUp = upIndexes.reduce(
(r, i) => r + (this.viewItems[i].maximumSize - sizes[i]),
0
);
const maxDeltaDown =
downIndexes.length === 0
? Number.POSITIVE_INFINITY
: downIndexes.reduce(
(r, i) => r + (sizes[i] - this.viewItems[i].minimumSize),
0
);
const minDeltaDown =
downIndexes.length === 0
? Number.NEGATIVE_INFINITY
: downIndexes.reduce(
(r, i) => r + (sizes[i] - this.viewItems[i].maximumSize),
0