mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
Finish native VirtualList sticky headers and scroll landing on main (#11192)
This commit is contained in:
@@ -171,12 +171,10 @@ export const Props = Schema.Struct({
|
||||
*/
|
||||
reserveStart: Schema.optional(Schema.Number),
|
||||
/**
|
||||
* Indices of items to keep mounted, such as section headers. The last one
|
||||
* before the window's first row stays rendered at its own offset, so the
|
||||
* previous header is mounted while the window starts at the current one.
|
||||
* The next one is also rendered once it is within the pinned item's size
|
||||
* of the viewport, for an item taller than the viewport.
|
||||
* This does not pin the item to the viewport.
|
||||
* Indices of items to keep mounted, such as section headers: the last one
|
||||
* before the window, and the next one once it could push off a pinned item
|
||||
* taller than the viewport. They render at their own offset; this does not
|
||||
* pin them.
|
||||
*/
|
||||
sticky: Schema.optional(Schema.Array(Schema.Number)),
|
||||
/**
|
||||
@@ -863,12 +861,10 @@ const windowOf = (
|
||||
const row = rowOf(layout, focused)
|
||||
keep(rows, [Math.max(0, row - 1), Math.min(layout.rows, row + 2)])
|
||||
}
|
||||
// Keep the last sticky item before the window's first row rendered, and
|
||||
// the first one after the viewport while it is close enough to push the
|
||||
// pinned one off: with the window as tall as the pinned item, it would be
|
||||
// in it. A header taller than the viewport needs the next one mounted
|
||||
// before it scrolls into the window. Without a band, the window starts at
|
||||
// row 0. A next item already in the window merges into it.
|
||||
// Keep the last sticky item before the window, and the next one once it
|
||||
// would be in a window as tall as the pinned item, so a header taller than
|
||||
// the viewport has it mounted before the push starts. Without a band the
|
||||
// window starts at row 0.
|
||||
if (band !== null) {
|
||||
const { offsets } = layout
|
||||
let before = -1
|
||||
|
||||
@@ -43,10 +43,9 @@
|
||||
*
|
||||
* `sticky` keeps the header of the section on screen mounted, and the one
|
||||
* before it. In a vertical list the native `EffectUIStickyHeader` view pins
|
||||
* it to the viewport until the next sticky item pushes it away. The view
|
||||
* ships with `@effect/ui-native` for iOS, Android and macOS, and needs a
|
||||
* build that includes it, such as a development build. Elsewhere, such as in
|
||||
* Expo Go, headers stay at their own offset, and development builds warn.
|
||||
* it to the viewport until the next sticky item pushes it away. That needs a
|
||||
* build with `@effect/ui-native`'s native code, such as a development build.
|
||||
* In Expo Go headers scroll with their rows, and development builds warn.
|
||||
*
|
||||
* **Example** (A list of rows)
|
||||
*
|
||||
@@ -173,7 +172,7 @@ const Model = Schema.Struct({
|
||||
),
|
||||
pending: Schema.Array(Measurement),
|
||||
momentum: Schema.Boolean,
|
||||
// The device's pixels per point, the grid native lands a scroll on.
|
||||
// Pixels per point, the grid native lands a scroll on.
|
||||
scale: Schema.Number
|
||||
})
|
||||
|
||||
@@ -203,9 +202,8 @@ const Message = UiSchema.TaggedEnum({
|
||||
|
||||
type Message = typeof Message.Type
|
||||
|
||||
// Read once, to keep `Dimensions` and its listener out of the bundle. A copy
|
||||
// of `deviceScale` in `internal/asset.ts`: under Metro, sharing it costs
|
||||
// `Image` apps more than the copy costs lists.
|
||||
// Read once, which keeps `Dimensions` out of the bundle. Copied from
|
||||
// `deviceScale` in `internal/asset.ts`, as sharing it costs `Image` apps more.
|
||||
const scaled = Subscription.make(
|
||||
"VirtualList.scale",
|
||||
Stream.fromEffect(Native.Device.useSync((device) => {
|
||||
@@ -286,28 +284,27 @@ const following = (model: Model, props: VirtualListProps): boolean =>
|
||||
// above it changes.
|
||||
const anchoredOf = (props: VirtualListProps): boolean => props.measure !== false || props.keys !== undefined
|
||||
|
||||
// Whether native landed on a scroll target: on the pixel it rounds the
|
||||
// unrounded target to. Android converts the target in float32, and far down a
|
||||
// list reports the landing as a float32 point, so compare pixels.
|
||||
// Whether native landed on the pixel it rounds `target` to. Android converts
|
||||
// the target and reports the landing in float32.
|
||||
const landed = ({ scale }: Model, offset: number, target: number): boolean =>
|
||||
Math.round(offset * scale) === Math.round(Math.fround(Math.fround(target) * scale))
|
||||
|
||||
// The row whose native placement maps the core's layout to native's: a
|
||||
// waiting request's target, which its plan's window holds, so a jump aims
|
||||
// from that row's own pixel, or else the window's first row.
|
||||
// waiting request's target, so a jump aims from its own pixel, or else the
|
||||
// window's first row.
|
||||
const placedOf = ({ request, window }: Virtualizer.Plan): number => request?.index ?? window[0]
|
||||
|
||||
// Whether a layout report is for the items the list renders now, rather than
|
||||
// a late one from before a dataset or key change.
|
||||
// Whether a layout report is current, not a late one from before a dataset
|
||||
// or key change.
|
||||
const currentOf = (
|
||||
plan: Virtualizer.Plan,
|
||||
props: VirtualListProps,
|
||||
{ epoch, index, key }: typeof Measurement.Type
|
||||
): boolean => epoch === plan.epoch && index < plan.count && key === Virtualizer.keyOf(props, index)
|
||||
|
||||
// Where native has the start of the core's layout. Measured from where
|
||||
// native placed the row from `placedOf`, since the core's estimates for the
|
||||
// items before it can change before native lays out the spacer they size.
|
||||
// Where native has the start of the core's layout, from where it placed the
|
||||
// `placedOf` row. Estimates above that row can change before native lays out
|
||||
// their spacer.
|
||||
const shiftOf = (
|
||||
model: Model,
|
||||
props: VirtualListProps,
|
||||
@@ -386,12 +383,12 @@ const aim = (
|
||||
const target = Virtualizer.targetOf(model, props, waiting)
|
||||
if (target !== null && waiting.target._tag === "End") to = -1
|
||||
else if (target !== null) {
|
||||
// Far down a list, native's float layout drifts from the plan by a
|
||||
// fraction of a point, so aim from where native put the window. A
|
||||
// larger difference is a layout native hasn't applied yet.
|
||||
// Far down a list native's float layout drifts from the plan by under
|
||||
// a point, so aim from where native put the row. A larger shift is a
|
||||
// layout native hasn't applied yet.
|
||||
const origin = originOf(model, props)
|
||||
const shift = shiftOf(model, props)
|
||||
// Unrounded, so native rounds it to its own pixels.
|
||||
// Unrounded: native rounds it to its pixels.
|
||||
to = Math.max(0, (Math.abs(shift - origin) < 1 ? shift : origin) + target)
|
||||
}
|
||||
}
|
||||
@@ -528,9 +525,8 @@ export const VirtualList = Native.component({
|
||||
Virtualizer.same(model.content, message.content) &&
|
||||
Virtualizer.same(model.viewport, message.viewport) &&
|
||||
landed(model, model.scrollOffset, target)
|
||||
// Short of a waiting request's target, aim again. Android runs a
|
||||
// scroll before the mount it was aimed for, then anchors that
|
||||
// mount's shift on top of it.
|
||||
// Short of a waiting target, aim again: Android can run the scroll
|
||||
// before its mount, then anchor the mount's shift on top.
|
||||
const missed = away && !leftTarget &&
|
||||
Virtualizer.waitingOf(model, props) !== null
|
||||
const current = leftTarget
|
||||
@@ -575,8 +571,8 @@ export const VirtualList = Native.component({
|
||||
case "LaidOutItem": {
|
||||
const { at, first, index, key, size } = message
|
||||
const id = key ?? String(index)
|
||||
// Native reports a mount's layouts before the scroll it adjusts in
|
||||
// that mount, such as for a prepend, so the placed row moves now.
|
||||
// Native reports a mount's layouts before its scroll adjustment, such
|
||||
// as for a prepend, so move the placed row now.
|
||||
const placed = model.placed?.key === id &&
|
||||
currentOf(Virtualizer.plan(model, props), props, message)
|
||||
? { ...model.placed, index, at }
|
||||
@@ -721,7 +717,6 @@ export const VirtualList = Native.component({
|
||||
rect: { readonly width: number; readonly height: number }
|
||||
) => horizontal ? rect.width : rect.height
|
||||
|
||||
// Native pins the headers where the build has the view.
|
||||
const docks: boolean = !horizontal &&
|
||||
(globalThis as any).__nativeComponentRegistry__hasComponent?.("EffectUIStickyHeader")
|
||||
if (!horizontal && !docks && props.sticky?.length) dev.unpinnedSticky()
|
||||
|
||||
@@ -181,10 +181,9 @@ describe("VirtualList", () => {
|
||||
assert.deepStrictEqual(list.scrolls(), [20 + 500 * 40])
|
||||
assert.include(list.children(), " 500 ")
|
||||
|
||||
// Every item measured so far is 80.125 tall, so the rest are taken to be
|
||||
// too. The target keeps its fraction, which native rounds to its pixels.
|
||||
await list.measure(500, 20 + 500 * 40, 80.125)
|
||||
const target = 20 + 500 * 80.125
|
||||
// Every item measured so far is 80 tall, so the rest are taken to be too.
|
||||
await list.measure(500, 20 + 500 * 40, 80)
|
||||
const target = 20 + 500 * 80
|
||||
assert.deepStrictEqual(list.scrolls(), [20 + 500 * 40, target])
|
||||
|
||||
// Native can land elsewhere, as Android does when it runs the scroll
|
||||
@@ -192,12 +191,11 @@ describe("VirtualList", () => {
|
||||
await list.scrollTo(target - 3000, 40020)
|
||||
assert.deepStrictEqual(list.scrolls(), [20 + 500 * 40, target, target])
|
||||
|
||||
// Far down a list, native's float layout drifts from the plan, also
|
||||
// between rows. Once the target reports where native put it, the list
|
||||
// aims there, not from the window's first row.
|
||||
// Native's layout drifts from the plan, also between rows, so the list
|
||||
// aims from where the target is, not the window's first row.
|
||||
const first = Number(list.children().split(" ")[2])
|
||||
await list.measure(first, 20 + first * 80.125 + 0.3, 80.125)
|
||||
await list.measure(500, target + 0.6, 80.125)
|
||||
await list.measure(first, 20 + first * 80 + 0.3, 80)
|
||||
await list.measure(500, target + 0.6, 80)
|
||||
await list.scrollTo(target, 40020)
|
||||
assert.deepStrictEqual(list.scrolls().at(-1), target + 0.6)
|
||||
const aimed = list.scrolls().length
|
||||
@@ -210,64 +208,31 @@ describe("VirtualList", () => {
|
||||
await list.close()
|
||||
})
|
||||
|
||||
// Android converts a target to pixels in float32 and reports where it
|
||||
// landed as a float32 point.
|
||||
it.each(
|
||||
[
|
||||
{ scale: 1, offset: 100.5, landings: [[121, true]] },
|
||||
{ scale: 2, offset: 100.5, landings: [[121, false]] },
|
||||
{
|
||||
scale: 2.625,
|
||||
offset: 240059.43,
|
||||
landings: [[Math.fround(630208 / 2.625), false], [Math.fround(630209 / 2.625), true]]
|
||||
}
|
||||
] as const
|
||||
)(
|
||||
"takes a landing on the pixel native rounds the target to as arrived, at scale $scale",
|
||||
async ({ landings, offset, scale }) => {
|
||||
const device = FakeDevice.make("android", {
|
||||
DeviceInfo: FakeDevice.module({
|
||||
getConstants: () => ({
|
||||
Dimensions: { window: { width: 300, height: 400, scale, fontScale: 1 } }
|
||||
})
|
||||
}).module
|
||||
}).device
|
||||
const scrollTo = {
|
||||
target: Virtualizer.ScrollTarget.Offset({ offset })
|
||||
}
|
||||
const list = await setup({ count: 7000, itemSize: 40, scrollTo }, device)
|
||||
await list.layOut()
|
||||
const target = 20 + offset
|
||||
assert.strictEqual(list.scrolls().at(-1), target)
|
||||
for (const [landing, arrived] of landings) {
|
||||
const sent = list.scrolls().length
|
||||
await list.scrollTo(landing, 280020)
|
||||
// A miss aims again. After an arrival, a wheel drops the request.
|
||||
if (!arrived) assert.deepStrictEqual(list.scrolls().slice(sent), [target])
|
||||
else {
|
||||
await list.scrollTo(landing + 40, 280020)
|
||||
assert.strictEqual(list.scrolls().length, sent)
|
||||
}
|
||||
}
|
||||
await list.close()
|
||||
it("re-aims at an unrounded target until native lands on the pixel it rounds it to", async () => {
|
||||
// Android converts a target to pixels in float32 and reports where it
|
||||
// landed as a float32 point.
|
||||
const device = FakeDevice.make("android", {
|
||||
DeviceInfo: FakeDevice.module({
|
||||
getConstants: () => ({
|
||||
Dimensions: { window: { width: 300, height: 400, scale: 2.625, fontScale: 1 } }
|
||||
})
|
||||
}).module
|
||||
}).device
|
||||
const scrollTo = {
|
||||
target: Virtualizer.ScrollTarget.Offset({ offset: 240059.43 })
|
||||
}
|
||||
)
|
||||
|
||||
it("ignores a late layout from before a dataset change for where native placed a row", async () => {
|
||||
const keys = Array.from({ length: 1000 }, (_, i) => "k" + i)
|
||||
const list = await setup({ keys, itemSize: 40 })
|
||||
const list = await setup({ count: 7000, itemSize: 40, scrollTo }, device)
|
||||
await list.layOut()
|
||||
await list.scrollTo(2020)
|
||||
await list.measure(47, 1900, 40)
|
||||
assert.strictEqual(list.events[list.events.length - 1]!.first, 50)
|
||||
// The old dataset's layout of a reused key arrives after the change.
|
||||
list.root.send(Message.Set({ props: { keys, itemSize: 40, dataset: "b" } }))
|
||||
list.fabric.emit(list.row(47), "topLayout", {
|
||||
layout: { x: 0, y: 2700, width: 300, height: 40 }
|
||||
})
|
||||
await list.settle()
|
||||
await list.scrollTo(2021)
|
||||
assert.strictEqual(list.events[list.events.length - 1]!.first, 50)
|
||||
const target = 20 + 240059.43
|
||||
assert.strictEqual(list.scrolls().at(-1), target)
|
||||
const sent = list.scrolls().length
|
||||
// Pixel 630208 is a miss, so it aims again.
|
||||
await list.scrollTo(Math.fround(630208 / 2.625), 280020)
|
||||
assert.deepStrictEqual(list.scrolls().slice(sent), [target])
|
||||
// Pixel 630209 is the target, so a wheel after it drops the request.
|
||||
await list.scrollTo(Math.fround(630209 / 2.625), 280020)
|
||||
await list.scrollTo(Math.fround(630209 / 2.625) + 40, 280020)
|
||||
assert.strictEqual(list.scrolls().length, sent + 1)
|
||||
await list.close()
|
||||
})
|
||||
|
||||
@@ -295,7 +260,6 @@ describe("VirtualList", () => {
|
||||
"renders headers as the native header view where the build has it, and anchors natively (native header: %s)",
|
||||
{ concurrent: false },
|
||||
async (nativeDock) => {
|
||||
// React Native's registry of the components the build has.
|
||||
const registry = globalThis as {
|
||||
__nativeComponentRegistry__hasComponent?: (name: string) => boolean
|
||||
}
|
||||
@@ -320,16 +284,8 @@ describe("VirtualList", () => {
|
||||
assert.strictEqual(list.row(100).viewName, header)
|
||||
assert.strictEqual(list.row(400).viewName, header)
|
||||
assert.strictEqual(list.row(399).viewName, "RCTView")
|
||||
// Without the view, a header is an ordinary row at its own offset.
|
||||
assert.isUndefined(list.row(100).props.top)
|
||||
// Native anchoring keeps the rows in place in the mount that moves
|
||||
// them, and headers mount after the rows, so it never anchors on one.
|
||||
assert.deepStrictEqual(
|
||||
list.scroll().props.maintainVisibleContentPosition,
|
||||
{
|
||||
minIndexForVisible: 0
|
||||
}
|
||||
)
|
||||
// Headers mount after the rows, so native anchoring never picks one.
|
||||
assert.deepStrictEqual(list.scroll().props.maintainVisibleContentPosition, { minIndexForVisible: 0 })
|
||||
await list.setProps({
|
||||
keys: ["prepended", ...keys],
|
||||
itemSize: 40,
|
||||
@@ -368,21 +324,29 @@ describe("VirtualList", () => {
|
||||
await list.close()
|
||||
})
|
||||
|
||||
it("keeps the visible row when native's scroll for a prepend arrives before its layouts apply", async () => {
|
||||
it("keeps the visible row when native's scroll for a prepend arrives before its layouts apply, but not for a late layout from before a dataset change", async () => {
|
||||
const keys = Array.from({ length: 1000 }, (_, i) => "k" + i)
|
||||
const list = await setup({ keys, itemSize: 40 })
|
||||
await list.layOut()
|
||||
await list.scrollTo(2020)
|
||||
await list.measure(47, 1900, 40)
|
||||
assert.strictEqual(list.events[list.events.length - 1]!.first, 50)
|
||||
const prepended = Array.from({ length: 20 }, (_, i) => "p" + i)
|
||||
await list.setProps({ keys: [...prepended, ...keys], itemSize: 40 })
|
||||
const prepended = [...Array.from({ length: 20 }, (_, i) => "p" + i), ...keys]
|
||||
await list.setProps({ keys: prepended, itemSize: 40 })
|
||||
// Native moves row k47 down by the prepended rows and keeps it in place.
|
||||
list.fabric.emit(list.row(67), "topLayout", {
|
||||
layout: { x: 0, y: 2700, width: 300, height: 40 }
|
||||
})
|
||||
await list.scrollTo(2820, 40820)
|
||||
assert.strictEqual(list.events[list.events.length - 1]!.first, 70)
|
||||
|
||||
// The old dataset's layout of a reused key arrives after the change.
|
||||
list.root.send(Message.Set({ props: { keys: prepended, itemSize: 40, dataset: "b" } }))
|
||||
list.fabric.emit(list.row(67), "topLayout", {
|
||||
layout: { x: 0, y: 3500, width: 300, height: 40 }
|
||||
})
|
||||
await list.settle()
|
||||
await list.scrollTo(2821, 40820)
|
||||
assert.strictEqual(list.events[list.events.length - 1]!.first, 70)
|
||||
await list.close()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user