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// HYPRLAND_INSTANCE_SIGNATURE can be stale; trust only the session's desktop names.
90const isHyprlandSession = (env: NodeJS.ProcessEnv): boolean =>
91 (trimNonEmpty(env.XDG_CURRENT_DESKTOP) ?? trimNonEmpty(env.XDG_SESSION_DESKTOP) ?? "")
92 .split(":")
93 .some((name) => name.trim().toLowerCase() === "hyprland");
94
95const HyprctlVersionJson = fromLenientJson(
96 Schema.Struct({
97 version: Schema.String,
98 tag: Schema.String,
99 commit: Schema.String,
100 dirty: Schema.Boolean,
101 }),
102);
103const decodeHyprctlVersionJson = Schema.decodeSync(HyprctlVersionJson);
104
105// Hyprland 0.48.0 rejects advertised set_primaries (hyprwm/Hyprland#9444); 0.51.0 accepts it (hyprwm/Hyprland#11499).
106// Check commits because dev and Nix builds can retain release versions and tags.
107const AFFECTED_HYPRLAND_RELEASE_COMMITS: ReadonlyMap<string, string> = new Map([
108 ["0.48.0", "5ee35f914f921e5696030698e74fb5566a804768"],
109 ["0.48.1", "29e2e59fdbab8ed2cc23a20e3c6043d5decb5cdc"],
110 ["0.49.0", "9958d297641b5c84dcff93f9039d80a5ad37ab00"],
111 ["0.50.0", "c4a4c341568944bd4fb9cd503558b2de602c0213"],
112 ["0.50.1", "4e242d086e20b32951fdc0ebcbfb4d41b5be8dcc"],
113]);
114
115const hasBrokenColorManager = (readHyprctlVersionJson: () => string): boolean => {
116 try {
117 const { version, tag, commit, dirty } = decodeHyprctlVersionJson(readHyprctlVersionJson());
118 return (
119 AFFECTED_HYPRLAND_RELEASE_COMMITS.get(version) === commit && tag === `v${version}` && !dirty
120 );
121 } catch {
122 return false;
123 }
124};
125
126export function resolveLinuxDisableFeaturesSwitch(input: {
127 readonly env: NodeJS.ProcessEnv;
128 readonly ozonePlatform: string | null;
129 readonly enableFeatures: string | null;
130 readonly disableFeatures: string | null;
131 readonly readHyprctlVersionJson: () => string;
132}): string | null {
133 if (input.ozonePlatform !== "wayland" || !isHyprlandSession(input.env)) return null;
134 if (featureListNames(input.enableFeatures).includes(WAYLAND_COLOR_MANAGER_FEATURE)) return null;
135 if (featureListNames(input.disableFeatures).includes(WAYLAND_COLOR_MANAGER_FEATURE)) return null;
136 if (!hasBrokenColorManager(input.readHyprctlVersionJson)) return null;
137 return input.disableFeatures === null
138 ? WAYLAND_COLOR_MANAGER_FEATURE
139 : `${input.disableFeatures},${WAYLAND_COLOR_MANAGER_FEATURE}`;
140}
141
142export function resolveEarlyLinuxElectronOptions(
143 input: EarlyLinuxElectronOptionsInput,
144): EarlyLinuxElectronOptions {
145 const preference = resolveEarlyLinuxPasswordStorePreference(input);
146 const isDevelopment = isDevelopmentEnvironment(input.env);
147 return {
148 isDevelopment,
149 linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
150 linuxDesktopEntryName: resolveLinuxDesktopEntryName(isDevelopment),
151 passwordStore: resolveLinuxPasswordStoreSwitch({
152 preference,
153 env: input.env,
154 }),
155 };
156}
157
158/** Remember the explicit Chromium scale across AppImage updates, which drop launch arguments. */
159export function restoreEarlyLinuxDeviceScaleFactor(
160 input: EarlyDesktopSettingsInput & {
161 readonly commandLine: {
162 readonly hasSwitch: (name: string) => boolean;
163 readonly getSwitchValue: (name: string) => string;
164 readonly appendSwitch: (name: string, value: string) => void;
165 };
166 readonly writeFileString: (path: string, value: string) => void;
167 },
168): void {
169 // Keep this separate from desktop-settings.json: the settings service rewrites
170 // that document, and Chromium needs the value before that service is available.
171 const path = input.joinPath(
172 resolveEarlyDesktopSettingsPath(input),
173 "..",
174 "linux-device-scale-factor",
175 );
176 const switchName = "force-device-scale-factor";
177 const isValid = (value: string) =>
178 value.trim().length > 0 && Number.isFinite(Number(value)) && Number(value) > 0;
179 if (input.commandLine.hasSwitch(switchName)) {
180 const value = input.commandLine.getSwitchValue(switchName);
181 if (isValid(value)) {
182 try {
183 input.writeFileString(path, value);
184 } catch {
185 // A read-only state directory must not prevent startup with the explicit switch.
186 }
187 }
188 return;
189 }
190 try {
191 const value = input.readFileString(path).trim();
192 if (isValid(value)) input.commandLine.appendSwitch(switchName, value);
193 } catch {
194 // First launch, missing state, or unreadable state: keep Chromium's default.
195 }
196}