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 requestSingleInstanceLock: Effect.Effect<boolean>;
55 readonly quit: Effect.Effect<void>;
56 readonly exit: (code: number) => Effect.Effect<void>;
57 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
58 readonly setPath: (
59 name: Parameters<Electron.App["setPath"]>[0],
60 path: string,
61 ) => Effect.Effect<void>;
62 readonly setName: (name: string) => Effect.Effect<void>;
63 readonly setAboutPanelOptions: (
64 options: Electron.AboutPanelOptionsOptions,
65 ) => Effect.Effect<void>;
66 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
67 readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>;
68 readonly setAsDefaultProtocolClient: (
69 protocol: string,
70 path?: string,
71 args?: readonly string[],
72 ) => Effect.Effect<boolean>;
73 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
74 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
75 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
76 readonly onBeforeQuitForUpdate: (
77 listener: () => void,
78 ) => Effect.Effect<void, never, Scope.Scope>;
79 readonly removeCommandLineSwitch: (switchName: string) => Effect.Effect<void>;
80 readonly on: <Args extends ReadonlyArray<unknown>>(
81 eventName: string,
82 listener: (...args: Args) => void,
83 ) => Effect.Effect<void, never, Scope.Scope>;
84 }
85>()("@t3tools/desktop/electron/ElectronApp") {}
86
87const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
88 eventName: string,
89 listener: (...args: Args) => void,
90): Effect.Effect<void, never, Scope.Scope> =>
91 Effect.acquireRelease(
92 Effect.sync(() => {
93 Electron.app.on(eventName as any, listener as any);
94 }),
95 () =>
96 Effect.sync(() => {
97 Electron.app.removeListener(eventName as any, listener as any);
98 }),
99 ).pipe(Effect.asVoid);
100
101/** @public Service construction is part of the canonical Effect module API. */
102export const make = ElectronApp.of({
103 metadata: Effect.gen(function* () {
104 const appVersion = yield* Effect.try({
105 try: () => Electron.app.getVersion(),
106 catch: (cause) =>
107 new ElectronAppMetadataReadError({
108 property: "app-version",
109 cause,
110 }),
111 });
112 const appPath = yield* Effect.try({
113 try: () => Electron.app.getAppPath(),
114 catch: (cause) =>
115 new ElectronAppMetadataReadError({
116 property: "app-path",
117 cause,
118 }),
119 });
120
121 return {
122 appVersion,
123 appPath,
124 isPackaged: Electron.app.isPackaged,
125 resourcesPath: process.resourcesPath,
126 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
127 };
128 }),
129 name: Effect.sync(() => Electron.app.name),
130 // macOS derives this from NSLocale, which uses POSIX-style identifiers
131 // (`en_GB`). `Intl` rejects those outright rather than normalizing them, so
132 // the tag is normalized here rather than in the renderer that consumes it.
133 systemLocale: Effect.sync(() => Electron.app.getSystemLocale().replace(/_/g, "-")),
134 whenReady: Effect.gen(function* () {
135 const isPackaged = Electron.app.isPackaged;
136 yield* Effect.tryPromise({
137 try: () => Electron.app.whenReady(),
138 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
139 });
140 }),
141 requestSingleInstanceLock: Effect.sync(() => Electron.app.requestSingleInstanceLock()),
142 quit: Effect.sync(() => {
143 Electron.app.quit();
144 }),
145 exit: (code) =>
146 Effect.sync(() => {
147 Electron.app.exit(code);
148 }),
149 relaunch: (options) =>
150 Effect.sync(() => {
151 Electron.app.relaunch(options);
152 }),
153 setPath: (name, path) =>
154 Effect.sync(() => {
155 Electron.app.setPath(name, path);
156 }),
157 setName: (name) =>
158 Effect.sync(() => {
159 Electron.app.setName(name);
160 }),
161 setAboutPanelOptions: (options) =>
162 Effect.sync(() => {
163 Electron.app.setAboutPanelOptions(options);
164 }),
165 setAppUserModelId: (id) =>
166 Effect.sync(() => {
167 Electron.app.setAppUserModelId(id);
168 }),
169 getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()),
170 setAsDefaultProtocolClient: (protocol, path, args) =>
171 Effect.sync(() => {
172 if (path === undefined) {
173 return Electron.app.setAsDefaultProtocolClient(protocol);
174 }
175 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
176 }),
177 setDesktopName: (desktopName) =>
178 Effect.sync(() => {
179 const linuxApp = Electron.app as Electron.App & {
180 setDesktopName?: (desktopName: string) => void;
181 };
182 linuxApp.setDesktopName?.(desktopName);
183 }),
184 setDockIcon: (iconPath) =>
185 Effect.sync(() => {
186 Electron.app.dock?.setIcon(iconPath);
187 }),
188 appendCommandLineSwitch: (switchName, value) =>
189 Effect.sync(() => {
190 if (value === undefined) {
191 Electron.app.commandLine.appendSwitch(switchName);
192 return;
193 }
194 Electron.app.commandLine.appendSwitch(switchName, value);
195 }),
196 onBeforeQuitForUpdate: (listener) =>
197 Effect.acquireRelease(
198 Effect.sync(() => {
199 Electron.autoUpdater.on("before-quit-for-update", listener);
200 }),
201 () =>
202 Effect.sync(() => {
203 Electron.autoUpdater.removeListener("before-quit-for-update", listener);
204 }),
205 ).pipe(Effect.asVoid),
206 removeCommandLineSwitch: (switchName) =>
207 Effect.sync(() => {
208 Electron.app.commandLine.removeSwitch(switchName);
209 }),
210 on: addScopedAppListener,
211});
212
213export const layer = Layer.succeed(ElectronApp, make);