diff --git a/packages/app/src/session/timeline/row-growth.test.ts b/packages/app/src/session/timeline/row-growth.test.ts index fc6cca97458..59f8f8907b3 100644 --- a/packages/app/src/session/timeline/row-growth.test.ts +++ b/packages/app/src/session/timeline/row-growth.test.ts @@ -29,6 +29,32 @@ async function settle(growth: ReturnType, frames: { held } describe("row growth", () => { + test.each([false, true])("closes a neighbour while a new row grows (close first: %s)", async (closeFirst) => { + const sizes = new Map() + const frame = Promise.withResolvers() + const growth = createRowGrowth({ + measure: () => {}, + resize: (key, size) => { + sizes.set(key, size) + }, + below: () => true, + settle: () => frame.resolve(), + }) + if (closeFirst) growth.grow("closing", 100, 24) + growth.grow("arriving", 0, 450) + if (!closeFirst) growth.grow("closing", 100, 24) + + try { + await frame.promise + expect(sizes.get("arriving")).toBeGreaterThan(0) + expect(sizes.get("arriving")).toBeLessThan(450) + expect(sizes.get("closing")).toBeLessThan(100) + expect(sizes.get("closing")).toBeGreaterThan(24) + } finally { + growth.finish() + } + }) + test("eases a row on the layout grid, behind a soft edge that clears when it lands", async () => { const { growth, sizes, wrapper, frames } = setup() growth.grow("row", 0, 120.5) diff --git a/packages/app/src/session/timeline/row-growth.ts b/packages/app/src/session/timeline/row-growth.ts index 32c142c77b4..8a37d924da5 100644 --- a/packages/app/src/session/timeline/row-growth.ts +++ b/packages/app/src/session/timeline/row-growth.ts @@ -194,7 +194,7 @@ export function createRowGrowth(input: { if (!growth.entering || !wrapper) growths.delete(key) return } - if (stepped) { + if (stepped && diff >= 0) { if (growth.coupled && growth.coupled.gaps.length > 0) { const shown = grid(growth.value) const ratio = Math.max(0, Math.min(1, 1 - shown / Math.max(1, growth.target))) @@ -247,10 +247,12 @@ export function createRowGrowth(input: { maxTake = grid(room) growth.freshRow = true } - const rowMaxTake = maxTake / travelFactor + // A complete arriving row follows the duration spring. Per-frame stream caps made a long sent prompt take + // seconds to open at lower refresh rates, long after its flight had arrived. + const rowMaxTake = !growth.streaming && !coupled && diff > 0 ? Infinity : maxTake / travelFactor const take = rem <= 0 ? 0 : Math.min(rem, rawBudget, done && rem <= rowMaxTake ? rem : rowMaxTake) - stepped = true - consumed = take + stepped ||= diff > 0 + consumed += take lastTake = take * travelFactor const nextRowValue = rem <= 0 ? growth.value : growth.value + Math.sign(diff) * take const rowDone = @@ -356,10 +358,14 @@ export function createRowGrowth(input: { const stepDt = fresh ? 1 / 60 : Math.min(dt, 1 / 30) fresh = false batch(() => advance(stepDt)) - // Wait briefly before closing what growth left of the gap so pauses between streamed chunks do not start and stop it. + // A send's freed composer space closes alongside its arriving row. Only a streaming tail waits briefly, so pauses + // between chunks do not repeatedly start and stop the gap. quiet = growths.size ? 0 : quiet + stepDt - if (held && (closing || quiet >= 0.12)) { + if ( + held && + (closing || quiet >= 0.12 || [...growths.values()].some((growth) => growth.entering && !growth.streaming)) + ) { closing = true held.tune(growthSpring()) held.step(stepDt) diff --git a/packages/app/src/session/timeline/send-flight.ts b/packages/app/src/session/timeline/send-flight.ts index 3148b2ac11d..99e2a520f2e 100644 --- a/packages/app/src/session/timeline/send-flight.ts +++ b/packages/app/src/session/timeline/send-flight.ts @@ -22,7 +22,7 @@ const waiting = new Map() // A bubble that mounted before its flight was prepared, as when a queued prompt's row leaves the queue a moment after // its message reaches the timeline. A flight prepared soon after still lands on it. -const unclaimed = new Map() +const unclaimed = new Map number }>() /** * Holds a copy of the composer's text, or a queued prompt's row, where it is for the bubble that `id` becomes. A @@ -99,17 +99,17 @@ export function prepareSendFlight(id: string, editor: HTMLElement | undefined, s const early = unclaimed.get(id) unclaimed.delete(id) - if (early && performance.now() - early.at < 400 && early.bubble.isConnected) landSendFlight(id, early.bubble) + if (early && performance.now() - early.at < 400 && early.bubble.isConnected) landSendFlight(id, early.bubble, early.offset) } /** Flies the waiting copy for `id` onto `bubble`. Returns false when nothing was waiting for it. */ -export function landSendFlight(id: string, bubble: HTMLElement) { +export function landSendFlight(id: string, bubble: HTMLElement, offset?: () => number) { const flight = waiting.get(id) if (!flight) { const now = performance.now() unclaimed.forEach((entry, key) => now - entry.at >= 400 && unclaimed.delete(key)) - unclaimed.set(id, { bubble, at: now }) + unclaimed.set(id, { bubble, at: now, offset }) return false } @@ -160,8 +160,10 @@ export function landSendFlight(id: string, bubble: HTMLElement) { flight.source?.style.setProperty("visibility", "hidden") const spring = Spring.fromDuration(0, growthSpring()).set(1) + let fresh = true const radius = style.borderRadius let rect = bubble.getBoundingClientRect() + let remaining = offset?.() ?? 0 // A prompt that wraps differently in its bubble crosses over quickly, so the two wraps barely overlap. const rewraps = Math.abs(rect.height - top - bottom - flight.textHeight) > 2 let clip = viewport.getBoundingClientRect() @@ -175,6 +177,7 @@ export function landSendFlight(id: string, bubble: HTMLElement) { frameLoop.addMeasured( () => { rect = bubble.getBoundingClientRect() + remaining = offset?.() ?? 0 clip = viewport.getBoundingClientRect() pending = bubble.closest('[data-component="user-message"]')?.hasAttribute("data-pending") ?? false }, @@ -186,14 +189,15 @@ export function landSendFlight(id: string, bubble: HTMLElement) { } // A delayed frame must not skip most of a new flight before its first paint. - const elapsed = Math.min(dt, 1 / 60) + const elapsed = fresh ? 1 / 60 : Math.min(dt, 1 / 30) + fresh = false spring.tune(growthSpring()) const p = Math.min(1, Math.max(0, spring.step(elapsed))) flight.layer.style.visibility = "visible" host.toggleAttribute("data-pending", pending) const at = { x: start.x + (rect.left - start.x) * p, - y: start.y + (rect.top - start.y) * p, + y: start.y + (rect.top + remaining - start.y) * p, w: start.w + (rect.width - start.w) * p, h: start.h + (rect.height - start.h) * p, } @@ -212,7 +216,9 @@ export function landSendFlight(id: string, bubble: HTMLElement) { flight.text.style.transform = `translate3d(${at.x + left - clip.left}px, ${at.y + top - flight.above - clip.top}px, 0)` flight.text.style.opacity = `${Math.max(0, 1 - p / (rewraps ? 0.25 : 0.4))}` - if (!spring.resting) return true + // The flight aims at the resting slot while neighbouring rows close. Reveal the real bubble only when that + // slot has reached it, so landing cannot be followed by a second move. + if (!spring.resting || Math.abs(remaining) > 1 / 64) return true land() return false diff --git a/packages/app/src/session/timeline/virtualizer.tsx b/packages/app/src/session/timeline/virtualizer.tsx index 4265173654c..eb8f5175fce 100644 --- a/packages/app/src/session/timeline/virtualizer.tsx +++ b/packages/app/src/session/timeline/virtualizer.tsx @@ -767,6 +767,26 @@ export function createTimelineVirtualizer(input: Input) { return input.pinned() && !coldPending && touchStart === undefined && !prefersReducedMotion() } + // Project the resting slot from the size model, including room that closing rows are still giving back. The flight + // measures the real bubble once and adds this correction; it does not chase every intermediate layout position. + function landingOffset(key: string) { + if (!following()) return 0 + const index = rowKeys().lastIndexOf(key) + let before = 0 + let total = 0 + virtualizer.measurementsCache.forEach((item) => { + if (streaming(rows()[item.index])) return + const itemKey = String(item.key) + const target = pendingSizes.get(itemKey) ?? growth.target(itemKey) ?? modelHeight(itemKey) ?? item.size + const delta = target - item.size + total += delta + if (item.index < index) before += delta + }) + const end = Math.max(0, topOffset() + virtualizer.getTotalSize() + total + bottomInset() - layoutState.viewport) + const remainder = parseFloat(virtualContent?.style.translate.split(" ")[1] ?? "0") || 0 + return grid(before - (end - (deferredTop ?? layoutState.offset - remainder))) + } + // A row near the end leaves while the timeline follows it: the neighbour row absorbs its height and closes the gap on // the shared growth spring, while the original row stays mounted until its fade or Working handoff finishes. function closeRemoved(previous: string[], keys: string[]) { @@ -1819,7 +1839,7 @@ export function createTimelineVirtualizer(input: Input) { queueMicrotask(() => { const bubble = value.querySelector('[data-slot="user-message-text"]') - if (bubble) landSendFlight(mounted.userMessageID, bubble) + if (bubble) landSendFlight(mounted.userMessageID, bubble, () => landingOffset(rowProps.rowKey)) }) if (