11import { AppProcess } from "$ts/apps/process" ;
2+ import { ConfigurationBuilder } from "$ts/config" ;
23import { Daemon } from "$ts/daemon" ;
34import { Env , Fs , SysDispatch } from "$ts/env" ;
45import { UserPaths } from "$ts/user/store" ;
5- import { arrayBufferToText , textToBlob } from "$ts/util/convert" ;
66import { MessageBox } from "$ts/util/dialog" ;
77import { getItemNameFromPath , join } from "$ts/util/fs" ;
8- import { tryJsonParse } from "$ts/util/json" ;
98import { Store } from "$ts/writable" ;
109import type { AppContextMenu , AppProcessData } from "$types/app" ;
1110import type { DirectoryReadReturn } from "$types/fs" ;
@@ -23,7 +22,14 @@ export class WallpaperRuntime extends AppProcess {
2322 orphaned = Store < string [ ] > ( [ ] ) ;
2423 loading = Store < boolean > ( false ) ;
2524 directory : string ;
26- Configuration = Store < DesktopIcons > ( { } ) ;
25+ Positions = Store < DesktopIcons > ( { } ) ;
26+ Configuration = new ConfigurationBuilder < DesktopIcons > ( )
27+ . ForProcess ( this )
28+ . ReadsFrom ( this . Positions )
29+ . WritesTo ( this . CONFIG_PATH )
30+ . WithDefaults ( { } )
31+ . WithCooldown ( 500 )
32+ . Build ( ) ;
2733
2834 public contextMenu : AppContextMenu = WallpaperContextMenu ( this ) ;
2935
@@ -45,18 +51,7 @@ export class WallpaperRuntime extends AppProcess {
4551 }
4652
4753 async start ( ) {
48- const migrated = await this . migrateDesktopIcons ( ) ;
49- if ( ! migrated ) await this . loadConfiguration ( ) ;
50-
51- let firstSub = false ;
52-
53- this . Configuration . subscribe ( ( v ) => {
54- if ( ! firstSub ) {
55- firstSub = true ;
56- return ;
57- }
58- this . writeConfiguration ( v ) ;
59- } ) ;
54+ await this . Configuration . initialize ( ) ;
6055 }
6156
6257 async render ( ) {
@@ -105,7 +100,7 @@ export class WallpaperRuntime extends AppProcess {
105100
106101 findAndDeleteOrphans ( contents : DirectoryReadReturn | undefined ) {
107102 const orphaned = this . orphaned ( ) ;
108- const config = this . Configuration ( ) ;
103+ const config = this . Positions ( ) ;
109104 let orphanedCount = 0 ;
110105
111106 for ( const id of Object . keys ( config ) ) {
@@ -123,7 +118,7 @@ export class WallpaperRuntime extends AppProcess {
123118 }
124119 }
125120
126- if ( orphanedCount ) this . Configuration . set ( config ) ;
121+ if ( orphanedCount ) this . Positions . set ( config ) ;
127122 this . orphaned . set ( orphaned ) ;
128123 }
129124
@@ -133,7 +128,7 @@ export class WallpaperRuntime extends AppProcess {
133128 if ( ! wrapper ) return { x : 0 , y : 0 } ;
134129
135130 return new Promise ( ( r ) => {
136- this . Configuration . update ( ( v ) => {
131+ this . Positions . update ( ( v ) => {
137132 function resolve ( x : number , y : number ) {
138133 r ( { x, y } ) ;
139134 v [ `icon$${ identifier } ` ] = { x, y } ;
@@ -247,55 +242,5 @@ export class WallpaperRuntime extends AppProcess {
247242 prog . mutDone ( + 1 ) ;
248243 }
249244
250- //#endregion
251- //#region CONFIGURATION
252-
253- async loadConfiguration ( ) {
254- this . Log ( `Loading configuration` ) ;
255-
256- try {
257- const contents = await Fs . readFile ( this . CONFIG_PATH ) ;
258- if ( ! contents ) return await this . writeConfiguration ( { } ) ;
259-
260- const json = tryJsonParse < DesktopIcons > ( arrayBufferToText ( contents ) ) ;
261- if ( ! json || typeof json === "string" ) return await this . writeConfiguration ( { } ) ;
262-
263- this . Configuration . set ( json ) ;
264- } catch { }
265- }
266-
267- async writeConfiguration ( data : DesktopIcons ) {
268- this . Log ( `Writing configuration` ) ;
269-
270- await Fs . writeFile ( this . CONFIG_PATH , textToBlob ( JSON . stringify ( data , null , 2 ) ) ) ;
271-
272- return data ;
273- }
274-
275- // 7.0.5 -> 7.0.6+
276- // Migration of desktop icons from the preferences to a dedicated file in U:/System
277- async migrateDesktopIcons ( ) {
278- this . Log ( `migrateDesktopIcons` ) ;
279-
280- const migrationPath = join ( UserPaths . Migrations , "DeskIconMig-706.lock" ) ;
281- const pref = this . userPreferences ( ) . appPreferences . desktopIcons ;
282- const migration = await Fs . stat ( migrationPath ) ;
283-
284- if ( pref && ! migration ) {
285- await this . writeConfiguration ( pref ) ;
286- this . Configuration . set ( pref ) ;
287-
288- this . userPreferences . update ( ( v ) => {
289- delete v . appPreferences . desktopIcons ;
290- return v ;
291- } ) ;
292-
293- await Fs . writeFile ( migrationPath , textToBlob ( `${ Date . now ( ) } ` ) ) ;
294- return true ;
295- }
296-
297- return false ;
298- }
299-
300245 //#endregion
301246}
0 commit comments