Open History to see the last commit
1import { fromLenientJson } from "@t3tools/shared/schemaJson";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4
5import {
6 DEFAULT_LINUX_PASSWORD_STORE,
7 normalizeLinuxPasswordStorePreference,
8 resolveLinuxPasswordStoreSwitch,
9 type LinuxPasswordStoreSwitch,
10 type LinuxPasswordStorePreference,
11} from "../linuxSecretStorage.ts";
12import {
13 resolveDesktopBaseDir,
14 resolveDesktopStateDir,
15 type JoinPath,
16} from "./DesktopStatePaths.ts";
17
18interface EarlyDesktopSettingsInput {
19 readonly env: NodeJS.ProcessEnv;
20 readonly homeDirectory: string;
21 readonly joinPath: JoinPath;
22 readonly readFileString: (path: string) => string;
23}
24
25type EarlyLinuxElectronOptionsInput = EarlyDesktopSettingsInput;
26
27export interface EarlyLinuxElectronOptions {
28 readonly isDevelopment: boolean;
29 readonly linuxWmClass: string;
30 readonly linuxDesktopEntryName: string;
31 readonly passwordStore: LinuxPasswordStoreSwitch | null;
32}
33
34export const resolveLinuxDesktopEntryName = (isDevelopment: boolean): string =>
35 isDevelopment ? "com.t3tools.T3Code.Development.desktop" : "com.t3tools.T3Code.desktop";
36
37const trimNonEmpty = (value: string | undefined): string | null => {
38 const trimmed = value?.trim();
39 return trimmed && trimmed.length > 0 ? trimmed : null;
40};
41
42const EarlyDesktopSettingsJson = fromLenientJson(
43 Schema.Struct({
44 linuxPasswordStore: Schema.optionalKey(Schema.Unknown),
45 }),
46);
47const decodeEarlyDesktopSettingsJson = Schema.decodeSync(EarlyDesktopSettingsJson);
48
49const isDevelopmentEnvironment = (env: NodeJS.ProcessEnv): boolean =>
50 trimNonEmpty(env.VITE_DEV_SERVER_URL) !== null;
51
52function resolveEarlyDesktopSettingsPath(input: {
53 readonly env: NodeJS.ProcessEnv;
54 readonly homeDirectory: string;
55 readonly joinPath: JoinPath;
56}): string {
57 const t3Home = Option.fromUndefinedOr(input.env.T3CODE_HOME);
58 const baseDir = resolveDesktopBaseDir({
59 homeDirectory: input.homeDirectory,
60 joinPath: input.joinPath,
61 t3Home,
62 });
63 const stateDir = resolveDesktopStateDir({
64 baseDir,
65 isDevelopment: isDevelopmentEnvironment(input.env),
66 joinPath: input.joinPath,
67 t3Home,
68 });
69 return input.joinPath(stateDir, "desktop-settings.json");
70}
71
72export function resolveEarlyLinuxPasswordStorePreference(
73 input: EarlyDesktopSettingsInput,
74): LinuxPasswordStorePreference {
75 const settingsPath = resolveEarlyDesktopSettingsPath(input);
76 try {
77 const parsed = decodeEarlyDesktopSettingsJson(input.readFileString(settingsPath));
78 return normalizeLinuxPasswordStorePreference(parsed.linuxPasswordStore);
79 } catch {
80 return DEFAULT_LINUX_PASSWORD_STORE;
81 }
82}
83
84const WAYLAND_COLOR_MANAGER_FEATURE = "WaylandWpColorManagerV1";
85
86const featureListNames = (value: string | null): ReadonlyArray<string> =>
87 (value ?? "").split(",").flatMap((entry) => entry.trim().match(/^\*?([^<:]+)/)?.[1] ?? []);
88
89// A stale HYPRLAND_INSTANCE_SIGNATURE is often inherited into other desktops, so only
90// trust the session's desktop names.
91const isHyprlandSession = (env: NodeJS.ProcessEnv): boolean =>
92 (trimNonEmpty(env.XDG_CURRENT_DESKTOP) ?? trimNonEmpty(env.XDG_SESSION_DESKTOP) ?? "")
93 .split(":")
94 .some((name) => name.trim().toLowerCase() === "hyprland");
95
96const HyprctlVersionJson = fromLenientJson(
97 Schema.Struct({
98 version: Schema.String,
99 tag: Schema.String,
100 commit: Schema.String,
101 dirty: Schema.Boolean,
102 }),
103);
104const decodeHyprctlVersionJson = Schema.decodeSync(HyprctlVersionJson);
105
106// Hyprland 0.48.0 added wp-color-manager-v1 (hyprwm/Hyprland#9444) advertising
107// FEATURE_SET_PRIMARIES while rejecting set_primaries outside debug mode, which Chromium
108// treats as a fatal protocol error. 0.51.0 accepts them (hyprwm/Hyprland#11499).
109// Only stock builds of these releases count. Matching the commit as well as the tag
110// keeps later development builds that still report an old VERSION and tag (Nix
111// synthesizes `v${VERSION}`) on the default.
112const AFFECTED_HYPRLAND_RELEASE_COMMITS: ReadonlyMap<string, string> = new Map([
113 ["0.48.0", "5ee35f914f921e5696030698e74fb5566a804768"],
114 ["0.48.1", "29e2e59fdbab8ed2cc23a20e3c6043d5decb5cdc"],
115 ["0.49.0", "9958d297641b5c84dcff93f9039d80a5ad37ab00"],
116 ["0.50.0", "c4a4c341568944bd4fb9cd503558b2de602c0213"],
117 ["0.50.1", "4e242d086e20b32951fdc0ebcbfb4d41b5be8dcc"],
118]);
119
120const hasBrokenColorManager = (readHyprctlVersionJson: () => string): boolean => {
121 try {
122 const { version, tag, commit, dirty } = decodeHyprctlVersionJson(readHyprctlVersionJson());
123 return (
124 AFFECTED_HYPRLAND_RELEASE_COMMITS.get(version) === commit && tag === `v${version}` && !dirty
125 );
126 } catch {
127 return false;
128 }
129};
130
131export function resolveLinuxDisableFeaturesSwitch(input: {
132 readonly env: NodeJS.ProcessEnv;
133 readonly ozonePlatform: string | null;
134 readonly enableFeatures: string | null;
135 readonly disableFeatures: string | null;
136 readonly readHyprctlVersionJson: () => string;
137}): string | null {
138 if (input.ozonePlatform !== "wayland" || !isHyprlandSession(input.env)) return null;
139 if (featureListNames(input.enableFeatures).includes(WAYLAND_COLOR_MANAGER_FEATURE)) return null;
140 if (featureListNames(input.disableFeatures).includes(WAYLAND_COLOR_MANAGER_FEATURE)) return null;
141 if (!hasBrokenColorManager(input.readHyprctlVersionJson)) return null;
142 return input.disableFeatures === null
143 ? WAYLAND_COLOR_MANAGER_FEATURE
144 : `${input.disableFeatures},${WAYLAND_COLOR_MANAGER_FEATURE}`;
145}
146
147export function resolveEarlyLinuxElectronOptions(
148 input: EarlyLinuxElectronOptionsInput,
149): EarlyLinuxElectronOptions {
150 const preference = resolveEarlyLinuxPasswordStorePreference(input);
151 const isDevelopment = isDevelopmentEnvironment(input.env);
152 return {
153 isDevelopment,
154 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
155 linuxDesktopEntryName: resolveLinuxDesktopEntryName(isDevelopment),
156 passwordStore: resolveLinuxPasswordStoreSwitch({
157 preference,
158 env: input.env,
159 }),
160 };
161}
162
163/** Remember the explicit Chromium scale across AppImage updates, which drop launch arguments. */
164export function restoreEarlyLinuxDeviceScaleFactor(
165 input: EarlyDesktopSettingsInput & {
166 readonly commandLine: {
167 readonly hasSwitch: (name: string) => boolean;
168 readonly getSwitchValue: (name: string) => string;
169 readonly appendSwitch: (name: string, value: string) => void;
170 };
171 readonly writeFileString: (path: string, value: string) => void;
172 },
173): void {
174 // Keep this separate from desktop-settings.json: the settings service rewrites
175 // that document, and Chromium needs the value before that service is available.
176 const path = input.joinPath(
177 resolveEarlyDesktopSettingsPath(input),
178 "..",
179 "linux-device-scale-factor",
180 );
181 const switchName = "force-device-scale-factor";
182 const isValid = (value: string) =>
183 value.trim().length > 0 && Number.isFinite(Number(value)) && Number(value) > 0;
184 if (input.commandLine.hasSwitch(switchName)) {
185 const value = input.commandLine.getSwitchValue(switchName);
186 if (isValid(value)) {
187 try {
188 input.writeFileString(path, value);
189 } catch {
190 // A read-only state directory must not prevent startup with the explicit switch.
191 }
192 }
193 return;
194 }
195 try {
196 const value = input.readFileString(path).trim();
197 if (isValid(value)) input.commandLine.appendSwitch(switchName, value);
198 } catch {
199 // First launch, missing state, or unreadable state: keep Chromium's default.
200 }
201}