Skip to content

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

Own desktop preferences in the Desktop

zohayrslilehab2e8c98a66f
diff --git a/bun.lock b/bun.lock
index c88f801..487bf6f 100644
--- a/bun.lock
+++ b/bun.lock
@@ -16,7 +16,6 @@
"gsap": "^3.15.0",
"hono": "^4.13.2",
"keyv": "^5.6.0",
- "next-themes": "^0.4.6",
"node-rsa": "^2.0.0",
"react": "^19.2.8",
"react-aria": "^3.51.0",
@@ -456,8 +455,6 @@
"nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w=="],
- "next-themes": ["[email protected]", "", { "peerDependencies": { "react": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc", "react-dom": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc" } }, "sha512-pZvgD5L0IEvX5/9GWyHMf3m8BKiVQwsCMHfoFosXtXBMnaS0ZnIJ9ST4b4NqLVKDEm8QBxoNNGNaBv2JNF6XNA=="],
-
"node-releases": ["[email protected]", "", {}, "sha512-D9UOmYG3UH1V+ENW56t5QXBwJw1YEY18ruVeus89Rw+SyIgjPkCO84bRzO3uNIYosJbNwiabWVn48o3uJLjxFQ=="],
"node-rsa": ["[email protected]", "", { "dependencies": { "@noble/hashes": "^2.2.0" } }, "sha512-7yfzccmBd5rm7pALJf709tPwLL987WvdT7LC/QK5Oxa5S2aabdBPjxRDxhWKkbfCc9/FiC82lwSc+ulGZHH44g=="],
diff --git a/package.json b/package.json
index 8bc37be..c838ab6 100644
--- a/package.json
+++ b/package.json
@@ -46,7 +46,6 @@
"gsap": "^3.15.0",
"hono": "^4.13.2",
"keyv": "^5.6.0",
- "next-themes": "^0.4.6",
"node-rsa": "^2.0.0",
"react": "^19.2.8",
"react-aria": "^3.51.0",
diff --git a/source/client/core/link-manager/link-manager.ts b/source/client/core/link-manager/link-manager.ts
index ebf03b5..a24d7b2 100644
--- a/source/client/core/link-manager/link-manager.ts
+++ b/source/client/core/link-manager/link-manager.ts
@@ -2,7 +2,7 @@ import { TransmittedAuthManager } from "@server/core/link-manager/auth-manager/a
import { TransmittedLinkManager } from "@server/core/link-manager/link-manager"
import { type AuthenticationState } from "@server/core/authentication/authentication"
import { type RegistrationResponse } from "@server/core/link-manager/link-manager"
-import { type Appearance, type Theme, type ThemePreference } from "@phreshos/core"
+import { type Appearance, type DesktopPreferences, type DesktopPreferencesUpdate } from "@phreshos/core"
import { Forward } from "@libs/the-link/decorators/escript"
import TheLink from "@libs/the-link/the-link"
import Property from "@libs/the-link/property"
@@ -21,13 +21,10 @@ export default class LinkManager extends TheLink {
/** Immediate unresolved System Appearance and future authoritative updates. */
public readonly appearance: Property<Appearance>
- /** Effective local Desktop Theme and its future updates. */
- public readonly theme: Property<Theme>
+ /** Complete effective local Desktop preferences and future updates. */
+ public readonly desktopPreferences: Property<DesktopPreferences>
- private themePreference: ThemePreference = "default"
- private readonly nativeTheme = matchMedia("(prefers-color-scheme: dark)")
-
- public constructor(application: Application, sourceLink: TheLink, payload: TransmittedLinkManager, connectionIdentity: string) {
+ public constructor(application: Application, sourceLink: TheLink, payload: TransmittedLinkManager, connectionIdentity: string, desktopPreferences: DesktopPreferences) {
super()
@@ -41,28 +38,15 @@ export default class LinkManager extends TheLink {
this.appearance = Property.consumer(this, payload.appearance.key, payload.appearance.value)
- this.theme = Property.private(this, this.effectiveTheme())
-
- this.nativeTheme.addEventListener("change", this.nativeThemeChanged)
- }
-
- public async updateTheme(theme: ThemePreference) {
- this.themePreference = theme
- await this.$inbound.publish("/change-theme", theme)
- }
-
- private effectiveTheme(): Theme {
- return this.themePreference === "default"
- ? this.nativeTheme.matches ? "dark" : "light"
- : this.themePreference
+ this.desktopPreferences = Property.private(this, desktopPreferences)
}
- private readonly nativeThemeChanged = () => {
- if (this.themePreference === "default") void this.theme.update(this.effectiveTheme())
+ public requestDesktopPreferences(preferences: DesktopPreferencesUpdate) {
+ return this.$inbound.publish("/change-desktop-preferences", preferences)
}
- public dispose() {
- this.nativeTheme.removeEventListener("change", this.nativeThemeChanged)
+ public updateDesktopPreferences(preferences: DesktopPreferences) {
+ return this.desktopPreferences.update(preferences)
}
@Forward("outbound")
diff --git a/source/client/view/appearance/appearance.tsx b/source/client/view/appearance/appearance.tsx
index b9a52ff..f80d6e0 100644
--- a/source/client/view/appearance/appearance.tsx
+++ b/source/client/view/appearance/appearance.tsx
@@ -1,29 +1,34 @@
import ReducedMotion from "@libs/react-motion"
import { standardAppearance } from "@phreshos/core"
-import { ThemeProvider } from "next-themes"
import { ReactNode } from "react"
+import DesktopPreferencesProvider, { useDesktopPreferences } from "./desktop-preferences"
import "./appearance.css"
-export default function ({ children, reducedMotion }: AppearanceProps) {
+export default function ({ children }: AppearanceProps) {
- return <ThemeProvider attribute="class" defaultTheme="system">
+ return <DesktopPreferencesProvider>
- <ReducedMotion reduced={reducedMotion}>
+ <AppearanceRoot>{children}</AppearanceRoot>
- <div className="relative isolate grid h-dvh overflow-hidden font-roboto" style={{ backgroundColor: standardAppearance.background.light }}>
+ </DesktopPreferencesProvider>
+}
+
+function AppearanceRoot({ children }: AppearanceProps) {
+ const { preferences } = useDesktopPreferences()
+ const background = standardAppearance.background[preferences.theme]
- {children}
+ return <ReducedMotion reduced={!preferences.animations}>
- </div>
+ <div className="relative isolate grid h-dvh overflow-hidden font-roboto" style={{ backgroundColor: background }}>
- </ReducedMotion>
+ {children}
- </ThemeProvider>
+ </div>
+
+ </ReducedMotion>
}
interface AppearanceProps {
children: ReactNode
-
- reducedMotion?: boolean
}
diff --git a/source/client/view/appearance/desktop-preferences.tsx b/source/client/view/appearance/desktop-preferences.tsx
new file mode 100644
index 0000000..992eea1
--- /dev/null
+++ b/source/client/view/appearance/desktop-preferences.tsx
@@ -0,0 +1,84 @@
+import useStorage from "@libs/storage-hook"
+import {
+ type DesktopPreferences,
+ type DesktopPreferencesUpdate,
+ type Theme
+} from "@phreshos/core"
+import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useSyncExternalStore, type ReactNode } from "react"
+
+const themeQuery = "(prefers-color-scheme: dark)"
+const reducedMotionQuery = "(prefers-reduced-motion: reduce)"
+const themeKey = "desktop-preferences:theme"
+const animationsKey = "desktop-preferences:animations"
+
+const DesktopPreferencesContext = createContext<DesktopPreferencesOwner | null>(null)
+
+/** Owns this browser Desktop's persisted choices and native defaults. */
+export default function DesktopPreferencesProvider({ children }: Readonly<{ children: ReactNode }>) {
+ const storedTheme = useStorage(themeKey)
+ const storedAnimations = useStorage(animationsKey)
+ const nativeDark = useMediaPreference(themeQuery)
+ const nativeReducedMotion = useMediaPreference(reducedMotionQuery)
+ const theme = selectedTheme(storedTheme.value, nativeDark)
+ const animations = selectedAnimations(storedAnimations.value, nativeReducedMotion)
+ const preferences = useMemo<DesktopPreferences>(() => ({ theme, animations }), [animations, theme])
+
+ const update = useCallback(function (change: DesktopPreferencesUpdate) {
+ if (change.theme !== undefined) {
+ if (change.theme === "default") storedTheme.remove()
+ else storedTheme.update(change.theme)
+ }
+
+ if (change.animations !== undefined) {
+ if (change.animations === "default") storedAnimations.remove()
+ else storedAnimations.update(change.animations ? "enabled" : "disabled")
+ }
+ }, [storedAnimations.remove, storedAnimations.update, storedTheme.remove, storedTheme.update])
+
+ useLayoutEffect(() => {
+ const root = document.documentElement
+ const previous = root.style.colorScheme
+
+ root.style.colorScheme = preferences.theme
+
+ return () => { root.style.colorScheme = previous }
+ }, [preferences.theme])
+
+ const owner = useMemo(() => ({ preferences, update }), [preferences, update])
+
+ return <DesktopPreferencesContext.Provider value={owner}>{children}</DesktopPreferencesContext.Provider>
+}
+
+/** Reads the complete effective state and its View-owned update operation. */
+export function useDesktopPreferences() {
+ const owner = useContext(DesktopPreferencesContext)
+ if (!owner) throw new Error("useDesktopPreferences() requires DesktopPreferencesProvider")
+ return owner
+}
+
+function selectedTheme(value: string | null, nativeDark: boolean): Theme {
+ return value === "light" || value === "dark" ? value : nativeDark ? "dark" : "light"
+}
+
+function selectedAnimations(value: string | null, nativeReducedMotion: boolean) {
+ if (value === "enabled") return true
+ if (value === "disabled") return false
+ return !nativeReducedMotion
+}
+
+function useMediaPreference(query: string) {
+ const media = useMemo(() => matchMedia(query), [query])
+ return useSyncExternalStore(
+ change => {
+ media.addEventListener("change", change)
+ return () => media.removeEventListener("change", change)
+ },
+ () => media.matches,
+ () => false
+ )
+}
+
+interface DesktopPreferencesOwner {
+ readonly preferences: DesktopPreferences
+ readonly update: (change: DesktopPreferencesUpdate) => void
+}
diff --git a/source/client/view/components/desktop-host/client-process-boundary.ts b/source/client/view/components/desktop-host/client-process-boundary.ts
index 4a64a58..9cc03a5 100644
--- a/source/client/view/components/desktop-host/client-process-boundary.ts
+++ b/source/client/view/components/desktop-host/client-process-boundary.ts
@@ -38,9 +38,9 @@ export default class ClientProcessBoundary extends TheLink {
private readonly trafficSubscriptions = new Map<string, TrafficSubscription>()
- private readonly themeSubscriptions = new Set<string>()
+ private readonly desktopPreferencesSubscriptions = new Set<string>()
- private stopTheme: (() => void) | null = null
+ private stopDesktopPreferences: (() => void) | null = null
private readonly appearanceSubscriptions = new Set<string>()
@@ -386,13 +386,13 @@ export default class ClientProcessBoundary extends TheLink {
if (this.owner && directSubscription(description)) this.authManager.processManager.subscribeFrame(this.pane, this.owner, subscription, kind, event).catch(() => undefined)
- if (themeSubscription(description)) {
+ if (desktopPreferencesSubscription(description)) {
- this.themeSubscriptions.add(subscription)
+ this.desktopPreferencesSubscriptions.add(subscription)
- if (!this.stopTheme) this.stopTheme = this.authManager.linkManager.theme.tunnel.subscribe("change", (theme: unknown) => {
+ if (!this.stopDesktopPreferences) this.stopDesktopPreferences = this.authManager.linkManager.desktopPreferences.tunnel.subscribe("change", (preferences: unknown) => {
- this.deliver("host-theme", "change", theme).catch(() => undefined)
+ this.deliver("host-desktop-preferences", "change", preferences).catch(() => undefined)
})
}
@@ -713,11 +713,11 @@ export default class ClientProcessBoundary extends TheLink {
this.subscriptions.delete(subscription)
- if (this.themeSubscriptions.delete(subscription) && this.themeSubscriptions.size === 0) {
+ if (this.desktopPreferencesSubscriptions.delete(subscription) && this.desktopPreferencesSubscriptions.size === 0) {
- this.stopTheme?.()
+ this.stopDesktopPreferences?.()
- this.stopTheme = null
+ this.stopDesktopPreferences = null
}
if (this.appearanceSubscriptions.delete(subscription) && this.appearanceSubscriptions.size === 0) {
@@ -859,13 +859,13 @@ export default class ClientProcessBoundary extends TheLink {
this.blockedSubscriptions.clear()
- this.themeSubscriptions.clear()
+ this.desktopPreferencesSubscriptions.clear()
this.appearanceSubscriptions.clear()
- this.stopTheme?.()
+ this.stopDesktopPreferences?.()
- this.stopTheme = null
+ this.stopDesktopPreferences = null
this.stopAppearance?.()
@@ -1001,9 +1001,9 @@ function pointerSubscription(subscription: EndpointSubscription) {
return subscription.kind === "publish" && subscription.route === "host-pointer" && (subscription.event === null || subscription.event === "move")
}
-function themeSubscription(subscription: EndpointSubscription) {
+function desktopPreferencesSubscription(subscription: EndpointSubscription) {
- return subscription.kind === "publish" && subscription.route === "host-theme" && (subscription.event === null || subscription.event === "change")
+ return subscription.kind === "publish" && subscription.route === "host-desktop-preferences" && (subscription.event === null || subscription.event === "change")
}
function appearanceSubscription(subscription: EndpointSubscription) {
@@ -1013,7 +1013,7 @@ function appearanceSubscription(subscription: EndpointSubscription) {
function localPropertySubscription(subscription: EndpointSubscription) {
- return themeSubscription(subscription) || appearanceSubscription(subscription)
+ return desktopPreferencesSubscription(subscription) || appearanceSubscription(subscription)
}
function directSubscription(subscription: EndpointSubscription) {
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index 545a74d..6039ac9 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -5,7 +5,7 @@ import { type ClientBody, type ProxiedResponse, type UploadValue } from "@client
import { type PointerHost } from "./pointer"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
import { sdkProcess, sdkProgram } from "./sdk-records"
-import { isPermissionName, isServiceKey, isUploadFile, type ProgramIconSize } from "@phreshos/core"
+import { isPermissionName, isServiceKey, isUploadFile, type DesktopPreferencesUpdate, type ProgramIconSize } from "@phreshos/core"
import {
localGeometry,
localPosition,
@@ -89,13 +89,13 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "appearance") return [authManager.linkManager.appearance.value]
- if (word === "theme") return [authManager.linkManager.theme.value]
+ if (word === "desktopPreferences") return [authManager.linkManager.desktopPreferences.value]
- if (word === "update-theme") {
+ if (word === "updateDesktopPreferences") {
- if (args[0] !== "light" && args[0] !== "dark" && args[0] !== "default") throw new Error("A Theme is light, dark, or default")
+ const preferences = desktopPreferencesUpdate(args[0])
- await authManager.linkManager.updateTheme(args[0])
+ await authManager.linkManager.requestDesktopPreferences(preferences)
return []
}
@@ -838,6 +838,25 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
}
}
+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>
+ const keys = Object.keys(preferences)
+
+ if (keys.length === 0 || keys.some(key => key !== "theme" && key !== "animations")) 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 }) as DesktopPreferencesUpdate
+}
+
function isTrafficKind(value: unknown): value is TrafficKind {
return value === "publish" || value === "ask" || value === "answer"
diff --git a/source/client/view/structure/structure.tsx b/source/client/view/structure/structure.tsx
index 74e489e..f7f424d 100644
--- a/source/client/view/structure/structure.tsx
+++ b/source/client/view/structure/structure.tsx
@@ -4,15 +4,15 @@ import { TransmittedLinkManager } from "@server/core/link-manager/link-manager"
import SocketLink from "@libs/the-link/plugins/client-link/socket-link"
import LinkManager from "@client/core/link-manager/link-manager"
import { AppearanceProvider, useResolveTheme } from "@phreshos/react-ui"
-import { useTheme } from "next-themes"
import Loading from "../components/loading"
import Alert from "../components/alert"
import { ApplicationContext, LinkManagerContext } from "../contexts"
import usePromise from "@libs/react-promise"
+import { useDesktopPreferences } from "../appearance/desktop-preferences"
import Authentication from "./authentication/authentication"
import { useCallback, useEffect, useState } from "react"
import Readiness, { useReady } from "@libs/readiness/main"
-import { standardAppearance, type ThemePreference } from "@phreshos/core"
+import { standardAppearance, type DesktopPreferencesUpdate } from "@phreshos/core"
const startupRequirements = ["connection", "session", "wallpaper"] as const
@@ -42,6 +42,7 @@ export default function () {
function Desktop() {
const application = ApplicationContext.useValue()
+ const { preferences } = useDesktopPreferences()
const [linkManager, setLinkManager] = useState<LinkManager | null>(null)
@@ -53,17 +54,14 @@ function Desktop() {
const [payload, connectionIdentity] = socketLink.payload
- const manager = new LinkManager(application, socketLink, payload, connectionIdentity)
+ const manager = new LinkManager(application, socketLink, payload, connectionIdentity, preferences)
socketLink.$internal.subscribeOnce("unsubscribe", () => {
-
- manager.dispose()
-
setLinkManager(current => current === manager ? null : current)
})
setLinkManager(manager)
- }, [application]))
+ }, [application, preferences]))
const connection = usePromise(async () => application.clientLink.subscribe(), [application, connectionRevision])
@@ -101,25 +99,23 @@ function ConnectedDesktop({ linkManager }: { linkManager: LinkManager }) {
useReady("connection")
- const { resolvedTheme, setTheme } = useTheme()
+ const { preferences, update } = useDesktopPreferences()
const inbound = ReactTunnel.useFactory(linkManager.$inbound)
- inbound.useSubscribe("/change-theme", useCallback(function (theme: ThemePreference) {
- setTheme(theme === "default" ? "system" : theme)
- }, [setTheme]))
+ inbound.useSubscribe("/change-desktop-preferences", useCallback(function (change: DesktopPreferencesUpdate) {
+ update(change)
+ }, [update]))
const appearance = useProperty(linkManager.appearance)
- const theme = resolvedTheme === "dark" ? "dark" : "light"
-
useEffect(function () {
- void linkManager.theme.update(theme)
- }, [linkManager, theme])
+ void linkManager.updateDesktopPreferences(preferences)
+ }, [linkManager, preferences])
return <LinkManagerContext.Provider value={linkManager}>
- <AppearanceProvider appearance={appearance} theme={theme}>
+ <AppearanceProvider appearance={appearance} theme={preferences.theme}>
<ConnectedAppearance>