An open-source, self-hosted system for apps built with web technologies.
Propagate Desktop scale preferences
zohayrslileh05ce4e1bddf1
diff --git a/source/client/view/appearance/desktop-preferences.tsx b/source/client/view/appearance/desktop-preferences.tsxindex 24b940f..047aac0 100644--- a/source/client/view/appearance/desktop-preferences.tsx+++ b/source/client/view/appearance/desktop-preferences.tsx@@ -1,9 +1,12 @@import useStorage from "@libs/storage-hook"import {+ desktopPreferencesLimits,+ defaultDesktopScale,defaultAppearance,type AppearanceTransaction,type DesktopPreferences,type DesktopPreferencesUpdate,+ type DesktopScalePreference,type Theme} from "@phreshos/core"import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode } from "react"@@ -13,6 +16,7 @@ const themeQuery = "(prefers-color-scheme: dark)"const reducedMotionQuery = "(prefers-reduced-motion: reduce)"const themeKey = "desktop-preferences:theme"const animationsKey = "desktop-preferences:animations"+const scaleKey = "desktop-preferences:scale"const DesktopPreferencesContext = createContext<DesktopPreferencesOwner | null>(null)@@ -20,11 +24,13 @@ const DesktopPreferencesContext = createContext<DesktopPreferencesOwner | null>(export default function DesktopPreferencesProvider({ children }: Readonly<{ children: ReactNode }>) {const storedTheme = useStorage(themeKey)const storedAnimations = useStorage(animationsKey)+ const storedScale = useStorage(scaleKey)const nativeDark = useMediaPreference(themeQuery)const nativeReducedMotion = useMediaPreference(reducedMotionQuery)const desiredTheme = selectedTheme(storedTheme.value, nativeDark)const desiredAnimations = selectedAnimations(storedAnimations.value, nativeReducedMotion)- const desired = useMemo<DesktopPreferences>(() => ({ theme: desiredTheme, animations: desiredAnimations }), [desiredAnimations, desiredTheme])+ const desiredScale = resolveStoredDesktopScale(storedScale.value)+ const desired = useMemo<DesktopPreferences>(() => ({ theme: desiredTheme, animations: desiredAnimations, scale: desiredScale }), [desiredAnimations, desiredScale, desiredTheme])const [preferences, setPreferences] = useState(desired)const current = useRef(preferences)const pending = useRef<PendingCommit | null>(null)@@ -41,7 +47,13 @@ export default function DesktopPreferencesProvider({ children }: Readonly<{ chilif (change.animations === "default") storedAnimations.remove()else storedAnimations.update(change.animations ? "enabled" : "disabled")}- }, [storedAnimations.remove, storedAnimations.update, storedTheme.remove, storedTheme.update])++ if (change.scale !== undefined) {+ const stored = serializeDesktopScalePreference(change.scale)+ if (stored === null) storedScale.remove()+ else storedScale.update(stored)+ }+ }, [storedAnimations.remove, storedAnimations.update, storedScale.remove, storedScale.update, storedTheme.remove, storedTheme.update])const setTransaction = useCallback(function (value: AppearanceTransaction) {transaction.current = value@@ -124,6 +136,18 @@ function selectedAnimations(value: string | null, nativeReducedMotion: boolean)return !nativeReducedMotion}+/** Resolves the effective scale from this Desktop's persisted representation. */+export function resolveStoredDesktopScale(value: string | null) {+ const scale = Number(value)+ const { minimum, maximum } = desktopPreferencesLimits.scale+ return value !== null && Number.isFinite(scale) && scale >= minimum && scale <= maximum ? scale : defaultDesktopScale+}++/** Converts a scale request into its persisted representation; null removes the override. */+export function serializeDesktopScalePreference(value: DesktopScalePreference) {+ return value === "default" ? null : String(value)+}+function useMediaPreference(query: string) {const media = useMemo(() => matchMedia(query), [query])return useSyncExternalStore(@@ -148,5 +172,5 @@ interface PendingCommit {}function samePreferences(first: DesktopPreferences, second: DesktopPreferences) {- return first.theme === second.theme && first.animations === second.animations+ return first.theme === second.theme && first.animations === second.animations && first.scale === second.scale}diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.tsindex d28bb3f..92f4971 100644--- a/source/client/view/components/desktop-host/host.ts+++ b/source/client/view/components/desktop-host/host.ts@@ -13,8 +13,8 @@ import { type default as ClientProcess } from "@client/core/link-manager/auth-maimport {isServiceKey,isUploadFile,+ parseDesktopPreferencesUpdate,parsePermissionName,- type DesktopPreferencesUpdate,type PermissionInput,type PermissionRequest,type ProgramIconSize@@ -288,7 +288,7 @@ export default function host(authManager: AuthManager, pane: string, viewport: (await access.require("desktopPreferences", [])- const preferences = desktopPreferencesUpdate(args[0])+ const preferences = parseDesktopPreferencesUpdate(args[0])await authManager.linkManager.requestDesktopPreferences(preferences)@@ -1256,26 +1256,6 @@ export default function host(authManager: AuthManager, pane: string, viewport: (}}-function desktopPreferencesUpdate(value: unknown): DesktopPreferencesUpdate {- if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error("Desktop preferences must be an object")-- const preferences = value as Record<string, unknown>- if (!("theme" in preferences) && !("animations" in preferences)) throw new Error("Desktop preferences must update theme, animations, or both")-- if ("theme" in preferences && preferences.theme !== "light" && preferences.theme !== "dark" && preferences.theme !== "default") {- throw new Error("The Desktop theme preference must be light, dark, or default")- }-- if ("animations" in preferences && typeof preferences.animations !== "boolean" && preferences.animations !== "default") {- throw new Error("The Desktop animations preference must be true, false, or default")- }-- return Object.freeze({- ...(preferences.theme === undefined ? {} : { theme: preferences.theme }),- ...(preferences.animations === undefined ? {} : { animations: preferences.animations })- }) as DesktopPreferencesUpdate-}-function isTrafficKind(value: unknown): value is TrafficKind {return value === "publish" || value === "ask" || value === "answer"diff --git a/source/client/view/structure/authentication/authentication.tsx b/source/client/view/structure/authentication/authentication.tsxindex fdc4d8d..3580413 100644--- a/source/client/view/structure/authentication/authentication.tsx+++ b/source/client/view/structure/authentication/authentication.tsx@@ -11,7 +11,8 @@ import { useEffect, useState, type ReactNode } from "react"import { type AuthenticationState } from "@server/core/authentication/authentication"import { WallpaperStage } from "../desktop/wallpaper/wallpaper"import { useReady } from "@libs/readiness"-import { useAppearance, useThemedValue } from "@phreshos/react-ui"+import { useThemedValue } from "@phreshos/react-ui"+import { useProperty } from "@the-link/react"export default function () {@@ -19,7 +20,9 @@ export default function () {const linkManager = LinkManagerContext.useValue()- const signInWallpaper = useThemedValue(useAppearance().signInWallpaper)+ const appearance = useProperty(linkManager.appearance)++ const signInWallpaper = useThemedValue(appearance.signInWallpaper)const [revision, setRevision] = useState(0)diff --git a/source/client/view/structure/desktop/desktop.tsx b/source/client/view/structure/desktop/desktop.tsxindex 1ae6bd9..7747731 100644--- a/source/client/view/structure/desktop/desktop.tsx+++ b/source/client/view/structure/desktop/desktop.tsx@@ -1,6 +1,6 @@import { type Layer } from "@phreshos/core"import { useCallback, useEffect, useRef, useState } from "react"-import { ApplicationContext, AuthManagerContext } from "../../contexts"+import { ApplicationContext, AuthManagerContext, LinkManagerContext } from "../../contexts"import useClientHost from "../../components/desktop-host/client-host"import DesktopDisplay from "./desktop-display"import useDesktopFocus from "./desktop-focus"@@ -17,7 +17,8 @@ import useWindows from "../../components/window-manager/window-manager"import { ReadyWallpaper, WallpaperBackground } from "./wallpaper/wallpaper"import Loading from "../../components/loading"import { useRequirement } from "@libs/readiness"-import { useAppearance, usePreferences, useThemedValue } from "@phreshos/react-ui"+import { usePreferences, useThemedValue } from "@phreshos/react-ui"+import { useProperty } from "@the-link/react"import { isDesktopReplacementLayer } from "@shared/desktop-replacement"import SharedResizeBoundaries from "./windows/shared-resize-boundaries"@@ -27,7 +28,7 @@ export default function Workspace() {const authManager = AuthManagerContext.useValue()- const appearance = useAppearance()+ const appearance = useProperty(LinkManagerContext.useValue().appearance)const desktopWallpaper = useThemedValue(appearance.desktopWallpaper)diff --git a/tests/desktop-preferences.test.ts b/tests/desktop-preferences.test.tsnew file mode 100644index 0000000..e5e4f6b--- /dev/null+++ b/tests/desktop-preferences.test.ts@@ -0,0 +1,20 @@+import { defaultDesktopScale, desktopPreferencesLimits } from "@phreshos/core"+import { describe, expect, it } from "vitest"+import { resolveStoredDesktopScale, serializeDesktopScalePreference } from "../source/client/view/appearance/desktop-preferences.js"++describe("Desktop preference persistence", () => {+ it("resolves a stored scale within the contract bounds", () => {+ expect(resolveStoredDesktopScale("1.25")).toBe(1.25)+ expect(resolveStoredDesktopScale(String(desktopPreferencesLimits.scale.minimum))).toBe(desktopPreferencesLimits.scale.minimum)+ expect(resolveStoredDesktopScale(String(desktopPreferencesLimits.scale.maximum))).toBe(desktopPreferencesLimits.scale.maximum)+ })++ it.each([null, "", "0.49", "2.01", "not-a-number"])("uses the default for stored value %s", value => {+ expect(resolveStoredDesktopScale(value)).toBe(defaultDesktopScale)+ })++ it("stores direct values and removes the override for default", () => {+ expect(serializeDesktopScalePreference(1.25)).toBe("1.25")+ expect(serializeDesktopScalePreference("default")).toBeNull()+ })+})diff --git a/tests/desktop-replacements.test.tsx b/tests/desktop-replacements.test.tsxindex cbd475e..798068f 100644--- a/tests/desktop-replacements.test.tsx+++ b/tests/desktop-replacements.test.tsx@@ -4,6 +4,7 @@ import { UIProvider } from "@phreshos/react-ui"import type Process from "@client/core/link-manager/auth-manager/process-manager/process"import Workspace from "@client/view/structure/desktop/desktop"import type { ReactNode } from "react"+import { defaultAppearance } from "@phreshos/core"const fixture = vi.hoisted(() => ({processes: new Map<string, Process>(),@@ -18,9 +19,13 @@ vi.mock("@client/view/contexts", () => ({["wallpaper-program", { assetId: "wallpaper-assets" }],["start-menu-program", { assetId: "start-menu-assets" }]]) }- }) }+ }) },+ LinkManagerContext: { useValue: () => ({ appearance: { value: defaultAppearance } }) }+}))+vi.mock("@the-link/react", () => ({+ ReactTunnel: { useFactory: () => ({ useSubscribe() {} }) },+ useProperty: (property: { value: unknown }) => property.value}))-vi.mock("@the-link/react", () => ({ ReactTunnel: { useFactory: () => ({ useSubscribe() {} }) } }))vi.mock("@client/view/components/desktop-host/client-host", () => ({ default: () => ({}) }))vi.mock("@client/view/components/program-access", () => ({ default: () => null }))vi.mock("@libs/readiness", () => ({ useRequirement: () => () => {} }))