Skip to content

Commit 6a3e4da

Browse files
author
Tiisetso
committed
fix(desktop): size macOS tray icon for the menu bar
1 parent 7819351 commit 6a3e4da

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect, test } from "bun:test"
2+
import { dirname, join } from "node:path"
3+
import { fileURLToPath } from "node:url"
4+
import { runInNewContext } from "node:vm"
5+
6+
const source = await Bun.file(new URL("./tray.ts", import.meta.url)).text()
7+
const compiled = new Bun.Transpiler({ loader: "ts" }).transformSync(
8+
source
9+
.replace(/^import .*$/gm, "")
10+
.replace("import.meta.url", JSON.stringify(new URL("./tray.ts", import.meta.url).href))
11+
.replace(/^export /gm, ""),
12+
)
13+
14+
for (const platform of ["darwin", "win32", "linux"]) {
15+
for (const packaged of [false, true]) {
16+
test(`tray image on ${platform}, packaged=${packaged}`, () => {
17+
const loaded: string[] = []
18+
const resized: object[] = []
19+
const image = {
20+
resize(options: object) {
21+
resized.push(options)
22+
return small
23+
},
24+
}
25+
const small = {}
26+
const received: unknown[] = []
27+
runInNewContext(`${compiled}\ncreateTray(() => {})`, {
28+
dirname,
29+
join,
30+
fileURLToPath,
31+
process: { platform, resourcesPath: "/packaged/resources" },
32+
app: { isPackaged: packaged, getName: () => "CookieMonster" },
33+
Menu: { buildFromTemplate: () => [] },
34+
nativeImage: {
35+
createFromPath(path: string) {
36+
loaded.push(path)
37+
return image
38+
},
39+
},
40+
Tray: class {
41+
constructor(value: unknown) {
42+
received.push(value)
43+
}
44+
setToolTip() {}
45+
setContextMenu() {}
46+
on() {}
47+
},
48+
})
49+
const directory = packaged
50+
? "/packaged/resources/icons"
51+
: join(dirname(fileURLToPath(new URL("./tray.ts", import.meta.url))), "../../resources/icons")
52+
expect(loaded).toEqual([
53+
join(directory, platform === "darwin" ? "32x32.png" : platform === "win32" ? "icon.ico" : "icon.png"),
54+
])
55+
expect(resized).toEqual(platform === "darwin" ? [{ width: 16, height: 16 }] : [])
56+
expect(received).toEqual([platform === "darwin" ? small : image])
57+
})
58+
}
59+
}
60+
61+
test("every build channel includes the small tray asset", async () => {
62+
for (const channel of ["dev", "beta", "prod"]) {
63+
const bytes = await Bun.file(new URL(`../../icons/${channel}/32x32.png`, import.meta.url)).bytes()
64+
expect([...bytes.subarray(0, 8)]).toEqual([137, 80, 78, 71, 13, 10, 26, 10])
65+
const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
66+
expect(header.getUint32(16)).toBe(32)
67+
expect(header.getUint32(20)).toBe(32)
68+
}
69+
})

‎packages/desktop/src/main/tray.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function iconsDir() {
1111
}
1212

1313
function trayImage() {
14+
if (process.platform === "darwin") {
15+
// Keep the colored icon at menu-bar logical size, not its source pixel size.
16+
return nativeImage.createFromPath(join(iconsDir(), "32x32.png")).resize({ width: 16, height: 16 })
17+
}
1418
const ext = process.platform === "win32" ? "ico" : "png"
1519
return nativeImage.createFromPath(join(iconsDir(), `icon.${ext}`))
1620
}

0 commit comments

Comments
 (0)