Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Schema from "effect/Schema";
5import * as Scope from "effect/Scope";
6
7import * as Electron from "electron";
8
9export interface ElectronAppMetadata {
10 readonly appVersion: string;
11 readonly appPath: string;
12 readonly isPackaged: boolean;
13 readonly resourcesPath: string;
14 readonly runningUnderArm64Translation: boolean;
15}
16
17export class ElectronAppMetadataReadError extends Schema.TaggedError<ElectronAppMetadataReadError>()(
18 "ElectronAppMetadataReadError",
19 {
20 property: Schema.Literals(["app-version", "app-path"]),
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Failed to read Electron app metadata property "${this.property}".`;
26 }
27}
28
29export class ElectronAppWhenReadyError extends Schema.TaggedError<ElectronAppWhenReadyError>()(
30 "ElectronAppWhenReadyError",
31 {
32 isPackaged: Schema.Boolean,
33 cause: Schema.Defect(),
34 },
35) {
36 override get message(): string {
37 return `Failed to wait for the Electron app to become ready (packaged: ${this.isPackaged}).`;
38 }
39}
40
41export class ElectronApp extends Context.Service<
42 ElectronApp,
43 {
44 readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>;
45 readonly name: Effect.Effect<string>;
46 /**
47 * The OS locale, read from the operating system rather than from Chromium's
48 * resolved application locale — the packaged app ships only the `en-US`
49 * locale pak, so `app.getLocale()` and the renderer's `Intl` default are
50 * pinned to `en-US` however the machine is configured.
51 */
52 readonly systemLocale: Effect.Effect<string>;
53 readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
54 readonly quit: Effect.Effect<void>;
55 readonly exit: (code: number) => Effect.Effect<void>;
56 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
57 readonly setPath: (
58 name: Parameters<Electron.App["setPath"]>[0],
59 path: string,
60 ) => Effect.Effect<void>;
61 readonly setName: (name: string) => Effect.Effect<void>;
62 readonly setAboutPanelOptions: (
63 options: Electron.AboutPanelOptionsOptions,
64 ) => Effect.Effect<void>;
65 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
66 readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>;
67 readonly setAsDefaultProtocolClient: (
68 protocol: string,
69 path?: string,
70 args?: readonly string[],
71 ) => Effect.Effect<boolean>;
72 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
73 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
74 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
75 readonly onBeforeQuitForUpdate: (
76 listener: () => void,
77 ) => Effect.Effect<void, never, Scope.Scope>;
78 readonly removeCommandLineSwitch: (switchName: string) => Effect.Effect<void>;
79 readonly on: <Args extends ReadonlyArray<unknown>>(
80 eventName: string,
81 listener: (...args: Args) => void,
82 ) => Effect.Effect<void, never, Scope.Scope>;
83 }
84>()("@t3tools/desktop/electron/ElectronApp") {}
85
86const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
87 eventName: string,
88 listener: (...args: Args) => void,
89): Effect.Effect<void, never, Scope.Scope> =>
90 Effect.acquireRelease(
91 Effect.sync(() => {
92 Electron.app.on(eventName as any, listener as any);
93 }),
94 () =>
95 Effect.sync(() => {
96 Electron.app.removeListener(eventName as any, listener as any);
97 }),
98 ).pipe(Effect.asVoid);
99
100/** @public Service construction is part of the canonical Effect module API. */
101export const make = ElectronApp.of({
102 metadata: Effect.gen(function* () {
103 const appVersion = yield* Effect.try({
104 try: () => Electron.app.getVersion(),
105 catch: (cause) =>
106 new ElectronAppMetadataReadError({
107 property: "app-version",
108 cause,
109 }),
110 });
111 const appPath = yield* Effect.try({
112 try: () => Electron.app.getAppPath(),
113 catch: (cause) =>
114 new ElectronAppMetadataReadError({
115 property: "app-path",
116 cause,
117 }),
118 });
119
120 return {
121 appVersion,
122 appPath,
123 isPackaged: Electron.app.isPackaged,
124 resourcesPath: process.resourcesPath,
125 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
126 };
127 }),
128 name: Effect.sync(() => Electron.app.name),
129 // macOS derives this from NSLocale, which uses POSIX-style identifiers
130 // (`en_GB`). `Intl` rejects those outright rather than normalizing them, so
131 // the tag is normalized here rather than in the renderer that consumes it.
132 systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
133 whenReady: Effect.gen(function* () {
134 const isPackaged = Electron.app.isPackaged;
135 yield* Effect.tryPromise({
136 try: () => Electron.app.whenReady(),
137 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
138 });
139 }),
140 quit: Effect.sync(() => {
141 Electron.app.quit();
142 }),
143 exit: (code) =>
144 Effect.sync(() => {
145 Electron.app.exit(code);
146 }),
147 relaunch: (options) =>
148 Effect.sync(() => {
149 Electron.app.relaunch(options);
150 }),
151 setPath: (name, path) =>
152 Effect.sync(() => {
153 Electron.app.setPath(name, path);
154 }),
155 setName: (name) =>
156 Effect.sync(() => {
157 Electron.app.setName(name);
158 }),
159 setAboutPanelOptions: (options) =>
160 Effect.sync(() => {
161 Electron.app.setAboutPanelOptions(options);
162 }),
163 setAppUserModelId: (id) =>
164 Effect.sync(() => {
165 Electron.app.setAppUserModelId(id);
166 }),
167 getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()),
168 setAsDefaultProtocolClient: (protocol, path, args) =>
169 Effect.sync(() => {
170 if (path === undefined) {
171 return Electron.app.setAsDefaultProtocolClient(protocol);
172 }
173 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
174 }),
175 setDesktopName: (desktopName) =>
176 Effect.sync(() => {
177 const linuxApp = Electron.app as Electron.App & {
178 setDesktopName?: (desktopName: string) => void;
179 };
180 linuxApp.setDesktopName?.(desktopName);
181 }),
182 setDockIcon: (iconPath) =>
183 Effect.sync(() => {
184 Electron.app.dock?.setIcon(iconPath);
185 }),
186 appendCommandLineSwitch: (switchName, value) =>
187 Effect.sync(() => {
188 if (value === undefined) {
189 Electron.app.commandLine.appendSwitch(switchName);
190 return;
191 }
192 Electron.app.commandLine.appendSwitch(switchName, value);
193 }),
194 onBeforeQuitForUpdate: (listener) =>
195 Effect.acquireRelease(
196 Effect.sync(() => {
197 Electron.autoUpdater.on("before-quit-for-update", listener);
198 }),
199 () =>
200 Effect.sync(() => {
201 Electron.autoUpdater.removeListener("before-quit-for-update", listener);
202 }),
203 ).pipe(Effect.asVoid),
204 removeCommandLineSwitch: (switchName) =>
205 Effect.sync(() => {
206 Electron.app.commandLine.removeSwitch(switchName);
207 }),
208 on: addScopedAppListener,
209});
210
211export const layer = Layer.succeed(ElectronApp, make);