xcode-graph 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/flow.js DELETED
@@ -1,883 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2021 Google LLC
4
- * SPDX-License-Identifier: BSD-3-Clause
5
- */
6
- class SizeCache {
7
- constructor(config) {
8
- this._map = new Map();
9
- this._roundAverageSize = false;
10
- this.totalSize = 0;
11
- if (config?.roundAverageSize === true) {
12
- this._roundAverageSize = true;
13
- }
14
- }
15
- set(index, value) {
16
- const prev = this._map.get(index) || 0;
17
- this._map.set(index, value);
18
- this.totalSize += value - prev;
19
- }
20
- get averageSize() {
21
- if (this._map.size > 0) {
22
- const average = this.totalSize / this._map.size;
23
- return this._roundAverageSize ? Math.round(average) : average;
24
- }
25
- return 0;
26
- }
27
- getSize(index) {
28
- return this._map.get(index);
29
- }
30
- clear() {
31
- this._map.clear();
32
- this.totalSize = 0;
33
- }
34
- }
35
-
36
- /**
37
- * @license
38
- * Copyright 2021 Google LLC
39
- * SPDX-License-Identifier: BSD-3-Clause
40
- */
41
- function dim1(direction) {
42
- return direction === 'horizontal' ? 'width' : 'height';
43
- }
44
- class BaseLayout {
45
- _getDefaultConfig() {
46
- return {
47
- direction: 'vertical',
48
- };
49
- }
50
- constructor(hostSink, config) {
51
- /**
52
- * The last set viewport scroll position.
53
- */
54
- this._latestCoords = { left: 0, top: 0 };
55
- /**
56
- * Scrolling direction.
57
- */
58
- this._direction = null;
59
- /**
60
- * Dimensions of the viewport.
61
- */
62
- this._viewportSize = { width: 0, height: 0 };
63
- this.totalScrollSize = { width: 0, height: 0 };
64
- this.offsetWithinScroller = { left: 0, top: 0 };
65
- /**
66
- * Flag for debouncing asynchronous reflow requests.
67
- */
68
- this._pendingReflow = false;
69
- this._pendingLayoutUpdate = false;
70
- this._pin = null;
71
- /**
72
- * The index of the first item intersecting the viewport.
73
- */
74
- this._firstVisible = 0;
75
- /**
76
- * The index of the last item intersecting the viewport.
77
- */
78
- this._lastVisible = 0;
79
- /**
80
- * Pixel offset in the scroll direction of the first child.
81
- */
82
- this._physicalMin = 0;
83
- /**
84
- * Pixel offset in the scroll direction of the last child.
85
- */
86
- this._physicalMax = 0;
87
- /**
88
- * Index of the first child.
89
- */
90
- this._first = -1;
91
- /**
92
- * Index of the last child.
93
- */
94
- this._last = -1;
95
- /**
96
- * Length in the scrolling direction.
97
- */
98
- this._sizeDim = 'height';
99
- /**
100
- * Length in the non-scrolling direction.
101
- */
102
- this._secondarySizeDim = 'width';
103
- /**
104
- * Position in the scrolling direction.
105
- */
106
- this._positionDim = 'top';
107
- /**
108
- * Position in the non-scrolling direction.
109
- */
110
- this._secondaryPositionDim = 'left';
111
- /**
112
- * Current scroll offset in pixels.
113
- */
114
- this._scrollPosition = 0;
115
- /**
116
- * Difference between current scroll offset and scroll offset calculated due
117
- * to a reflow.
118
- */
119
- this._scrollError = 0;
120
- /**
121
- * Total number of items that could possibly be displayed. Used to help
122
- * calculate the scroll size.
123
- */
124
- this._items = [];
125
- /**
126
- * The total (estimated) length of all items in the scrolling direction.
127
- */
128
- this._scrollSize = 1;
129
- /**
130
- * Number of pixels beyond the viewport to still include
131
- * in the active range of items.
132
- */
133
- // TODO (graynorton): Probably want to make this something we calculate based
134
- // on viewport size, item size, other factors, possibly still with a dial of some kind
135
- this._overhang = 1000;
136
- this._hostSink = hostSink;
137
- // Delay setting config so that subclasses do setup work first
138
- Promise.resolve().then(() => (this.config = config || this._getDefaultConfig()));
139
- }
140
- set config(config) {
141
- Object.assign(this, Object.assign({}, this._getDefaultConfig(), config));
142
- }
143
- get config() {
144
- return {
145
- direction: this.direction,
146
- };
147
- }
148
- /**
149
- * Maximum index of children + 1, to help estimate total height of the scroll
150
- * space.
151
- */
152
- get items() {
153
- return this._items;
154
- }
155
- set items(items) {
156
- this._setItems(items);
157
- }
158
- _setItems(items) {
159
- if (items !== this._items) {
160
- this._items = items;
161
- this._scheduleReflow();
162
- }
163
- }
164
- /**
165
- * Primary scrolling direction.
166
- */
167
- get direction() {
168
- return this._direction;
169
- }
170
- set direction(dir) {
171
- // Force it to be either horizontal or vertical.
172
- dir = dir === 'horizontal' ? dir : 'vertical';
173
- if (dir !== this._direction) {
174
- this._direction = dir;
175
- this._sizeDim = dir === 'horizontal' ? 'width' : 'height';
176
- this._secondarySizeDim = dir === 'horizontal' ? 'height' : 'width';
177
- this._positionDim = dir === 'horizontal' ? 'left' : 'top';
178
- this._secondaryPositionDim = dir === 'horizontal' ? 'top' : 'left';
179
- this._triggerReflow();
180
- }
181
- }
182
- /**
183
- * Height and width of the viewport.
184
- */
185
- get viewportSize() {
186
- return this._viewportSize;
187
- }
188
- set viewportSize(dims) {
189
- const { _viewDim1, _viewDim2 } = this;
190
- Object.assign(this._viewportSize, dims);
191
- if (_viewDim2 !== this._viewDim2) {
192
- // this._viewDim2Changed();
193
- this._scheduleLayoutUpdate();
194
- }
195
- else if (_viewDim1 !== this._viewDim1) {
196
- this._checkThresholds();
197
- }
198
- }
199
- /**
200
- * Scroll offset of the viewport.
201
- */
202
- get viewportScroll() {
203
- return this._latestCoords;
204
- }
205
- set viewportScroll(coords) {
206
- Object.assign(this._latestCoords, coords);
207
- const oldPos = this._scrollPosition;
208
- this._scrollPosition = this._latestCoords[this._positionDim];
209
- const change = Math.abs(oldPos - this._scrollPosition);
210
- if (change >= 1) {
211
- this._checkThresholds();
212
- }
213
- }
214
- /**
215
- * Perform a reflow if one has been scheduled.
216
- */
217
- reflowIfNeeded(force = false) {
218
- if (force || this._pendingReflow) {
219
- this._pendingReflow = false;
220
- this._reflow();
221
- }
222
- }
223
- set pin(options) {
224
- this._pin = options;
225
- this._triggerReflow();
226
- }
227
- get pin() {
228
- if (this._pin !== null) {
229
- const { index, block } = this._pin;
230
- return {
231
- index: Math.max(0, Math.min(index, this.items.length - 1)),
232
- block,
233
- };
234
- }
235
- return null;
236
- }
237
- _clampScrollPosition(val) {
238
- return Math.max(-this.offsetWithinScroller[this._positionDim], Math.min(val, this.totalScrollSize[dim1(this.direction)] - this._viewDim1));
239
- }
240
- unpin() {
241
- if (this._pin !== null) {
242
- this._sendUnpinnedMessage();
243
- this._pin = null;
244
- }
245
- }
246
- _updateLayout() {
247
- // Override
248
- }
249
- // protected _viewDim2Changed(): void {
250
- // this._scheduleLayoutUpdate();
251
- // }
252
- /**
253
- * The height or width of the viewport, whichever corresponds to the scrolling direction.
254
- */
255
- get _viewDim1() {
256
- return this._viewportSize[this._sizeDim];
257
- }
258
- /**
259
- * The height or width of the viewport, whichever does NOT correspond to the scrolling direction.
260
- */
261
- get _viewDim2() {
262
- return this._viewportSize[this._secondarySizeDim];
263
- }
264
- _scheduleReflow() {
265
- this._pendingReflow = true;
266
- }
267
- _scheduleLayoutUpdate() {
268
- this._pendingLayoutUpdate = true;
269
- this._scheduleReflow();
270
- }
271
- // For triggering a reflow based on incoming changes to
272
- // the layout config.
273
- _triggerReflow() {
274
- this._scheduleLayoutUpdate();
275
- // TODO graynorton@: reflowIfNeeded() isn't really supposed
276
- // to be called internally. Address in larger cleanup
277
- // of virtualizer / layout interaction pattern.
278
- // this.reflowIfNeeded(true);
279
- Promise.resolve().then(() => this.reflowIfNeeded());
280
- }
281
- _reflow() {
282
- if (this._pendingLayoutUpdate) {
283
- this._updateLayout();
284
- this._pendingLayoutUpdate = false;
285
- }
286
- this._updateScrollSize();
287
- this._setPositionFromPin();
288
- this._getActiveItems();
289
- this._updateVisibleIndices();
290
- this._sendStateChangedMessage();
291
- }
292
- /**
293
- * If we are supposed to be pinned to a particular
294
- * item or set of coordinates, we set `_scrollPosition`
295
- * accordingly and adjust `_scrollError` as needed
296
- * so that the virtualizer can keep the scroll
297
- * position in the DOM in sync
298
- */
299
- _setPositionFromPin() {
300
- if (this.pin !== null) {
301
- const lastScrollPosition = this._scrollPosition;
302
- const { index, block } = this.pin;
303
- this._scrollPosition =
304
- this._calculateScrollIntoViewPosition({
305
- index,
306
- block: block || 'start',
307
- }) - this.offsetWithinScroller[this._positionDim];
308
- this._scrollError = lastScrollPosition - this._scrollPosition;
309
- }
310
- }
311
- /**
312
- * Calculate the coordinates to scroll to, given
313
- * a request to scroll to the element at a specific
314
- * index.
315
- *
316
- * Supports the same positioning options (`start`,
317
- * `center`, `end`, `nearest`) as the standard
318
- * `Element.scrollIntoView()` method, but currently
319
- * only considers the provided value in the `block`
320
- * dimension, since we don't yet have any layouts
321
- * that support virtualization in two dimensions.
322
- */
323
- _calculateScrollIntoViewPosition(options) {
324
- const { block } = options;
325
- const index = Math.min(this.items.length, Math.max(0, options.index));
326
- const itemStartPosition = this._getItemPosition(index)[this._positionDim];
327
- let scrollPosition = itemStartPosition;
328
- if (block !== 'start') {
329
- const itemSize = this._getItemSize(index)[this._sizeDim];
330
- if (block === 'center') {
331
- scrollPosition =
332
- itemStartPosition - 0.5 * this._viewDim1 + 0.5 * itemSize;
333
- }
334
- else {
335
- const itemEndPosition = itemStartPosition - this._viewDim1 + itemSize;
336
- if (block === 'end') {
337
- scrollPosition = itemEndPosition;
338
- }
339
- else {
340
- // block === 'nearest'
341
- const currentScrollPosition = this._scrollPosition;
342
- scrollPosition =
343
- Math.abs(currentScrollPosition - itemStartPosition) <
344
- Math.abs(currentScrollPosition - itemEndPosition)
345
- ? itemStartPosition
346
- : itemEndPosition;
347
- }
348
- }
349
- }
350
- scrollPosition += this.offsetWithinScroller[this._positionDim];
351
- return this._clampScrollPosition(scrollPosition);
352
- }
353
- getScrollIntoViewCoordinates(options) {
354
- return {
355
- [this._positionDim]: this._calculateScrollIntoViewPosition(options),
356
- };
357
- }
358
- _sendUnpinnedMessage() {
359
- this._hostSink({
360
- type: 'unpinned',
361
- });
362
- }
363
- _sendVisibilityChangedMessage() {
364
- this._hostSink({
365
- type: 'visibilityChanged',
366
- firstVisible: this._firstVisible,
367
- lastVisible: this._lastVisible,
368
- });
369
- }
370
- _sendStateChangedMessage() {
371
- const childPositions = new Map();
372
- if (this._first !== -1 && this._last !== -1) {
373
- for (let idx = this._first; idx <= this._last; idx++) {
374
- childPositions.set(idx, this._getItemPosition(idx));
375
- }
376
- }
377
- const message = {
378
- type: 'stateChanged',
379
- scrollSize: {
380
- [this._sizeDim]: this._scrollSize,
381
- [this._secondarySizeDim]: null,
382
- },
383
- range: {
384
- first: this._first,
385
- last: this._last,
386
- firstVisible: this._firstVisible,
387
- lastVisible: this._lastVisible,
388
- },
389
- childPositions,
390
- };
391
- if (this._scrollError) {
392
- message.scrollError = {
393
- [this._positionDim]: this._scrollError,
394
- [this._secondaryPositionDim]: 0,
395
- };
396
- this._scrollError = 0;
397
- }
398
- this._hostSink(message);
399
- }
400
- /**
401
- * Number of items to display.
402
- */
403
- get _num() {
404
- if (this._first === -1 || this._last === -1) {
405
- return 0;
406
- }
407
- return this._last - this._first + 1;
408
- }
409
- _checkThresholds() {
410
- if ((this._viewDim1 === 0 && this._num > 0) || this._pin !== null) {
411
- this._scheduleReflow();
412
- }
413
- else {
414
- const min = Math.max(0, this._scrollPosition - this._overhang);
415
- const max = Math.min(this._scrollSize, this._scrollPosition + this._viewDim1 + this._overhang);
416
- if (this._physicalMin > min || this._physicalMax < max) {
417
- this._scheduleReflow();
418
- }
419
- else {
420
- this._updateVisibleIndices({ emit: true });
421
- }
422
- }
423
- }
424
- /**
425
- * Find the indices of the first and last items to intersect the viewport.
426
- * Emit a visibleindiceschange event when either index changes.
427
- */
428
- _updateVisibleIndices(options) {
429
- if (this._first === -1 || this._last === -1)
430
- return;
431
- let firstVisible = this._first;
432
- while (firstVisible < this._last &&
433
- Math.round(this._getItemPosition(firstVisible)[this._positionDim] +
434
- this._getItemSize(firstVisible)[this._sizeDim]) <= Math.round(this._scrollPosition)) {
435
- firstVisible++;
436
- }
437
- let lastVisible = this._last;
438
- while (lastVisible > this._first &&
439
- Math.round(this._getItemPosition(lastVisible)[this._positionDim]) >=
440
- Math.round(this._scrollPosition + this._viewDim1)) {
441
- lastVisible--;
442
- }
443
- if (firstVisible !== this._firstVisible ||
444
- lastVisible !== this._lastVisible) {
445
- this._firstVisible = firstVisible;
446
- this._lastVisible = lastVisible;
447
- if (options && options.emit) {
448
- this._sendVisibilityChangedMessage();
449
- }
450
- }
451
- }
452
- }
453
-
454
- /**
455
- * @license
456
- * Copyright 2021 Google LLC
457
- * SPDX-License-Identifier: BSD-3-Clause
458
- */
459
- function leadingMargin(direction) {
460
- return direction === 'horizontal' ? 'marginLeft' : 'marginTop';
461
- }
462
- function trailingMargin(direction) {
463
- return direction === 'horizontal' ? 'marginRight' : 'marginBottom';
464
- }
465
- function offset(direction) {
466
- return direction === 'horizontal' ? 'xOffset' : 'yOffset';
467
- }
468
- function collapseMargins(a, b) {
469
- const m = [a, b].sort();
470
- return m[1] <= 0 ? Math.min(...m) : m[0] >= 0 ? Math.max(...m) : m[0] + m[1];
471
- }
472
- class MetricsCache {
473
- constructor() {
474
- this._childSizeCache = new SizeCache();
475
- this._marginSizeCache = new SizeCache();
476
- this._metricsCache = new Map();
477
- }
478
- update(metrics, direction) {
479
- const marginsToUpdate = new Set();
480
- Object.keys(metrics).forEach((key) => {
481
- const k = Number(key);
482
- this._metricsCache.set(k, metrics[k]);
483
- this._childSizeCache.set(k, metrics[k][dim1(direction)]);
484
- marginsToUpdate.add(k);
485
- marginsToUpdate.add(k + 1);
486
- });
487
- for (const k of marginsToUpdate) {
488
- const a = this._metricsCache.get(k)?.[leadingMargin(direction)] || 0;
489
- const b = this._metricsCache.get(k - 1)?.[trailingMargin(direction)] || 0;
490
- this._marginSizeCache.set(k, collapseMargins(a, b));
491
- }
492
- }
493
- get averageChildSize() {
494
- return this._childSizeCache.averageSize;
495
- }
496
- get totalChildSize() {
497
- return this._childSizeCache.totalSize;
498
- }
499
- get averageMarginSize() {
500
- return this._marginSizeCache.averageSize;
501
- }
502
- get totalMarginSize() {
503
- return this._marginSizeCache.totalSize;
504
- }
505
- getLeadingMarginValue(index, direction) {
506
- return this._metricsCache.get(index)?.[leadingMargin(direction)] || 0;
507
- }
508
- getChildSize(index) {
509
- return this._childSizeCache.getSize(index);
510
- }
511
- getMarginSize(index) {
512
- return this._marginSizeCache.getSize(index);
513
- }
514
- clear() {
515
- this._childSizeCache.clear();
516
- this._marginSizeCache.clear();
517
- this._metricsCache.clear();
518
- }
519
- }
520
- class FlowLayout extends BaseLayout {
521
- constructor() {
522
- super(...arguments);
523
- /**
524
- * Initial estimate of item size
525
- */
526
- this._itemSize = { width: 100, height: 100 };
527
- /**
528
- * Indices of children mapped to their (position and length) in the scrolling
529
- * direction. Used to keep track of children that are in range.
530
- */
531
- this._physicalItems = new Map();
532
- /**
533
- * Used in tandem with _physicalItems to track children in range across
534
- * reflows.
535
- */
536
- this._newPhysicalItems = new Map();
537
- /**
538
- * Width and height of children by their index.
539
- */
540
- this._metricsCache = new MetricsCache();
541
- /**
542
- * anchorIdx is the anchor around which we reflow. It is designed to allow
543
- * jumping to any point of the scroll size. We choose it once and stick with
544
- * it until stable. _first and _last are deduced around it.
545
- */
546
- this._anchorIdx = null;
547
- /**
548
- * Position in the scrolling direction of the anchor child.
549
- */
550
- this._anchorPos = null;
551
- /**
552
- * Whether all children in range were in range during the previous reflow.
553
- */
554
- this._stable = true;
555
- this._measureChildren = true;
556
- this._estimate = true;
557
- }
558
- // protected _defaultConfig: BaseLayoutConfig = Object.assign({}, super._defaultConfig, {
559
- // })
560
- // constructor(config: Layout1dConfig) {
561
- // super(config);
562
- // }
563
- get measureChildren() {
564
- return this._measureChildren;
565
- }
566
- /**
567
- * Determine the average size of all children represented in the sizes
568
- * argument.
569
- */
570
- updateItemSizes(sizes) {
571
- this._metricsCache.update(sizes, this.direction);
572
- // if (this._nMeasured) {
573
- // this._updateItemSize();
574
- this._scheduleReflow();
575
- // }
576
- }
577
- /**
578
- * Set the average item size based on the total length and number of children
579
- * in range.
580
- */
581
- // _updateItemSize() {
582
- // // Keep integer values.
583
- // this._itemSize[this._sizeDim] = this._metricsCache.averageChildSize;
584
- // }
585
- _getPhysicalItem(idx) {
586
- return this._newPhysicalItems.get(idx) ?? this._physicalItems.get(idx);
587
- }
588
- _getSize(idx) {
589
- const item = this._getPhysicalItem(idx);
590
- return item && this._metricsCache.getChildSize(idx);
591
- }
592
- _getAverageSize() {
593
- return this._metricsCache.averageChildSize || this._itemSize[this._sizeDim];
594
- }
595
- _estimatePosition(idx) {
596
- const c = this._metricsCache;
597
- if (this._first === -1 || this._last === -1) {
598
- return (c.averageMarginSize +
599
- idx * (c.averageMarginSize + this._getAverageSize()));
600
- }
601
- else {
602
- if (idx < this._first) {
603
- const delta = this._first - idx;
604
- const refItem = this._getPhysicalItem(this._first);
605
- return (refItem.pos -
606
- (c.getMarginSize(this._first - 1) || c.averageMarginSize) -
607
- (delta * c.averageChildSize + (delta - 1) * c.averageMarginSize));
608
- }
609
- else {
610
- const delta = idx - this._last;
611
- const refItem = this._getPhysicalItem(this._last);
612
- return (refItem.pos +
613
- (c.getChildSize(this._last) || c.averageChildSize) +
614
- (c.getMarginSize(this._last) || c.averageMarginSize) +
615
- delta * (c.averageChildSize + c.averageMarginSize));
616
- }
617
- }
618
- }
619
- /**
620
- * Returns the position in the scrolling direction of the item at idx.
621
- * Estimates it if the item at idx is not in the DOM.
622
- */
623
- _getPosition(idx) {
624
- const item = this._getPhysicalItem(idx);
625
- const { averageMarginSize } = this._metricsCache;
626
- return idx === 0
627
- ? this._metricsCache.getMarginSize(0) ?? averageMarginSize
628
- : item
629
- ? item.pos
630
- : this._estimatePosition(idx);
631
- }
632
- _calculateAnchor(lower, upper) {
633
- if (lower <= 0) {
634
- return 0;
635
- }
636
- if (upper > this._scrollSize - this._viewDim1) {
637
- return this.items.length - 1;
638
- }
639
- return Math.max(0, Math.min(this.items.length - 1, Math.floor((lower + upper) / 2 / this._delta)));
640
- }
641
- _getAnchor(lower, upper) {
642
- if (this._physicalItems.size === 0) {
643
- return this._calculateAnchor(lower, upper);
644
- }
645
- if (this._first < 0) {
646
- return this._calculateAnchor(lower, upper);
647
- }
648
- if (this._last < 0) {
649
- return this._calculateAnchor(lower, upper);
650
- }
651
- const firstItem = this._getPhysicalItem(this._first), lastItem = this._getPhysicalItem(this._last), firstMin = firstItem.pos, lastMin = lastItem.pos, lastMax = lastMin + this._metricsCache.getChildSize(this._last);
652
- if (lastMax < lower) {
653
- // Window is entirely past physical items, calculate new anchor
654
- return this._calculateAnchor(lower, upper);
655
- }
656
- if (firstMin > upper) {
657
- // Window is entirely before physical items, calculate new anchor
658
- return this._calculateAnchor(lower, upper);
659
- }
660
- // Window contains a physical item
661
- // Find one, starting with the one that was previously first visible
662
- let candidateIdx = this._firstVisible - 1;
663
- let cMax = -Infinity;
664
- while (cMax < lower) {
665
- const candidate = this._getPhysicalItem(++candidateIdx);
666
- cMax = candidate.pos + this._metricsCache.getChildSize(candidateIdx);
667
- }
668
- return candidateIdx;
669
- }
670
- /**
671
- * Updates _first and _last based on items that should be in the current
672
- * viewed range.
673
- */
674
- _getActiveItems() {
675
- if (this._viewDim1 === 0 || this.items.length === 0) {
676
- this._clearItems();
677
- }
678
- else {
679
- this._getItems();
680
- }
681
- }
682
- /**
683
- * Sets the range to empty.
684
- */
685
- _clearItems() {
686
- this._first = -1;
687
- this._last = -1;
688
- this._physicalMin = 0;
689
- this._physicalMax = 0;
690
- const items = this._newPhysicalItems;
691
- this._newPhysicalItems = this._physicalItems;
692
- this._newPhysicalItems.clear();
693
- this._physicalItems = items;
694
- this._stable = true;
695
- }
696
- /*
697
- * Updates _first and _last based on items that should be in the given range.
698
- */
699
- _getItems() {
700
- const items = this._newPhysicalItems;
701
- this._stable = true;
702
- let lower, upper;
703
- // The anchorIdx is the anchor around which we reflow. It is designed to
704
- // allow jumping to any point of the scroll size. We choose it once and
705
- // stick with it until stable. first and last are deduced around it.
706
- // If we have a pinned item, we anchor on it
707
- if (this.pin !== null) {
708
- const { index } = this.pin;
709
- this._anchorIdx = index;
710
- this._anchorPos = this._getPosition(index);
711
- }
712
- // Determine the lower and upper bounds of the region to be
713
- // rendered, relative to the viewport
714
- lower = this._scrollPosition - this._overhang; //leadingOverhang;
715
- upper = this._scrollPosition + this._viewDim1 + this._overhang; // trailingOverhang;
716
- if (upper < 0 || lower > this._scrollSize) {
717
- this._clearItems();
718
- return;
719
- }
720
- // If we are scrolling to a specific index or if we are doing another
721
- // pass to stabilize a previously started reflow, we will already
722
- // have an anchor. If not, establish an anchor now.
723
- if (this._anchorIdx === null || this._anchorPos === null) {
724
- this._anchorIdx = this._getAnchor(lower, upper);
725
- this._anchorPos = this._getPosition(this._anchorIdx);
726
- }
727
- let anchorSize = this._getSize(this._anchorIdx);
728
- if (anchorSize === undefined) {
729
- this._stable = false;
730
- anchorSize = this._getAverageSize();
731
- }
732
- const anchorLeadingMargin = this._metricsCache.getMarginSize(this._anchorIdx) ??
733
- this._metricsCache.averageMarginSize;
734
- const anchorTrailingMargin = this._metricsCache.getMarginSize(this._anchorIdx + 1) ??
735
- this._metricsCache.averageMarginSize;
736
- if (this._anchorIdx === 0) {
737
- this._anchorPos = anchorLeadingMargin;
738
- }
739
- if (this._anchorIdx === this.items.length - 1) {
740
- this._anchorPos = this._scrollSize - anchorTrailingMargin - anchorSize;
741
- }
742
- // Anchor might be outside bounds, so prefer correcting the error and keep
743
- // that anchorIdx.
744
- let anchorErr = 0;
745
- if (this._anchorPos + anchorSize + anchorTrailingMargin < lower) {
746
- anchorErr = lower - (this._anchorPos + anchorSize + anchorTrailingMargin);
747
- }
748
- if (this._anchorPos - anchorLeadingMargin > upper) {
749
- anchorErr = upper - (this._anchorPos - anchorLeadingMargin);
750
- }
751
- if (anchorErr) {
752
- this._scrollPosition -= anchorErr;
753
- lower -= anchorErr;
754
- upper -= anchorErr;
755
- this._scrollError += anchorErr;
756
- }
757
- items.set(this._anchorIdx, { pos: this._anchorPos, size: anchorSize });
758
- this._first = this._last = this._anchorIdx;
759
- this._physicalMin = this._anchorPos - anchorLeadingMargin;
760
- this._physicalMax = this._anchorPos + anchorSize + anchorTrailingMargin;
761
- while (this._physicalMin > lower && this._first > 0) {
762
- let size = this._getSize(--this._first);
763
- if (size === undefined) {
764
- this._stable = false;
765
- size = this._getAverageSize();
766
- }
767
- let margin = this._metricsCache.getMarginSize(this._first);
768
- if (margin === undefined) {
769
- this._stable = false;
770
- margin = this._metricsCache.averageMarginSize;
771
- }
772
- this._physicalMin -= size;
773
- const pos = this._physicalMin;
774
- items.set(this._first, { pos, size });
775
- this._physicalMin -= margin;
776
- if (this._stable === false && this._estimate === false) {
777
- break;
778
- }
779
- }
780
- while (this._physicalMax < upper && this._last < this.items.length - 1) {
781
- let size = this._getSize(++this._last);
782
- if (size === undefined) {
783
- this._stable = false;
784
- size = this._getAverageSize();
785
- }
786
- let margin = this._metricsCache.getMarginSize(this._last);
787
- if (margin === undefined) {
788
- this._stable = false;
789
- margin = this._metricsCache.averageMarginSize;
790
- }
791
- const pos = this._physicalMax;
792
- items.set(this._last, { pos, size });
793
- this._physicalMax += size + margin;
794
- if (!this._stable && !this._estimate) {
795
- break;
796
- }
797
- }
798
- // This handles the cases where we were relying on estimated sizes.
799
- const extentErr = this._calculateError();
800
- if (extentErr) {
801
- this._physicalMin -= extentErr;
802
- this._physicalMax -= extentErr;
803
- this._anchorPos -= extentErr;
804
- this._scrollPosition -= extentErr;
805
- items.forEach((item) => (item.pos -= extentErr));
806
- this._scrollError += extentErr;
807
- }
808
- if (this._stable) {
809
- this._newPhysicalItems = this._physicalItems;
810
- this._newPhysicalItems.clear();
811
- this._physicalItems = items;
812
- }
813
- }
814
- _calculateError() {
815
- if (this._first === 0) {
816
- return this._physicalMin;
817
- }
818
- else if (this._physicalMin <= 0) {
819
- return this._physicalMin - this._first * this._delta;
820
- }
821
- else if (this._last === this.items.length - 1) {
822
- return this._physicalMax - this._scrollSize;
823
- }
824
- else if (this._physicalMax >= this._scrollSize) {
825
- return (this._physicalMax -
826
- this._scrollSize +
827
- (this.items.length - 1 - this._last) * this._delta);
828
- }
829
- return 0;
830
- }
831
- _reflow() {
832
- const { _first, _last } = this;
833
- super._reflow();
834
- if ((this._first === -1 && this._last == -1) ||
835
- (this._first === _first && this._last === _last)) {
836
- this._resetReflowState();
837
- }
838
- }
839
- _resetReflowState() {
840
- this._anchorIdx = null;
841
- this._anchorPos = null;
842
- this._stable = true;
843
- }
844
- _updateScrollSize() {
845
- const { averageMarginSize } = this._metricsCache;
846
- this._scrollSize = Math.max(1, this.items.length * (averageMarginSize + this._getAverageSize()) +
847
- averageMarginSize);
848
- }
849
- /**
850
- * Returns the average size (precise or estimated) of an item in the scrolling direction,
851
- * including any surrounding space.
852
- */
853
- get _delta() {
854
- const { averageMarginSize } = this._metricsCache;
855
- return this._getAverageSize() + averageMarginSize;
856
- }
857
- /**
858
- * Returns the top and left positioning of the item at idx.
859
- */
860
- _getItemPosition(idx) {
861
- return {
862
- [this._positionDim]: this._getPosition(idx),
863
- [this._secondaryPositionDim]: 0,
864
- [offset(this.direction)]: -(this._metricsCache.getLeadingMarginValue(idx, this.direction) ??
865
- this._metricsCache.averageMarginSize),
866
- };
867
- }
868
- /**
869
- * Returns the height and width of the item at idx.
870
- */
871
- _getItemSize(idx) {
872
- return {
873
- [this._sizeDim]: this._getSize(idx) || this._getAverageSize(),
874
- [this._secondarySizeDim]: this._itemSize[this._secondarySizeDim],
875
- };
876
- }
877
- _viewDim2Changed() {
878
- this._metricsCache.clear();
879
- this._scheduleReflow();
880
- }
881
- }
882
-
883
- export { FlowLayout };