An open-source, self-hosted system for apps built with web technologies.
Use Motion scale-based window resize transitions
zohayrslilehc57c5d970c95
diff --git a/source/client/view/structure/desktop/windows/snap-preview.tsx b/source/client/view/structure/desktop/windows/snap-preview.tsxindex a2fadea..81f582b 100644--- a/source/client/view/structure/desktop/windows/snap-preview.tsx+++ b/source/client/view/structure/desktop/windows/snap-preview.tsx@@ -22,19 +22,23 @@ export default function SnapPreview({ shown, visible, bare, paintSurfaceSize, rereturn <motion.divref={geometry.frame}className="pointer-events-none absolute"- initial={false}- animate={{ scale: visible ? 1 : 0.98, opacity: visible ? 1 : 0 }}- transition={motionTransition(transaction, reducedMotion)}- style={{ left: 0, top: 0, transformOrigin: "center", zIndex, ...geometry.style }}+ style={{ left: 0, top: 0, zIndex, ...geometry.style }}>- <Surface- data-snap-preview-frame- material={{ opacity: "small" }}- style={{- position: "absolute",- ...(bare ? { inset: 0 } : windowPaintInsets(shown.position, shown.size, paintSurfaceSize, windowPaintInset))- }}- />+ <motion.div+ initial={false}+ animate={{ scale: visible ? 1 : 0.98, opacity: visible ? 1 : 0 }}+ transition={motionTransition(transaction, reducedMotion)}+ style={{ position: "absolute", inset: 0, transformOrigin: "center" }}+ >+ <Surface+ data-snap-preview-frame+ material={{ opacity: "small" }}+ style={{+ position: "absolute",+ ...(bare ? { inset: 0 } : windowPaintInsets(shown.position, shown.size, paintSurfaceSize, windowPaintInset))+ }}+ />+ </motion.div></motion.div>}diff --git a/source/client/view/structure/desktop/windows/window-geometry-animation.ts b/source/client/view/structure/desktop/windows/window-geometry-animation.tsindex 511102e..644cc9f 100644--- a/source/client/view/structure/desktop/windows/window-geometry-animation.ts+++ b/source/client/view/structure/desktop/windows/window-geometry-animation.ts@@ -11,14 +11,17 @@ type Flight = {control?: AnimationPlaybackControls}-/** Retargets geometry without interrupting axes that are already travelling to their destination. */+/** Interpolates visible geometry while the viewport is laid out at its destination size. */export class WindowGeometryAnimation {private flights = new Map<Axis, Flight>()private complete?: () => voidprivate updating = false- constructor(private values: Record<Axis, MotionValue<number>>) {}+ constructor(+ private values: Record<Axis, MotionValue<number>>,+ private layout: Pick<Record<Axis, MotionValue<number>>, "width" | "height">+ ) {}stop() {@@ -26,12 +29,14 @@ export class WindowGeometryAnimation {const flights = [...this.flights.values()]this.flights.clear()for (const flight of flights) flight.control?.stop()+ this.settleLayout()}set(region: WindowRegion) {this.stop()for (const axis of axes) this.values[axis].set(region[axis])+ this.settleLayout()}transition(region: WindowRegion, transaction: AppearanceTransaction, complete?: () => void) {@@ -46,6 +51,11 @@ export class WindowGeometryAnimation {this.updating = trueconst 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.+ 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 axes) {const previous = this.flights.get(axis)@@ -76,8 +86,14 @@ export class WindowGeometryAnimation {private finish() {if (this.updating || this.flights.size) return+ this.settleLayout()const complete = this.completethis.complete = undefinedcomplete?.()}++ 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-geometry-motion.ts b/source/client/view/structure/desktop/windows/window-geometry-motion.tsindex 4d5310d..d23385a 100644--- a/source/client/view/structure/desktop/windows/window-geometry-motion.ts+++ b/source/client/view/structure/desktop/windows/window-geometry-motion.ts@@ -1,7 +1,7 @@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 { useMotionValue, type MotionStyle } from "motion/react"+import { useMotionValue, useTransform, type MotionStyle } from "motion/react"import { useLayoutEffect, useRef } from "react"import { WindowGeometryAnimation } from "./window-geometry-animation"import { useAppearance } from "@phreshos/react-ui"@@ -29,8 +29,12 @@ export default function useWindowGeometryMotion({ position, size, animation, immconst 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 layoutWidth = useMotionValue(width.get())+ const layoutHeight = useMotionValue(height.get())+ const scaleX = useTransform(() => layoutWidth.get() === 0 ? 1 : width.get() / layoutWidth.get())+ const scaleY = useTransform(() => layoutHeight.get() === 0 ? 1 : height.get() / layoutHeight.get())const animator = useRef<WindowGeometryAnimation | null>(null)- if (!animator.current) animator.current = new WindowGeometryAnimation({ x, y, width, height })+ if (!animator.current) animator.current = new WindowGeometryAnimation({ x, y, width, height }, { width: layoutWidth, height: layoutHeight })const gesturing = useRef(false)const initialized = useRef(false)const values = useRef({ position, size, animation, immediate, onComplete })@@ -139,10 +143,7 @@ export default function useWindowGeometryMotion({ position, size, animation, immfunction updateGesture(region: WindowRegion) {- x.set(region.x)- y.set(region.y)- width.set(region.width)- height.set(region.height)+ set(region)}function finishGesture(region?: WindowRegion) {@@ -163,7 +164,7 @@ export default function useWindowGeometryMotion({ position, size, animation, immreturn {frame,- style: { x, y, width, height } satisfies MotionStyle,+ style: { x, y, width: layoutWidth, height: layoutHeight, scaleX, scaleY, transformOrigin: "0 0" } satisfies MotionStyle,read,beginGesture,updateGesture,diff --git a/tests/window-geometry-animation.test.ts b/tests/window-geometry-animation.test.tsindex 9959f7b..52319b6 100644--- a/tests/window-geometry-animation.test.ts+++ b/tests/window-geometry-animation.test.ts@@ -5,8 +5,10 @@ import { WindowGeometryAnimation } from "@client/view/structure/desktop/windows/vi.mock("motion/react", async importOriginal => ({...await importOriginal<typeof import("motion/react")>(),animate: vi.fn((value: MotionValue<number>, target: number, options: ValueAnimationTransition<number>) => {+ const start = value.get()const run = {stop: vi.fn(),+ progress: (fraction: number) => value.set(start + (target - start) * fraction),finish: () => { value.set(target); options.onComplete?.() }}runs.push(run)@@ -14,7 +16,7 @@ vi.mock("motion/react", async importOriginal => ({})}))-const runs: { stop: ReturnType<typeof vi.fn>, finish: () => void }[] = []+const runs: { stop: ReturnType<typeof vi.fn>, progress: (fraction: number) => void, finish: () => void }[] = []const timing = { duration: 120, easing: "ease-out" } as constconst initial = { x: 10, y: 20, width: 300, height: 200 }@@ -28,9 +30,55 @@ function create() {x: motionValue(initial.x), y: motionValue(initial.y),width: motionValue(initial.width), height: motionValue(initial.height)}- return { values, animator: new WindowGeometryAnimation(values) }+ const layout = { width: motionValue(initial.width), height: motionValue(initial.height) }+ return { values, layout, animator: new WindowGeometryAnimation(values, layout) }}+test("resize keeps destination layout fixed while visible dimensions interpolate", () => {+ const { animator, values, layout } = create()+ animator.transition({ ...initial, width: 600, height: 400 }, timing)+ expect(layout.width.get()).toBe(600)+ expect(layout.height.get()).toBe(400)+ expect(values.width.get() / layout.width.get()).toBe(0.5)+ for (const run of runs) run.progress(0.5)+ expect(values.width.get()).toBe(450)+ expect(values.height.get()).toBe(300)+ expect(layout.width.get()).toBe(600)+ expect(layout.height.get()).toBe(400)+ expect(values.width.get() / layout.width.get()).toBe(0.75)+ for (const run of runs) run.finish()+ expect(values.width.get() / layout.width.get()).toBe(1)+ expect(values.height.get() / layout.height.get()).toBe(1)+})++test("retargeting preserves visible size and pointer interruption restores unscaled layout", () => {+ const { animator, values, layout } = create()+ animator.transition({ ...initial, width: 600 }, timing)+ runs[0].progress(0.5)+ animator.transition({ ...initial, width: 900 }, timing)+ expect(values.width.get()).toBe(450)+ expect(layout.width.get()).toBe(900)+ animator.stop()+ expect(values.width.get()).toBe(450)+ expect(layout.width.get()).toBe(450)+ animator.set({ ...initial, width: 480 })+ expect(values.width.get()).toBe(480)+ expect(layout.width.get()).toBe(480)+})++test("zero-size destinations retain finite backing dimensions until completion", () => {+ const { animator, values, layout } = create()+ animator.transition({ ...initial, width: 0, height: 0 }, timing)+ expect(layout.width.get()).toBe(300)+ expect(layout.height.get()).toBe(200)+ for (const run of runs) run.finish()+ expect(values.width.get()).toBe(0)+ expect(layout.width.get()).toBe(0)+ animator.transition(initial, timing)+ expect(layout.width.get()).toBe(300)+ expect(values.width.get()).toBe(0)+})+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)