Open History to see the last commit
1import {
2 DesktopServerExposureModeSchema,
3 DesktopUpdateChannelSchema,
4 type DesktopServerExposureMode,
5 type DesktopUpdateChannel,
6} from "@t3tools/contracts";
7import { fromLenientJson } from "@t3tools/shared/schemaJson";
8import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
9import * as Context from "effect/Context";
10import * as SecureRandom from "effect/crypto/SecureRandom";
11import * as Effect from "effect/Effect";
12import * as FileSystem from "effect/FileSystem";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as Path from "effect/Path";
16import * as PlatformError from "effect/PlatformError";
17import * as Ref from "effect/Ref";
18import * as Schema from "effect/Schema";
19import * as SynchronizedRef from "effect/SynchronizedRef";
20
21import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
22import {
23 DEFAULT_LINUX_PASSWORD_STORE,
24 normalizeLinuxPasswordStorePreference,
25 type LinuxPasswordStorePreference,
26} from "../linuxSecretStorage.ts";
27import { desktopUpdateChannelOf } from "@t3tools/shared/cliRelease";
28import { isValidDistroName } from "../wsl/wslPathParsing.ts";
29
30export interface DesktopSettings {
31 readonly localEnvironmentEnabled: boolean;
32 readonly linuxPasswordStore: LinuxPasswordStorePreference;
33 readonly mainWindowBounds: DesktopWindowBounds | null;
34 readonly mainWindowMaximized: boolean;
35 readonly serverExposureMode: DesktopServerExposureMode;
36 readonly tailscaleServeEnabled: boolean;
37 readonly tailscaleServePort: number;
38 readonly updateChannel: DesktopUpdateChannel;
39 readonly updateChannelConfiguredByUser: boolean;
40 readonly wslBackendEnabled: boolean;
41 readonly wslDistro: string | null;
42 // Requires restart because the pool chooses its primary spec at layer initialization.
43 readonly wslOnly: boolean;
44}
45
46export interface DesktopSettingsChange {
47 readonly settings: DesktopSettings;
48 readonly changed: boolean;
49}
50
51const DEFAULT_TAILSCALE_SERVE_PORT = 443;
52// Fits half and third tiles of common scaled displays (down to 480x432 DIP) on
53// Wayland, where Electron's client-side frame adds 32x42 DIP to the enforced
54// minimum. The web layout already handles phone widths.
55export const MIN_MAIN_WINDOW_SIZE = {
56 width: 400,
57 height: 360,
58} as const;
59export const DesktopWindowBoundsSchema = Schema.Struct({
60 x: Schema.Int,
61 y: Schema.Int,
62 width: Schema.Int.check(Schema.isGreaterThanOrEqualTo(MIN_MAIN_WINDOW_SIZE.width)),
63 height: Schema.Int.check(Schema.isGreaterThanOrEqualTo(MIN_MAIN_WINDOW_SIZE.height)),
64});
65export type DesktopWindowBounds = typeof DesktopWindowBoundsSchema.Type;
66export const DEFAULT_MAIN_WINDOW_SIZE = {
67 width: 1100,
68 height: 780,
69} as const;
70
71export const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = {
72 localEnvironmentEnabled: true,
73 linuxPasswordStore: DEFAULT_LINUX_PASSWORD_STORE,
74 mainWindowBounds: null,
75 mainWindowMaximized: false,
76 serverExposureMode: "local-only",
77 tailscaleServeEnabled: false,
78 tailscaleServePort: DEFAULT_TAILSCALE_SERVE_PORT,
79 updateChannel: "latest",
80 updateChannelConfiguredByUser: false,
81 wslBackendEnabled: false,
82 wslDistro: null,
83 wslOnly: false,
84};
85
86const DesktopWindowBoundsDocument = Schema.Struct({
87 x: Schema.Number,
88 y: Schema.Number,
89 width: Schema.Number,
90 height: Schema.Number,
91});
92
93const DesktopSettingsDocument = Schema.Struct({
94 localEnvironmentEnabled: Schema.optionalKey(Schema.Boolean),
95 linuxPasswordStore: Schema.optionalKey(Schema.Unknown),
96 mainWindowBounds: Schema.optionalKey(Schema.NullOr(DesktopWindowBoundsDocument)),
97 mainWindowMaximized: Schema.optionalKey(Schema.Boolean),
98 serverExposureMode: Schema.optionalKey(DesktopServerExposureModeSchema),
99 tailscaleServeEnabled: Schema.optionalKey(Schema.Boolean),
100 tailscaleServePort: Schema.optionalKey(Schema.Number),
101 updateChannel: Schema.optionalKey(DesktopUpdateChannelSchema),
102 updateChannelConfiguredByUser: Schema.optionalKey(Schema.Boolean),
103 wslBackendEnabled: Schema.optionalKey(Schema.Boolean),
104 wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])),
105 wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)),
106 wslOnly: Schema.optionalKey(Schema.Boolean),
107});
108
109type DesktopSettingsDocument = typeof DesktopSettingsDocument.Type;
110type Mutable<T> = { -readonly [K in keyof T]: T[K] };
111
112const DesktopSettingsJson = fromLenientJson(DesktopSettingsDocument);
113const decodeDesktopSettingsJson = Schema.decodeEffect(DesktopSettingsJson);
114const encodeDesktopSettingsJson = Schema.encodeEffect(DesktopSettingsJson);
115const decodeDesktopWindowBounds = Schema.decodeUnknownOption(DesktopWindowBoundsSchema);
116const desktopWindowBoundsEquivalence = Schema.toEquivalence(DesktopWindowBoundsSchema);
117
118const settingsChange = (settings: DesktopSettings, changed: boolean): DesktopSettingsChange => ({
119 settings,
120 changed,
121});
122
123const DesktopSettingsWriteOperation = Schema.Literals([
124 "create-temporary-file-name",
125 "resolve-symlink",
126 "encode-document",
127 "create-directory",
128 "write-temporary-file",
129 "replace-settings-file",
130]);
131type DesktopSettingsWriteOperation = typeof DesktopSettingsWriteOperation.Type;
132
133export class DesktopSettingsWriteError extends Schema.TaggedError<DesktopSettingsWriteError>()(
134 "DesktopSettingsWriteError",
135 {
136 operation: DesktopSettingsWriteOperation,
137 path: Schema.String,
138 cause: Schema.Defect(),
139 },
140) {
141 override get message(): string {
142 return `Desktop settings write failed during ${this.operation} at ${this.path}.`;
143 }
144}
145
146export class DesktopAppSettings extends Context.Service<
147 DesktopAppSettings,
148 {
149 readonly load: Effect.Effect<DesktopSettings>;
150 readonly get: Effect.Effect<DesktopSettings>;
151 readonly setLocalEnvironmentEnabled: (
152 enabled: boolean,
153 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
154 readonly setMainWindowBounds: (
155 bounds: DesktopWindowBounds,
156 isMaximized: boolean,
157 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
158 readonly setServerExposureMode: (
159 mode: DesktopServerExposureMode,
160 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
161 readonly setTailscaleServe: (input: {
162 readonly enabled: boolean;
163 readonly port: Option.Option<number>;
164 }) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
165 readonly setUpdateChannel: (
166 channel: DesktopUpdateChannel,
167 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
168 readonly setWslBackendEnabled: (
169 enabled: boolean,
170 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
171 readonly setWslDistro: (
172 distro: string | null,
173 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
174 readonly setWslOnly: (
175 enabled: boolean,
176 ) => Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError>;
177 readonly applyWslWindowsFallback: Effect.Effect<
178 DesktopSettingsChange,
179 DesktopSettingsWriteError
180 >;
181 readonly applyWslWindowsFallbackInMemory: Effect.Effect<DesktopSettingsChange>;
182 }
183>()("@t3tools/desktop/settings/DesktopAppSettings") {}
184
185export function resolveDefaultDesktopSettings(appVersion: string): DesktopSettings {
186 return {
187 ...DEFAULT_DESKTOP_SETTINGS,
188 updateChannel: desktopUpdateChannelOf(appVersion),
189 };
190}
191
192function normalizeTailscaleServePort(value: unknown): number {
193 return typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 65_535
194 ? value
195 : DEFAULT_TAILSCALE_SERVE_PORT;
196}
197
198function normalizeWslDistro(value: unknown): string | null {
199 return typeof value === "string" && isValidDistroName(value) ? value : null;
200}
201
202export function normalizeMainWindowBounds(value: unknown): DesktopWindowBounds | null {
203 return Option.getOrNull(decodeDesktopWindowBounds(value));
204}
205
206function normalizeDesktopSettingsDocument(
207 parsed: DesktopSettingsDocument,
208 appVersion: string,
209): DesktopSettings {
210 const defaultSettings = resolveDefaultDesktopSettings(appVersion);
211 const mainWindowBounds = normalizeMainWindowBounds(parsed.mainWindowBounds);
212 const parsedUpdateChannel = Option.fromNullishOr(parsed.updateChannel);
213 const isLegacySettings = parsed.updateChannelConfiguredByUser === undefined;
214 const updateChannelConfiguredByUser =
215 parsed.updateChannelConfiguredByUser === true ||
216 (isLegacySettings && Option.contains(parsedUpdateChannel, "nightly"));
217
218 const wslBackendEnabled =
219 parsed.wslBackendEnabled === true ||
220 (parsed.wslBackendEnabled === undefined && parsed.wslMode === "wsl");
221
222 return {
223 localEnvironmentEnabled: parsed.localEnvironmentEnabled !== false,
224 linuxPasswordStore: normalizeLinuxPasswordStorePreference(parsed.linuxPasswordStore),
225 mainWindowBounds,
226 mainWindowMaximized: mainWindowBounds !== null && parsed.mainWindowMaximized === true,
227 serverExposureMode:
228 parsed.serverExposureMode === "network-accessible" ? "network-accessible" : "local-only",
229 tailscaleServeEnabled: parsed.tailscaleServeEnabled === true,
230 tailscaleServePort: normalizeTailscaleServePort(parsed.tailscaleServePort),
231 updateChannel: updateChannelConfiguredByUser
232 ? Option.getOrElse(parsedUpdateChannel, () => defaultSettings.updateChannel)
233 : defaultSettings.updateChannel,
234 updateChannelConfiguredByUser,
235 wslBackendEnabled,
236 wslDistro: normalizeWslDistro(parsed.wslDistro),
237 wslOnly: parsed.wslOnly === true,
238 };
239}
240
241function toDesktopSettingsDocument(
242 settings: DesktopSettings,
243 defaults: DesktopSettings,
244): DesktopSettingsDocument {
245 const document: Mutable<DesktopSettingsDocument> = {};
246
247 if (settings.localEnvironmentEnabled !== defaults.localEnvironmentEnabled) {
248 document.localEnvironmentEnabled = settings.localEnvironmentEnabled;
249 }
250
251 if (settings.linuxPasswordStore !== defaults.linuxPasswordStore) {
252 document.linuxPasswordStore = settings.linuxPasswordStore;
253 }
254 if (settings.mainWindowBounds !== null) {
255 document.mainWindowBounds = settings.mainWindowBounds;
256 }
257 if (settings.mainWindowMaximized) {
258 document.mainWindowMaximized = true;
259 }
260 if (settings.serverExposureMode !== defaults.serverExposureMode) {
261 document.serverExposureMode = settings.serverExposureMode;
262 }
263 if (settings.tailscaleServeEnabled !== defaults.tailscaleServeEnabled) {
264 document.tailscaleServeEnabled = settings.tailscaleServeEnabled;
265 }
266 if (settings.tailscaleServePort !== defaults.tailscaleServePort) {
267 document.tailscaleServePort = settings.tailscaleServePort;
268 }
269 if (settings.updateChannel !== defaults.updateChannel) {
270 document.updateChannel = settings.updateChannel;
271 }
272 if (settings.updateChannelConfiguredByUser !== defaults.updateChannelConfiguredByUser) {
273 document.updateChannelConfiguredByUser = settings.updateChannelConfiguredByUser;
274 }
275 if (settings.wslBackendEnabled !== defaults.wslBackendEnabled) {
276 document.wslBackendEnabled = settings.wslBackendEnabled;
277 }
278 if (settings.wslDistro !== defaults.wslDistro) {
279 document.wslDistro = settings.wslDistro;
280 }
281 if (settings.wslOnly !== defaults.wslOnly) {
282 document.wslOnly = settings.wslOnly;
283 }
284
285 return document;
286}
287
288function setServerExposureMode(
289 settings: DesktopSettings,
290 requestedMode: DesktopServerExposureMode,
291): DesktopSettings {
292 return settings.serverExposureMode === requestedMode
293 ? settings
294 : {
295 ...settings,
296 serverExposureMode: requestedMode,
297 };
298}
299
300function setMainWindowBounds(
301 settings: DesktopSettings,
302 bounds: DesktopWindowBounds,
303 isMaximized: boolean,
304): DesktopSettings {
305 return settings.mainWindowBounds !== null &&
306 desktopWindowBoundsEquivalence(settings.mainWindowBounds, bounds) &&
307 settings.mainWindowMaximized === isMaximized
308 ? settings
309 : {
310 ...settings,
311 mainWindowBounds: bounds,
312 mainWindowMaximized: isMaximized,
313 };
314}
315
316function setTailscaleServe(
317 settings: DesktopSettings,
318 input: { readonly enabled: boolean; readonly port: Option.Option<number> },
319): DesktopSettings {
320 const port = Option.match(input.port, {
321 onNone: () => settings.tailscaleServePort,
322 onSome: normalizeTailscaleServePort,
323 });
324 return settings.tailscaleServeEnabled === input.enabled && settings.tailscaleServePort === port
325 ? settings
326 : {
327 ...settings,
328 tailscaleServeEnabled: input.enabled,
329 tailscaleServePort: port,
330 };
331}
332
333function setUpdateChannel(
334 settings: DesktopSettings,
335 requestedChannel: DesktopUpdateChannel,
336): DesktopSettings {
337 return settings.updateChannel === requestedChannel
338 ? settings
339 : {
340 ...settings,
341 updateChannel: requestedChannel,
342 updateChannelConfiguredByUser: true,
343 };
344}
345
346function setWslBackendEnabled(settings: DesktopSettings, enabled: boolean): DesktopSettings {
347 return settings.wslBackendEnabled === enabled
348 ? settings
349 : {
350 ...settings,
351 wslBackendEnabled: enabled,
352 };
353}
354
355function setWslDistro(settings: DesktopSettings, distro: string | null): DesktopSettings {
356 const normalized = normalizeWslDistro(distro);
357 return settings.wslDistro === normalized
358 ? settings
359 : {
360 ...settings,
361 wslDistro: normalized,
362 };
363}
364
365function setWslOnly(settings: DesktopSettings, enabled: boolean): DesktopSettings {
366 return settings.wslOnly === enabled
367 ? settings
368 : {
369 ...settings,
370 wslOnly: enabled,
371 };
372}
373
374function setLocalEnvironmentEnabled(settings: DesktopSettings, enabled: boolean): DesktopSettings {
375 return settings.localEnvironmentEnabled === enabled
376 ? settings
377 : { ...settings, localEnvironmentEnabled: enabled };
378}
379
380function applyWslWindowsFallback(settings: DesktopSettings): DesktopSettings {
381 return setWslOnly(setWslBackendEnabled(settings, false), false);
382}
383
384// Fails only when the file may exist but could not be read; a missing file means defaults.
385function readSettings(
386 fileSystem: FileSystem.FileSystem,
387 settingsPath: string,
388 appVersion: string,
389): Effect.Effect<DesktopSettings, PlatformError.PlatformError> {
390 const defaultSettings = resolveDefaultDesktopSettings(appVersion);
391
392 return fileSystem.readFileString(settingsPath).pipe(
393 Effect.asSome,
394 Effect.catchIf(
395 (cause) => cause.reason._tag === "NotFound",
396 () => Effect.succeed(Option.none<string>()),
397 ),
398 Effect.flatMap(
399 Option.match({
400 onNone: () => Effect.succeed(defaultSettings),
401 onSome: (raw) =>
402 decodeDesktopSettingsJson(raw).pipe(
403 Effect.map((parsed) => normalizeDesktopSettingsDocument(parsed, appVersion)),
404 Effect.orElseSucceed(() => defaultSettings),
405 ),
406 }),
407 ),
408 );
409}
410
411const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (input: {
412 readonly fileSystem: FileSystem.FileSystem;
413 readonly path: Path.Path;
414 readonly settingsPath: string;
415 readonly settings: DesktopSettings;
416 readonly defaultSettings: DesktopSettings;
417 readonly suffix: string;
418}): Effect.fn.Return<void, DesktopSettingsWriteError> {
419 const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
420 Effect.provideService(FileSystem.FileSystem, input.fileSystem),
421 Effect.provideService(Path.Path, input.path),
422 Effect.mapError(
423 (cause) =>
424 new DesktopSettingsWriteError({
425 operation: "resolve-symlink",
426 path: input.settingsPath,
427 cause,
428 }),
429 ),
430 );
431 const directory = input.path.dirname(targetPath);
432 const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;
433 const encoded = yield* encodeDesktopSettingsJson(
434 toDesktopSettingsDocument(input.settings, input.defaultSettings),
435 ).pipe(
436 Effect.mapError(
437 (cause) =>
438 new DesktopSettingsWriteError({
439 operation: "encode-document",
440 path: input.settingsPath,
441 cause,
442 }),
443 ),
444 );
445 yield* input.fileSystem.makeDirectory(directory, { recursive: true }).pipe(
446 Effect.mapError(
447 (cause) =>
448 new DesktopSettingsWriteError({
449 operation: "create-directory",
450 path: directory,
451 cause,
452 }),
453 ),
454 );
455 yield* Effect.gen(function* () {
456 yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe(
457 Effect.mapError(
458 (cause) =>
459 new DesktopSettingsWriteError({
460 operation: "write-temporary-file",
461 path: tempPath,
462 cause,
463 }),
464 ),
465 );
466 yield* input.fileSystem.rename(tempPath, targetPath).pipe(
467 Effect.mapError(
468 (cause) =>
469 new DesktopSettingsWriteError({
470 operation: "replace-settings-file",
471 path: input.settingsPath,
472 cause,
473 }),
474 ),
475 );
476 }).pipe(Effect.ensuring(input.fileSystem.remove(tempPath, { force: true }).pipe(Effect.ignore)));
477});
478
479/** @public */
480export const make = Effect.gen(function* () {
481 const environment = yield* DesktopEnvironment.DesktopEnvironment;
482 const fileSystem = yield* FileSystem.FileSystem;
483 const path = yield* Path.Path;
484 const random = yield* SecureRandom.SecureRandom;
485 const settingsRef = yield* SynchronizedRef.make(environment.defaultDesktopSettings);
486 // Writing over a settings file we could not read would erase the preferences it holds.
487 const persistenceEnabled = yield* Ref.make(true);
488
489 const updateInMemory = (update: (settings: DesktopSettings) => DesktopSettings) =>
490 SynchronizedRef.modify(settingsRef, (settings) => {
491 const nextSettings = update(settings);
492 return [settingsChange(nextSettings, nextSettings !== settings), nextSettings] as const;
493 });
494
495 const persist = (
496 update: (settings: DesktopSettings) => DesktopSettings,
497 ): Effect.Effect<DesktopSettingsChange, DesktopSettingsWriteError> =>
498 SynchronizedRef.modifyEffect(settingsRef, (settings) => {
499 const nextSettings = update(settings);
500 if (nextSettings === settings) {
501 return Effect.succeed([settingsChange(settings, false), settings] as const);
502 }
503
504 return Ref.get(persistenceEnabled).pipe(
505 Effect.flatMap((enabled) =>
506 enabled
507 ? random.uuidV4().pipe(
508 Effect.map((uuid) => uuid.replace(/-/g, "")),
509 Effect.flatMap((suffix) =>
510 writeSettings({
511 fileSystem,
512 path,
513 settingsPath: environment.desktopSettingsPath,
514 settings: nextSettings,
515 defaultSettings: environment.defaultDesktopSettings,
516 suffix,
517 }),
518 ),
519 )
520 : Effect.void,
521 ),
522 Effect.as([settingsChange(nextSettings, true), nextSettings] as const),
523 );
524 });
525
526 return DesktopAppSettings.of({
527 get: SynchronizedRef.get(settingsRef),
528 load: Effect.gen(function* () {
529 const settings = yield* readSettings(
530 fileSystem,
531 environment.desktopSettingsPath,
532 environment.appVersion,
533 ).pipe(
534 Effect.catch((cause) =>
535 Effect.logWarning(
536 "Could not read desktop settings; using defaults without saving changes.",
537 cause,
538 ).pipe(
539 Effect.annotateLogs({ settingsPath: environment.desktopSettingsPath }),
540 Effect.andThen(Ref.set(persistenceEnabled, false)),
541 Effect.as(resolveDefaultDesktopSettings(environment.appVersion)),
542 ),
543 ),
544 );
545 return yield* SynchronizedRef.setAndGet(settingsRef, settings);
546 }).pipe(Effect.withSpan("desktop.settings.load")),
547 setMainWindowBounds: Effect.fn("desktop.settings.setMainWindowBounds")(
548 function* (bounds, isMaximized) {
549 yield* Effect.annotateCurrentSpan({
550 x: bounds.x,
551 y: bounds.y,
552 width: bounds.width,
553 height: bounds.height,
554 isMaximized,
555 });
556
557 return yield* persist((settings) => setMainWindowBounds(settings, bounds, isMaximized));
558 },
559 ),
560 setServerExposureMode: Effect.fn("desktop.settings.setServerExposureMode")(function* (mode) {
561 yield* Effect.annotateCurrentSpan({ mode });
562
563 return yield* persist((settings) => setServerExposureMode(settings, mode));
564 }),
565 setTailscaleServe: Effect.fn("desktop.settings.setTailscaleServe")(function* (input) {
566 yield* Effect.annotateCurrentSpan(input);
567
568 return yield* persist((settings) => setTailscaleServe(settings, input));
569 }),
570 setUpdateChannel: Effect.fn("desktop.settings.setUpdateChannel")(function* (channel) {
571 yield* Effect.annotateCurrentSpan({ channel });
572
573 return yield* persist((settings) => setUpdateChannel(settings, channel));
574 }),
575 setWslBackendEnabled: Effect.fn("desktop.settings.setWslBackendEnabled")(function* (enabled) {
576 yield* Effect.annotateCurrentSpan({ enabled });
577
578 return yield* persist((settings) => setWslBackendEnabled(settings, enabled));
579 }),
580 setWslDistro: Effect.fn("desktop.settings.setWslDistro")(function* (distro) {
581 yield* Effect.annotateCurrentSpan({ distro: distro ?? null });
582
583 return yield* persist((settings) => setWslDistro(settings, distro));
584 }),
585 setWslOnly: Effect.fn("desktop.settings.setWslOnly")(function* (enabled) {
586 yield* Effect.annotateCurrentSpan({ enabled });
587
588 return yield* persist((settings) => setWslOnly(settings, enabled));
589 }),
590 setLocalEnvironmentEnabled: Effect.fn("desktop.settings.setLocalEnvironmentEnabled")(
591 function* (enabled) {
592 yield* Effect.annotateCurrentSpan({ enabled });
593
594 return yield* persist((settings) => setLocalEnvironmentEnabled(settings, enabled));
595 },
596 ),
597 applyWslWindowsFallback: persist(applyWslWindowsFallback).pipe(
598 Effect.withSpan("desktop.settings.applyWslWindowsFallback"),
599 ),
600 applyWslWindowsFallbackInMemory: updateInMemory(applyWslWindowsFallback).pipe(
601 Effect.withSpan("desktop.settings.applyWslWindowsFallbackInMemory"),
602 ),
603 });
604});
605
606export const layer = Layer.effect(DesktopAppSettings, make);
607
608export const layerTest = (initialSettings: DesktopSettings = DEFAULT_DESKTOP_SETTINGS) =>
609 Layer.effect(
610 DesktopAppSettings,
611 Effect.gen(function* () {
612 const settingsRef = yield* SynchronizedRef.make(initialSettings);
613 const update = (f: (settings: DesktopSettings) => DesktopSettings) =>
614 SynchronizedRef.modify(settingsRef, (settings) => {
615 const nextSettings = f(settings);
616 return [
617 {
618 settings: nextSettings,
619 changed: nextSettings !== settings,
620 },
621 nextSettings,
622 ] as const;
623 });
624
625 return DesktopAppSettings.of({
626 get: SynchronizedRef.get(settingsRef),
627 load: SynchronizedRef.get(settingsRef),
628 setMainWindowBounds: Effect.fn("DesktopAppSettings.setMainWindowBounds")(
629 function (bounds, isMaximized) {
630 return update((settings) => setMainWindowBounds(settings, bounds, isMaximized));
631 },
632 ),
633 setServerExposureMode: Effect.fn("DesktopAppSettings.setServerExposureMode")(
634 function (mode) {
635 return update((settings) => setServerExposureMode(settings, mode));
636 },
637 ),
638 setTailscaleServe: Effect.fn("DesktopAppSettings.setTailscaleServe")(function (input) {
639 return update((settings) => setTailscaleServe(settings, input));
640 }),
641 setUpdateChannel: Effect.fn("DesktopAppSettings.setUpdateChannel")(function (channel) {
642 return update((settings) => setUpdateChannel(settings, channel));
643 }),
644 setWslBackendEnabled: Effect.fn("DesktopAppSettings.setWslBackendEnabled")(
645 function (enabled) {
646 return update((settings) => setWslBackendEnabled(settings, enabled));
647 },
648 ),
649 setWslDistro: Effect.fn("DesktopAppSettings.setWslDistro")(function (distro) {
650 return update((settings) => setWslDistro(settings, distro));
651 }),
652 setWslOnly: Effect.fn("DesktopAppSettings.setWslOnly")(function (enabled) {
653 return update((settings) => setWslOnly(settings, enabled));
654 }),
655 setLocalEnvironmentEnabled: Effect.fn("DesktopAppSettings.setLocalEnvironmentEnabled")(
656 function (enabled) {
657 return update((settings) => setLocalEnvironmentEnabled(settings, enabled));
658 },
659 ),
660 applyWslWindowsFallback: update(applyWslWindowsFallback),
661 applyWslWindowsFallbackInMemory: update(applyWslWindowsFallback),
662 });
663 }),
664 );