@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 98 lines 3.19 KB
Raw
1 import { DesktopThemeSchema, type DesktopTheme } from "@t3tools/contracts";
2 import * as HostProcess from "@t3tools/shared/HostProcess";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Schema from "effect/Schema";
7 import * as Scope from "effect/Scope";
8
9 import * as Electron from "electron";
10
11 import { watchLinuxColorScheme, type LinuxColorScheme } from "./LinuxColorScheme.ts";
12
13 export class ElectronThemeSetSourceError extends Schema.TaggedError<ElectronThemeSetSourceError>()(
14 "ElectronThemeSetSourceError",
15 {
16 source: DesktopThemeSchema,
17 cause: Schema.Defect(),
18 },
19 ) {
20 override get message(): string {
21 return `Failed to set the Electron theme source to ${this.source}.`;
22 }
23 }
24
25 export class ElectronTheme extends Context.Service<
26 ElectronTheme,
27 {
28 readonly shouldUseDarkColors: Effect.Effect<boolean>;
29 readonly setSource: (theme: DesktopTheme) => Effect.Effect<void, ElectronThemeSetSourceError>;
30 readonly onUpdated: (listener: () => void) => Effect.Effect<void, never, Scope.Scope>;
31 }
32 >()("@t3tools/desktop/electron/ElectronTheme") {}
33
34 type WatchSystemColorScheme = (onChange: (scheme: LinuxColorScheme) => void) => {
35 readonly stop: () => void;
36 };
37
38 export const makeWith = Effect.fn("ElectronTheme.make")(function* (
39 watchSystemColorScheme: WatchSystemColorScheme | undefined,
40 ) {
41 let source: DesktopTheme = Electron.nativeTheme.themeSource;
42 let systemScheme: LinuxColorScheme = null;
43 // On Linux, Electron reads the desktop color scheme once and then ignores the settings
44 // portal (electron/electron#48736), so "system" is resolved from the portal directly.
45 const apply = () => {
46 Electron.nativeTheme.themeSource = source === "system" ? (systemScheme ?? source) : source;
47 };
48
49 if (watchSystemColorScheme) {
50 yield* Effect.acquireRelease(
51 Effect.sync(() => {
52 try {
53 return watchSystemColorScheme((scheme) => {
54 systemScheme = scheme;
55 apply();
56 });
57 } catch {
58 return undefined;
59 }
60 }),
61 (watcher) => Effect.sync(() => watcher?.stop()),
62 );
63 }
64
65 return ElectronTheme.of({
66 shouldUseDarkColors: Effect.sync(() => Electron.nativeTheme.shouldUseDarkColors),
67 setSource: Effect.fn("ElectronTheme.setSource")(function (theme) {
68 return Effect.try({
69 try: () => {
70 source = theme;
71 apply();
72 },
73 catch: (cause) => new ElectronThemeSetSourceError({ source: theme, cause }),
74 });
75 }),
76 onUpdated: Effect.fn("ElectronTheme.onUpdated")(function (listener) {
77 return Effect.acquireRelease(
78 Effect.suspend(() => {
79 Electron.nativeTheme.on("updated", listener);
80 return Effect.void;
81 }),
82 () =>
83 Effect.suspend(() => {
84 Electron.nativeTheme.removeListener("updated", listener);
85 return Effect.void;
86 }),
87 );
88 }),
89 });
90 });
91
92 /** @public */
93 export const make = Effect.gen(function* () {
94 const platform = yield* HostProcess.Platform;
95 return yield* makeWith(platform === "linux" ? watchLinuxColorScheme : undefined);
96 });
97
98 export const layer = Layer.effect(ElectronTheme, make);