Skip to content

The agent that grows with you

feat(desktop): one function applies set_theme values

alt-glitch3812eaa3460d
diff --git a/apps/desktop/src/store/appearance-apply.test.ts b/apps/desktop/src/store/appearance-apply.test.ts
new file mode 100644
index 00000000000..b378f221cad
--- /dev/null
+++ b/apps/desktop/src/store/appearance-apply.test.ts
@@ -0,0 +1,41 @@
+import { afterEach, beforeAll, describe, expect, it } from 'vitest'
+
+import { registerLayoutPresets } from '@/app/contrib/layout-presets'
+import { $activePresetId } from '@/components/pane-shell/tree/store'
+import { $pendingSkinApply } from '@/themes/backend-sync'
+import { $pendingModeApply } from '@/themes/request'
+
+import { applyAppearance } from './appearance-apply'
+import { $interfaceMode, setInterfaceMode } from './interface-mode'
+
+describe('applyAppearance', () => {
+ beforeAll(() => {
+ registerLayoutPresets()
+ })
+
+ afterEach(() => {
+ $pendingSkinApply.set(null)
+ $pendingModeApply.set(null)
+ })
+
+ it('applies every value and reports the ids the agent reads', () => {
+ setInterfaceMode('simple')
+
+ expect(applyAppearance({ accent: 'violet', layout: 'terminal-deck', mode: 'dark' })).toEqual({
+ applied: { accent: 'violet', layout: 'terminal-deck', mode: 'dark' },
+ success: true
+ })
+ expect($pendingSkinApply.get()).toBe('nous-accent-8a2be2')
+ expect($pendingModeApply.get()).toBe('dark')
+ expect($activePresetId.get()).toBe('terminal-deck')
+ expect($interfaceMode.get()).toBe('advanced')
+ })
+
+ it('applies nothing when one value is unknown, and names what exists', () => {
+ const answer = applyAppearance({ accent: 'violet', layout: 'cockpit' })
+
+ expect(answer.success).toBe(false)
+ expect(!answer.success && answer.available).toEqual(expect.arrayContaining(['sidebar-left', 'terminal-deck']))
+ expect($pendingSkinApply.get()).toBeNull()
+ })
+})
diff --git a/apps/desktop/src/store/appearance-apply.ts b/apps/desktop/src/store/appearance-apply.ts
new file mode 100644
index 00000000000..b0bac65f698
--- /dev/null
+++ b/apps/desktop/src/store/appearance-apply.ts
@@ -0,0 +1,106 @@
+/**
+ * What `set_theme` and its picker cards change: accent, light/dark mode and
+ * layout preset, validated as a whole before anything is applied so an
+ * unknown layout never leaves a new accent half-applied.
+ *
+ * Accent persists through `persistAccent` and mode through `requestMode`
+ * (both drained by the ThemeProvider); a layout applies through the same
+ * registry the layout picker reads, switching Simple/Advanced to the preset's
+ * tier first because Simple only shows its two sidebar presets.
+ */
+
+import { isLayoutNode } from '@/components/pane-shell/tree/model'
+import { layoutPresetTier, LAYOUTS_AREA } from '@/components/pane-shell/tree/presets'
+import { registry } from '@/contrib/registry'
+import { matchesQuery } from '@/hooks/use-media-query'
+import { ACCENT_SWATCH_IDS, resolveAccentPick, type ResolvedAccent } from '@/themes/accents'
+import type { ThemeMode } from '@/themes/context'
+import { persistAccent } from '@/themes/persist-accent'
+import { requestMode } from '@/themes/request'
+
+import { $interfaceMode, setInterfaceMode } from './interface-mode'
+import { applyDesktopLayoutPreset } from './pane-focus'
+
+export const THEME_MODES: readonly ThemeMode[] = ['light', 'dark', 'system']
+
+export interface AppearanceValues {
+ accent?: string
+ layout?: string
+ mode?: ThemeMode
+}
+
+/** What was applied, in the ids the agent reads: `accent` is a swatch id, `custom:#rrggbb`, or null for the theme's own. */
+export interface AppliedAppearance {
+ accent?: null | string
+ layout?: string
+ mode?: ThemeMode
+}
+
+export type AppearanceAnswer =
+ | { applied: AppliedAppearance; success: true }
+ | { available: string[]; error: string; success: false }
+
+const listedLayoutIds = (): string[] =>
+ registry
+ .getArea(LAYOUTS_AREA)
+ .filter(entry => isLayoutNode(entry.data))
+ .map(entry => entry.id)
+
+// Mono is black on a light surface and white on a dark one: resolve against the surface the pick lands on.
+function paintsDark(mode: ThemeMode | undefined): boolean {
+ if (mode === 'system') {
+ return matchesQuery('(prefers-color-scheme: dark)')
+ }
+
+ return mode === undefined ? document.documentElement.classList.contains('dark') : mode === 'dark'
+}
+
+function applyLayout(id: string): void {
+ // A user's saved layouts are Advanced, like the layout picker's Custom section.
+ const mode = layoutPresetTier(id) === 'simple' ? 'simple' : 'advanced'
+
+ if ($interfaceMode.get() !== mode) {
+ setInterfaceMode(mode)
+ }
+
+ applyDesktopLayoutPreset(id)
+}
+
+export function applyAppearance(values: AppearanceValues): AppearanceAnswer {
+ if (values.mode !== undefined && !THEME_MODES.includes(values.mode)) {
+ return { available: [...THEME_MODES], error: `Unknown mode "${values.mode}".`, success: false }
+ }
+
+ let accent: ResolvedAccent | undefined
+
+ if (values.accent !== undefined) {
+ accent = resolveAccentPick(values.accent, paintsDark(values.mode))
+
+ if (!accent) {
+ return { available: [...ACCENT_SWATCH_IDS, 'theme'], error: `Unknown accent "${values.accent}".`, success: false }
+ }
+ }
+
+ if (values.layout !== undefined && !listedLayoutIds().includes(values.layout)) {
+ return { available: listedLayoutIds(), error: `Unknown layout "${values.layout}".`, success: false }
+ }
+
+ const applied: AppliedAppearance = {}
+
+ if (values.mode !== undefined) {
+ requestMode(values.mode)
+ applied.mode = values.mode
+ }
+
+ if (accent) {
+ persistAccent(accent.hex)
+ applied.accent = accent.id
+ }
+
+ if (values.layout !== undefined) {
+ applyLayout(values.layout)
+ applied.layout = values.layout
+ }
+
+ return { applied, success: true }
+}
diff --git a/apps/desktop/src/themes/context.tsx b/apps/desktop/src/themes/context.tsx
index 7cdc326a30b..e581fa801a1 100644
--- a/apps/desktop/src/themes/context.tsx
+++ b/apps/desktop/src/themes/context.tsx
@@ -32,6 +32,7 @@ import {
import { $chatFontFamily, resolveChatFontFamily } from './chat-font'
import { harmonize, readableInk } from './color'
import { BUILTIN_THEME_LIST, DEFAULT_SKIN_NAME, DEFAULT_TYPOGRAPHY, nousTheme, RETIRED_SKINS } from './presets'
+import { $pendingModeApply } from './request'
import { retintTheme } from './retint'
import type { DesktopTheme, DesktopThemeColors, DesktopThemeTypography } from './types'
import { $userThemes, listAllThemes, resolveTheme } from './user-themes'
@@ -618,6 +619,16 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
}
}, [pendingSkin, setTheme])
+ // Same door for light/dark/system (set_theme from the agent).
+ const pendingMode = useStore($pendingModeApply)
+
+ useEffect(() => {
+ if (pendingMode) {
+ setMode(pendingMode)
+ $pendingModeApply.set(null)
+ }
+ }, [pendingMode, setMode])
+
// The light/dark toggle (Shift+X by default) is owned by the keybind runtime
// (`appearance.toggleMode`) so it shows up in the hotkey map and is rebindable.
diff --git a/apps/desktop/src/themes/request.test.tsx b/apps/desktop/src/themes/request.test.tsx
index 538599297e1..3c8e8f52f92 100644
--- a/apps/desktop/src/themes/request.test.tsx
+++ b/apps/desktop/src/themes/request.test.tsx
@@ -4,9 +4,9 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { registry } from '@/contrib/registry'
import { __resetBackendSkinSync } from './backend-sync'
-import { skinPref, ThemeProvider, useTheme } from './context'
+import { modePref, skinPref, ThemeProvider, useTheme } from './context'
import { midnightTheme } from './presets'
-import { requestTheme } from './request'
+import { requestMode, requestTheme } from './request'
import type { DesktopTheme } from './types'
import { THEMES_AREA } from './user-themes'
@@ -57,6 +57,15 @@ describe('requestTheme', () => {
expect(skinPref.resolve('default')).toBe('midnight')
})
+ it('switches and persists light/dark from outside React', () => {
+ renderProbe()
+
+ act(() => requestMode('dark'))
+
+ expect(ctx.mode).toBe('dark')
+ expect(modePref.resolve('default')).toBe('dark')
+ })
+
it('refuses a name that does not resolve, leaving the appearance untouched', () => {
renderProbe()
diff --git a/apps/desktop/src/themes/request.ts b/apps/desktop/src/themes/request.ts
index e806c2d464f..b002dc22e62 100644
--- a/apps/desktop/src/themes/request.ts
+++ b/apps/desktop/src/themes/request.ts
@@ -10,7 +10,10 @@
* exactly like a manual pick — one policy, one owner.
*/
+import { atom } from 'nanostores'
+
import { $pendingSkinApply } from './backend-sync'
+import type { ThemeMode } from './context'
import { resolveTheme } from './user-themes'
/**
@@ -32,3 +35,11 @@ export function requestTheme(name: string): boolean {
return true
}
+
+/** One-shot light/dark/system switch the ThemeProvider drains through `setMode` (it clears this). */
+export const $pendingModeApply = atom<null | ThemeMode>(null)
+
+/** Ask for a mode switch from outside React; persisted per profile like a manual pick. */
+export function requestMode(mode: ThemeMode): void {
+ $pendingModeApply.set(mode)
+}