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

ElectronTheme.ts

56 lines1.8 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 const isElectronThemeSetSourceError = Schema.is(ElectronThemeSetSourceError);
23​
24export class ElectronTheme extends Context.Service<
25 ElectronTheme,
26 {
27 readonly shouldUseDarkColors: Effect.Effect<boolean>;
28 readonly setSource: (theme: DesktopTheme) => Effect.Effect<void, ElectronThemeSetSourceError>;
29 readonly onUpdated: (listener: () => void) => Effect.Effect<void, never, Scope.Scope>;
30 }
31>()("@t3tools/desktop/electron/ElectronTheme") {}
32​
33export const make = ElectronTheme.of({
34 shouldUseDarkColors: Effect.sync(() => Electron.nativeTheme.shouldUseDarkColors),
35 setSource: (theme) =>
36 Effect.try({
37 try: () => {
38 Electron.nativeTheme.themeSource = theme;
39 },
40 catch: (cause) => new ElectronThemeSetSourceError({ source: theme, cause }),
41 }),
42 onUpdated: (listener) =>
43 Effect.acquireRelease(
44 Effect.suspend(() => {
45 Electron.nativeTheme.on("updated", listener);
46 return Effect.void;
47 }),
48 () =>
49 Effect.suspend(() => {
50 Electron.nativeTheme.removeListener("updated", listener);
51 return Effect.void;
52 }),
53 ),
54});
55​
56export const layer = Layer.succeed(ElectronTheme, make);