11import {
22 DesktopServerExposureModeSchema ,
33 DesktopUpdateChannelSchema ,
4+ DesktopWindowBackgroundSchema ,
45 type DesktopServerExposureMode ,
56 type DesktopUpdateChannel ,
7+ type DesktopWindowBackground ,
8+ type DesktopWindowCanvas ,
69} from "@t3tools/contracts" ;
710import { fromLenientJson } from "@t3tools/shared/schemaJson" ;
811import { resolveSymlinkTarget } from "@t3tools/shared/symlink" ;
@@ -39,6 +42,8 @@ export interface DesktopSettings {
3942 readonly wslDistro : string | null ;
4043 // Requires restart because the pool chooses its primary spec at layer initialization.
4144 readonly wslOnly : boolean ;
45+ /** The renderer's theme canvases, painted before the page loads. */
46+ readonly windowBackground : DesktopWindowBackground | null ;
4247}
4348
4449export interface DesktopSettingsChange {
@@ -79,6 +84,7 @@ export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = {
7984 wslBackendEnabled : false ,
8085 wslDistro : null ,
8186 wslOnly : false ,
87+ windowBackground : null ,
8288} ;
8389
8490const DesktopWindowBoundsDocument = Schema . Struct ( {
@@ -102,6 +108,7 @@ const DesktopSettingsDocument = Schema.Struct({
102108 wslMode : Schema . optionalKey ( Schema . Literals ( [ "local" , "wsl" ] ) ) ,
103109 wslDistro : Schema . optionalKey ( Schema . NullOr ( Schema . String ) ) ,
104110 wslOnly : Schema . optionalKey ( Schema . Boolean ) ,
111+ windowCanvases : Schema . optionalKey ( Schema . Unknown ) ,
105112} ) ;
106113
107114type DesktopSettingsDocument = typeof DesktopSettingsDocument . Type ;
@@ -112,6 +119,8 @@ const decodeDesktopSettingsJson = Schema.decodeEffect(DesktopSettingsJson);
112119const encodeDesktopSettingsJson = Schema . encodeEffect ( DesktopSettingsJson ) ;
113120const decodeDesktopWindowBounds = Schema . decodeUnknownOption ( DesktopWindowBoundsSchema ) ;
114121const desktopWindowBoundsEquivalence = Schema . toEquivalence ( DesktopWindowBoundsSchema ) ;
122+ const decodeDesktopWindowBackground = Schema . decodeUnknownOption ( DesktopWindowBackgroundSchema ) ;
123+ const desktopWindowBackgroundEquivalence = Schema . toEquivalence ( DesktopWindowBackgroundSchema ) ;
115124
116125const settingsChange = ( settings : DesktopSettings , changed : boolean ) : DesktopSettingsChange => ( {
117126 settings,
@@ -172,6 +181,9 @@ export class DesktopAppSettings extends Context.Service<
172181 readonly setWslOnly : (
173182 enabled : boolean ,
174183 ) => Effect . Effect < DesktopSettingsChange , DesktopSettingsWriteError > ;
184+ readonly setWindowBackground : (
185+ background : DesktopWindowBackground ,
186+ ) => Effect . Effect < DesktopSettingsChange , DesktopSettingsWriteError > ;
175187 readonly applyWslWindowsFallback : Effect . Effect <
176188 DesktopSettingsChange ,
177189 DesktopSettingsWriteError
@@ -197,6 +209,21 @@ function normalizeWslDistro(value: unknown): string | null {
197209 return typeof value === "string" && isValidDistroName ( value ) ? value : null ;
198210}
199211
212+ function normalizeWindowColor ( value : unknown ) : string | null {
213+ return typeof value === "string" && / ^ # [ 0 - 9 a - f ] { 6 } $ / i. test ( value ) ? value . toLowerCase ( ) : null ;
214+ }
215+
216+ function normalizeWindowBackground ( value : unknown ) : DesktopWindowBackground | null {
217+ const canvas = ( { appearance, color } : DesktopWindowCanvas ) : DesktopWindowCanvas => ( {
218+ appearance,
219+ color : normalizeWindowColor ( color ) ,
220+ } ) ;
221+ return Option . match ( decodeDesktopWindowBackground ( value ) , {
222+ onNone : ( ) => null ,
223+ onSome : ( background ) => ( { light : canvas ( background . light ) , dark : canvas ( background . dark ) } ) ,
224+ } ) ;
225+ }
226+
200227export function normalizeMainWindowBounds ( value : unknown ) : DesktopWindowBounds | null {
201228 return Option . getOrNull ( decodeDesktopWindowBounds ( value ) ) ;
202229}
@@ -233,6 +260,7 @@ function normalizeDesktopSettingsDocument(
233260 wslBackendEnabled,
234261 wslDistro : normalizeWslDistro ( parsed . wslDistro ) ,
235262 wslOnly : parsed . wslOnly === true ,
263+ windowBackground : normalizeWindowBackground ( parsed . windowCanvases ) ,
236264 } ;
237265}
238266
@@ -279,6 +307,9 @@ function toDesktopSettingsDocument(
279307 if ( settings . wslOnly !== defaults . wslOnly ) {
280308 document . wslOnly = settings . wslOnly ;
281309 }
310+ if ( settings . windowBackground !== null ) {
311+ document . windowCanvases = settings . windowBackground ;
312+ }
282313
283314 return document ;
284315}
@@ -369,6 +400,18 @@ function setWslOnly(settings: DesktopSettings, enabled: boolean): DesktopSetting
369400 } ;
370401}
371402
403+ function setWindowBackground (
404+ settings : DesktopSettings ,
405+ background : DesktopWindowBackground ,
406+ ) : DesktopSettings {
407+ const normalized = normalizeWindowBackground ( background ) ;
408+ return settings . windowBackground !== null &&
409+ normalized !== null &&
410+ desktopWindowBackgroundEquivalence ( settings . windowBackground , normalized )
411+ ? settings
412+ : { ...settings , windowBackground : normalized } ;
413+ }
414+
372415function setLocalEnvironmentEnabled ( settings : DesktopSettings , enabled : boolean ) : DesktopSettings {
373416 return settings . localEnvironmentEnabled === enabled
374417 ? settings
@@ -561,6 +604,14 @@ export const make = Effect.gen(function* () {
561604
562605 return yield * persist ( ( settings ) => setWslOnly ( settings , enabled ) ) ;
563606 } ) ,
607+ setWindowBackground : Effect . fn ( "desktop.settings.setWindowBackground" ) ( function * ( background ) {
608+ yield * Effect . annotateCurrentSpan ( {
609+ light : background . light . color ,
610+ dark : background . dark . color ,
611+ } ) ;
612+
613+ return yield * persist ( ( settings ) => setWindowBackground ( settings , background ) ) ;
614+ } ) ,
564615 setLocalEnvironmentEnabled : Effect . fn ( "desktop.settings.setLocalEnvironmentEnabled" ) (
565616 function * ( enabled ) {
566617 yield * Effect . annotateCurrentSpan ( { enabled } ) ;
@@ -626,6 +677,11 @@ export const layerTest = (initialSettings: DesktopSettings = DEFAULT_DESKTOP_SET
626677 setWslOnly : Effect . fn ( "DesktopAppSettings.setWslOnly" ) ( function ( enabled ) {
627678 return update ( ( settings ) => setWslOnly ( settings , enabled ) ) ;
628679 } ) ,
680+ setWindowBackground : Effect . fn ( "DesktopAppSettings.setWindowBackground" ) (
681+ function ( background ) {
682+ return update ( ( settings ) => setWindowBackground ( settings , background ) ) ;
683+ } ,
684+ ) ,
629685 setLocalEnvironmentEnabled : Effect . fn ( "DesktopAppSettings.setLocalEnvironmentEnabled" ) (
630686 function ( enabled ) {
631687 return update ( ( settings ) => setLocalEnvironmentEnabled ( settings , enabled ) ) ;
0 commit comments