Skip to content
TypeScript

fix(desktop): preserve Linux device scale across updates (#14421)

jkramer5103f7ad7f2dca98
diff --git a/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts b/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts
index 01d1aa1b115..6f72aa1f509 100644
--- a/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts
+++ b/apps/desktop/src/app/DesktopEarlyElectronStartup.test.ts
@@ -3,6 +3,7 @@ import * as NodePath from "node:path";
import { assert, describe, it } from "@effect/vitest";
import {
+ restoreEarlyLinuxDeviceScaleFactor,
resolveEarlyLinuxElectronOptions,
resolveEarlyLinuxPasswordStorePreference,
resolveLinuxDisableFeaturesSwitch,
@@ -269,3 +270,70 @@ describe("DesktopEarlyElectronStartup", () => {
});
});
});
+
+describe("Linux device scale persistence failures", () => {
+ const input = {
+ env: { T3CODE_HOME: "/isolated" },
+ homeDirectory: "/home/user",
+ joinPath: NodePath.posix.join,
+ };
+
+ it.each(["0", "Infinity", "garbage"])("ignores invalid persisted scale %j", (value) => {
+ const applied: Array<[string, string]> = [];
+ const writes: Array<string> = [];
+ restoreEarlyLinuxDeviceScaleFactor({
+ ...input,
+ commandLine: {
+ hasSwitch: () => false,
+ getSwitchValue: () => "",
+ appendSwitch: (name, scale) => applied.push([name, scale]),
+ },
+ readFileString: () => value,
+ writeFileString: (_path, contents) => writes.push(contents),
+ });
+ assert.deepEqual(applied, []);
+ assert.deepEqual(writes, []);
+ });
+
+ it("keeps startup available with default scaling when saved state cannot be read", () => {
+ const applied: Array<[string, string]> = [];
+ restoreEarlyLinuxDeviceScaleFactor({
+ ...input,
+ commandLine: {
+ hasSwitch: () => false,
+ getSwitchValue: () => "",
+ appendSwitch: (name, value) => applied.push([name, value]),
+ },
+ readFileString: () => {
+ throw new Error("ENOENT");
+ },
+ writeFileString: () => {},
+ });
+ assert.deepEqual(applied, []);
+ });
+
+ it("keeps an explicit scale when saving it fails, without falling back to saved state", () => {
+ const applied: Array<[string, string]> = [];
+ const reads: Array<string> = [];
+ const writes: Array<string> = [];
+ restoreEarlyLinuxDeviceScaleFactor({
+ ...input,
+ commandLine: {
+ hasSwitch: () => true,
+ getSwitchValue: () => "1",
+ appendSwitch: (name, value) => applied.push([name, value]),
+ },
+ readFileString: (path) => {
+ reads.push(path);
+ return "1.75";
+ },
+ writeFileString: (_path, value) => {
+ writes.push(value);
+ throw new Error("EACCES");
+ },
+ });
+ assert.deepEqual(writes, ["1"]);
+ assert.deepEqual(reads, []);
+ assert.deepEqual(applied, []);
+ });
+});
diff --git a/apps/desktop/src/app/DesktopEarlyElectronStartup.ts b/apps/desktop/src/app/DesktopEarlyElectronStartup.ts
index ad294701377..0015e642672 100644
--- a/apps/desktop/src/app/DesktopEarlyElectronStartup.ts
+++ b/apps/desktop/src/app/DesktopEarlyElectronStartup.ts
@@ -159,3 +159,43 @@ export function resolveEarlyLinuxElectronOptions(
}),
};
}
+
+/** Remember the explicit Chromium scale across AppImage updates, which drop launch arguments. */
+export function restoreEarlyLinuxDeviceScaleFactor(
+ input: EarlyDesktopSettingsInput & {
+ readonly commandLine: {
+ readonly hasSwitch: (name: string) => boolean;
+ readonly getSwitchValue: (name: string) => string;
+ readonly appendSwitch: (name: string, value: string) => void;
+ };
+ readonly writeFileString: (path: string, value: string) => void;
+ },
+): void {
+ // Keep this separate from desktop-settings.json: the settings service rewrites
+ // that document, and Chromium needs the value before that service is available.
+ const path = input.joinPath(
+ resolveEarlyDesktopSettingsPath(input),
+ "..",
+ "linux-device-scale-factor",
+ );
+ const switchName = "force-device-scale-factor";
+ const isValid = (value: string) =>
+ value.trim().length > 0 && Number.isFinite(Number(value)) && Number(value) > 0;
+ if (input.commandLine.hasSwitch(switchName)) {
+ const value = input.commandLine.getSwitchValue(switchName);
+ if (isValid(value)) {
+ try {
+ input.writeFileString(path, value);
+ } catch {
+ // A read-only state directory must not prevent startup with the explicit switch.
+ }
+ }
+ return;
+ }
+ try {
+ const value = input.readFileString(path).trim();
+ if (isValid(value)) input.commandLine.appendSwitch(switchName, value);
+ } catch {
+ // First launch, missing state, or unreadable state: keep Chromium's default.
+ }
+}
diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
index 845e89331f9..d27251d7b2f 100644
--- a/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
+++ b/apps/desktop/src/app/DesktopPreReadyPlatform.test.ts
@@ -15,6 +15,7 @@ const {
writeFileSyncMock,
copyFileSyncMock,
execFileSyncMock,
+ readFileSyncMock,
} = vi.hoisted(() => ({
appendSwitchMock: vi.fn(),
getSwitchValueMock: vi.fn(),
@@ -25,6 +26,7 @@ const {
writeFileSyncMock: vi.fn(),
copyFileSyncMock: vi.fn(),
execFileSyncMock: vi.fn(),
+ readFileSyncMock: vi.fn(),
}));
vi.mock("electron", () => ({
@@ -45,7 +47,7 @@ vi.mock("electron", () => ({
}));
vi.mock("node:fs", () => ({
- readFileSync: () => "{}",
+ readFileSync: readFileSyncMock,
mkdirSync: mkdirSyncMock,
writeFileSync: writeFileSyncMock,
copyFileSync: copyFileSyncMock,
@@ -68,6 +70,66 @@ describe("DesktopPreReadyPlatform", () => {
writeFileSyncMock.mockReset();
copyFileSyncMock.mockReset();
execFileSyncMock.mockReset();
+ readFileSyncMock.mockReset().mockReturnValue("{}");
+ });
+
+ it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => {
+ vi.stubEnv("T3CODE_HOME", "/isolated");
+ vi.stubEnv("VITE_DEV_SERVER_URL", "");
+ const settingsPath = "/isolated/userdata/desktop-settings.json";
+ const files = new Map([[settingsPath, "{}"]]);
+ const commandLine = new Map<string, string>();
+ readFileSyncMock.mockImplementation((path: string) => {
+ const contents = files.get(path);
+ if (contents === undefined) throw new Error("ENOENT");
+ return contents;
+ });
+ writeFileSyncMock.mockImplementation((path: string, contents: string) => {
+ files.set(path, contents);
+ });
+ hasSwitchMock.mockImplementation((name: string) => commandLine.has(name));
+ getSwitchValueMock.mockImplementation((name: string) => commandLine.get(name) ?? "");
+ appendSwitchMock.mockImplementation((name: string, value: string) => {
+ commandLine.set(name, value);
+ });
+ const launch = Effect.gen(function* () {
+ const startup = Promise.resolve().then(() => commandLine.get("force-device-scale-factor"));
+ yield* DesktopPreReadyPlatform.make;
+ return yield* Effect.promise(() => startup);
+ });
+
+ return Effect.gen(function* () {
+ for (const explicitScale of ["1.75", "1"]) {
+ commandLine.clear();
+ commandLine.set("force-device-scale-factor", explicitScale);
+ appendSwitchMock.mockClear();
+ assert.equal(yield* launch, explicitScale);
+ assert.isFalse(
+ appendSwitchMock.mock.calls.some(([name]) => name === "force-device-scale-factor"),
+ );
+
+ // The settings service discards fields it does not own when rewriting its document.
+ files.set(settingsPath, JSON.stringify({ linuxPasswordStore: "basic" }));
+ commandLine.clear();
+ assert.equal(yield* launch, explicitScale);
+ }
+ }).pipe(
+ Effect.provideService(HostProcess.Platform, "linux"),
+ Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
+ );
+ });
+
+ it.effect.each(["darwin", "win32"] as const)("leaves device scaling alone on %s", (platform) => {
+ readFileSyncMock.mockReturnValue("1.75");
+ hasSwitchMock.mockImplementation((name: string) => name === "force-device-scale-factor");
+ getSwitchValueMock.mockReturnValue("2");
+
+ return Effect.gen(function* () {
+ yield* DesktopPreReadyPlatform.make;
+ assert.deepEqual(readFileSyncMock.mock.calls, []);
+ assert.deepEqual(writeFileSyncMock.mock.calls, []);
+ assert.deepEqual(appendSwitchMock.mock.calls, []);
+ }).pipe(Effect.provideService(HostProcess.Platform, platform));
});
it.effect("preserves an explicit Linux password-store switch", () => {
diff --git a/apps/desktop/src/app/DesktopPreReadyPlatform.ts b/apps/desktop/src/app/DesktopPreReadyPlatform.ts
index a90246c73b3..58670439a52 100644
--- a/apps/desktop/src/app/DesktopPreReadyPlatform.ts
+++ b/apps/desktop/src/app/DesktopPreReadyPlatform.ts
@@ -62,6 +62,17 @@ export const make = Effect.gen(function* () {
platform === "linux" ? resolveEarlyLinuxElectronOptionsFromProcess(homeDirectory) : null;
if (linux !== null) {
+ DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
+ env: process.env,
+ homeDirectory,
+ joinPath: NodePath.posix.join,
+ commandLine: Electron.app.commandLine,
+ readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
+ writeFileString: (path, value) => {
+ NodeFS.mkdirSync(NodePath.posix.dirname(path), { recursive: true });
+ NodeFS.writeFileSync(path, value, "utf8");
+ },
+ });
// The portal also requires a valid desktop entry. An AppImage update may
// have removed the executable referenced by the previous launch's entry.
try {