diff --git a/apps/app/src/assistant/follow.tsx b/apps/app/src/assistant/follow.tsx index 24c02d0fd2..39d8c5aaeb 100644 --- a/apps/app/src/assistant/follow.tsx +++ b/apps/app/src/assistant/follow.tsx @@ -145,7 +145,9 @@ function ghostOf(section: HTMLElement, composer: boolean) { } /** Ends a sheet's motions where they stand. */ -function stop(motions: readonly Animation[]) { +function stop(owner: RefObject) { + const motions = owner.current; + owner.current = []; for (const motion of motions) { motion.finished.catch(() => undefined); motion.cancel(); @@ -327,14 +329,24 @@ export function useSheetFollow({ send, kept, canvas, detent, pill, comms, voice, if (!held) return; const first = !held.drag.moved; if (first && voice) return; + const opening = first && held.drag.open && motionsRef.current.length > 0 ? sectionRef.current : null; + if (opening) { + const transform = getComputedStyle(opening).transform; + const offset = transform === "none" ? 0 : new DOMMatrixReadOnly(transform).m42; + held.drag.start = opening.getBoundingClientRect().height - offset; + } if (!moveDrag(held.drag, y - held.y)) return; + if (opening) stop(motionsRef); if (first) void send({ type: "drag" }); const drawn = held.form === "open" || held.drag.height > 0; const hold = comms || !animates() ? undefined : Math.max(held.drag.full, held.drag.floor); if (first || drawn !== live?.drawn) { unhold(sectionRef.current); // eslint-disable-next-line @eslint-react/dom-no-flush-sync -- v119 drew the sheet for the first move before the next frame (chat-overlay-motion-adapter.ts:836), so the bar never shows under the finger's first paint - flushSync(() => setLive({ form: held.form, start: held.start, drawn, painted: dragPaint(held.drag), hold })); + flushSync(() => { + if (opening) setSeen((s) => ({ ...s, growing: undefined })); + setLive({ form: held.form, start: held.start, drawn, painted: dragPaint(held.drag), hold }); + }); held.painting = undefined; if (drawn && hold !== undefined) sectionRef.current?.setAttribute("data-alpha-sliding", ""); } @@ -398,7 +410,7 @@ export function useSheetFollow({ send, kept, canvas, detent, pill, comms, voice, const animate = useCallback((element: HTMLElement | null) => { sectionRef.current = element; if (!element || !animates()) return; - stop(motionsRef.current.splice(0)); + stop(motionsRef); element.removeAttribute("data-alpha-sliding"); const drawnHeight = element.getBoundingClientRect().height; if (!growing && !handing) { @@ -422,10 +434,9 @@ export function useSheetFollow({ send, kept, canvas, detent, pill, comms, voice, const motions = [grow, ...rest, ...(header ? [header.animate(revealFrames(from, drawnHeight, fit), { duration: resize.duration, easing: "linear" })] : [])]; motionsRef.current = motions; const settled = () => { - if (motionsRef.current === motions) { - stop(motionsRef.current.splice(0)); - element.removeAttribute("data-alpha-sliding"); - } + if (motionsRef.current !== motions) return; + stop(motionsRef); + element.removeAttribute("data-alpha-sliding"); finish(); }; grow?.finished.then(settled, settled); diff --git a/apps/app/test/dock.browser.test.tsx b/apps/app/test/dock.browser.test.tsx index ea29fc7ecf..d41a0be33d 100644 --- a/apps/app/test/dock.browser.test.tsx +++ b/apps/app/test/dock.browser.test.tsx @@ -300,13 +300,32 @@ describe("Ask Alpha dock", () => { expect(blackOnTheBar).toEqual([]); }); - const openHomeSheet = async () => { + const openHomeSheet = async (when: "settled" | "opening" = "settled") => { await page.viewport(390, 844); const { runtime } = await mountDock({ view: "Home" }); const section = () => document.querySelector("section[aria-label='Ask Alpha']"); - await page.getByRole("button", { name: "Open conversation" }).click(); - await shellReaches(runtime, (s) => s.assistant === "Sheet"); - await Effect.runPromise(Effect.sleep("600 millis")); + let paused = false; + const opening = new MutationObserver(() => { + const sheet = section(); + if (!sheet?.hasAttribute("data-alpha-sliding")) return; + const motions = sheet.getAnimations({ subtree: true }).filter((motion) => !(motion instanceof CSSAnimation || motion instanceof CSSTransition)); + if (motions.length === 0) return; + for (const motion of motions) { + motion.pause(); + motion.currentTime = 100; + } + paused = true; + opening.disconnect(); + }); + if (when === "opening") opening.observe(document.body, { subtree: true, childList: true, attributes: true }); + try { + await openSheetByMark(runtime); + await expect.element(page.getByRole("button", { name: "Resize chat" })).toBeVisible(); + if (when === "opening") await expect.poll(() => paused).toBe(true); + else await settled(page.getByRole("region", { name: "Ask Alpha" }).element()); + } finally { + opening.disconnect(); + } return { runtime, section }; }; @@ -425,14 +444,18 @@ describe("Ask Alpha dock", () => { }); // spec/app/assistant/dock.md, "Story: Drag Ask Alpha where I want it": as v119's (45f6bed30 chat-overlay-motion-adapter.ts:261-272, 1037-1039), an open sheet pulled down fades by how far it is into the zone above a floor of 62 and 28 points, where no bar is drawn at the press, and a flick down lets its header slide down onto the bar as it fades - it("Flick an open sheet down onto the bar as v119 drew it (spec/app/assistant/dock.md, \"Story: Drag Ask Alpha where I want it\")", async () => { - const { runtime, section } = await openHomeSheet(); + it.each(["settled", "opening"] as const)("Flick an open sheet down while %s (spec/app/assistant/dock.md, \"Story: Drag Ask Alpha where I want it\")", async (when) => { + const { runtime, section } = await openHomeSheet(when); const handle = section()?.querySelector("[aria-label='Resize chat']"); const box = handle?.getBoundingClientRect(); const y = Math.round((box?.top ?? 0) + (box?.height ?? 0) / 2); const finger = (type: string, clientY: number) => (handle?.isConnected ? handle : section())?.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, clientY, pointerId: 13, pointerType: "touch", isPrimary: true })); const header = () => section()?.querySelector("[data-alpha-chat-header]")?.getBoundingClientRect().top ?? 0; - const opened = section()?.getBoundingClientRect().height ?? 0; + const visibleHeight = () => { + const box = page.getByRole("region", { name: "Ask Alpha" }).element().getBoundingClientRect(); + return Math.min(box.bottom, window.innerHeight) - box.top; + }; + const opened = visibleHeight(); const floor = 62 + 28; const v119Opacity = (height: number) => 1 - Math.min(1, Math.max(0, (floor + 120 - height) / 120)); @@ -443,8 +466,8 @@ describe("Ask Alpha dock", () => { by += 20; await pause(10); finger("pointermove", y + by); - const box = section()?.getBoundingClientRect(); - const height = box === undefined ? 0 : Math.min(box.bottom, window.innerHeight) - box.top; + const height = visibleHeight(); + expect(Math.abs(height - (opened - by))).toBeLessThanOrEqual(2); drawn.push({ height: Math.round(height), opacity: Number(getComputedStyle(section() as HTMLElement).opacity), v119: v119Opacity(height) }); } const inZone = drawn.filter(({ height }) => height < floor + 120);