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

DesktopPreReadyPlatform.ts

143 lines6.0 KB

1// @effect-diagnostics nodeBuiltinImport:off - pre-ready Electron setup reads settings, prepares the Linux desktop entry, and asks Hyprland for its version synchronously before app services are available.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5import * as Context from "effect/Context";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8​
9import * as Electron from "electron";
10import * as HostProcess from "@t3tools/shared/HostProcess";
11​
12import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
13import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
14import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
15import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
16​
17export interface DesktopPreReadyCommandLineReader {
18 readonly hasSwitch: (switchName: string) => boolean;
19 readonly getSwitchValue: (switchName: string) => string;
21​
22function readCommandLineSwitchValue(
23 commandLine: DesktopPreReadyCommandLineReader,
24 switchName: string,
25): string | null {
26 if (!commandLine.hasSwitch(switchName)) {
27 return null;
28 }
29​
30 const value = commandLine.getSwitchValue(switchName).trim();
31 return value.length > 0 ? value : null;
33​
34export const resolveEarlyLinuxElectronOptionsFromProcess = (
35 homeDirectory: string,
36): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
37 DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
38 env: process.env,
39 homeDirectory,
40 joinPath: NodePath.posix.join,
41 readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
42 });
43​
44export class DesktopPreReadyElectronOptions extends Context.Service<
45 DesktopPreReadyElectronOptions,
46 {
47 readonly linux: DesktopEarlyElectronStartup.EarlyLinuxElectronOptions | null;
48 readonly linuxPasswordStoreCommandLine: string | null;
49 }
50>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
51​
52/** @public Service construction is part of the canonical Effect module API. */
53export const make = Effect.gen(function* () {
54 const platform = yield* HostProcess.Platform;
55 const homeDirectory = yield* HostProcess.HomeDirectory;
56 return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
57 const linuxPasswordStoreCommandLine =
58 platform === "linux"
59 ? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")
60 : null;
61 const linux =
62 platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
63​
64 if (linux !== null) {
65 // The portal also requires a valid desktop entry. An AppImage update may
66 // have removed the executable referenced by the previous launch's entry.
67 try {
68 const applicationsDir = NodePath.posix.join(
69 process.env.XDG_DATA_HOME?.trim() ||
70 NodePath.posix.join(homeDirectory, ".local", "share"),
71 "applications",
72 );
73 NodeFS.mkdirSync(applicationsDir, { recursive: true });
74 const iconPath = Electron.app.isPackaged
75 ? NodePath.posix.join(
76 applicationsDir,
77 "..",
78 "icons",
79 `${linux.linuxDesktopEntryName}.png`,
80 )
81 : undefined;
82 if (iconPath !== undefined) {
83 try {
84 NodeFS.mkdirSync(NodePath.posix.dirname(iconPath), { recursive: true });
85 NodeFS.copyFileSync(
86 NodePath.posix.join(
87 Electron.app.getAppPath(),
88 "apps/desktop/prod-resources/icon.png",
89 ),
90 iconPath,
91 );
92 } catch {}
93 }
94 NodeFS.writeFileSync(
95 NodePath.posix.join(applicationsDir, linux.linuxDesktopEntryName),
96 renderUrlHandlerDesktopEntry({
97 displayName: resolveDesktopAppBranding({
98 isDevelopment: linux.isDevelopment,
99 appVersion: Electron.app.getVersion(),
100 }).displayName,
101 execTarget: process.env.APPIMAGE?.trim() || process.execPath,
102 scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
103 ...(iconPath === undefined ? {} : { iconPath }),
104 }),
105 "utf8",
106 );
107 } catch {}
108 // Chromium caches its portal registration during startup. Set the identity
109 // before any asynchronous work can initialize it with Electron's default.
110 Electron.app.setDesktopName(linux.linuxDesktopEntryName);
111 Electron.app.commandLine.appendSwitch("class", linux.linuxWmClass);
112 if (linux.passwordStore !== null && linuxPasswordStoreCommandLine === null) {
113 Electron.app.commandLine.appendSwitch("password-store", linux.passwordStore);
114 }
115 // Chromium keeps only the last --disable-features value, so pass the merged list.
116 const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
117 env: process.env,
118 ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
119 enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
120 disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),
121 readHyprctlVersionJson: () =>
122 NodeChildProcess.execFileSync("hyprctl", ["version", "-j"], {
123 encoding: "utf8",
124 stdio: ["ignore", "pipe", "ignore"],
125 timeout: 1000,
126 maxBuffer: 64 * 1024,
127 }),
128 });
129 if (disableFeatures !== null) {
130 Electron.app.commandLine.appendSwitch("disable-features", disableFeatures);
131 }
132 }
133​
134 return { linux, linuxPasswordStoreCommandLine };
135 });
136}).pipe(Effect.withSpan("desktop.electron.configureBeforeReady"));
137​
138// Keep Electron's strict pre-ready setup isolated so later runtime layers cannot
139// observe app readiness before scheme privileges and command-line switches exist.
140export const layer = Layer.mergeAll(
141 ElectronProtocol.layerSchemePrivileges,
142 Layer.effect(DesktopPreReadyElectronOptions, make),
143);