An open-source, self-hosted system for apps built with web technologies.
Fix desktop motion lifecycles
zohayrslileh052e8346244d
diff --git a/source/client/view/appearance/motion.ts b/source/client/view/appearance/motion.tsindex dfc92af..21d5adf 100644--- a/source/client/view/appearance/motion.ts+++ b/source/client/view/appearance/motion.ts@@ -13,11 +13,12 @@ const curves: Record<Exclude<SystemEasing, readonly number[]>, Easing> = {export const motionDurations = Object.freeze({control: 100,minimize: 110,+ dismiss: 140,close: 160,snap: 180,+ presence: 180,feedback: 200,morph: 220,- presence: 240,restore: 240,geometry: 300})diff --git a/source/client/view/appearance/surface-presence.ts b/source/client/view/appearance/surface-presence.tsindex 4b3b7f2..3d33bf5 100644--- a/source/client/view/appearance/surface-presence.ts+++ b/source/client/view/appearance/surface-presence.ts@@ -1,11 +1,13 @@-import { animate, type AnimationPlaybackControlsWithThen } from "motion"+import { animate, motionValue, styleEffect, type AnimationPlaybackControlsWithThen, type MotionValue, type ValueAnimationTransition } from "motion"import { motionDuration, motionDurations, motionEase } from "./motion"-const entered = { scale: 1, y: 0 }+const entered = "translateY(0px) scale(1)"-const entering = { scale: 0.96, y: 8 }+const entering = "translateY(8px) scale(0.96)"-const animations = new WeakMap<HTMLElement, AnimationPlaybackControlsWithThen>()+const resting = "none"++const motions = new WeakMap<HTMLElement, SurfaceMotion>()/** Establish the first frame before a surface becomes visible. */export function prepareSurfaceEntrance(surface: HTMLElement | null, reducedMotion: boolean) {@@ -13,7 +15,7 @@ export function prepareSurfaceEntrance(surface: HTMLElement | null, reducedMotioif (!surface) returnstop(surface)- set(surface, reducedMotion ? entered : entering)+ set(surface, reducedMotion ? resting : entering)}/** Give every system surface the same entrance motion. */@@ -25,19 +27,34 @@ export function enterSurface(surface: HTMLElement | null, reducedMotion: booleanif (reducedMotion) {- set(surface, entered)+ set(surface, resting)return null}- const animation = animate(surface, entered, {+ return animateSurfaceTransform(surface, 1, 0, {duration: motionDuration(motionDurations.presence),ease: motionEase([0.33, 1, 0.68, 1])})+}- animations.set(surface, animation)+/** Let a dismissible surface leave before its host removes it from paint. */+export function exitSurface(surface: HTMLElement | null, reducedMotion: boolean) {- return animation+ if (!surface) return null++ if (reducedMotion) {++ restSurface(surface)++ return null+ }++ return animateSurfaceTransform(surface, 0.96, 8, {+ duration: motionDuration(motionDurations.dismiss),+ ease: motionEase("ease-in"),+ onComplete: () => restSurface(surface)+ })}/** Stop presence motion and leave the surface ready for its next entrance. */@@ -46,14 +63,81 @@ export function restSurface(surface: HTMLElement | null) {if (!surface) returnstop(surface)- set(surface, entered)+ set(surface, resting)+}++/** Set a surface pose through the same transform owner used by its motion. */+export function setSurfaceTransform(surface: HTMLElement | null, scale: number, y: number) {++ if (!surface) return++ stop(surface)+ set(surface, surfaceTransform(scale, y))+}++/** Move a surface without creating a second transform owner on its element. */+export function animateSurfaceTransform(surface: HTMLElement | null, scale: number, y: number, options: ValueAnimationTransition<string>) {++ if (!surface) return null++ stop(surface)++ const values = motion(surface)+ const target = surfaceTransform(scale, y)+ const { onComplete, ...transition } = options++ // `none` and a composed transform look identical at rest but are not+ // compatible complex values. Keep the identity representation while a+ // transition runs, then normalize it back to `none` when it settles.+ if (values.transform.get() === resting) values.transform.jump(entered)++ const animation = animate(values.transform, target === resting ? entered : target, {+ ...transition,+ onComplete: function () {+ values.transform.set(target)+ values.animation = null+ onComplete?.()+ }+ })++ values.animation = animation++ return animation}function stop(surface: HTMLElement) {- animations.get(surface)?.stop()- animations.delete(surface)+ const values = motions.get(surface)+ values?.animation?.stop()+ if (values) values.animation = null+}++function set(surface: HTMLElement, target: string) {+ const values = motion(surface)+ values.transform.jump(target)+}++function motion(surface: HTMLElement) {+ const existing = motions.get(surface)++ if (existing) return existing++ const transform = motionValue(resting)+ const values: SurfaceMotion = {+ transform,+ animation: null+ }++ styleEffect(surface, { transform })+ motions.set(surface, values)++ return values+}++function surfaceTransform(scale: number, y: number) {+ return scale === 1 && y === 0 ? resting : `translateY(${y}px) scale(${scale})`}-function set(surface: HTMLElement, target: typeof entered) {- surface.style.transform = target.scale === 1 && target.y === 0 ? "" : `translateY(${target.y}px) scale(${target.scale})`+interface SurfaceMotion {+ transform: MotionValue<string>+ animation: AnimationPlaybackControlsWithThen | null}diff --git a/source/client/view/structure/desktop/taskbar/launcher/launcher.tsx b/source/client/view/structure/desktop/taskbar/launcher/launcher.tsxindex e543568..c0d36a0 100644--- a/source/client/view/structure/desktop/taskbar/launcher/launcher.tsx+++ b/source/client/view/structure/desktop/taskbar/launcher/launcher.tsx@@ -1,5 +1,6 @@import { ComponentProps, ReactNode, useCallback, useEffect, useId, useRef, useState } from "react"-import { enterSurface, prepareSurfaceEntrance, restSurface } from "../../../../appearance/surface-presence"+import { enterSurface, exitSurface, prepareSurfaceEntrance } from "../../../../appearance/surface-presence"+import { motionDurations } from "../../../../appearance/motion"import { useReducedMotion } from "@libs/react-motion"import { useAppearance, useResolveTheme, useScale } from "@phreshos/react-ui"import TaskbarSurface, { taskbarSurfaceClassName } from "../taskbar-surface"@@ -86,13 +87,21 @@ export default function ({ label, trigger, children, className, style, ...propstabIndex={-1}- style={{ ...style, borderRadius: radius }}+ style={{+ ...style,+ borderRadius: radius,+ transitionBehavior: "allow-discrete",+ transitionDuration: reducedMotion ? "0s" : `${motionDurations.dismiss}ms`,+ transitionProperty: "display, overlay"+ }}className={`${taskbarSurfaceClassName} hidden open:block ${className ?? ""}`}onBeforeToggle={event => {if (event.newState === "open") prepareSurfaceEntrance(event.currentTarget, reducedMotion)++ else exitSurface(event.currentTarget, reducedMotion)}}onToggle={event => {@@ -110,7 +119,6 @@ export default function ({ label, trigger, children, className, style, ...propsfocusTarget.focus()}- else restSurface(event.currentTarget)}}>diff --git a/source/client/view/structure/desktop/windows/snap-preview.tsx b/source/client/view/structure/desktop/windows/snap-preview.tsxindex 3eeac61..013f118 100644--- a/source/client/view/structure/desktop/windows/snap-preview.tsx+++ b/source/client/view/structure/desktop/windows/snap-preview.tsx@@ -1,4 +1,4 @@-import { type CSSProperties, useLayoutEffect, useRef, useState } from "react"+import { type CSSProperties, useLayoutEffect, useRef } from "react"import { animate } from "motion"import { motionDuration, motionDurations, motionEase } from "../../../appearance/motion"import { resolveWindowGeometry, resolveWindowValue, windowPaintInsets, type WindowSurfaceSize } from "../../../components/window-manager/window-geometry"@@ -11,7 +11,7 @@ export default function SnapPreview({ shown, visible, bare, paintSurfaceSize, raconst element = useRef<HTMLDivElement>(null)const firstRender = useRef(true)- const [rendered, setRendered] = useState(shown)+ const initial = useRef(shown)useLayoutEffect(function () {@@ -21,6 +21,13 @@ export default function SnapPreview({ shown, visible, bare, paintSurfaceSize, raif (!preview || !parent) returnconst parentBounds = parent.getBoundingClientRect()+ const currentBounds = preview.getBoundingClientRect()+ const current = {+ x: currentBounds.left - parentBounds.left,+ y: currentBounds.top - parentBounds.top,+ width: currentBounds.width,+ height: currentBounds.height+ }const target = resolveWindowGeometry(shown.position, shown.size, parentBounds)const fromOpacity = firstRender.current ? 0 : Number(getComputedStyle(preview).opacity)@@ -29,21 +36,19 @@ export default function SnapPreview({ shown, visible, bare, paintSurfaceSize, raif (reducedMotion) {setPreview(preview, target, visible ? 1 : 0)- setRendered(shown)return}const animation = animate(preview, {- left: target.x,- top: target.y,- width: target.width,- height: target.height,+ left: [current.x, target.x],+ top: [current.y, target.y],+ width: [current.width, target.width],+ height: [current.height, target.height],opacity: [fromOpacity, visible ? 1 : 0]}, {duration: motionDuration(motionDurations.snap),- ease: motionEase([0.33, 1, 0.68, 1]),- onComplete: () => setRendered(shown)+ ease: motionEase([0.33, 1, 0.68, 1])})return () => { animation.stop() }@@ -54,11 +59,11 @@ export default function SnapPreview({ shown, visible, bare, paintSurfaceSize, raref={element}className="pointer-events-none absolute"style={{- left: resolveWindowValue(rendered.position.x),- top: resolveWindowValue(rendered.position.y),- width: resolveWindowValue(rendered.size.width),- height: resolveWindowValue(rendered.size.height),- opacity: visible ? 1 : 0,+ left: resolveWindowValue(initial.current.position.x),+ top: resolveWindowValue(initial.current.position.y),+ width: resolveWindowValue(initial.current.size.width),+ height: resolveWindowValue(initial.current.size.height),+ opacity: 0,zIndex}}>diff --git a/source/client/view/structure/desktop/windows/window.tsx b/source/client/view/structure/desktop/windows/window.tsxindex b6bd93a..55bd54a 100644--- a/source/client/view/structure/desktop/windows/window.tsx+++ b/source/client/view/structure/desktop/windows/window.tsx@@ -1,6 +1,6 @@import { ComponentProps, PointerEvent as ReactPointerEvent, ReactNode, useLayoutEffect, useRef, useState } from "react"import { useReducedMotion } from "@libs/react-motion"-import { enterSurface, prepareSurfaceEntrance, restSurface } from "../../../appearance/surface-presence"+import { animateSurfaceTransform, enterSurface, prepareSurfaceEntrance, restSurface, setSurfaceTransform } from "../../../appearance/surface-presence"import { Surface, useAppearance, useResolveTheme, useScale } from "@phreshos/react-ui"import { absoluteWindowGeometry, resolveWindowGeometry, resolveWindowValue, wholeWindowGeometry, windowPaintInsets, type WindowRegion, type WindowSurfaceSize } from "../../../components/window-manager/window-geometry"import { type Position, type Size, type WindowGeometry } from "@phreshos/core"@@ -157,11 +157,20 @@ export default function ({ title, icon, children, onClose, onClosed, onMinimize,setRegion(element, target)element.style.transformOrigin = "0 0"+ // Motion resolves independent transform values after this layout+ // effect. Seed and animate the composed transform itself so laying+ // out the target can never expose it before the inverse is installed.+ const inverse = windowTransform(+ current.x - target.x,+ current.y - target.y,+ current.width / target.width,+ current.height / target.height+ )++ element.style.transform = inverse+const animation = animate(element, {- x: [current.x - target.x, 0],- y: [current.y - target.y, 0],- scaleX: [current.width / target.width, 1],- scaleY: [current.height / target.height, 1]+ transform: [inverse, windowTransform(0, 0, 1, 1)]}, {duration: motionDuration(duration),ease: transaction ? motionEase(transaction.easing) : motionEase([0.33, 1, 0.68, 1]),@@ -373,11 +382,11 @@ export default function ({ title, icon, children, onClose, onClosed, onMinimize,const animation = minimized- ? animate(surfaceElement.current, { scale: 0.86, y: 28 }, { duration: motionDuration(motionDurations.minimize), ease: motionEase("ease-in"), onComplete: () => setVisibility(surfaceElement.current, "hidden") })+ ? animateSurfaceTransform(surfaceElement.current, 0.86, 28, { duration: motionDuration(motionDurations.minimize), ease: motionEase("ease-in"), onComplete: () => setVisibility(surfaceElement.current, "hidden") })- : animate(surfaceElement.current, { scale: 1, y: 0 }, { duration: motionDuration(motionDurations.restore), ease: motionEase("ease-out") })+ : animateSurfaceTransform(surfaceElement.current, 1, 0, { duration: motionDuration(motionDurations.restore), ease: motionEase("ease-out") })- return () => { animation.stop() }+ return () => { animation?.stop() }}, [minimized, bare, reducedMotion])@@ -399,13 +408,13 @@ export default function ({ title, icon, children, onClose, onClosed, onMinimize,return}- const animation = animate(surfaceElement.current, { scale: 0.86, y: 12 }, {+ const animation = animateSurfaceTransform(surfaceElement.current, 0.86, 12, {duration: motionDuration(motionDurations.close),ease: motionEase("ease-in"),onComplete: completeClosure})- return () => { animation.stop() }+ return () => { animation?.stop() }}, [closing, bare, reducedMotion])@@ -905,9 +914,13 @@ function setRegion(element: HTMLElement, region: WindowRegion) {element.style.transformOrigin = ""}+function windowTransform(x: number, y: number, scaleX: number, scaleY: number) {+ return `translate(${x}px, ${y}px) scale(${scaleX}, ${scaleY})`+}+function setSurface(element: HTMLElement | null, scale: number, y: number, visibility: "hidden" | "visible") {if (!element) return- element.style.transform = scale === 1 && y === 0 ? "" : `translateY(${y}px) scale(${scale})`+ setSurfaceTransform(element, scale, y)element.style.visibility = visibility}