diff --git a/src/main/appBadge.ts b/src/main/appBadge.ts index 4b32fe9..791bf57 100644 --- a/src/main/appBadge.ts +++ b/src/main/appBadge.ts @@ -12,11 +12,11 @@ import { BADGE_DIR } from "shared/paths"; import { mainWin } from "./mainWindow"; const imgCache = new Map(); -export function loadBadge(index: number) { +function loadBadge(index: number) { const cached = imgCache.get(index); if (cached) return cached; - const img = nativeImage.createFromPath(join(BADGE_DIR, `${index}.png`)); + const img = nativeImage.createFromPath(join(BADGE_DIR, "ico", `${index}.ico`)); imgCache.set(index, img); return img; diff --git a/src/main/tray.ts b/src/main/tray.ts index cdec95d..0b2152e 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -5,13 +5,13 @@ */ import { dialog, NativeImage, nativeImage } from "electron"; -import { mkdirSync, writeFileSync } from "fs"; +import { mkdirSync, readFileSync, writeFileSync } from "fs"; import { readFile } from "fs/promises"; import { join } from "path"; import { IpcEvents } from "shared/IpcEvents"; -import { ICONS_DIR, STATIC_DIR } from "shared/paths"; +import { BADGE_DIR, ICONS_DIR, STATIC_DIR } from "shared/paths"; -import { lastBadgeCount, loadBadge } from "./appBadge"; +import { lastBadgeCount } from "./appBadge"; import { mainWin, tray } from "./mainWindow"; import { Settings } from "./settings"; @@ -44,9 +44,10 @@ export async function setTrayIcon(iconName: string) { trayImage = nativeImage.createFromPath(join(ICONS_DIR, "icon.png")); } - const badgeImg = loadBadge(lastBadgeCount); + const badge = lastBadgeCount > 9 ? 10 : lastBadgeCount; + const badgeSvg = readFileSync(join(BADGE_DIR, "svg", `${badge}.svg`), "utf8"); // and send IPC call to renderer to add to image - mainWin.webContents.send(IpcEvents.ADD_BADGE_TO_ICON, trayImage.toDataURL(), badgeImg.toDataURL()); + mainWin.webContents.send(IpcEvents.ADD_BADGE_TO_ICON, trayImage.toDataURL(), badgeSvg); return; } diff --git a/src/renderer/patches/tray.ts b/src/renderer/patches/tray.ts index 221264f..29a30f1 100644 --- a/src/renderer/patches/tray.ts +++ b/src/renderer/patches/tray.ts @@ -27,17 +27,35 @@ export function setCurrentTrayIcon() { } } -VesktopNative.tray.createIconRequest(async (iconName: string) => { - const pickedColor = VesktopNative.settings.get().trayColor; - const fillColor = VesktopNative.settings.get().trayAutoFill ?? "auto"; +function backgroundTooBright(color: string) { + // calculate relative luminance + const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color); + if (!result) { + return false; + } + const normalizedR = parseInt(result[1], 16) / 255; + const normalizedG = parseInt(result[2], 16) / 255; + const normalizedB = parseInt(result[3], 16) / 255; + const lumR = normalizedR <= 0.03928 ? normalizedR / 12.92 : Math.pow((normalizedR + 0.055) / 1.055, 2.4); + const lumG = normalizedG <= 0.03928 ? normalizedG / 12.92 : Math.pow((normalizedG + 0.055) / 1.055, 2.4); + const lumB = normalizedB <= 0.03928 ? normalizedB / 12.92 : Math.pow((normalizedB + 0.055) / 1.055, 2.4); + return 0.2126 * lumR + 0.7152 * lumG + 0.0722 * lumB > 0.5; +} + +function changeColorsInSvg(svg: string, stockColor: string, isBadge: boolean = false) { + const pickedColor = VesktopNative.settings.get().trayColor; + const reg = new RegExp(stockColor, "gim"); + svg = svg.replace(reg, "#" + (pickedColor ?? stockColor)); + if (backgroundTooBright(pickedColor ?? stockColor)) svg = svg.replace(/white/gim, "black"); + + return svg; +} + +VesktopNative.tray.createIconRequest(async (iconName: string) => { try { var svg = await VesktopNative.tray.getIcon(iconName); - svg = svg.replace(/#f6bfac/gim, "#" + (pickedColor ?? "3DB77F")); - if (fillColor !== "auto") { - svg = svg.replace(/black/gim, fillColor); - svg = svg.replace(/white/gim, fillColor); - } + svg = changeColorsInSvg(svg, "#f6bfac"); const canvas = document.createElement("canvas"); canvas.width = 128; canvas.height = 128; @@ -58,7 +76,12 @@ VesktopNative.tray.createIconRequest(async (iconName: string) => { } }); -VesktopNative.tray.addBadgeToIcon(async (iconDataURL: string, badgeDataURL: string) => { +VesktopNative.tray.addBadgeToIcon(async (iconDataURL: string, badgeDataSVG: string) => { + const pickedColor = VesktopNative.settings.get().trayColor; + const fillColor = VesktopNative.settings.get().trayAutoFill ?? "white"; + badgeDataSVG = changeColorsInSvg(badgeDataSVG, "#F35959"); + if (fillColor !== "auto") badgeDataSVG = badgeDataSVG.replace(/white/gim, fillColor); + const canvas = document.createElement("canvas"); canvas.width = 128; canvas.height = 128; @@ -81,7 +104,7 @@ VesktopNative.tray.addBadgeToIcon(async (iconDataURL: string, badgeDataURL: stri VesktopNative.tray.returnIconWithBadge(canvas.toDataURL()); }; - iconImg.src = badgeDataURL; + iconImg.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(badgeDataSVG)}`; } }; diff --git a/static/badges/1.png b/static/badges/1.png deleted file mode 100644 index da9c163..0000000 Binary files a/static/badges/1.png and /dev/null differ diff --git a/static/badges/10.png b/static/badges/10.png deleted file mode 100644 index 16bb2da..0000000 Binary files a/static/badges/10.png and /dev/null differ diff --git a/static/badges/11.png b/static/badges/11.png deleted file mode 100644 index 789bb2d..0000000 Binary files a/static/badges/11.png and /dev/null differ diff --git a/static/badges/2.png b/static/badges/2.png deleted file mode 100644 index 5958198..0000000 Binary files a/static/badges/2.png and /dev/null differ diff --git a/static/badges/3.png b/static/badges/3.png deleted file mode 100644 index bda3265..0000000 Binary files a/static/badges/3.png and /dev/null differ diff --git a/static/badges/4.png b/static/badges/4.png deleted file mode 100644 index 89c71e0..0000000 Binary files a/static/badges/4.png and /dev/null differ diff --git a/static/badges/5.png b/static/badges/5.png deleted file mode 100644 index b75b3d5..0000000 Binary files a/static/badges/5.png and /dev/null differ diff --git a/static/badges/6.png b/static/badges/6.png deleted file mode 100644 index a146e5b..0000000 Binary files a/static/badges/6.png and /dev/null differ diff --git a/static/badges/7.png b/static/badges/7.png deleted file mode 100644 index 8b2cf06..0000000 Binary files a/static/badges/7.png and /dev/null differ diff --git a/static/badges/8.png b/static/badges/8.png deleted file mode 100644 index 463f2d1..0000000 Binary files a/static/badges/8.png and /dev/null differ diff --git a/static/badges/9.png b/static/badges/9.png deleted file mode 100644 index 2025b41..0000000 Binary files a/static/badges/9.png and /dev/null differ diff --git a/static/badges/ico/1.ico b/static/badges/ico/1.ico new file mode 100644 index 0000000..0e2003f Binary files /dev/null and b/static/badges/ico/1.ico differ diff --git a/static/badges/ico/10.ico b/static/badges/ico/10.ico new file mode 100644 index 0000000..1e02de3 Binary files /dev/null and b/static/badges/ico/10.ico differ diff --git a/static/badges/ico/11.ico b/static/badges/ico/11.ico new file mode 100644 index 0000000..7ebd2ed Binary files /dev/null and b/static/badges/ico/11.ico differ diff --git a/static/badges/ico/2.ico b/static/badges/ico/2.ico new file mode 100644 index 0000000..d7b6f44 Binary files /dev/null and b/static/badges/ico/2.ico differ diff --git a/static/badges/ico/3.ico b/static/badges/ico/3.ico new file mode 100644 index 0000000..43ce7cf Binary files /dev/null and b/static/badges/ico/3.ico differ diff --git a/static/badges/ico/4.ico b/static/badges/ico/4.ico new file mode 100644 index 0000000..91bd61f Binary files /dev/null and b/static/badges/ico/4.ico differ diff --git a/static/badges/ico/5.ico b/static/badges/ico/5.ico new file mode 100644 index 0000000..1d6bf8f Binary files /dev/null and b/static/badges/ico/5.ico differ diff --git a/static/badges/ico/6.ico b/static/badges/ico/6.ico new file mode 100644 index 0000000..d0c0cd6 Binary files /dev/null and b/static/badges/ico/6.ico differ diff --git a/static/badges/ico/7.ico b/static/badges/ico/7.ico new file mode 100644 index 0000000..b50750c Binary files /dev/null and b/static/badges/ico/7.ico differ diff --git a/static/badges/ico/8.ico b/static/badges/ico/8.ico new file mode 100644 index 0000000..8d0ca36 Binary files /dev/null and b/static/badges/ico/8.ico differ diff --git a/static/badges/ico/9.ico b/static/badges/ico/9.ico new file mode 100644 index 0000000..c153779 Binary files /dev/null and b/static/badges/ico/9.ico differ diff --git a/static/badges/svg/1.svg b/static/badges/svg/1.svg new file mode 100755 index 0000000..85007c8 --- /dev/null +++ b/static/badges/svg/1.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/10.svg b/static/badges/svg/10.svg new file mode 100755 index 0000000..695e197 --- /dev/null +++ b/static/badges/svg/10.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/2.svg b/static/badges/svg/2.svg new file mode 100755 index 0000000..8bd92cc --- /dev/null +++ b/static/badges/svg/2.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/3.svg b/static/badges/svg/3.svg new file mode 100755 index 0000000..d40165f --- /dev/null +++ b/static/badges/svg/3.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/4.svg b/static/badges/svg/4.svg new file mode 100755 index 0000000..5df4605 --- /dev/null +++ b/static/badges/svg/4.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/5.svg b/static/badges/svg/5.svg new file mode 100755 index 0000000..51c1eeb --- /dev/null +++ b/static/badges/svg/5.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/6.svg b/static/badges/svg/6.svg new file mode 100755 index 0000000..f6cf9d7 --- /dev/null +++ b/static/badges/svg/6.svg @@ -0,0 +1,6 @@ + + + + + 6 + diff --git a/static/badges/svg/7.svg b/static/badges/svg/7.svg new file mode 100755 index 0000000..89695fd --- /dev/null +++ b/static/badges/svg/7.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/8.svg b/static/badges/svg/8.svg new file mode 100755 index 0000000..a5a9903 --- /dev/null +++ b/static/badges/svg/8.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/static/badges/svg/9.svg b/static/badges/svg/9.svg new file mode 100755 index 0000000..3cfe0e0 --- /dev/null +++ b/static/badges/svg/9.svg @@ -0,0 +1,5 @@ + + + + +