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.TaggedErrorClass<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.TaggedErrorClass<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 readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
47 readonly quit: Effect.Effect<void>;
48 readonly exit: (code: number) => Effect.Effect<void>;
49 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
50 readonly setPath: (
51 name: Parameters<Electron.App["setPath"]>[0],
52 path: string,
53 ) => Effect.Effect<void>;
54 readonly setName: (name: string) => Effect.Effect<void>;
55 readonly setAboutPanelOptions: (
56 options: Electron.AboutPanelOptionsOptions,
57 ) => Effect.Effect<void>;
58 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
59 readonly requestSingleInstanceLock: Effect.Effect<boolean>;
60 readonly isDefaultProtocolClient: (protocol: string) => Effect.Effect<boolean>;
61 readonly setAsDefaultProtocolClient: (
62 protocol: string,
63 path?: string,
64 args?: readonly string[],
65 ) => Effect.Effect<boolean>;
66 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
67 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
68 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
69 readonly on: <Args extends ReadonlyArray<unknown>>(
70 eventName: string,
71 listener: (...args: Args) => void,
72 ) => Effect.Effect<void, never, Scope.Scope>;
73 }
74>()("@t3tools/desktop/electron/ElectronApp") {}
75
76const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
77 eventName: string,
78 listener: (...args: Args) => void,
79): Effect.Effect<void, never, Scope.Scope> =>
80 Effect.acquireRelease(
81 Effect.sync(() => {
82 Electron.app.on(eventName as any, listener as any);
83 }),
84 () =>
85 Effect.sync(() => {
86 Electron.app.removeListener(eventName as any, listener as any);
87 }),
88 ).pipe(Effect.asVoid);
89
90export const make = ElectronApp.of({
91 metadata: Effect.gen(function* () {
92 const appVersion = yield* Effect.try({
93 try: () => Electron.app.getVersion(),
94 catch: (cause) =>
95 new ElectronAppMetadataReadError({
96 property: "app-version",
97 cause,
98 }),
99 });
100 const appPath = yield* Effect.try({
101 try: () => Electron.app.getAppPath(),
102 catch: (cause) =>
103 new ElectronAppMetadataReadError({
104 property: "app-path",
105 cause,
106 }),
107 });
108
109 return {
110 appVersion,
111 appPath,
112 isPackaged: Electron.app.isPackaged,
113 resourcesPath: process.resourcesPath,
114 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
115 };
116 }),
117 name: Effect.sync(() => Electron.app.name),
118 whenReady: Effect.gen(function* () {
119 const isPackaged = Electron.app.isPackaged;
120 yield* Effect.tryPromise({
121 try: () => Electron.app.whenReady(),
122 catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }),
123 });
124 }),
125 quit: Effect.sync(() => {
126 Electron.app.quit();
127 }),
128 exit: (code) =>
129 Effect.sync(() => {
130 Electron.app.exit(code);
131 }),
132 relaunch: (options) =>
133 Effect.sync(() => {
134 Electron.app.relaunch(options);
135 }),
136 setPath: (name, path) =>
137 Effect.sync(() => {
138 Electron.app.setPath(name, path);
139 }),
140 setName: (name) =>
141 Effect.sync(() => {
142 Electron.app.setName(name);
143 }),
144 setAboutPanelOptions: (options) =>
145 Effect.sync(() => {
146 Electron.app.setAboutPanelOptions(options);
147 }),
148 setAppUserModelId: (id) =>
149 Effect.sync(() => {
150 Electron.app.setAppUserModelId(id);
151 }),
152 requestSingleInstanceLock: Effect.sync(() => Electron.app.requestSingleInstanceLock()),
153 isDefaultProtocolClient: (protocol) =>
154 Effect.sync(() => Electron.app.isDefaultProtocolClient(protocol)),
155 setAsDefaultProtocolClient: (protocol, path, args) =>
156 Effect.sync(() => {
157 if (path === undefined) {
158 return Electron.app.setAsDefaultProtocolClient(protocol);
159 }
160 return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]);
161 }),
162 setDesktopName: (desktopName) =>
163 Effect.sync(() => {
164 const linuxApp = Electron.app as Electron.App & {
165 setDesktopName?: (desktopName: string) => void;
166 };
167 linuxApp.setDesktopName?.(desktopName);
168 }),
169 setDockIcon: (iconPath) =>
170 Effect.sync(() => {
171 Electron.app.dock?.setIcon(iconPath);
172 }),
173 appendCommandLineSwitch: (switchName, value) =>
174 Effect.sync(() => {
175 if (value === undefined) {
176 Electron.app.commandLine.appendSwitch(switchName);
177 return;
178 }
179 Electron.app.commandLine.appendSwitch(switchName, value);
180 }),
181 on: addScopedAppListener,
182});
183
184export const layer = Layer.succeed(ElectronApp, make);