The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import * as Scope from "effect/Scope"; |
| 7 | |
| 8 | import * as Electron from "electron"; |
| 9 | |
| 10 | export interface ElectronAppMetadata { |
| 11 | readonly appVersion: string; |
| 12 | readonly appPath: string; |
| 13 | readonly isPackaged: boolean; |
| 14 | readonly resourcesPath: string; |
| 15 | readonly runningUnderArm64Translation: boolean; |
| 16 | } |
| 17 | |
| 18 | export class ElectronAppMetadataReadError extends Schema.TaggedError<ElectronAppMetadataReadError>()( |
| 19 | "ElectronAppMetadataReadError", |
| 20 | { |
| 21 | property: Schema.Literals(["app-version", "app-path"]), |
| 22 | cause: Schema.Defect(), |
| 23 | }, |
| 24 | ) { |
| 25 | override get message(): string { |
| 26 | return `Failed to read Electron app metadata property "${this.property}".`; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | export class ElectronAppWhenReadyError extends Schema.TaggedError<ElectronAppWhenReadyError>()( |
| 31 | "ElectronAppWhenReadyError", |
| 32 | { |
| 33 | isPackaged: Schema.Boolean, |
| 34 | cause: Schema.Defect(), |
| 35 | }, |
| 36 | ) { |
| 37 | override get message(): string { |
| 38 | return `Failed to wait for the Electron app to become ready (packaged: ${this.isPackaged}).`; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | export class ElectronApp extends Context.Service< |
| 43 | ElectronApp, |
| 44 | { |
| 45 | readonly metadata: Effect.Effect<ElectronAppMetadata, ElectronAppMetadataReadError>; |
| 46 | readonly name: Effect.Effect<string>; |
| 47 | /** Packaged builds ship only en-US; app.getLocale() and Intl defaults do not reflect the OS locale. */ |
| 48 | readonly systemLocale: Effect.Effect<string>; |
| 49 | readonly preferredSystemLanguages: Effect.Effect<ReadonlyArray<string>>; |
| 50 | readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>; |
| 51 | readonly requestSingleInstanceLock: Effect.Effect<boolean>; |
| 52 | readonly quit: Effect.Effect<void>; |
| 53 | readonly exit: (code: number) => Effect.Effect<void>; |
| 54 | readonly relaunch: (options: Electron.RelaunchOptions) => Effect.Effect<void>; |
| 55 | readonly setPath: ( |
| 56 | name: Parameters<Electron.App["setPath"]>[0], |
| 57 | path: string, |
| 58 | ) => Effect.Effect<void>; |
| 59 | readonly setName: (name: string) => Effect.Effect<void>; |
| 60 | readonly setAboutPanelOptions: ( |
| 61 | options: Electron.AboutPanelOptionsOptions, |
| 62 | ) => Effect.Effect<void>; |
| 63 | readonly setAppUserModelId: (id: string) => Effect.Effect<void>; |
| 64 | readonly getAppMetrics: Effect.Effect<ReadonlyArray<Electron.ProcessMetric>>; |
| 65 | readonly setAsDefaultProtocolClient: ( |
| 66 | protocol: string, |
| 67 | path?: string, |
| 68 | args?: readonly string[], |
| 69 | ) => Effect.Effect<boolean>; |
| 70 | readonly setDesktopName: (desktopName: string) => Effect.Effect<void>; |
| 71 | readonly setDockIcon: (iconPath: string) => Effect.Effect<void>; |
| 72 | readonly appendCommandLineSwitch: (switchName: string, value?: string) => Effect.Effect<void>; |
| 73 | readonly onBeforeQuitForUpdate: ( |
| 74 | listener: () => void, |
| 75 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 76 | readonly removeCommandLineSwitch: (switchName: string) => Effect.Effect<void>; |
| 77 | readonly on: <Args extends ReadonlyArray<unknown>>( |
| 78 | eventName: string, |
| 79 | listener: (...args: Args) => void, |
| 80 | ) => Effect.Effect<void, never, Scope.Scope>; |
| 81 | } |
| 82 | >()("@t3tools/desktop/electron/ElectronApp") {} |
| 83 | |
| 84 | const addScopedAppListener = <Args extends ReadonlyArray<unknown>>( |
| 85 | eventName: string, |
| 86 | listener: (...args: Args) => void, |
| 87 | ): Effect.Effect<void, never, Scope.Scope> => |
| 88 | Effect.acquireRelease( |
| 89 | Effect.sync(() => { |
| 90 | Electron.app.on(eventName as any, listener as any); |
| 91 | }), |
| 92 | () => |
| 93 | Effect.sync(() => { |
| 94 | Electron.app.removeListener(eventName as any, listener as any); |
| 95 | }), |
| 96 | ).pipe(Effect.asVoid); |
| 97 | |
| 98 | export function toSystemLocaleTag(identifier: string, hourCycle?: "h12" | "h23"): string { |
| 99 | const [base = "", keywords = ""] = identifier.split("@"); |
| 100 | const languageTag = base.replace(/_/g, "-"); |
| 101 | // Intl ignores rg extensions, so the macOS region override must replace the region subtag. |
| 102 | const region = /(?:^|;)rg=([a-z]{2}|\d{3})zzzz(?:;|$)/i.exec(keywords)?.[1]?.toUpperCase(); |
| 103 | try { |
| 104 | return new Intl.Locale(languageTag, { |
| 105 | ...(region ? { region } : {}), |
| 106 | ...(hourCycle ? { hourCycle } : {}), |
| 107 | }).toString(); |
| 108 | } catch { |
| 109 | return languageTag; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** @public */ |
| 114 | export const make = ElectronApp.of({ |
| 115 | metadata: Effect.gen(function* () { |
| 116 | const appVersion = yield* Effect.try({ |
| 117 | try: () => Electron.app.getVersion(), |
| 118 | catch: (cause) => |
| 119 | new ElectronAppMetadataReadError({ |
| 120 | property: "app-version", |
| 121 | cause, |
| 122 | }), |
| 123 | }); |
| 124 | const appPath = yield* Effect.try({ |
| 125 | try: () => Electron.app.getAppPath(), |
| 126 | catch: (cause) => |
| 127 | new ElectronAppMetadataReadError({ |
| 128 | property: "app-path", |
| 129 | cause, |
| 130 | }), |
| 131 | }); |
| 132 | |
| 133 | return { |
| 134 | appVersion, |
| 135 | appPath, |
| 136 | isPackaged: Electron.app.isPackaged, |
| 137 | resourcesPath: process.resourcesPath, |
| 138 | runningUnderArm64Translation: Electron.app.runningUnderARM64Translation === true, |
| 139 | }; |
| 140 | }), |
| 141 | name: Effect.sync(() => Electron.app.name), |
| 142 | systemLocale: Effect.gen(function* () { |
| 143 | const platform = yield* HostProcess.Platform; |
| 144 | let identifier = Electron.app.getSystemLocale(); |
| 145 | let hourCycle: "h12" | "h23" | undefined; |
| 146 | if (platform === "linux") { |
| 147 | const env = yield* HostProcess.Environment; |
| 148 | const timeLocale = (env.LC_ALL || env.LC_TIME || env.LANG)?.split(".")[0]; |
| 149 | if (timeLocale && timeLocale !== "C" && timeLocale !== "POSIX") identifier = timeLocale; |
| 150 | } else if (platform === "darwin") { |
| 151 | if (Electron.systemPreferences.getUserDefault("AppleICUForce24HourTime", "boolean")) { |
| 152 | hourCycle = "h23"; |
| 153 | } else if (Electron.systemPreferences.getUserDefault("AppleICUForce12HourTime", "boolean")) { |
| 154 | hourCycle = "h12"; |
| 155 | } |
| 156 | } |
| 157 | return toSystemLocaleTag(identifier, hourCycle); |
| 158 | }), |
| 159 | preferredSystemLanguages: Effect.sync(() => Electron.app.getPreferredSystemLanguages()), |
| 160 | whenReady: Effect.gen(function* () { |
| 161 | const isPackaged = Electron.app.isPackaged; |
| 162 | yield* Effect.tryPromise({ |
| 163 | try: () => Electron.app.whenReady(), |
| 164 | catch: (cause) => new ElectronAppWhenReadyError({ isPackaged, cause }), |
| 165 | }); |
| 166 | }), |
| 167 | requestSingleInstanceLock: Effect.sync(() => Electron.app.requestSingleInstanceLock()), |
| 168 | quit: Effect.sync(() => { |
| 169 | Electron.app.quit(); |
| 170 | }), |
| 171 | exit: Effect.fn("ElectronApp.exit")(function (code) { |
| 172 | return Effect.sync(() => { |
| 173 | Electron.app.exit(code); |
| 174 | }); |
| 175 | }), |
| 176 | relaunch: Effect.fn("ElectronApp.relaunch")(function (options) { |
| 177 | return Effect.sync(() => { |
| 178 | Electron.app.relaunch(options); |
| 179 | }); |
| 180 | }), |
| 181 | setPath: Effect.fn("ElectronApp.setPath")(function (name, path) { |
| 182 | return Effect.sync(() => { |
| 183 | Electron.app.setPath(name, path); |
| 184 | }); |
| 185 | }), |
| 186 | setName: Effect.fn("ElectronApp.setName")(function (name) { |
| 187 | return Effect.sync(() => { |
| 188 | Electron.app.setName(name); |
| 189 | }); |
| 190 | }), |
| 191 | setAboutPanelOptions: Effect.fn("ElectronApp.setAboutPanelOptions")(function (options) { |
| 192 | return Effect.sync(() => { |
| 193 | Electron.app.setAboutPanelOptions(options); |
| 194 | }); |
| 195 | }), |
| 196 | setAppUserModelId: Effect.fn("ElectronApp.setAppUserModelId")(function (id) { |
| 197 | return Effect.sync(() => { |
| 198 | Electron.app.setAppUserModelId(id); |
| 199 | }); |
| 200 | }), |
| 201 | getAppMetrics: Effect.sync(() => Electron.app.getAppMetrics()), |
| 202 | setAsDefaultProtocolClient: Effect.fn("ElectronApp.setAsDefaultProtocolClient")( |
| 203 | function (protocol, path, args) { |
| 204 | return Effect.sync(() => { |
| 205 | if (path === undefined) { |
| 206 | return Electron.app.setAsDefaultProtocolClient(protocol); |
| 207 | } |
| 208 | return Electron.app.setAsDefaultProtocolClient(protocol, path, [...(args ?? [])]); |
| 209 | }); |
| 210 | }, |
| 211 | ), |
| 212 | setDesktopName: Effect.fn("ElectronApp.setDesktopName")(function (desktopName) { |
| 213 | return Effect.sync(() => { |
| 214 | const linuxApp = Electron.app as Electron.App & { |
| 215 | setDesktopName?: (desktopName: string) => void; |
| 216 | }; |
| 217 | linuxApp.setDesktopName?.(desktopName); |
| 218 | }); |
| 219 | }), |
| 220 | setDockIcon: Effect.fn("ElectronApp.setDockIcon")(function (iconPath) { |
| 221 | return Effect.sync(() => { |
| 222 | Electron.app.dock?.setIcon(iconPath); |
| 223 | }); |
| 224 | }), |
| 225 | appendCommandLineSwitch: Effect.fn("ElectronApp.appendCommandLineSwitch")( |
| 226 | function (switchName, value) { |
| 227 | return Effect.sync(() => { |
| 228 | if (value === undefined) { |
| 229 | Electron.app.commandLine.appendSwitch(switchName); |
| 230 | return; |
| 231 | } |
| 232 | Electron.app.commandLine.appendSwitch(switchName, value); |
| 233 | }); |
| 234 | }, |
| 235 | ), |
| 236 | onBeforeQuitForUpdate: Effect.fn("ElectronApp.onBeforeQuitForUpdate")(function (listener) { |
| 237 | return Effect.acquireRelease( |
| 238 | Effect.sync(() => { |
| 239 | Electron.autoUpdater.on("before-quit-for-update", listener); |
| 240 | }), |
| 241 | () => |
| 242 | Effect.sync(() => { |
| 243 | Electron.autoUpdater.removeListener("before-quit-for-update", listener); |
| 244 | }), |
| 245 | ).pipe(Effect.asVoid); |
| 246 | }), |
| 247 | removeCommandLineSwitch: Effect.fn("ElectronApp.removeCommandLineSwitch")(function (switchName) { |
| 248 | return Effect.sync(() => { |
| 249 | Electron.app.commandLine.removeSwitch(switchName); |
| 250 | }); |
| 251 | }), |
| 252 | on: addScopedAppListener, |
| 253 | }); |
| 254 | |
| 255 | export const layer = Layer.succeed(ElectronApp, make); |