@devries-cameron20 / t3code-1 / commits / f98118d033
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): Install Linux theme icons named after the app id (#14471)

Mike Olson committed Oct 18, 2026 at 15:15 UTC f98118d033d28ab309917610cd4ff5285f8b696e
2 files changed +363
apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts
+289
@@ -1,3 +1,4 @@
1 +import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, describe, it } from "@effect/vitest";
3 import * as Deferred from "effect/Deferred";
4 import * as Effect from "effect/Effect";
@@ -389,3 +390,291 @@ describe("DesktopLinuxUrlHandler", () => {
390 }),
391 );
392 });
393 +
394 +interface ThemeIconRun {
395 + readonly root: string;
396 + readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
397 + readonly iconCacheOptions?: Array<Record<string, unknown>>;
398 + readonly sourceMissing?: boolean;
399 + readonly iconCacheExitCode?: number;
400 + readonly iconCacheSpawnFails?: boolean;
401 + readonly iconCacheStalled?: boolean;
402 + readonly iconCacheStarted?: Deferred.Deferred<void>;
403 + // Stands in for gtk-update-icon-cache rewriting the cache on success.
404 + readonly onIconCacheRefresh?: Effect.Effect<void>;
405 +}
406 +
407 +const registerWithRealFileSystem = (run: ThemeIconRun) =>
408 + Effect.gen(function* () {
409 + const handler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
410 + yield* handler.register;
411 + }).pipe(
412 + Effect.provide(
413 + DesktopLinuxUrlHandler.layer.pipe(
414 + Layer.provide(
415 + Layer.mergeAll(
416 + Layer.effect(
417 + DesktopEnvironment.DesktopEnvironment,
418 + Path.Path.pipe(
419 + Effect.map((path) =>
420 + makeEnvironment(path, {
421 + linuxApplicationsDir: path.join(run.root, "applications"),
422 + }),
423 + ),
424 + ),
425 + ),
426 + Layer.effect(
427 + DesktopAssets.DesktopAssets,
428 + Path.Path.pipe(
429 + Effect.map((path) => ({
430 + iconPaths: Effect.succeed({
431 + png: Option.none(),
432 + ico: Option.none(),
433 + icns: Option.none(),
434 + }),
435 + resolveResourcePath: (fileName: string) =>
436 + Effect.succeed(
437 + fileName === "icons/256x256.png"
438 + ? Option.some(
439 + path.join(
440 + run.root,
441 + run.sourceMissing ? "missing" : "resources",
442 + fileName,
443 + ),
444 + )
445 + : Option.none(),
446 + ),
447 + })),
448 + ),
449 + ),
450 + Layer.succeed(
451 + ChildProcessSpawner.ChildProcessSpawner,
452 + ChildProcessSpawner.make((command) => {
453 + const childProcess = command as unknown as {
454 + readonly command: string;
455 + readonly args: ReadonlyArray<string>;
456 + };
457 + run.commands.push({ command: childProcess.command, args: childProcess.args });
458 + if (childProcess.command !== "gtk-update-icon-cache") {
459 + return Effect.succeed(mockProcess(0));
460 + }
461 + run.iconCacheOptions?.push(
462 + (command as unknown as { readonly options: Record<string, unknown> }).options,
463 + );
464 + if (run.iconCacheSpawnFails) {
465 + return Effect.fail(
466 + PlatformError.systemError({
467 + _tag: "NotFound",
468 + module: "ChildProcess",
469 + method: "spawn",
470 + description: "gtk-update-icon-cache is not installed",
471 + }),
472 + );
473 + }
474 + const exitCode = run.iconCacheExitCode ?? 0;
475 + const handle = mockProcess(exitCode, run.iconCacheStalled === true);
476 + const refreshed =
477 + exitCode === 0 && run.onIconCacheRefresh ? run.onIconCacheRefresh : Effect.void;
478 + return refreshed.pipe(
479 + Effect.andThen(
480 + run.iconCacheStarted
481 + ? Deferred.succeed(run.iconCacheStarted, undefined)
482 + : Effect.void,
483 + ),
484 + Effect.as(handle),
485 + );
486 + }),
487 + ),
488 + ),
489 + ),
490 + ),
491 + ),
492 + );
493 +
494 +const themeIconFixture = (source: Uint8Array) =>
495 + Effect.gen(function* () {
496 + const fileSystem = yield* FileSystem.FileSystem;
497 + const path = yield* Path.Path;
498 + const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-theme-icons-" });
499 + yield* fileSystem.makeDirectory(path.join(root, "resources", "icons"), { recursive: true });
500 + yield* fileSystem.writeFile(path.join(root, "resources", "icons", "256x256.png"), source);
501 + const hicolorDir = path.join(root, "icons", "hicolor");
502 + const appsDir = path.join(hicolorDir, "256x256", "apps");
503 + const cachePath = path.join(hicolorDir, "icon-theme.cache");
504 + // A fixed time in the past, in whole seconds as fs.utimes expects.
505 + const pastSeconds = 1_600_000_000;
506 + // An existing cache that predates any installed icon.
507 + const seedCache = Effect.gen(function* () {
508 + yield* fileSystem.makeDirectory(appsDir, { recursive: true });
509 + yield* fileSystem.writeFile(cachePath, new Uint8Array([0]));
510 + yield* fileSystem.utimes(cachePath, pastSeconds, pastSeconds);
511 + });
512 + const rewriteCache = fileSystem
513 + .writeFile(cachePath, new Uint8Array([1]))
514 + .pipe(Effect.orDie, Effect.provideService(FileSystem.FileSystem, fileSystem));
515 + return { root, hicolorDir, appsDir, cachePath, pastSeconds, seedCache, rewriteCache };
516 + });
517 +
518 +const iconCacheRuns = (run: ThemeIconRun) =>
519 + run.commands.filter(({ command }) => command === "gtk-update-icon-cache").length;
520 +
521 +it.layer(NodeServices.layer)("DesktopLinuxUrlHandler theme icons", (it) => {
522 + it.effect("installs both app id spellings without creating an icon cache", () =>
523 + Effect.gen(function* () {
524 + const fileSystem = yield* FileSystem.FileSystem;
525 + const path = yield* Path.Path;
526 + const icon = new Uint8Array([137, 80, 78, 71, 1, 2, 3]);
527 + const fixture = yield* themeIconFixture(icon);
528 + const run: ThemeIconRun = { root: fixture.root, commands: [] };
529 +
530 + yield* registerWithRealFileSystem(run);
531 + assert.deepEqual([...(yield* fileSystem.readDirectory(fixture.appsDir))].sort(), [
532 + "com.t3tools.T3Code.png",
533 + "com.t3tools.t3code.png",
534 + ]);
535 + for (const name of ["com.t3tools.t3code", "com.t3tools.T3Code"]) {
536 + const target = path.join(fixture.appsDir, `${name}.png`);
537 + assert.deepEqual([...(yield* fileSystem.readFile(target))], [...icon]);
538 + yield* fileSystem.utimes(target, fixture.pastSeconds, fixture.pastSeconds);
539 + }
540 +
541 + yield* registerWithRealFileSystem(run);
542 + for (const name of ["com.t3tools.t3code", "com.t3tools.T3Code"]) {
543 + const info = yield* fileSystem.stat(path.join(fixture.appsDir, `${name}.png`));
544 + assert.equal(Option.getOrThrow(info.mtime).getTime(), fixture.pastSeconds * 1000);
545 + }
546 + assert.isFalse(yield* fileSystem.exists(fixture.cachePath));
547 + assert.equal(iconCacheRuns(run), 0);
548 + }).pipe(Effect.scoped),
549 + );
550 +
551 + it.effect("replaces same-length stale icons and refreshes an existing cache once", () =>
552 + Effect.gen(function* () {
553 + const fileSystem = yield* FileSystem.FileSystem;
554 + const path = yield* Path.Path;
555 + const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
556 + yield* fixture.seedCache;
557 + const lower = path.join(fixture.appsDir, "com.t3tools.t3code.png");
558 + const upper = path.join(fixture.appsDir, "com.t3tools.T3Code.png");
559 + yield* fileSystem.writeFile(lower, new Uint8Array([3, 2, 1]));
560 + yield* fileSystem.writeFile(upper, new Uint8Array([1, 2, 3]));
561 + for (const target of [lower, upper]) {
562 + yield* fileSystem.utimes(target, fixture.pastSeconds, fixture.pastSeconds);
563 + }
564 + const run: ThemeIconRun = {
565 + root: fixture.root,
566 + commands: [],
567 + onIconCacheRefresh: fixture.rewriteCache,
568 + };
569 +
570 + yield* registerWithRealFileSystem(run);
571 + yield* registerWithRealFileSystem(run);
572 +
573 + assert.deepEqual([...(yield* fileSystem.readFile(lower))], [1, 2, 3]);
574 + const upperInfo = yield* fileSystem.stat(upper);
575 + assert.equal(Option.getOrThrow(upperInfo.mtime).getTime(), fixture.pastSeconds * 1000);
576 + assert.deepEqual(
577 + run.commands.filter(({ command }) => command === "gtk-update-icon-cache"),
578 + [{ command: "gtk-update-icon-cache", args: ["-f", "-t", fixture.hicolorDir] }],
579 + );
580 + }).pipe(Effect.scoped),
581 + );
582 +
583 + it.effect("refreshes when this launch changed an icon even if the cache looks newer", () =>
584 + Effect.gen(function* () {
585 + const fileSystem = yield* FileSystem.FileSystem;
586 + const path = yield* Path.Path;
587 + const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
588 + yield* fixture.seedCache;
589 + const run: ThemeIconRun = {
590 + root: fixture.root,
591 + commands: [],
592 + onIconCacheRefresh: fixture.rewriteCache,
593 + };
594 + // A cache stamped later than the new icons, as after a clock correction.
595 + const future = fixture.pastSeconds + 10 * 365 * 24 * 60 * 60;
596 + yield* fileSystem.utimes(fixture.cachePath, future, future);
597 +
598 + yield* registerWithRealFileSystem(run);
599 +
600 + assert.isTrue(yield* fileSystem.exists(path.join(fixture.appsDir, "com.t3tools.T3Code.png")));
601 + assert.equal(iconCacheRuns(run), 1);
602 + }).pipe(Effect.scoped),
603 + );
604 +
605 + it.effect("retries a failed icon cache refresh on the next launch", () =>
606 + Effect.gen(function* () {
607 + const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
608 + yield* fixture.seedCache;
609 + const failing: ThemeIconRun = { root: fixture.root, commands: [], iconCacheExitCode: 1 };
610 + yield* registerWithRealFileSystem(failing);
611 + assert.equal(iconCacheRuns(failing), 1);
612 + assert.deepEqual(failing.commands.map(({ command }) => command).slice(1), [
613 + "update-desktop-database",
614 + "xdg-mime",
615 + ]);
616 +
617 + const recovering: ThemeIconRun = {
618 + root: fixture.root,
619 + commands: [],
620 + onIconCacheRefresh: fixture.rewriteCache,
621 + };
622 + yield* registerWithRealFileSystem(recovering);
623 + yield* registerWithRealFileSystem(recovering);
624 + assert.equal(iconCacheRuns(recovering), 1);
625 + }).pipe(Effect.scoped),
626 + );
627 +
628 + it.effect("still registers the handler when the theme icon install fails", () =>
629 + Effect.gen(function* () {
630 + const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
631 + const missingSource: ThemeIconRun = { root: fixture.root, commands: [], sourceMissing: true };
632 + yield* registerWithRealFileSystem(missingSource);
633 + assert.deepEqual(
634 + missingSource.commands.map(({ command }) => command),
635 + ["update-desktop-database", "xdg-mime"],
636 + );
637 +
638 + yield* fixture.seedCache;
639 + const missingTool: ThemeIconRun = {
640 + root: fixture.root,
641 + commands: [],
642 + iconCacheSpawnFails: true,
643 + };
644 + yield* registerWithRealFileSystem(missingTool);
645 + assert.deepEqual(
646 + missingTool.commands.map(({ command }) => command),
647 + ["gtk-update-icon-cache", "update-desktop-database", "xdg-mime"],
648 + );
649 + }).pipe(Effect.scoped),
650 + );
651 +
652 + it.effect("continues to xdg-mime when the icon cache refresh stalls", () =>
653 + Effect.gen(function* () {
654 + const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
655 + yield* fixture.seedCache;
656 + const started = yield* Deferred.make<void>();
657 + const iconCacheOptions: Array<Record<string, unknown>> = [];
658 + const run: ThemeIconRun = {
659 + root: fixture.root,
660 + commands: [],
661 + iconCacheOptions,
662 + iconCacheStalled: true,
663 + iconCacheStarted: started,
664 + };
665 +
666 + const registration = yield* registerWithRealFileSystem(run).pipe(Effect.forkChild);
667 + yield* Deferred.await(started);
668 + yield* TestClock.adjust("5 seconds");
669 + yield* Fiber.join(registration);
670 +
671 + assert.deepEqual(
672 + run.commands.map(({ command }) => command),
673 + ["gtk-update-icon-cache", "update-desktop-database", "xdg-mime"],
674 + );
675 + // Without a forced kill, releasing a helper that ignores SIGTERM after
676 + // the timeout would still wait for it to exit.
677 + assert.equal(iconCacheOptions[0]?.forceKillAfter, "1 second");
678 + }).pipe(Effect.scoped),
679 + );
680 +});
apps/desktop/src/app/DesktopLinuxUrlHandler.ts
+74
@@ -97,6 +97,19 @@ export function renderUrlHandlerDesktopEntry(input: {
97 ].join("\n");
98 }
99
100 +// Some docks and bars look the Wayland app id up in the icon theme when no
101 +// visible desktop entry matches the window, for example an AppImage run without
102 +// integration. Theme lookups are case-sensitive, so install both spellings.
103 +const LINUX_THEME_ICON_SIZE = "256x256";
104 +
105 +function linuxThemeIconNames(desktopEntryName: string): readonly string[] {
106 + const appId = desktopEntryName.replace(/\.desktop$/u, "");
107 + return [...new Set([appId.toLowerCase(), appId])];
108 +}
109 +
110 +const sameBytes = (left: Uint8Array, right: Uint8Array): boolean =>
111 + left.length === right.length && left.every((byte, index) => byte === right[index]);
112 +
113 export class DesktopLinuxUrlHandler extends Context.Service<
114 DesktopLinuxUrlHandler,
115 {
@@ -118,6 +131,8 @@ export const make = Effect.gen(function* () {
131 );
132 const iconsDir = environment.path.join(environment.linuxApplicationsDir, "..", "icons");
133 const iconPath = environment.path.join(iconsDir, `${environment.linuxDesktopEntryName}.png`);
134 + const hicolorDir = environment.path.join(iconsDir, "hicolor");
135 + const themeIconDir = environment.path.join(hicolorDir, LINUX_THEME_ICON_SIZE, "apps");
136
137 const writeDesktopEntry = Effect.gen(function* () {
138 // Inside the mounted AppImage, process.execPath points at a transient
@@ -180,6 +195,63 @@ export const make = Effect.gen(function* () {
195 ),
196 );
197
198 + const refreshIconThemeCache = Effect.scoped(
199 + Effect.gen(function* () {
200 + const command = ChildProcess.make("gtk-update-icon-cache", ["-f", "-t", hicolorDir], {
201 + stdin: "ignore",
202 + stdout: "ignore",
203 + stderr: "ignore",
204 + // Releasing a timed-out helper otherwise waits on SIGTERM indefinitely.
205 + forceKillAfter: "1 second",
206 + });
207 + const handle = yield* spawner.spawn(command);
208 + return yield* handle.exitCode.pipe(Effect.timeout("5 seconds"));
209 + }),
210 + );
211 +
212 + const modifiedAt = (filePath: string) =>
213 + fileSystem
214 + .stat(filePath)
215 + .pipe(Effect.map((info) => Option.getOrUndefined(info.mtime)?.getTime() ?? 0));
216 +
217 + const installThemeIcons = Effect.gen(function* () {
218 + const source = yield* assets.resolveResourcePath(`icons/${LINUX_THEME_ICON_SIZE}.png`);
219 + if (Option.isNone(source)) return;
220 + const icon = yield* fileSystem.readFile(source.value);
221 + const targets = linuxThemeIconNames(environment.linuxDesktopEntryName).map((name) =>
222 + environment.path.join(themeIconDir, `${name}.png`),
223 + );
224 + let changed = false;
225 + for (const target of targets) {
226 + const current = yield* fileSystem.readFile(target).pipe(Effect.option);
227 + if (Option.isSome(current) && sameBytes(current.value, icon)) continue;
228 + yield* fileSystem.makeDirectory(themeIconDir, { recursive: true });
229 + yield* fileSystem.writeFile(target, icon);
230 + changed = true;
231 + }
232 + // Only keep an existing cache current. Creating one would leave GTK reading
233 + // a cache that other tools installing user icons do not update. An icon
234 + // newer than the cache also retries a refresh that failed on an earlier
235 + // launch.
236 + const cachePath = environment.path.join(hicolorDir, "icon-theme.cache");
237 + const cachedAt = yield* modifiedAt(cachePath).pipe(Effect.option);
238 + if (Option.isNone(cachedAt)) return;
239 + let stale = changed;
240 + for (const target of targets) {
241 + if (stale) break;
242 + if ((yield* modifiedAt(target)) > cachedAt.value) stale = true;
243 + }
244 + if (!stale) return;
245 + const exitCode = yield* refreshIconThemeCache;
246 + if (exitCode !== 0) {
247 + yield* logWarning("icon theme cache refresh failed", { hicolorDir, exitCode });
248 + }
249 + }).pipe(
250 + Effect.catch((error) =>
251 + logWarning("app id theme icon install failed", { themeIconDir, category: error._tag }),
252 + ),
253 + );
254 +
255 const setDefaultHandler = Effect.scoped(
256 Effect.gen(function* () {
257 const command = ChildProcess.make(
@@ -232,6 +304,8 @@ export const make = Effect.gen(function* () {
304 ),
305 );
306
307 + yield* installThemeIcons;
308 +
309 yield* updateDesktopDatabase.pipe(
310 // Some MIME implementations, including GIO, use mimeinfo.cache to verify
311 // that a desktop entry is associated with a scheme. Cache refresh is