An open-source, self-hosted system for apps built with web technologies.
Motions start at once again
zohayrslilehae1427e545c0
diff --git a/source/client/view/structure/desktop/drawn.ts b/source/client/view/structure/desktop/drawn.tsdeleted file mode 100644index 4b1cad8..0000000--- a/source/client/view/structure/desktop/drawn.ts+++ /dev/null@@ -1,45 +0,0 @@-import { useEffect, useState } from "react"--/**- * Runs `callback` once what changed now has been laid out and drawn: the frame after the next one.- *- * A motion starts its clock when it starts. Started together with work that reveals or lays out- * content, it would spend its first frames waiting for that work and appear already late, often at- * its end. Safari makes this common: it lays out and draws Program frames on the Desktop's own- * thread. So a motion starts once the change it shows is on the screen. Returns a cancel.- */-export function afterDrawn(callback: () => void): () => void {-- let handle = 0-- handle = requestAnimationFrame(() => {- handle = requestAnimationFrame(() => {- handle = 0- callback()- })- })-- return () => cancelAnimationFrame(handle)-}--/**- * The value as last drawn: a new value takes effect once the change that brought it is on the- * screen (see `afterDrawn`). Give it to what starts a motion, such as a pose, never to what lays- * out the content the motion reveals. `immediate` takes a new value at once, where nothing moves.- */-export function useDrawn<Value>(value: Value, { initial = value, immediate = false }: Readonly<{ initial?: Value, immediate?: boolean }> = {}): Value {-- const [drawn, setDrawn] = useState(initial)-- useEffect(function () {-- if (Object.is(drawn, value)) return-- if (immediate) return setDrawn(value)-- return afterDrawn(() => setDrawn(value))-- }, [value, drawn, immediate])-- return immediate ? value : drawn-}diff --git a/source/client/view/structure/desktop/layers/shell/shell-popover.ts b/source/client/view/structure/desktop/layers/shell/shell-popover.tsindex a74225b..379005b 100644--- a/source/client/view/structure/desktop/layers/shell/shell-popover.ts+++ b/source/client/view/structure/desktop/layers/shell/shell-popover.ts@@ -1,7 +1,6 @@import { useCallback, useEffect, useRef, useState } from "react"import { useReducedMotion } from "@libs/react-motion"import { surfaceLifecyclePose } from "@client/view/appearance/surface-presence"-import { useDrawn } from "../../drawn"/*** A Shell surface opened from its Taskbar button, such as the Start Menu or the map: a popover, so@@ -17,10 +16,9 @@ export function useShellPopover(onOpenChange?: (open: boolean) => void) {const surface = useRef<HTMLDivElement>(null)const [open, setOpenState] = useState(false)const reducedMotion = useReducedMotion()- const shown = useDrawn(open, { immediate: reducedMotion })- const current = useRef({ open, shown, reducedMotion, onOpenChange })+ const current = useRef({ open, reducedMotion, onOpenChange })- current.current = { open, shown, reducedMotion, onOpenChange }+ current.current = { open, reducedMotion, onOpenChange }const setOpen = useCallback(function (next: boolean) {@@ -56,8 +54,8 @@ export function useShellPopover(onOpenChange?: (open: boolean) => void) {setOpen(false)- // Not yet entered, or without motion, it has no leaving to wait for.- if (!current.current.shown || current.current.reducedMotion) hide()+ // Without motion, it has no leaving to wait for.+ if (current.current.reducedMotion) hide()}, [setOpen, hide])const toggle = useCallback(function () {@@ -118,7 +116,7 @@ export function useShellPopover(onOpenChange?: (open: boolean) => void) {motion: {popover: "manual" as const,initial: false as const,- animate: shown ? surfaceLifecyclePose.visible : surfaceLifecyclePose.hidden,+ animate: open ? surfaceLifecyclePose.visible : surfaceLifecyclePose.hidden,onAnimationComplete: hide}}diff --git a/source/client/view/structure/desktop/windows/window-geometry-animation.ts b/source/client/view/structure/desktop/windows/window-geometry-animation.tsindex 5c5f4fb..3492985 100644--- a/source/client/view/structure/desktop/windows/window-geometry-animation.ts+++ b/source/client/view/structure/desktop/windows/window-geometry-animation.ts@@ -2,7 +2,6 @@ import type { WindowRegion } from "@client/view/components/window-manager/windowimport type { Transaction } from "@phreshos/core"import { animate, type AnimationPlaybackControls, type MotionValue } from "motion/react"import { motionTransition } from "@client/view/appearance/motion"-import { afterDrawn } from "../drawn"const axes = ["x", "y", "width", "height"] as constconst sizeAxes = ["width", "height"] as const@@ -19,7 +18,6 @@ export class WindowGeometryAnimation {private flights = new Map<Axis, Flight>()private complete?: () => voidprivate updating = false- private waiting: (() => void) | null = nullconstructor(private values: Record<Axis, MotionValue<number>>,@@ -29,7 +27,6 @@ export class WindowGeometryAnimation {stop() {this.complete = undefined- this.cancelWait()const flights = [...this.flights.values()]this.flights.clear()for (const flight of flights) flight.control?.stop()@@ -79,36 +76,13 @@ export class WindowGeometryAnimation {}this.complete = complete- this.cancelWait()+ this.updating = true+ const timing = JSON.stringify([transaction.duration, transaction.easing])// A nonzero backing viewport permits scaling to or from zero size.// Only these destination changes reflow content, not every tween frame.- const width = region.width || Math.max(this.values.width.get(), 1)- const height = region.height || Math.max(this.values.height.get(), 1)- const reflows = !this.flights.size && (width !== this.layout.width.get() || height !== this.layout.height.get())- this.layout.width.set(width)- this.layout.height.set(height)-- // Starting from rest, the tween waits for the content laid out anew to be drawn; a running- // one retargets at once.- if (reflows) {- let drawn = false- const cancel = afterDrawn(() => {- drawn = true- this.waiting = null- this.fly(region, transaction, animatedAxes)- })- if (!drawn) this.waiting = cancel- return- }-- this.fly(region, transaction, animatedAxes)- }-- private fly(region: WindowRegion, transaction: Transaction, animatedAxes: readonly Axis[]) {-- this.updating = true- const timing = JSON.stringify([transaction.duration, transaction.easing])+ this.layout.width.set(region.width || Math.max(this.values.width.get(), 1))+ this.layout.height.set(region.height || Math.max(this.values.height.get(), 1))for (const axis of animatedAxes) {@@ -139,18 +113,13 @@ export class WindowGeometryAnimation {private finish() {- if (this.updating || this.waiting || this.flights.size) return+ if (this.updating || this.flights.size) returnthis.settleLayout()const complete = this.completethis.complete = undefinedcomplete?.()}- private cancelWait() {- this.waiting?.()- this.waiting = null- }-private settleLayout() {this.layout.width.set(this.values.width.get())this.layout.height.set(this.values.height.get())diff --git a/source/client/view/structure/desktop/windows/window-surface.tsx b/source/client/view/structure/desktop/windows/window-surface.tsxindex 868b76d..0d6d857 100644--- a/source/client/view/structure/desktop/windows/window-surface.tsx+++ b/source/client/view/structure/desktop/windows/window-surface.tsx@@ -8,7 +8,6 @@ import { surfaceLifecyclePose, surfacePresenceTransition } from "@client/view/apimport { resolvePresentationTransaction } from "@client/view/appearance/motion"import { floatingShadow } from "@client/view/appearance/floating-shadow"import { useAppearance, useTiming } from "@phreshos/react-ui"-import { useDrawn } from "../drawn"/** Paints the optional Desktop-owned backing surface for a supported presentation. */export default function WindowSurface({ surface, active = true, floating = false, animation, onComplete }: WindowSurfaceProps) {@@ -26,9 +25,6 @@ export default function WindowSurface({ surface, active = true, floating = falseif (surface !== false) retained.current = surface- // Its motion starts once the change it shows is drawn.- const shown = useDrawn(visible, { initial: animated && visible && !reducedMotion ? false : visible, immediate: reducedMotion || !animated })-useLayoutEffect(function () {if (visible) setHidden(false)@@ -52,7 +48,7 @@ export default function WindowSurface({ surface, active = true, floating = falsereturn <motion.divinitial={animated && visible && !reducedMotion ? surfaceLifecyclePose.hidden : surfaceLifecyclePose.visible}- animate={shown ? surfaceLifecyclePose.visible : surfaceLifecyclePose.hidden}+ animate={visible ? surfaceLifecyclePose.visible : surfaceLifecyclePose.hidden}transition={animated? surfacePresenceTransition(reducedMotion, transaction): { duration: 0 }}diff --git a/source/client/view/structure/desktop/windows/window.tsx b/source/client/view/structure/desktop/windows/window.tsxindex 49c0fe0..2af54bc 100644--- a/source/client/view/structure/desktop/windows/window.tsx+++ b/source/client/view/structure/desktop/windows/window.tsx@@ -17,7 +17,6 @@ import SnapPreview, { type SnapTarget } from "./snap-preview"import useWindowGeometryMotion from "./window-geometry-motion"import WindowGestureCommit from "./window-gesture-commit"import { physicalToDesktopPixels, useDesktopScale } from "../desktop-scale"-import { useDrawn } from "../drawn"/*** One drawing on the Desktop: a standard Window, which the Desktop designs, or a Program's own drawing@@ -283,19 +282,12 @@ export default function ({ title, header = true, surface, layer, icon, children,? opening ? windowSurfaceLifecyclePose.hidden : windowSurfaceLifecyclePose.visible: opening ? surfaceLifecyclePose.hidden : surfaceLifecyclePose.visible- // Each presence motion starts once the change it shows is drawn; the state itself is current.- const shownClosing = useDrawn(closing, { immediate: reducedMotion })- const shownMinimized = useDrawn(minimized, { immediate: reducedMotion })- const arrived = useDrawn(true, { initial: opening === null, immediate: reducedMotion })-- const presencePose = shownClosing && standard+ const presencePose = closing && standard? windowSurfaceLifecyclePose.hidden- : shownMinimized+ : minimized// Only a standard Window is ever minimized.? windowMinimizePose(taskbarPosition)- : !arrived- ? initialPresence- : standard ? windowSurfaceLifecyclePose.visible : surfaceLifecyclePose.visible+ : standard ? windowSurfaceLifecyclePose.visible : surfaceLifecyclePose.visibleconst presenceTransition = reducedMotion? { duration: 0 }diff --git a/tests/window-geometry-animation.test.ts b/tests/window-geometry-animation.test.tsindex 2020aa7..ca7fc8b 100644--- a/tests/window-geometry-animation.test.ts+++ b/tests/window-geometry-animation.test.ts@@ -20,26 +20,11 @@ const runs: { stop: ReturnType<typeof vi.fn>, progress: (fraction: number) => voconst timing = { duration: 120, easing: "ease-out" } as constconst initial = { x: 10, y: 20, width: 300, height: 200 }-// Animation frames run at once unless a test holds them.-let frames: FrameRequestCallback[] | null = null-beforeEach(() => {runs.length = 0vi.mocked(animate).mockClear()- frames = null- vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {- if (frames) return frames.push(callback)- callback(0)- return 0- })- vi.stubGlobal("cancelAnimationFrame", (handle: number) => { if (frames && handle > 0) frames[handle - 1] = () => undefined })})-function drawFrame() {- const pending = frames!.splice(0)- for (const callback of pending) callback(0)-}-function create() {const values = {x: motionValue(initial.x), y: motionValue(initial.y),@@ -188,33 +173,3 @@ test("immediate geometry cancels pending completion and sets all axes", () => {expect(interrupted).not.toHaveBeenCalled()expect(complete).toHaveBeenCalledOnce()})--test("a resize from rest starts its tween once the new layout has been drawn", () => {- const { animator, layout } = create()- frames = []- animator.transition({ ...initial, width: 600, height: 400 }, timing)- expect(layout.width.get()).toBe(600)- expect(runs).toHaveLength(0)- drawFrame()- expect(runs).toHaveLength(0)- drawFrame()- expect(runs).toHaveLength(2)-})--test("a move from rest starts at once, since nothing is laid out anew", () => {- const { animator } = create()- frames = []- animator.transition({ ...initial, x: 200 }, timing)- expect(runs).toHaveLength(1)-})--test("stopping during the wait leaves the layout as shown and starts nothing", () => {- const { animator, values, layout } = create()- frames = []- animator.transition({ ...initial, width: 600 }, timing)- animator.stop()- drawFrame()- drawFrame()- expect(runs).toHaveLength(0)- expect(layout.width.get()).toBe(values.width.get())-})