@m3ui-vue/m3ui-vue 0.8.8 → 0.9.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/README.md +13 -3
- package/dist/{MMenuItem-5xojJYAL.js → MTextField-B0wGv1Mx.js} +374 -364
- package/dist/MTextField-B0wGv1Mx.js.map +1 -0
- package/dist/barcode.js +9 -11
- package/dist/barcode.js.map +1 -1
- package/dist/code-editor.js +2 -2
- package/dist/code-editor.js.map +1 -1
- package/dist/components/MCarousel.vue.d.ts +55 -36
- package/dist/components/MDialog.vue.d.ts +4 -2
- package/dist/components/MEmojiSelector.vue.d.ts +21 -5
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +15 -5
- package/dist/components/MScheduler.vue.d.ts +38 -1
- package/dist/components/MShape.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +2 -2
- package/dist/components/MSlider.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +1 -1
- package/dist/composables/_carouselStrategy.d.ts +101 -0
- package/dist/composables/useLocale.d.ts +19 -0
- package/dist/data/emoji-names/emoji-names-de.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-en.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-es.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-fr.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ja.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ko.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-pt.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-zh.d.ts +2 -0
- package/dist/data/emojis.d.ts +7 -1
- package/dist/emoji-names-de-BphO5XpV.js +1877 -0
- package/dist/emoji-names-de-BphO5XpV.js.map +1 -0
- package/dist/emoji-names-en-9ZvMRvzi.js +1877 -0
- package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -0
- package/dist/emoji-names-es-DvfWU3Pj.js +1877 -0
- package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -0
- package/dist/emoji-names-fr-B-jhUlsP.js +1877 -0
- package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -0
- package/dist/emoji-names-ja-l8cMFJo1.js +1877 -0
- package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -0
- package/dist/emoji-names-ko-YP-o9G0c.js +1877 -0
- package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -0
- package/dist/emoji-names-pt-DGj_Js4M.js +1877 -0
- package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -0
- package/dist/emoji-names-zh-Dzecwtw_.js +1877 -0
- package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/locales/index.js +140 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3885 -2400
- package/dist/m3ui.js.map +1 -1
- package/dist/plugin.d.ts +6 -0
- package/dist/rich-text-editor.js +9 -9
- package/dist/styles.css +1 -1
- package/dist/terminal.js +34 -27
- package/dist/terminal.js.map +1 -1
- package/dist/{useLocale-D3ZWZ8jY.js → useLocale-CmI675Ab.js} +21 -2
- package/dist/useLocale-CmI675Ab.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MCarousel.test.ts +183 -21
- package/src/__tests__/MDialog.test.ts +18 -0
- package/src/__tests__/MEmojiSelector.test.ts +138 -0
- package/src/__tests__/MLightbox.test.ts +313 -5
- package/src/__tests__/MScheduler.test.ts +122 -0
- package/src/__tests__/MTerminal.test.ts +63 -0
- package/src/__tests__/attrsFallthrough.test.ts +65 -0
- package/src/__tests__/plugin.test.ts +28 -1
- package/src/components/MBarcode.vue +4 -2
- package/src/components/MBottomSheet.vue +1 -1
- package/src/components/MCarousel.vue +526 -200
- package/src/components/MCodeEditor.vue +3 -2
- package/src/components/MCommandPalette.vue +1 -1
- package/src/components/MContextMenu.vue +3 -1
- package/src/components/MDatePickerModal.vue +1 -1
- package/src/components/MDateRangePickerModal.vue +1 -1
- package/src/components/MDialog.vue +19 -6
- package/src/components/MEmojiSelector.vue +479 -78
- package/src/components/MFab.vue +3 -1
- package/src/components/MLightbox.vue +750 -268
- package/src/components/MLoadingOverlay.vue +6 -1
- package/src/components/MMenu.vue +3 -1
- package/src/components/MNavigationDrawer.vue +5 -1
- package/src/components/MNavigationRail.vue +3 -0
- package/src/components/MOverlay.vue +1 -0
- package/src/components/MScheduler.vue +282 -115
- package/src/components/MSideSheet.vue +1 -1
- package/src/components/MSplitButton.vue +3 -1
- package/src/components/MSpotlightSearch.vue +1 -1
- package/src/components/MTerminal.vue +22 -10
- package/src/components/MTimePickerModal.vue +1 -1
- package/src/components/MTooltip.vue +3 -0
- package/src/components/MTour.vue +2 -2
- package/src/composables/_carouselStrategy.ts +712 -0
- package/src/composables/useLocale.ts +38 -0
- package/src/data/emoji-names/emoji-names-de.ts +1875 -0
- package/src/data/emoji-names/emoji-names-en.ts +1875 -0
- package/src/data/emoji-names/emoji-names-es.ts +1875 -0
- package/src/data/emoji-names/emoji-names-fr.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ja.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ko.ts +1875 -0
- package/src/data/emoji-names/emoji-names-pt.ts +1875 -0
- package/src/data/emoji-names/emoji-names-zh.ts +1875 -0
- package/src/data/emojis.ts +29 -198
- package/src/index.ts +2 -2
- package/src/locales/de.ts +19 -0
- package/src/locales/es.ts +19 -0
- package/src/locales/fr.ts +19 -0
- package/src/locales/ja.ts +19 -0
- package/src/locales/ko.ts +19 -0
- package/src/locales/pt.ts +19 -0
- package/src/locales/zh.ts +19 -0
- package/src/plugin.ts +12 -1
- package/dist/MMenuItem-5xojJYAL.js.map +0 -1
- package/dist/useLocale-D3ZWZ8jY.js.map +0 -1
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Port of the Compose Material3 carousel engine (androidx compose/material3/carousel:
|
|
3
|
+
* Arrangement.kt, KeylineList.kt, Keylines.kt, Strategy.kt, KeylineSnapPosition.kt), kept close to
|
|
4
|
+
* the original so it can be diffed against upstream. Pure functions, no DOM: MCarousel feeds it the
|
|
5
|
+
* container size and reads back where each item goes and how much of it is visible.
|
|
6
|
+
*
|
|
7
|
+
* Vocabulary (same as upstream): a keyline is a slot along the main axis with a size. Items are
|
|
8
|
+
* all laid out at the same size (the focal size) end to end; each one is masked to the size of the
|
|
9
|
+
* keyline it sits on and translated so masked items touch. Anchors are small keylines just outside
|
|
10
|
+
* the container that items shrink into as they leave.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface Keyline {
|
|
14
|
+
size: number
|
|
15
|
+
offset: number
|
|
16
|
+
unadjustedOffset: number
|
|
17
|
+
isFocal: boolean
|
|
18
|
+
isAnchor: boolean
|
|
19
|
+
isPivot: boolean
|
|
20
|
+
cutoff: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const MIN_SMALL_ITEM_SIZE = 40
|
|
24
|
+
export const MAX_SMALL_ITEM_SIZE = 56
|
|
25
|
+
const ANCHOR_SIZE = 10
|
|
26
|
+
const MEDIUM_LARGE_ITEM_DIFF_THRESHOLD = 0.85
|
|
27
|
+
const MEDIUM_ITEM_FLEX_PERCENTAGE = 0.1
|
|
28
|
+
|
|
29
|
+
const lerpN = (a: number, b: number, t: number) => a + (b - a) * t
|
|
30
|
+
|
|
31
|
+
// ── KeylineList ─────────────────────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
export class KeylineList {
|
|
34
|
+
readonly minSize: number
|
|
35
|
+
readonly maxSize: number
|
|
36
|
+
readonly pivotIndex: number
|
|
37
|
+
readonly firstNonAnchorIndex: number
|
|
38
|
+
readonly lastNonAnchorIndex: number
|
|
39
|
+
readonly firstFocalIndex: number
|
|
40
|
+
readonly lastFocalIndex: number
|
|
41
|
+
|
|
42
|
+
constructor(readonly keylines: Keyline[]) {
|
|
43
|
+
let min = Number.MAX_VALUE
|
|
44
|
+
let max = 0
|
|
45
|
+
for (const k of keylines) {
|
|
46
|
+
if (k.size < min) min = k.size
|
|
47
|
+
if (k.size > max) max = k.size
|
|
48
|
+
}
|
|
49
|
+
this.minSize = min
|
|
50
|
+
this.maxSize = max
|
|
51
|
+
this.pivotIndex = keylines.findIndex((k) => k.isPivot)
|
|
52
|
+
this.firstNonAnchorIndex = keylines.findIndex((k) => !k.isAnchor)
|
|
53
|
+
this.lastNonAnchorIndex = findLastIndex(keylines, (k) => !k.isAnchor)
|
|
54
|
+
this.firstFocalIndex = keylines.findIndex((k) => k.isFocal)
|
|
55
|
+
this.lastFocalIndex = findLastIndex(keylines, (k) => k.isFocal)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get length() { return this.keylines.length }
|
|
59
|
+
get isEmpty() { return this.keylines.length === 0 }
|
|
60
|
+
at(i: number): Keyline { return this.keylines[i]! }
|
|
61
|
+
get first() { return this.at(0) }
|
|
62
|
+
get last() { return this.at(this.length - 1) }
|
|
63
|
+
get lastIndex() { return this.length - 1 }
|
|
64
|
+
get pivot() { return this.at(this.pivotIndex) }
|
|
65
|
+
get firstNonAnchor() { return this.at(this.firstNonAnchorIndex) }
|
|
66
|
+
get lastNonAnchor() { return this.at(this.lastNonAnchorIndex) }
|
|
67
|
+
get firstFocal() { return this.at(this.firstFocalIndex) }
|
|
68
|
+
get lastFocal() { return this.at(this.lastFocalIndex) }
|
|
69
|
+
get focalCount() { return this.lastFocalIndex - this.firstFocalIndex + 1 }
|
|
70
|
+
|
|
71
|
+
isFirstFocalItemAtStartOfContainer() {
|
|
72
|
+
const firstFocalLeft = this.firstFocal.offset - this.firstFocal.size / 2
|
|
73
|
+
return firstFocalLeft >= 0 && this.firstFocal === this.firstNonAnchor
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
isLastFocalItemAtEndOfContainer(carouselMainAxisSize: number) {
|
|
77
|
+
const lastFocalRight = this.lastFocal.offset + this.lastFocal.size / 2
|
|
78
|
+
return lastFocalRight <= carouselMainAxisSize && this.lastFocal === this.lastNonAnchor
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
firstIndexAfterFocalRangeWithSize(size: number) {
|
|
82
|
+
for (let i = this.lastFocalIndex; i <= this.lastIndex; i++) if (this.at(i).size === size) return i
|
|
83
|
+
return this.lastIndex
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
lastIndexBeforeFocalRangeWithSize(size: number) {
|
|
87
|
+
for (let i = this.firstFocalIndex - 1; i >= 0; i--) if (this.at(i).size === size) return i
|
|
88
|
+
return 0
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getKeylineBefore(unadjustedOffset: number): Keyline {
|
|
92
|
+
for (let i = this.lastIndex; i >= 0; i--) {
|
|
93
|
+
const k = this.at(i)
|
|
94
|
+
if (k.unadjustedOffset < unadjustedOffset) return k
|
|
95
|
+
}
|
|
96
|
+
return this.first
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
getKeylineAfter(unadjustedOffset: number): Keyline {
|
|
100
|
+
return this.keylines.find((k) => k.unadjustedOffset >= unadjustedOffset) ?? this.last
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function findLastIndex<T>(arr: T[], pred: (t: T) => boolean) {
|
|
105
|
+
for (let i = arr.length - 1; i >= 0; i--) if (pred(arr[i]!)) return i
|
|
106
|
+
return -1
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const EMPTY = new KeylineList([])
|
|
110
|
+
|
|
111
|
+
type Alignment = 'start' | 'center' | 'end'
|
|
112
|
+
interface TmpKeyline { size: number; isAnchor: boolean }
|
|
113
|
+
|
|
114
|
+
class KeylineListBuilder {
|
|
115
|
+
private firstFocalIndex = -1
|
|
116
|
+
private focalItemSize = 0
|
|
117
|
+
readonly tmp: TmpKeyline[] = []
|
|
118
|
+
|
|
119
|
+
add(size: number, isAnchor = false) {
|
|
120
|
+
this.tmp.push({ size, isAnchor })
|
|
121
|
+
// The first focal item is the first index of the largest item added.
|
|
122
|
+
if (!isAnchor && size > this.focalItemSize) {
|
|
123
|
+
this.firstFocalIndex = this.tmp.length - 1
|
|
124
|
+
this.focalItemSize = size
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private findLastFocalIndex() {
|
|
129
|
+
if (this.firstFocalIndex < 0) return -1
|
|
130
|
+
let last = this.firstFocalIndex
|
|
131
|
+
while (last < this.tmp.length - 1 && this.tmp[last + 1]!.size === this.focalItemSize) last++
|
|
132
|
+
return last
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
createWithPivot(carouselMainAxisSize: number, itemSpacing: number, pivotIndex: number, pivotOffset: number) {
|
|
136
|
+
return new KeylineList(createKeylinesWithPivot(
|
|
137
|
+
pivotIndex, pivotOffset, this.firstFocalIndex, this.findLastFocalIndex(),
|
|
138
|
+
this.focalItemSize, carouselMainAxisSize, itemSpacing, this.tmp,
|
|
139
|
+
))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
createWithAlignment(carouselMainAxisSize: number, itemSpacing: number, alignment: Alignment) {
|
|
143
|
+
const lastFocalIndex = this.findLastFocalIndex()
|
|
144
|
+
const focalItemCount = lastFocalIndex - this.firstFocalIndex
|
|
145
|
+
const pivotIndex = this.firstFocalIndex
|
|
146
|
+
let pivotOffset: number
|
|
147
|
+
if (alignment === 'center') {
|
|
148
|
+
// With an even number of keylines the item spacing sits in the middle of the container.
|
|
149
|
+
const itemSpacingSplit = itemSpacing === 0 || focalItemCount % 2 === 0 ? 0 : itemSpacing / 2
|
|
150
|
+
const itemSpaceCounts = Math.trunc(focalItemCount / 2) * itemSpacing
|
|
151
|
+
pivotOffset = carouselMainAxisSize / 2 - (this.focalItemSize / 2) * focalItemCount - itemSpacingSplit - itemSpaceCounts
|
|
152
|
+
} else if (alignment === 'end') {
|
|
153
|
+
pivotOffset = carouselMainAxisSize - this.focalItemSize / 2
|
|
154
|
+
} else {
|
|
155
|
+
pivotOffset = this.focalItemSize / 2
|
|
156
|
+
}
|
|
157
|
+
return new KeylineList(createKeylinesWithPivot(
|
|
158
|
+
pivotIndex, pivotOffset, this.firstFocalIndex, lastFocalIndex,
|
|
159
|
+
this.focalItemSize, carouselMainAxisSize, itemSpacing, this.tmp,
|
|
160
|
+
))
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function keylineListOf(carouselMainAxisSize: number, itemSpacing: number, alignment: Alignment, build: (b: KeylineListBuilder) => void) {
|
|
165
|
+
const b = new KeylineListBuilder()
|
|
166
|
+
build(b)
|
|
167
|
+
return b.createWithAlignment(carouselMainAxisSize, itemSpacing, alignment)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function keylineListWithPivot(carouselMainAxisSize: number, itemSpacing: number, pivotIndex: number, pivotOffset: number, build: (b: KeylineListBuilder) => void) {
|
|
171
|
+
const b = new KeylineListBuilder()
|
|
172
|
+
build(b)
|
|
173
|
+
return b.createWithPivot(carouselMainAxisSize, itemSpacing, pivotIndex, pivotOffset)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const isCutoffLeft = (size: number, offset: number) => offset - size / 2 < 0 && offset + size / 2 > 0
|
|
177
|
+
const isCutoffRight = (size: number, offset: number, mainAxis: number) =>
|
|
178
|
+
offset - size / 2 < mainAxis && offset + size / 2 > mainAxis
|
|
179
|
+
|
|
180
|
+
function createKeylinesWithPivot(
|
|
181
|
+
pivotIndex: number, pivotOffset: number, firstFocalIndex: number, lastFocalIndex: number,
|
|
182
|
+
itemMainAxisSize: number, carouselMainAxisSize: number, itemSpacing: number, tmp: TmpKeyline[],
|
|
183
|
+
): Keyline[] {
|
|
184
|
+
if (tmp.length === 0 || pivotIndex < 0 || pivotIndex >= tmp.length) return []
|
|
185
|
+
const pivot = tmp[pivotIndex]!
|
|
186
|
+
const isFocal = (i: number) => i >= firstFocalIndex && i <= lastFocalIndex
|
|
187
|
+
const pivotCutoff = isCutoffLeft(pivot.size, pivotOffset)
|
|
188
|
+
? pivotOffset - pivot.size / 2
|
|
189
|
+
: isCutoffRight(pivot.size, pivotOffset, carouselMainAxisSize)
|
|
190
|
+
? pivotOffset + pivot.size / 2 - carouselMainAxisSize
|
|
191
|
+
: 0
|
|
192
|
+
const keylines: Keyline[] = [{
|
|
193
|
+
size: pivot.size, offset: pivotOffset, unadjustedOffset: pivotOffset,
|
|
194
|
+
isFocal: isFocal(pivotIndex), isAnchor: pivot.isAnchor, isPivot: true, cutoff: pivotCutoff,
|
|
195
|
+
}]
|
|
196
|
+
|
|
197
|
+
let offset = pivotOffset - itemMainAxisSize / 2 - itemSpacing
|
|
198
|
+
let unadjustedOffset = pivotOffset - itemMainAxisSize / 2 - itemSpacing
|
|
199
|
+
for (let i = pivotIndex - 1; i >= 0; i--) {
|
|
200
|
+
const t = tmp[i]!
|
|
201
|
+
const tmpOffset = offset - t.size / 2
|
|
202
|
+
const tmpUnadjustedOffset = unadjustedOffset - itemMainAxisSize / 2
|
|
203
|
+
const cutoff = isCutoffLeft(t.size, tmpOffset) ? Math.abs(tmpOffset - t.size / 2) : 0
|
|
204
|
+
keylines.unshift({
|
|
205
|
+
size: t.size, offset: tmpOffset, unadjustedOffset: tmpUnadjustedOffset,
|
|
206
|
+
isFocal: isFocal(i), isAnchor: t.isAnchor, isPivot: false, cutoff,
|
|
207
|
+
})
|
|
208
|
+
offset -= t.size + itemSpacing
|
|
209
|
+
unadjustedOffset -= itemMainAxisSize + itemSpacing
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
offset = pivotOffset + itemMainAxisSize / 2 + itemSpacing
|
|
213
|
+
unadjustedOffset = pivotOffset + itemMainAxisSize / 2 + itemSpacing
|
|
214
|
+
for (let i = pivotIndex + 1; i < tmp.length; i++) {
|
|
215
|
+
const t = tmp[i]!
|
|
216
|
+
const tmpOffset = offset + t.size / 2
|
|
217
|
+
const tmpUnadjustedOffset = unadjustedOffset + itemMainAxisSize / 2
|
|
218
|
+
const cutoff = isCutoffRight(t.size, tmpOffset, carouselMainAxisSize)
|
|
219
|
+
? tmpOffset + t.size / 2 - carouselMainAxisSize
|
|
220
|
+
: 0
|
|
221
|
+
keylines.push({
|
|
222
|
+
size: t.size, offset: tmpOffset, unadjustedOffset: tmpUnadjustedOffset,
|
|
223
|
+
isFocal: isFocal(i), isAnchor: t.isAnchor, isPivot: false, cutoff,
|
|
224
|
+
})
|
|
225
|
+
offset += t.size + itemSpacing
|
|
226
|
+
unadjustedOffset += itemMainAxisSize + itemSpacing
|
|
227
|
+
}
|
|
228
|
+
return keylines
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function lerpKeyline(a: Keyline, b: Keyline, t: number): Keyline {
|
|
232
|
+
return {
|
|
233
|
+
size: lerpN(a.size, b.size, t),
|
|
234
|
+
offset: lerpN(a.offset, b.offset, t),
|
|
235
|
+
unadjustedOffset: lerpN(a.unadjustedOffset, b.unadjustedOffset, t),
|
|
236
|
+
isFocal: t < 0.5 ? a.isFocal : b.isFocal,
|
|
237
|
+
isAnchor: t < 0.5 ? a.isAnchor : b.isAnchor,
|
|
238
|
+
isPivot: t < 0.5 ? a.isPivot : b.isPivot,
|
|
239
|
+
cutoff: lerpN(a.cutoff, b.cutoff, t),
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function lerpKeylineList(from: KeylineList, to: KeylineList, t: number) {
|
|
244
|
+
return new KeylineList(from.keylines.map((k, i) => lerpKeyline(k, to.at(i), t)))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ── Arrangement ─────────────────────────────────────────────────────────────────────────────
|
|
248
|
+
|
|
249
|
+
export interface Arrangement {
|
|
250
|
+
priority: number
|
|
251
|
+
smallSize: number
|
|
252
|
+
smallCount: number
|
|
253
|
+
mediumSize: number
|
|
254
|
+
mediumCount: number
|
|
255
|
+
largeSize: number
|
|
256
|
+
largeCount: number
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const itemCount = (a: Arrangement) => a.largeCount + a.mediumCount + a.smallCount
|
|
260
|
+
|
|
261
|
+
function isValidArrangement(a: Arrangement) {
|
|
262
|
+
if (a.largeCount > 0 && a.smallCount > 0 && a.mediumCount > 0) return a.largeSize > a.mediumSize && a.mediumSize > a.smallSize
|
|
263
|
+
if (a.largeCount > 0 && a.smallCount > 0) return a.largeSize > a.smallSize
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function arrangementCost(a: Arrangement, targetLargeSize: number) {
|
|
268
|
+
if (!isValidArrangement(a)) return Number.MAX_VALUE
|
|
269
|
+
return Math.abs(targetLargeSize - a.largeSize) * a.priority
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function findLowestCostArrangement(o: {
|
|
273
|
+
availableSpace: number; itemSpacing: number
|
|
274
|
+
targetSmallSize: number; minSmallSize: number; maxSmallSize: number; smallCounts: number[]
|
|
275
|
+
targetMediumSize: number; mediumCounts: number[]
|
|
276
|
+
targetLargeSize: number; largeCounts: number[]
|
|
277
|
+
}): Arrangement | null {
|
|
278
|
+
let lowest: Arrangement | null = null
|
|
279
|
+
let priority = 1
|
|
280
|
+
for (const largeCount of o.largeCounts) {
|
|
281
|
+
for (const mediumCount of o.mediumCounts) {
|
|
282
|
+
for (const smallCount of o.smallCounts) {
|
|
283
|
+
const a = fit(priority, o.availableSpace, o.itemSpacing, smallCount, o.targetSmallSize, o.minSmallSize, o.maxSmallSize, mediumCount, o.targetMediumSize, largeCount, o.targetLargeSize)
|
|
284
|
+
if (lowest === null || arrangementCost(a, o.targetLargeSize) < arrangementCost(lowest, o.targetLargeSize)) {
|
|
285
|
+
lowest = a
|
|
286
|
+
// Arrangements come in priority order, so a zero-cost one can't be beaten.
|
|
287
|
+
if (arrangementCost(lowest, o.targetLargeSize) === 0) return lowest
|
|
288
|
+
}
|
|
289
|
+
priority++
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return lowest
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function fit(
|
|
297
|
+
priority: number, availableSpace: number, itemSpacing: number,
|
|
298
|
+
smallCount: number, smallSize: number, minSmallSize: number, maxSmallSize: number,
|
|
299
|
+
mediumCount: number, mediumSize: number, largeCount: number, largeSize: number,
|
|
300
|
+
): Arrangement {
|
|
301
|
+
const totalItemCount = largeCount + mediumCount + smallCount
|
|
302
|
+
const availableSpaceWithoutSpacing = availableSpace - (totalItemCount - 1) * itemSpacing
|
|
303
|
+
let arrangedSmallSize = Math.min(Math.max(smallSize, minSmallSize), maxSmallSize)
|
|
304
|
+
let arrangedMediumSize = mediumSize
|
|
305
|
+
let arrangedLargeSize = largeSize
|
|
306
|
+
const totalSpaceTaken = arrangedLargeSize * largeCount + arrangedMediumSize * mediumCount + arrangedSmallSize * smallCount
|
|
307
|
+
const delta = availableSpaceWithoutSpacing - totalSpaceTaken
|
|
308
|
+
// First resize small items within their min-max range.
|
|
309
|
+
if (smallCount > 0 && delta > 0) arrangedSmallSize += Math.min(delta / smallCount, maxSmallSize - arrangedSmallSize)
|
|
310
|
+
else if (smallCount > 0 && delta < 0) arrangedSmallSize += Math.max(delta / smallCount, minSmallSize - arrangedSmallSize)
|
|
311
|
+
arrangedSmallSize = smallCount > 0 ? arrangedSmallSize : 0
|
|
312
|
+
arrangedLargeSize = (availableSpaceWithoutSpacing - (smallCount + mediumCount / 2) * arrangedSmallSize) / (largeCount + mediumCount / 2)
|
|
313
|
+
arrangedMediumSize = (arrangedLargeSize + arrangedSmallSize) / 2
|
|
314
|
+
// If the large size moved away from its target, win some of it back from the medium items.
|
|
315
|
+
if (mediumCount > 0 && arrangedLargeSize !== largeSize) {
|
|
316
|
+
const targetAdjustment = (largeSize - arrangedLargeSize) * largeCount
|
|
317
|
+
const availableMediumFlex = arrangedMediumSize * MEDIUM_ITEM_FLEX_PERCENTAGE * mediumCount
|
|
318
|
+
const distribute = Math.min(Math.abs(targetAdjustment), availableMediumFlex)
|
|
319
|
+
if (targetAdjustment > 0) {
|
|
320
|
+
arrangedMediumSize -= distribute / mediumCount
|
|
321
|
+
arrangedLargeSize += distribute / largeCount
|
|
322
|
+
} else {
|
|
323
|
+
arrangedMediumSize += distribute / mediumCount
|
|
324
|
+
arrangedLargeSize -= distribute / largeCount
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return { priority, smallSize: arrangedSmallSize, smallCount, mediumSize: arrangedMediumSize, mediumCount, largeSize: arrangedLargeSize, largeCount }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ── Keyline lists per carousel type (Keylines.kt) ───────────────────────────────────────────
|
|
331
|
+
|
|
332
|
+
function createLeftAlignedKeylineList(carouselMainAxisSize: number, itemSpacing: number, leftAnchorSize: number, rightAnchorSize: number, a: Arrangement) {
|
|
333
|
+
return keylineListOf(carouselMainAxisSize, itemSpacing, 'start', (b) => {
|
|
334
|
+
b.add(leftAnchorSize, true)
|
|
335
|
+
for (let i = 0; i < a.largeCount; i++) b.add(a.largeSize)
|
|
336
|
+
for (let i = 0; i < a.mediumCount; i++) b.add(a.mediumSize)
|
|
337
|
+
for (let i = 0; i < a.smallCount; i++) b.add(a.smallSize)
|
|
338
|
+
b.add(rightAnchorSize, true)
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function createCenterAlignedKeylineList(carouselMainAxisSize: number, itemSpacing: number, leftAnchorSize: number, rightAnchorSize: number, a: Arrangement) {
|
|
343
|
+
return keylineListOf(carouselMainAxisSize, itemSpacing, 'center', (b) => {
|
|
344
|
+
b.add(leftAnchorSize, true)
|
|
345
|
+
for (let i = 0; i < Math.trunc(a.smallCount / 2); i++) b.add(a.smallSize)
|
|
346
|
+
for (let i = 0; i < Math.trunc(a.mediumCount / 2); i++) b.add(a.mediumSize)
|
|
347
|
+
for (let i = 0; i < a.largeCount; i++) b.add(a.largeSize)
|
|
348
|
+
for (let i = 0; i < Math.trunc(a.mediumCount / 2); i++) b.add(a.mediumSize)
|
|
349
|
+
for (let i = 0; i < Math.trunc(a.smallCount / 2); i++) b.add(a.smallSize)
|
|
350
|
+
b.add(rightAnchorSize, true)
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export function multiBrowseKeylineList(
|
|
355
|
+
carouselMainAxisSize: number, preferredItemSize: number, itemSpacing: number, count: number,
|
|
356
|
+
minSmallItemSize = MIN_SMALL_ITEM_SIZE, maxSmallItemSize = MAX_SMALL_ITEM_SIZE,
|
|
357
|
+
): KeylineList {
|
|
358
|
+
if (carouselMainAxisSize === 0 || preferredItemSize === 0) return EMPTY
|
|
359
|
+
let smallCounts = [1]
|
|
360
|
+
const mediumCounts = [1, 0]
|
|
361
|
+
const targetLargeSize = Math.min(preferredItemSize, carouselMainAxisSize)
|
|
362
|
+
// A small item is ideally 1/3 of the large one, clamped to the min-max range.
|
|
363
|
+
const targetSmallSize = Math.min(Math.max(targetLargeSize / 3, minSmallItemSize), maxSmallItemSize)
|
|
364
|
+
const targetMediumSize = (targetLargeSize + targetSmallSize) / 2
|
|
365
|
+
if (carouselMainAxisSize < minSmallItemSize * 2) smallCounts = [0]
|
|
366
|
+
const minAvailableLargeSpace = carouselMainAxisSize - targetMediumSize * Math.max(...mediumCounts) - maxSmallItemSize * Math.max(...smallCounts)
|
|
367
|
+
const minLargeCount = Math.max(1, Math.floor(minAvailableLargeSpace / targetLargeSize))
|
|
368
|
+
const maxLargeCount = Math.ceil(carouselMainAxisSize / targetLargeSize)
|
|
369
|
+
const largeCounts = Array.from({ length: maxLargeCount - minLargeCount + 1 }, (_, i) => maxLargeCount - i)
|
|
370
|
+
const base = { availableSpace: carouselMainAxisSize, itemSpacing, targetSmallSize, minSmallSize: minSmallItemSize, maxSmallSize: maxSmallItemSize, targetMediumSize, targetLargeSize, largeCounts }
|
|
371
|
+
let arrangement = findLowestCostArrangement({ ...base, smallCounts, mediumCounts })
|
|
372
|
+
|
|
373
|
+
if (arrangement !== null && itemCount(arrangement) > count) {
|
|
374
|
+
let surplus = itemCount(arrangement) - count
|
|
375
|
+
let smallCount = arrangement.smallCount
|
|
376
|
+
let mediumCount = arrangement.mediumCount
|
|
377
|
+
while (surplus > 0) {
|
|
378
|
+
if (smallCount > 0) smallCount -= 1
|
|
379
|
+
// Keep at least one medium so the large items don't fill the whole carousel.
|
|
380
|
+
else if (mediumCount > 1) mediumCount -= 1
|
|
381
|
+
surplus -= 1
|
|
382
|
+
}
|
|
383
|
+
arrangement = findLowestCostArrangement({ ...base, smallCounts: [smallCount], mediumCounts: [mediumCount] })
|
|
384
|
+
}
|
|
385
|
+
if (arrangement === null) return EMPTY
|
|
386
|
+
return createLeftAlignedKeylineList(carouselMainAxisSize, itemSpacing, ANCHOR_SIZE, ANCHOR_SIZE, arrangement)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function uncontainedKeylineList(carouselMainAxisSize: number, itemSize: number, itemSpacing: number): KeylineList {
|
|
390
|
+
if (carouselMainAxisSize === 0 || itemSize === 0) return EMPTY
|
|
391
|
+
const largeItemSize = Math.min(itemSize + itemSpacing, carouselMainAxisSize)
|
|
392
|
+
const largeCount = Math.max(1, Math.floor(carouselMainAxisSize / largeItemSize))
|
|
393
|
+
const remainingSpace = carouselMainAxisSize - largeCount * largeItemSize
|
|
394
|
+
const mediumCount = remainingSpace > 0 ? 1 : 0
|
|
395
|
+
const mediumItemSize = calculateMediumChildSize(ANCHOR_SIZE, largeItemSize, remainingSpace)
|
|
396
|
+
const arrangement: Arrangement = { priority: 0, smallSize: 0, smallCount: 0, mediumSize: mediumItemSize, mediumCount, largeSize: largeItemSize, largeCount }
|
|
397
|
+
const xSmallSize = Math.min(ANCHOR_SIZE, itemSize)
|
|
398
|
+
// Half the cut-off item's size on the left makes the motion there closer to the right side's.
|
|
399
|
+
const leftAnchorSize = Math.max(xSmallSize, mediumItemSize * 0.5)
|
|
400
|
+
return createLeftAlignedKeylineList(carouselMainAxisSize, itemSpacing, leftAnchorSize, ANCHOR_SIZE, arrangement)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export function heroKeylineList(
|
|
404
|
+
carouselMainAxisSize: number, preferredItemSize: number | null, itemSpacing: number, count: number,
|
|
405
|
+
isCentered: boolean, minSmallItemSize = MIN_SMALL_ITEM_SIZE, maxSmallItemSize = MAX_SMALL_ITEM_SIZE,
|
|
406
|
+
): KeylineList {
|
|
407
|
+
if (carouselMainAxisSize === 0) return EMPTY
|
|
408
|
+
// Fewer than 3 items can't be centered.
|
|
409
|
+
const shouldCenter = isCentered && count >= 3
|
|
410
|
+
let smallCounts = count <= 1 ? [0] : shouldCenter ? [2] : [1]
|
|
411
|
+
const targetLargeSize = Math.min(preferredItemSize ?? carouselMainAxisSize, carouselMainAxisSize)
|
|
412
|
+
const targetSmallSize = Math.min(Math.max(targetLargeSize / 3, minSmallItemSize), maxSmallItemSize)
|
|
413
|
+
// Room for the small items plus a large item at least 25% bigger than them.
|
|
414
|
+
const fullscreenThreshold = minSmallItemSize * Math.max(...smallCounts) + minSmallItemSize * 1.25
|
|
415
|
+
if (carouselMainAxisSize < fullscreenThreshold) smallCounts = [0]
|
|
416
|
+
const minAvailableLargeSpace = carouselMainAxisSize - minSmallItemSize * Math.max(...smallCounts)
|
|
417
|
+
const minLargeCount = Math.max(1, Math.floor(minAvailableLargeSpace / targetLargeSize))
|
|
418
|
+
const maxLargeCount = Math.ceil(carouselMainAxisSize / targetLargeSize)
|
|
419
|
+
const largeCounts = Array.from({ length: maxLargeCount - minLargeCount + 1 }, (_, i) => maxLargeCount - i)
|
|
420
|
+
const arrangement = findLowestCostArrangement({
|
|
421
|
+
availableSpace: carouselMainAxisSize, itemSpacing, targetSmallSize, minSmallSize: minSmallItemSize, maxSmallSize: maxSmallItemSize,
|
|
422
|
+
smallCounts, targetMediumSize: 0, mediumCounts: [0], targetLargeSize, largeCounts,
|
|
423
|
+
})
|
|
424
|
+
if (arrangement === null) return EMPTY
|
|
425
|
+
return shouldCenter && count >= itemCount(arrangement)
|
|
426
|
+
? createCenterAlignedKeylineList(carouselMainAxisSize, itemSpacing, ANCHOR_SIZE, ANCHOR_SIZE, arrangement)
|
|
427
|
+
: createLeftAlignedKeylineList(carouselMainAxisSize, itemSpacing, ANCHOR_SIZE, ANCHOR_SIZE, arrangement)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** MDC-Android's FullScreenCarouselStrategy: one large item filling the container. */
|
|
431
|
+
export function fullScreenKeylineList(carouselMainAxisSize: number, itemSpacing: number): KeylineList {
|
|
432
|
+
if (carouselMainAxisSize === 0) return EMPTY
|
|
433
|
+
const arrangement: Arrangement = { priority: 0, smallSize: 0, smallCount: 0, mediumSize: 0, mediumCount: 0, largeSize: carouselMainAxisSize, largeCount: 1 }
|
|
434
|
+
return createLeftAlignedKeylineList(carouselMainAxisSize, itemSpacing, ANCHOR_SIZE, ANCHOR_SIZE, arrangement)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function calculateMediumChildSize(minimumMediumSize: number, largeItemSize: number, remainingSpace: number) {
|
|
438
|
+
// Ideally a third of the medium item is cut off, so it's 1.5x the remaining space.
|
|
439
|
+
let mediumItemSize = Math.max(remainingSpace * 1.5, minimumMediumSize)
|
|
440
|
+
// Too close to the large size gives too little motion between them.
|
|
441
|
+
const largeItemThreshold = largeItemSize * MEDIUM_LARGE_ITEM_DIFF_THRESHOLD
|
|
442
|
+
if (mediumItemSize > largeItemThreshold) {
|
|
443
|
+
const sizeWithFifthCutOff = remainingSpace * 1.2
|
|
444
|
+
mediumItemSize = Math.min(Math.max(largeItemThreshold, sizeWithFifthCutOff), largeItemSize)
|
|
445
|
+
}
|
|
446
|
+
return mediumItemSize
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ── Strategy ────────────────────────────────────────────────────────────────────────────────
|
|
450
|
+
|
|
451
|
+
export class Strategy {
|
|
452
|
+
readonly startKeylineSteps: KeylineList[]
|
|
453
|
+
readonly endKeylineSteps: KeylineList[]
|
|
454
|
+
readonly minItemSize: number
|
|
455
|
+
readonly maxItemSize: number
|
|
456
|
+
private readonly startShiftDistance: number
|
|
457
|
+
private readonly endShiftDistance: number
|
|
458
|
+
private readonly startShiftPoints: number[]
|
|
459
|
+
private readonly endShiftPoints: number[]
|
|
460
|
+
private lastStartAndEndSteps: KeylineList[] | null = null
|
|
461
|
+
|
|
462
|
+
constructor(
|
|
463
|
+
readonly defaultKeylines: KeylineList,
|
|
464
|
+
readonly availableSpace: number,
|
|
465
|
+
readonly itemSpacing: number,
|
|
466
|
+
readonly beforeContentPadding: number,
|
|
467
|
+
readonly afterContentPadding: number,
|
|
468
|
+
) {
|
|
469
|
+
this.startKeylineSteps = getStartKeylineSteps(defaultKeylines, availableSpace, itemSpacing, beforeContentPadding)
|
|
470
|
+
this.endKeylineSteps = getEndKeylineSteps(defaultKeylines, availableSpace, itemSpacing, afterContentPadding)
|
|
471
|
+
let min = defaultKeylines.minSize
|
|
472
|
+
let max = defaultKeylines.maxSize
|
|
473
|
+
for (const step of [...this.startKeylineSteps, ...this.endKeylineSteps]) {
|
|
474
|
+
if (step.minSize < min) min = step.minSize
|
|
475
|
+
if (step.maxSize > max) max = step.maxSize
|
|
476
|
+
}
|
|
477
|
+
this.minItemSize = min
|
|
478
|
+
this.maxItemSize = max
|
|
479
|
+
this.startShiftDistance = getStartShiftDistance(this.startKeylineSteps, beforeContentPadding)
|
|
480
|
+
this.endShiftDistance = getEndShiftDistance(this.endKeylineSteps, afterContentPadding)
|
|
481
|
+
this.startShiftPoints = getStepInterpolationPoints(this.startShiftDistance, this.startKeylineSteps, true)
|
|
482
|
+
this.endShiftPoints = getStepInterpolationPoints(this.endShiftDistance, this.endKeylineSteps, false)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
get itemMainAxisSize() { return this.defaultKeylines.isEmpty ? 0 : this.defaultKeylines.firstFocal.size }
|
|
486
|
+
|
|
487
|
+
get isValid() { return !this.defaultKeylines.isEmpty && this.availableSpace !== 0 && this.itemMainAxisSize !== 0 }
|
|
488
|
+
|
|
489
|
+
getKeylineListForScrollOffset(scrollOffset: number, maxScrollOffset: number, roundToNearestStep = false): KeylineList {
|
|
490
|
+
const positiveScrollOffset = Math.max(0, scrollOffset)
|
|
491
|
+
const startShiftOffset = this.startShiftDistance
|
|
492
|
+
const endShiftOffset = Math.max(0, maxScrollOffset - this.endShiftDistance)
|
|
493
|
+
if (positiveScrollOffset >= startShiftOffset && positiveScrollOffset <= endShiftOffset) return this.defaultKeylines
|
|
494
|
+
|
|
495
|
+
let interpolation = lerpRange(1, 0, 0, startShiftOffset, positiveScrollOffset)
|
|
496
|
+
let shiftPoints = this.startShiftPoints
|
|
497
|
+
let steps = this.startKeylineSteps
|
|
498
|
+
if (positiveScrollOffset > endShiftOffset) {
|
|
499
|
+
interpolation = lerpRange(0, 1, endShiftOffset, maxScrollOffset, positiveScrollOffset)
|
|
500
|
+
shiftPoints = this.endShiftPoints
|
|
501
|
+
steps = this.endKeylineSteps
|
|
502
|
+
// End shifting from offset 0: go straight from the last start step to the last end step.
|
|
503
|
+
if (endShiftOffset < 0.01 && this.startKeylineSteps.length === 2 && this.endKeylineSteps.length === 2) {
|
|
504
|
+
this.lastStartAndEndSteps ??= [this.startKeylineSteps[1]!, this.endKeylineSteps[1]!]
|
|
505
|
+
steps = this.lastStartAndEndSteps
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
let fromStepIndex = 0
|
|
510
|
+
let toStepIndex = 0
|
|
511
|
+
let steppedInterpolation = 0
|
|
512
|
+
let lowerBounds = shiftPoints[0] ?? 0
|
|
513
|
+
for (let i = 1; i < steps.length; i++) {
|
|
514
|
+
const upperBounds = shiftPoints[i]!
|
|
515
|
+
if (interpolation <= upperBounds) {
|
|
516
|
+
fromStepIndex = i - 1
|
|
517
|
+
toStepIndex = i
|
|
518
|
+
steppedInterpolation = lerpRange(0, 1, lowerBounds, upperBounds, interpolation)
|
|
519
|
+
break
|
|
520
|
+
}
|
|
521
|
+
lowerBounds = upperBounds
|
|
522
|
+
}
|
|
523
|
+
if (roundToNearestStep) return steps[Math.round(steppedInterpolation) === 0 ? fromStepIndex : toStepIndex]!
|
|
524
|
+
return lerpKeylineList(steps[fromStepIndex]!, steps[toStepIndex]!, steppedInterpolation)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function lerpRange(outputMin: number, outputMax: number, inputMin: number, inputMax: number, value: number) {
|
|
529
|
+
if (value <= inputMin) return outputMin
|
|
530
|
+
if (value >= inputMax) return outputMax
|
|
531
|
+
return lerpN(outputMin, outputMax, (value - inputMin) / (inputMax - inputMin))
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function getStartShiftDistance(steps: KeylineList[], beforeContentPadding: number) {
|
|
535
|
+
if (steps.length === 0) return 0
|
|
536
|
+
return Math.max(steps[steps.length - 1]!.first.unadjustedOffset - steps[0]!.first.unadjustedOffset, beforeContentPadding)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function getEndShiftDistance(steps: KeylineList[], afterContentPadding: number) {
|
|
540
|
+
if (steps.length === 0) return 0
|
|
541
|
+
return Math.max(steps[0]!.last.unadjustedOffset - steps[steps.length - 1]!.last.unadjustedOffset, afterContentPadding)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function getStartKeylineSteps(defaultKeylines: KeylineList, mainAxis: number, itemSpacing: number, beforeContentPadding: number): KeylineList[] {
|
|
545
|
+
if (defaultKeylines.isEmpty) return []
|
|
546
|
+
const steps = [defaultKeylines]
|
|
547
|
+
if (defaultKeylines.isFirstFocalItemAtStartOfContainer()) {
|
|
548
|
+
if (beforeContentPadding !== 0) {
|
|
549
|
+
steps.push(createShiftedKeylineListForContentPadding(defaultKeylines, mainAxis, itemSpacing, beforeContentPadding, defaultKeylines.firstFocal, defaultKeylines.firstFocalIndex))
|
|
550
|
+
}
|
|
551
|
+
return steps
|
|
552
|
+
}
|
|
553
|
+
const startIndex = defaultKeylines.firstNonAnchorIndex
|
|
554
|
+
const endIndex = defaultKeylines.firstFocalIndex
|
|
555
|
+
const numberOfSteps = endIndex - startIndex
|
|
556
|
+
if (numberOfSteps <= 0 && defaultKeylines.firstFocal.cutoff > 0) {
|
|
557
|
+
steps.push(moveKeylineAndCreateShiftedKeylineList(defaultKeylines, 0, 0, mainAxis, itemSpacing))
|
|
558
|
+
return steps
|
|
559
|
+
}
|
|
560
|
+
for (let i = 0; i < numberOfSteps; i++) {
|
|
561
|
+
const prevStep = steps[steps.length - 1]!
|
|
562
|
+
const originalItemIndex = startIndex + i
|
|
563
|
+
let dstIndex = defaultKeylines.lastIndex
|
|
564
|
+
if (originalItemIndex > 0) {
|
|
565
|
+
const originalNeighborBeforeSize = defaultKeylines.at(originalItemIndex - 1).size
|
|
566
|
+
dstIndex = prevStep.firstIndexAfterFocalRangeWithSize(originalNeighborBeforeSize) - 1
|
|
567
|
+
}
|
|
568
|
+
steps.push(moveKeylineAndCreateShiftedKeylineList(prevStep, defaultKeylines.firstNonAnchorIndex, dstIndex, mainAxis, itemSpacing))
|
|
569
|
+
}
|
|
570
|
+
if (beforeContentPadding !== 0) {
|
|
571
|
+
const last = steps[steps.length - 1]!
|
|
572
|
+
steps[steps.length - 1] = createShiftedKeylineListForContentPadding(last, mainAxis, itemSpacing, beforeContentPadding, last.firstFocal, last.firstFocalIndex)
|
|
573
|
+
}
|
|
574
|
+
return steps
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function getEndKeylineSteps(defaultKeylines: KeylineList, mainAxis: number, itemSpacing: number, afterContentPadding: number): KeylineList[] {
|
|
578
|
+
if (defaultKeylines.isEmpty) return []
|
|
579
|
+
const steps = [defaultKeylines]
|
|
580
|
+
if (defaultKeylines.isLastFocalItemAtEndOfContainer(mainAxis)) {
|
|
581
|
+
if (afterContentPadding !== 0) {
|
|
582
|
+
steps.push(createShiftedKeylineListForContentPadding(defaultKeylines, mainAxis, itemSpacing, -afterContentPadding, defaultKeylines.lastFocal, defaultKeylines.lastFocalIndex))
|
|
583
|
+
}
|
|
584
|
+
return steps
|
|
585
|
+
}
|
|
586
|
+
const startIndex = defaultKeylines.lastFocalIndex
|
|
587
|
+
const endIndex = defaultKeylines.lastNonAnchorIndex
|
|
588
|
+
const numberOfSteps = endIndex - startIndex
|
|
589
|
+
if (numberOfSteps <= 0 && defaultKeylines.lastFocal.cutoff > 0) {
|
|
590
|
+
steps.push(moveKeylineAndCreateShiftedKeylineList(defaultKeylines, 0, 0, mainAxis, itemSpacing))
|
|
591
|
+
return steps
|
|
592
|
+
}
|
|
593
|
+
for (let i = 0; i < numberOfSteps; i++) {
|
|
594
|
+
const prevStep = steps[steps.length - 1]!
|
|
595
|
+
const originalItemIndex = endIndex - i
|
|
596
|
+
let dstIndex = 0
|
|
597
|
+
if (originalItemIndex < defaultKeylines.lastIndex) {
|
|
598
|
+
const originalNeighborAfterSize = defaultKeylines.at(originalItemIndex + 1).size
|
|
599
|
+
dstIndex = prevStep.lastIndexBeforeFocalRangeWithSize(originalNeighborAfterSize) + 1
|
|
600
|
+
}
|
|
601
|
+
steps.push(moveKeylineAndCreateShiftedKeylineList(prevStep, defaultKeylines.lastNonAnchorIndex, dstIndex, mainAxis, itemSpacing))
|
|
602
|
+
}
|
|
603
|
+
if (afterContentPadding !== 0) {
|
|
604
|
+
const last = steps[steps.length - 1]!
|
|
605
|
+
steps[steps.length - 1] = createShiftedKeylineListForContentPadding(last, mainAxis, itemSpacing, -afterContentPadding, last.lastFocal, last.lastFocalIndex)
|
|
606
|
+
}
|
|
607
|
+
return steps
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function createShiftedKeylineListForContentPadding(from: KeylineList, mainAxis: number, itemSpacing: number, contentPadding: number, pivot: Keyline, pivotIndex: number) {
|
|
611
|
+
const numberOfNonAnchorKeylines = from.keylines.filter((k) => !k.isAnchor).length
|
|
612
|
+
const sizeReduction = contentPadding / numberOfNonAnchorKeylines
|
|
613
|
+
const newKeylines = keylineListWithPivot(mainAxis, itemSpacing, pivotIndex, pivot.offset - sizeReduction / 2 + contentPadding, (b) => {
|
|
614
|
+
for (const k of from.keylines) b.add(k.size - Math.abs(sizeReduction), k.isAnchor)
|
|
615
|
+
})
|
|
616
|
+
// Items are still laid out end to end at the original size, so keep the original unadjusted offsets.
|
|
617
|
+
return new KeylineList(newKeylines.keylines.map((k, i) => ({ ...k, unadjustedOffset: from.at(i).unadjustedOffset })))
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function moveKeylineAndCreateShiftedKeylineList(from: KeylineList, srcIndex: number, dstIndex: number, mainAxis: number, itemSpacing: number) {
|
|
621
|
+
const pivotDir = srcIndex > dstIndex ? 1 : -1
|
|
622
|
+
const pivotDelta = (from.at(srcIndex).size - from.at(srcIndex).cutoff + itemSpacing) * pivotDir
|
|
623
|
+
const newPivotIndex = from.pivotIndex + pivotDir
|
|
624
|
+
const newPivotOffset = from.pivot.offset + pivotDelta
|
|
625
|
+
const moved = [...from.keylines]
|
|
626
|
+
const [k] = moved.splice(srcIndex, 1)
|
|
627
|
+
moved.splice(dstIndex, 0, k!)
|
|
628
|
+
return keylineListWithPivot(mainAxis, itemSpacing, newPivotIndex, newPivotOffset, (b) => {
|
|
629
|
+
for (const m of moved) b.add(m.size, m.isAnchor)
|
|
630
|
+
})
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function getStepInterpolationPoints(totalShiftDistance: number, steps: KeylineList[], isShiftingLeft: boolean) {
|
|
634
|
+
const points = [0]
|
|
635
|
+
if (totalShiftDistance === 0 || steps.length === 0) return points
|
|
636
|
+
for (let i = 1; i < steps.length; i++) {
|
|
637
|
+
const prev = steps[i - 1]!
|
|
638
|
+
const curr = steps[i]!
|
|
639
|
+
const distanceShifted = isShiftingLeft
|
|
640
|
+
? curr.first.unadjustedOffset - prev.first.unadjustedOffset
|
|
641
|
+
: prev.last.unadjustedOffset - curr.last.unadjustedOffset
|
|
642
|
+
const stepPercentage = distanceShifted / totalShiftDistance
|
|
643
|
+
points.push(i === steps.length - 1 ? 1 : points[i - 1]! + stepPercentage)
|
|
644
|
+
}
|
|
645
|
+
return points
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// ── Scroll geometry (Carousel.kt / KeylineSnapPosition.kt) ──────────────────────────────────
|
|
649
|
+
|
|
650
|
+
export function maxScrollOffset(strategy: Strategy, count: number) {
|
|
651
|
+
const total = strategy.itemMainAxisSize * count + strategy.itemSpacing * (count - 1)
|
|
652
|
+
return Math.max(total - strategy.availableSpace, 0)
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** Where item `index` sits, in pager terms, when it's the snapped item. */
|
|
656
|
+
export function snapPositionOffset(strategy: Strategy, index: number, count: number) {
|
|
657
|
+
if (!strategy.isValid) return 0
|
|
658
|
+
let offset = Math.round(strategy.defaultKeylines.firstFocal.unadjustedOffset - strategy.itemMainAxisSize / 2)
|
|
659
|
+
const startSteps = strategy.startKeylineSteps
|
|
660
|
+
if (index <= startSteps.length - 1) {
|
|
661
|
+
const stepIndex = clamp(startSteps.length - 1 - index, 0, startSteps.length - 1)
|
|
662
|
+
offset = Math.round(startSteps[stepIndex]!.firstFocal.unadjustedOffset - strategy.itemMainAxisSize / 2)
|
|
663
|
+
}
|
|
664
|
+
const endSteps = strategy.endKeylineSteps
|
|
665
|
+
const lastItemIndex = count - 1
|
|
666
|
+
if (index >= lastItemIndex - (endSteps.length - 1) && count > strategy.defaultKeylines.focalCount) {
|
|
667
|
+
const stepIndex = clamp(endSteps.length - 1 - (lastItemIndex - index), 0, endSteps.length - 1)
|
|
668
|
+
offset = Math.round(endSteps[stepIndex]!.lastFocal.unadjustedOffset - strategy.itemMainAxisSize / 2)
|
|
669
|
+
}
|
|
670
|
+
return offset
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/** The scroll offset at which item `index` is snapped. */
|
|
674
|
+
export function snapScrollOffset(strategy: Strategy, index: number, count: number) {
|
|
675
|
+
const stride = strategy.itemMainAxisSize + strategy.itemSpacing
|
|
676
|
+
return clamp(index * stride - snapPositionOffset(strategy, index, count), 0, maxScrollOffset(strategy, count))
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
export interface ItemPlacement {
|
|
680
|
+
/** Translation of the item along the main axis, from the container's start edge. */
|
|
681
|
+
offset: number
|
|
682
|
+
/** Visible (masked) size along the main axis. */
|
|
683
|
+
maskSize: number
|
|
684
|
+
/** Start of the mask inside the item, which is `strategy.itemMainAxisSize` long. */
|
|
685
|
+
maskStart: number
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** Modifier.carouselItem's layout block: where item `index` goes and how much of it shows. */
|
|
689
|
+
export function placeItem(strategy: Strategy, index: number, scrollOffset: number, maxScroll: number): ItemPlacement {
|
|
690
|
+
const size = strategy.itemMainAxisSize
|
|
691
|
+
const keylines = strategy.getKeylineListForScrollOffset(scrollOffset, maxScroll)
|
|
692
|
+
const stride = size + strategy.itemSpacing
|
|
693
|
+
const unadjustedCenter = index * stride + size / 2 - scrollOffset
|
|
694
|
+
const before = keylines.getKeylineBefore(unadjustedCenter)
|
|
695
|
+
const after = keylines.getKeylineAfter(unadjustedCenter)
|
|
696
|
+
const progress = before === after ? 1 : (unadjustedCenter - before.unadjustedOffset) / (after.unadjustedOffset - before.unadjustedOffset)
|
|
697
|
+
const k = lerpKeyline(before, after, progress)
|
|
698
|
+
let translation = k.offset - unadjustedCenter
|
|
699
|
+
if (before === after) {
|
|
700
|
+
// Beyond the first or last keyline: keep offsetting by the item's masked size.
|
|
701
|
+
translation += (unadjustedCenter - k.unadjustedOffset) / k.size
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
offset: index * stride - scrollOffset + translation,
|
|
705
|
+
maskSize: k.size,
|
|
706
|
+
maskStart: size / 2 - k.size / 2,
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function clamp(v: number, min: number, max: number) {
|
|
711
|
+
return Math.min(Math.max(v, min), max)
|
|
712
|
+
}
|