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

ElectronApp.ts

118 lines3.8 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Scope from "effect/Scope";
5​
6import * as Electron from "electron";
7​
8export interface ElectronAppMetadata {
9 readonly appVersion: string;
10 readonly appPath: string;
11 readonly isPackaged: boolean;
12 readonly resourcesPath: string;
13 readonly runningUnderArm64Translation: boolean;
15​
16export interface ElectronAppShape {
17 readonly metadata: Effect.Effect<ElectronAppMetadata>;
18 readonly name: Effect.Effect<string>;
19 readonly whenReady: Effect.Effect<void>;
20 readonly quit: Effect.Effect<void>;
21 readonly exit: (code: number) => Effect.Effect<void>;
22 readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>;
23 readonly setPath: (
24 name: Parameters<Electron.App["setPath"]>[0],
25 path: string,
26 ) => Effect.Effect<void>;
27 readonly setName: (name: string) => Effect.Effect<void>;
28 readonly setAboutPanelOptions: (
29 options: Electron.AboutPanelOptionsOptions,
30 ) => Effect.Effect<void>;
31 readonly setAppUserModelId: (id: string) => Effect.Effect<void>;
32 readonly setDesktopName: (desktopName: string) => Effect.Effect<void>;
33 readonly setDockIcon: (iconPath: string) => Effect.Effect<void>;
34 readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>;
35 readonly on: <Args extends ReadonlyArray<unknown>>(
36 eventName: string,
37 listener: (...args: Args) => void,
38 ) => Effect.Effect<void, never, Scope.Scope>;
40​
41export class ElectronApp extends Context.Service<ElectronApp, ElectronAppShape>()(
42 "t3/desktop/electron/App",
43) {}
44​
45const addScopedAppListener = <Args extends ReadonlyArray<unknown>>(
46 eventName: string,
47 listener: (...args: Args) => void,
48): Effect.Effect<void, never, Scope.Scope> =>
49 Effect.acquireRelease(
50 Effect.sync(() => {
51 Electron.app.on(eventName as any, listener as any);
52 }),
53 () =>
54 Effect.sync(() => {
55 Electron.app.removeListener(eventName as any, listener as any);
56 }),
57 ).pipe(Effect.asVoid);
58​
59const make = ElectronApp.of({
60 metadata: Effect.sync(() => ({
61 appVersion: Electron.app.getVersion(),
62 appPath: Electron.app.getAppPath(),
63 isPackaged: Electron.app.isPackaged,
64 resourcesPath: process.resourcesPath,
65 runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true,
66 })),
67 name: Effect.sync(() => Electron.app.name),
68 whenReady: Effect.promise(() => Electron.app.whenReady()).pipe(Effect.asVoid),
69 quit: Effect.sync(() => {
70 Electron.app.quit();
71 }),
72 exit: (code) =>
73 Effect.sync(() => {
74 Electron.app.exit(code);
75 }),
76 relaunch: (options) =>
77 Effect.sync(() => {
78 Electron.app.relaunch(options);
79 }),
80 setPath: (name, path) =>
81 Effect.sync(() => {
82 Electron.app.setPath(name, path);
83 }),
84 setName: (name) =>
85 Effect.sync(() => {
86 Electron.app.setName(name);
87 }),
88 setAboutPanelOptions: (options) =>
89 Effect.sync(() => {
90 Electron.app.setAboutPanelOptions(options);
91 }),
92 setAppUserModelId: (id) =>
93 Effect.sync(() => {
94 Electron.app.setAppUserModelId(id);
95 }),
96 setDesktopName: (desktopName) =>
97 Effect.sync(() => {
98 const linuxApp = Electron.app as Electron.App & {
99 setDesktopName?: (desktopName: string) => void;
100 };
101 linuxApp.setDesktopName?.(desktopName);
102 }),
103 setDockIcon: (iconPath) =>
104 Effect.sync(() => {
105 Electron.app.dock?.setIcon(iconPath);
106 }),
107 appendCommandLineSwitch: (switchName, value) =>
108 Effect.sync(() => {
109 if (value === undefined) {
110 Electron.app.commandLine.appendSwitch(switchName);
111 return;
112 }
113 Electron.app.commandLine.appendSwitch(switchName, value);
114 }),
115 on: addScopedAppListener,
116});
117​
118export const layer = Layer.succeed(ElectronApp, make);