Open History to see the last commit
1import 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 Scope from "effect/Scope";
6
7import * as Electron from "electron";
8
9export interface ElectronThemeShape {
10 readonly shouldUseDarkColors: Effect.Effect<boolean>;
11 readonly setSource: (theme: DesktopTheme) => Effect.Effect<void>;
12 readonly onUpdated: (listener: () => void) => Effect.Effect<void, never, Scope.Scope>;
13}
14
15export class ElectronTheme extends Context.Service<ElectronTheme, ElectronThemeShape>()(
16 "t3/desktop/electron/Theme",
17) {}
18
19const make = ElectronTheme.of({
20 shouldUseDarkColors: Effect.sync(() => Electron.nativeTheme.shouldUseDarkColors),
21 setSource: (theme) =>
22 Effect.suspend(() => {
23 Electron.nativeTheme.themeSource = theme;
24 return Effect.void;
25 }),
26 onUpdated: (listener) =>
27 Effect.acquireRelease(
28 Effect.suspend(() => {
29 Electron.nativeTheme.on("updated", listener);
30 return Effect.void;
31 }),
32 () =>
33 Effect.suspend(() => {
34 Electron.nativeTheme.removeListener("updated", listener);
35 return Effect.void;
36 }),
37 ),
38});
39
40export const layer = Layer.succeed(ElectronTheme, make);