An open-source, self-hosted system for apps built with web technologies.
Stabilize desktop readiness and wallpaper transitions
zohayrslileh0dddffa0ee81
diff --git a/source/client/view/structure/desktop/wallpaper/wallpaper.tsx b/source/client/view/structure/desktop/wallpaper/wallpaper.tsxindex 6d142f6..62125c4 100644--- a/source/client/view/structure/desktop/wallpaper/wallpaper.tsx+++ b/source/client/view/structure/desktop/wallpaper/wallpaper.tsx@@ -1,22 +1,29 @@import darkWallpaper from "@/assets/bundled/dark-wallpaper.png"import lightWallpaper from "@/assets/bundled/light-wallpaper.png"import { ApplicationContext } from "@client/view/contexts"-import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"+import { useEffect, useEffectEvent, useRef, useState, type ReactNode, type TransitionEvent } from "react"import Loading from "@client/view/components/loading"import { useReady } from "@libs/readiness"import { useTheme } from "@phreshos/react-ui"-type WallpaperSource = Readonly<{- key: string- source: string+const bundledWallpapers = [darkWallpaper, lightWallpaper] as const++const wallpaperImages = new Map<string, HTMLImageElement>()++const wallpaperLoads = new Map<string, Promise<void>>()++type WallpaperLayers = Readonly<{+ displayed: string | null+ incoming: string | null+ switching: boolean}>/**- * Displays only a completely loaded wallpaper source.+ * Displays one completely loaded wallpaper source.** A filename asks for a served image and `null` selects the effective Theme's- * bundled wallpaper. The previous loaded source stays visible until its- * replacement is ready.+ * bundled wallpaper. Both bundled sources remain preloaded so a Theme change+ * only replaces the source of this one visual layer.*/export function WallpaperBackground({ file, onReady }: WallpaperBackgroundProps) {@@ -24,127 +31,126 @@ export function WallpaperBackground({ file, onReady }: WallpaperBackgroundProps)const theme = useTheme()- const desired = useMemo<WallpaperSource>(() => {- return file === null- ? {- key: `bundled:${theme}`,- source: theme === "dark" ? darkWallpaper : lightWallpaper- }- : {- key: `file:${file}`,- source: `${application.doors.uploads}/${encodeURIComponent(file)}`- }-- }, [application.doors.uploads, file, theme])-- const [displayed, setDisplayed] = useState<WallpaperSource | null>(null)- const [previous, setPrevious] = useState<WallpaperSource | null>(null)- const loadedSources = useRef(new Set<string>())- const transition = useRef<number | null>(null)+ const desired = file === null+ ? theme === "dark" ? darkWallpaper : lightWallpaper+ : `${application.doors.uploads}/${encodeURIComponent(file)}`- useEffect(() => () => {- if (transition.current !== null) clearTimeout(transition.current)- }, [])-- const sources = distinct([previous, displayed, desired])-- useEffect(() => {- if (loadedSources.current.has(desired.key)) display(desired)- }, [desired])-- function loaded(source: WallpaperSource) {- loadedSources.current.add(source.key)+ const [layers, setLayers] = useState<WallpaperLayers>({+ displayed: null,+ incoming: null,+ switching: false+ })+ const current = useRef(layers)+ const frame = useRef<number | null>(null)- display(source)- }+ current.current = layers- function display(source: WallpaperSource) {- if (source.key !== desired.key) return+ const ready = useEffectEvent(() => onReady?.())+ const display = useEffectEvent((source: string) => {+ const shown = current.current- if (source.key === displayed?.key) {- onReady?.()+ if (!shown.displayed) {+ setLayers({ displayed: source, incoming: null, switching: false })return}- if (transition.current !== null) clearTimeout(transition.current)+ if (shown.displayed === source && !shown.incoming) return- setPrevious(displayed)- setDisplayed(source)- onReady?.()+ if (frame.current !== null) cancelAnimationFrame(frame.current)+ setLayers({ displayed: shown.incoming ?? shown.displayed, incoming: source, switching: false })+ frame.current = requestAnimationFrame(() => {+ frame.current = requestAnimationFrame(() => {+ frame.current = null+ setLayers(value => value.incoming === source ? { ...value, switching: true } : value)+ })+ })+ })- if (displayed) {- transition.current = window.setTimeout(() => {- setPrevious(null)- transition.current = null- }, 700)- }- }+ useEffect(() => () => {+ if (frame.current !== null) cancelAnimationFrame(frame.current)+ }, [])- function failed(source: WallpaperSource) {- if (source.key === desired.key) onReady?.()+ useEffect(() => {+ let active = true+ const desiredLoad = loadWallpaper(desired)+ const bundledLoads = bundledWallpapers.map(loadWallpaper)+ const loading = file === null+ ? Promise.allSettled(bundledLoads).then(() => desiredLoad)+ : desiredLoad++ for (const preload of bundledLoads) void preload.catch(() => undefined)++ loading.then(() => {+ if (!active) return+ display(desired)+ ready()+ }, () => {+ if (active) ready()+ })++ return () => { active = false }+ }, [desired, file])++ function transitionEnded(event: TransitionEvent<HTMLDivElement>, source: string) {+ if (event.propertyName !== "opacity" || layers.incoming !== source || !layers.switching) return+ setLayers({ displayed: source, incoming: null, switching: false })}- return <>{sources.map(source => <WallpaperLayer-- key={source.key}+ const incoming = layers.incoming- source={source}-- visible={source.key === displayed?.key}-- transitioning={previous !== null}-- onLoad={() => loaded(source)}-- onError={() => failed(source)}+ return <>- />)}</>-}+ {layers.displayed && <WallpaperLayer+ key={layers.displayed}+ source={layers.displayed}+ visible+ />}-function distinct(sources: readonly (WallpaperSource | null)[]) {- const keys = new Set<string>()+ {incoming && <WallpaperLayer+ key={incoming}+ source={incoming}+ visible={layers.switching}+ onTransitionEnd={event => transitionEnded(event, incoming)}+ />}- return sources.filter((source): source is WallpaperSource => {- if (!source || keys.has(source.key)) return false- keys.add(source.key)- return true- })+ </>}-function WallpaperLayer({ source, visible, transitioning, onLoad, onError }: {- source: WallpaperSource+function WallpaperLayer({ source, visible, onTransitionEnd }: {+ source: stringvisible: boolean- transitioning: boolean- onLoad: () => void- onError: () => void+ onTransitionEnd?: (event: TransitionEvent<HTMLDivElement>) => void}) {- return <>-- <div-- aria-hidden="true"-- className={`pointer-events-none absolute inset-0 bg-cover bg-center bg-no-repeat ${transitioning ? "transition-opacity duration-700 ease-out" : ""} ${visible ? "opacity-100" : "opacity-0"}`}-- style={{ backgroundImage: `url(${source.source})` }}-- />-- <img-- src={source.source}+ return <div+ aria-hidden="true"+ className={`pointer-events-none absolute inset-0 bg-cover bg-center bg-no-repeat transition-opacity duration-700 ease-out ${visible ? "opacity-100" : "opacity-0"}`}+ style={{ backgroundImage: `url(${source})` }}+ onTransitionEnd={onTransitionEnd}+ />+}- alt=""+function loadWallpaper(source: string) {+ const existing = wallpaperLoads.get(source)- className="hidden"+ if (existing) return existing- onLoad={onLoad}+ const image = new Image()+ const loading = new Promise<void>((resolve, reject) => {+ const failed = () => {+ wallpaperImages.delete(source)+ wallpaperLoads.delete(source)+ reject(new Error(`The wallpaper could not be loaded: ${source}`))+ }- onError={onError}+ image.onload = () => void image.decode().then(resolve, failed)+ image.onerror = failed+ })- />+ wallpaperImages.set(source, image)+ wallpaperLoads.set(source, loading)+ image.src = source- </>+ return loading}/** A complete surface whose content sits above one file-backed wallpaper. */diff --git a/source/client/view/structure/structure.tsx b/source/client/view/structure/structure.tsxindex 8e8c5e7..b685608 100644--- a/source/client/view/structure/structure.tsx+++ b/source/client/view/structure/structure.tsx@@ -27,7 +27,9 @@ export default function () {aria-hidden={pending.length === 0}- className={`transition-opacity duration-200 ease-out ${pending.length ? "" : "pointer-events-none opacity-0"}`}+ className={pending.length+ ? "opacity-100"+ : "pointer-events-none opacity-0 transition-opacity duration-200 ease-out"}style={{ backgroundColor: standardAppearance.background.light }}