Skip to content

An open-source, self-hosted system for apps built with web technologies.

Preserve unchanged window geometry animations

zohayrslileh029f0809a566
diff --git a/source/client/view/structure/desktop/windows/window-geometry-animation.ts b/source/client/view/structure/desktop/windows/window-geometry-animation.ts
new file mode 100644
index 0000000..511102e
--- /dev/null
+++ b/source/client/view/structure/desktop/windows/window-geometry-animation.ts
@@ -0,0 +1,83 @@
+import type { WindowRegion } from "@client/view/components/window-manager/window-geometry"
+import type { AppearanceTransaction } from "@phreshos/core"
+import { animate, type AnimationPlaybackControls, type MotionValue } from "motion/react"
+import { motionTransition } from "@client/view/appearance/motion"
+
+const axes = ["x", "y", "width", "height"] as const
+type Axis = typeof axes[number]
+type Flight = {
+ target: number
+ timing: string
+ control?: AnimationPlaybackControls
+}
+
+/** Retargets geometry without interrupting axes that are already travelling to their destination. */
+export class WindowGeometryAnimation {
+
+ private flights = new Map<Axis, Flight>()
+ private complete?: () => void
+ private updating = false
+
+ constructor(private values: Record<Axis, MotionValue<number>>) {}
+
+ stop() {
+
+ this.complete = undefined
+ const flights = [...this.flights.values()]
+ this.flights.clear()
+ for (const flight of flights) flight.control?.stop()
+ }
+
+ set(region: WindowRegion) {
+
+ this.stop()
+ for (const axis of axes) this.values[axis].set(region[axis])
+ }
+
+ transition(region: WindowRegion, transaction: AppearanceTransaction, complete?: () => void) {
+
+ if (transaction.duration === 0) {
+ this.set(region)
+ complete?.()
+ return
+ }
+
+ this.complete = complete
+ this.updating = true
+ const timing = JSON.stringify([transaction.duration, transaction.easing])
+
+ for (const axis of axes) {
+
+ const previous = this.flights.get(axis)
+ if (previous?.target === region[axis] && previous.timing === timing) continue
+
+ this.flights.delete(axis)
+ previous?.control?.stop()
+
+ const value = this.values[axis]
+ if (value.get() === region[axis]) continue
+
+ const flight: Flight = { target: region[axis], timing }
+ this.flights.set(axis, flight)
+ flight.control = animate(value, region[axis], {
+ ...motionTransition(transaction),
+ onComplete: () => {
+ if (this.flights.get(axis) !== flight) return
+ this.flights.delete(axis)
+ this.finish()
+ }
+ })
+ }
+
+ this.updating = false
+ this.finish()
+ }
+
+ private finish() {
+
+ if (this.updating || this.flights.size) return
+ const complete = this.complete
+ this.complete = undefined
+ complete?.()
+ }
+}
diff --git a/source/client/view/structure/desktop/windows/window-geometry-motion.ts b/source/client/view/structure/desktop/windows/window-geometry-motion.ts
index 20c6718..4d5310d 100644
--- a/source/client/view/structure/desktop/windows/window-geometry-motion.ts
+++ b/source/client/view/structure/desktop/windows/window-geometry-motion.ts
@@ -1,9 +1,9 @@
import { resolveWindowGeometry, type WindowRegion } from "@client/view/components/window-manager/window-geometry"
import { type LocalAnimation } from "@client/view/components/desktop-host/local-window"
import { type AppearanceTransaction, type Position, type Size } from "@phreshos/core"
-import { animate, useMotionValue, type AnimationPlaybackControls, type MotionStyle } from "motion/react"
+import { useMotionValue, type MotionStyle } from "motion/react"
import { useLayoutEffect, useRef } from "react"
-import { motionTransition } from "@client/view/appearance/motion"
+import { WindowGeometryAnimation } from "./window-geometry-animation"
import { useAppearance } from "@phreshos/react-ui"
interface WindowGeometryMotionOptions {
@@ -29,8 +29,8 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
const y = useMotionValue(typeof position.y === "number" ? position.y : 0)
const width = useMotionValue(typeof size.width === "number" ? size.width : 0)
const height = useMotionValue(typeof size.height === "number" ? size.height : 0)
- const controls = useRef<AnimationPlaybackControls[]>([])
- const target = useRef<WindowRegion | null>(null)
+ const animator = useRef<WindowGeometryAnimation | null>(null)
+ if (!animator.current) animator.current = new WindowGeometryAnimation({ x, y, width, height })
const gesturing = useRef(false)
const initialized = useRef(false)
const values = useRef({ position, size, animation, immediate, onComplete })
@@ -44,27 +44,17 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
function stop() {
- for (const control of controls.current) control.stop()
-
- controls.current = []
+ animator.current!.stop()
}
function set(region: WindowRegion) {
- stop()
- target.current = region
- x.set(region.x)
- y.set(region.y)
- width.set(region.width)
- height.set(region.height)
+ animator.current!.set(region)
}
function transition(region: WindowRegion, transaction: AppearanceTransaction = appearanceTransaction, complete?: () => void) {
- stop()
- target.current = region
-
- if (immediate || transaction?.duration === 0 || sameRegion(read(), region)) {
+ if (immediate) {
set(region)
complete?.()
@@ -72,19 +62,7 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
return
}
- const options = motionTransition(transaction)
-
- let remaining = 4
- const completed = () => {
- if (--remaining === 0) complete?.()
- }
- const timing = { ...options, onComplete: completed }
- controls.current = [
- animate(x, region.x, timing),
- animate(y, region.y, timing),
- animate(width, region.width, timing),
- animate(height, region.height, timing)
- ]
+ animator.current!.transition(region, transaction, complete)
}
function resolve() {
@@ -112,11 +90,6 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
return
}
- if (target.current && sameRegion(target.current, region) && sameRegion(read(), region)) {
- if (revision !== undefined) onComplete?.(revision)
- return
- }
-
transition(region, animation?.transaction, revision === undefined ? undefined : () => onComplete?.(revision))
}, [position.x, position.y, size.width, size.height, animation?.revision, immediate])
@@ -160,7 +133,6 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
gesturing.current = true
stop()
- target.current = null
return { bounds: parent.getBoundingClientRect(), region: read() }
}
@@ -178,8 +150,6 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
gesturing.current = false
if (region) transition(region)
-
- else target.current = read()
}
function cancelGesture() {
@@ -201,11 +171,3 @@ export default function useWindowGeometryMotion({ position, size, animation, imm
cancelGesture
}
}
-
-function sameRegion(left: WindowRegion, right: WindowRegion) {
-
- return Math.abs(left.x - right.x) <= 0.5
- && Math.abs(left.y - right.y) <= 0.5
- && Math.abs(left.width - right.width) <= 0.5
- && Math.abs(left.height - right.height) <= 0.5
-}
diff --git a/tests/window-geometry-animation.test.ts b/tests/window-geometry-animation.test.ts
new file mode 100644
index 0000000..9959f7b
--- /dev/null
+++ b/tests/window-geometry-animation.test.ts
@@ -0,0 +1,98 @@
+import { beforeEach, expect, test, vi } from "vitest"
+import { animate, motionValue, type MotionValue, type ValueAnimationTransition } from "motion/react"
+import { WindowGeometryAnimation } from "@client/view/structure/desktop/windows/window-geometry-animation"
+
+vi.mock("motion/react", async importOriginal => ({
+ ...await importOriginal<typeof import("motion/react")>(),
+ animate: vi.fn((value: MotionValue<number>, target: number, options: ValueAnimationTransition<number>) => {
+ const run = {
+ stop: vi.fn(),
+ finish: () => { value.set(target); options.onComplete?.() }
+ }
+ runs.push(run)
+ return run
+ })
+}))
+
+const runs: { stop: ReturnType<typeof vi.fn>, finish: () => void }[] = []
+const timing = { duration: 120, easing: "ease-out" } as const
+const initial = { x: 10, y: 20, width: 300, height: 200 }
+
+beforeEach(() => {
+ runs.length = 0
+ vi.mocked(animate).mockClear()
+})
+
+function create() {
+ const values = {
+ x: motionValue(initial.x), y: motionValue(initial.y),
+ width: motionValue(initial.width), height: motionValue(initial.height)
+ }
+ return { values, animator: new WindowGeometryAnimation(values) }
+}
+
+test("a stream of width targets does not restart the position or height animation", () => {
+ const { animator } = create()
+ animator.transition({ ...initial, x: 100, width: 400, height: 500 }, timing)
+ for (let width = 401; width <= 420; width++) {
+ animator.transition({ ...initial, x: 100, width, height: 500 }, timing)
+ }
+ expect(runs).toHaveLength(23)
+ expect(runs[0].stop).not.toHaveBeenCalled()
+ expect(runs[2].stop).not.toHaveBeenCalled()
+ expect(runs[1].stop).toHaveBeenCalledOnce()
+ expect(vi.mocked(animate).mock.calls[0][2]).toMatchObject({
+ type: "tween", duration: 0.12, ease: "easeOut"
+ })
+})
+
+test("repeated destinations retain their animation and complete only the latest request", () => {
+ const { animator } = create()
+ const original = vi.fn()
+ const latest = vi.fn()
+ const target = { ...initial, width: 400 }
+ animator.transition(target, timing, original)
+ for (let i = 0; i < 100; i++) animator.transition(target, timing, latest)
+ expect(runs).toHaveLength(1)
+ expect(runs[0].stop).not.toHaveBeenCalled()
+ expect(latest).not.toHaveBeenCalled()
+ runs[0].finish()
+ expect(original).not.toHaveBeenCalled()
+ expect(latest).toHaveBeenCalledOnce()
+})
+
+test("completion waits for retained axes as well as the replacement axis", () => {
+ const { animator } = create()
+ const original = vi.fn()
+ const latest = vi.fn()
+ animator.transition({ ...initial, width: 400, height: 500 }, timing, original)
+ animator.transition({ ...initial, width: 450, height: 500 }, timing, latest)
+ runs[0].finish() // A late callback from the interrupted tween must be ignored.
+ runs[2].finish()
+ expect(latest).not.toHaveBeenCalled()
+ runs[1].finish()
+ expect(original).not.toHaveBeenCalled()
+ expect(latest).toHaveBeenCalledOnce()
+})
+
+test("a different transaction retimes an unchanged destination", () => {
+ const { animator } = create()
+ animator.transition({ ...initial, width: 400 }, timing)
+ animator.transition({ ...initial, width: 400 }, { duration: 240, easing: "linear" })
+ expect(runs[0].stop).toHaveBeenCalledOnce()
+ expect(runs).toHaveLength(2)
+ expect(vi.mocked(animate).mock.calls[1][2]).toMatchObject({ duration: 0.24, ease: "linear" })
+})
+
+test("immediate geometry cancels pending completion and sets all axes", () => {
+ const { animator, values } = create()
+ const interrupted = vi.fn()
+ const complete = vi.fn()
+ animator.transition({ ...initial, width: 400 }, timing, interrupted)
+ const target = { x: 30, y: 40, width: 500, height: 600 }
+ animator.transition(target, { ...timing, duration: 0 }, complete)
+ expect(Object.fromEntries(Object.entries(values).map(([key, value]) => [key, value.get()]))).toEqual(target)
+ expect(runs[0].stop).toHaveBeenCalledOnce()
+ expect(interrupted).not.toHaveBeenCalled()
+ expect(complete).toHaveBeenCalledOnce()
+})