Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronTheme.ts

54 lines1.7 KB

1import { DesktopThemeSchema, type DesktopTheme } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6import * as Scope from "effect/Scope";
7​
8import * as Electron from "electron";
9​
10export class ElectronThemeSetSourceError extends Schema.TaggedErrorClass<ElectronThemeSetSourceError>()(
11 "ElectronThemeSetSourceError",
12 {
13 source: DesktopThemeSchema,
14 cause: Schema.Defect(),
15 },
16) {
17 override get message(): string {
18 return `Failed to set the Electron theme source to ${this.source}.`;
19 }
21​
22export class ElectronTheme extends Context.Service<
23 ElectronTheme,
24 {
25 readonly shouldUseDarkColors: Effect.Effect<boolean>;
26 readonly setSource: (theme: DesktopTheme) => Effect.Effect<void, ElectronThemeSetSourceError>;
27 readonly onUpdated: (listener: () => void) => Effect.Effect<void, never, Scope.Scope>;
28 }
29>()("@t3tools/desktop/electron/ElectronTheme") {}
30​
31export const make = ElectronTheme.of({
32 shouldUseDarkColors: Effect.sync(() => Electron.nativeTheme.shouldUseDarkColors),
33 setSource: (theme) =>
34 Effect.try({
35 try: () => {
36 Electron.nativeTheme.themeSource = theme;
37 },
38 catch: (cause) => new ElectronThemeSetSourceError({ source: theme, cause }),
39 }),
40 onUpdated: (listener) =>
41 Effect.acquireRelease(
42 Effect.suspend(() => {
43 Electron.nativeTheme.on("updated", listener);
44 return Effect.void;
45 }),
46 () =>
47 Effect.suspend(() => {
48 Electron.nativeTheme.removeListener("updated", listener);
49 return Effect.void;
50 }),
51 ),
52});
53​
54export const layer = Layer.succeed(ElectronTheme, make);