|
| 1 | +// Fork: gives the unpackaged dev app an icon on Linux desktops. |
| 2 | +// |
| 3 | +// BrowserWindow's `icon` reaches the shell through X11's _NET_WM_ICON, which |
| 4 | +// Wayland has no equivalent for (xdg_toplevel_icon_v1 is new and Chromium does |
| 5 | +// not speak it). Wayland shells instead resolve a window's icon by matching its |
| 6 | +// app_id to an installed .desktop file, so on GNOME/Wayland an unpackaged run |
| 7 | +// falls back to the generic executable icon — while macOS (app.dock.setIcon) |
| 8 | +// and Windows (the HWND icon) are unaffected. |
| 9 | +// |
| 10 | +// Packaged builds ship that .desktop file via electron-builder; this writes the |
| 11 | +// dev equivalent into the user's applications dir. Idempotent, so predev can |
| 12 | +// run it on every launch. |
| 13 | +import { mkdir } from "node:fs/promises" |
| 14 | +import { homedir } from "node:os" |
| 15 | +import { join, resolve } from "node:path" |
| 16 | +import { resolveChannel } from "./utils" |
| 17 | + |
| 18 | +const CHANNEL_NAMES = { dev: "CoDev Code Dev", beta: "CoDev Code Beta", prod: "CoDev Code" } as const |
| 19 | + |
| 20 | +// Electron announces a slug of the app's package.json productName (falling back |
| 21 | +// to name) as the Wayland app_id and the X11 WM_CLASS: lowercased, with every |
| 22 | +// run of non-alphanumerics collapsed to a dash. Neither app.setName() nor |
| 23 | +// --class nor CHROME_DESKTOP overrides it — it is read before main runs — so |
| 24 | +// the entry has to be named after whatever the manifest yields. |
| 25 | +export function electronAppId(manifest: { productName?: string; name?: string }) { |
| 26 | + const source = manifest.productName ?? manifest.name ?? "" |
| 27 | + const slug = source |
| 28 | + .toLowerCase() |
| 29 | + .replace(/[^a-z0-9]+/g, "-") |
| 30 | + .replace(/^-+|-+$/g, "") |
| 31 | + if (!slug.length) throw new Error("could not derive an app id from the desktop package manifest") |
| 32 | + return slug |
| 33 | +} |
| 34 | + |
| 35 | +// import.meta.main keeps importing this module (the test does) side-effect free. |
| 36 | +if (import.meta.main && process.platform === "linux") { |
| 37 | + const packageDir = resolve(import.meta.dir, "..") |
| 38 | + const appId = electronAppId(await Bun.file(join(packageDir, "package.json")).json()) |
| 39 | + const applications = join(process.env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"), "applications") |
| 40 | + await mkdir(applications, { recursive: true }) |
| 41 | + const entry = join(applications, `${appId}.desktop`) |
| 42 | + // NoDisplay keeps it out of the launcher — it exists only so the shell can |
| 43 | + // associate a running dev window with a name and an icon. StartupWMClass |
| 44 | + // covers the X11 side, where the match is on WM_CLASS rather than the |
| 45 | + // filename. The icon is the channel copy predev already staged, so this |
| 46 | + // tracks whichever channel was last built. |
| 47 | + await Bun.write( |
| 48 | + entry, |
| 49 | + `[Desktop Entry] |
| 50 | +Type=Application |
| 51 | +Name=${CHANNEL_NAMES[resolveChannel()]} |
| 52 | +Comment=Open source AI coding agent (development build) |
| 53 | +Icon=${join(packageDir, "resources", "icons", "icon.png")} |
| 54 | +Exec=bun --cwd ${packageDir} dev |
| 55 | +StartupWMClass=${appId} |
| 56 | +NoDisplay=true |
| 57 | +Categories=Development; |
| 58 | +`, |
| 59 | + ) |
| 60 | + console.log(`Installed dev desktop entry for app id ${appId} at ${entry}`) |
| 61 | +} |
0 commit comments