Skip to content

Edge-Drop is a clipboard shelf on the edge of your screen. Hover to open it. Drag files and images into any app, click to copy text and links. Free and open-source for Windows.

feat(tray): adapt system tray icon for Windows Light Theme (#64)

Deepender2556abf3f33b53
diff --git a/electron/main/tray.ts b/electron/main/tray.ts
index 5d3742b..b9b7960 100644
--- a/electron/main/tray.ts
+++ b/electron/main/tray.ts
@@ -6,8 +6,9 @@
* state (checkmarks) is rebuilt every time the menu opens so it always reflects
* current settings.
*/
-import { Menu, Tray, app, nativeImage, Notification, screen } from 'electron'
+import { Menu, Tray, app, nativeImage, Notification, screen, nativeTheme } from 'electron'
import { existsSync } from 'node:fs'
+import { execFileSync } from 'node:child_process'
import { PATHS } from '../store/paths'
import { loadSettings, saveSettings } from '../store/settings'
import { getMainWindow, setVisible, repositionWindow, getDisplayListOptions, registerWindowRepositionListener, popUpAndRetract } from './window'
@@ -16,6 +17,7 @@ import { pushState } from './state'
import { TRANSLATIONS, en } from '../../src/i18n/translations'
let tray: Tray | null = null
+let themeListenerRegistered = false
/** Build a tiny monochrome tray icon if no on-disk icon exists (first run). */
function fallbackIcon(): Electron.NativeImage {
@@ -29,22 +31,83 @@ function fallbackIcon(): Electron.NativeImage {
return nativeImage.createFromBuffer(png).resize({ width: 16, height: 16 })
}
-export function createTray(): Tray {
- const iconPath = PATHS.trayIcon()
- let image: Electron.NativeImage
+/**
+ * Detects whether the Windows taskbar/system tray is using light mode.
+ *
+ * Windows 10/11 allows a "Custom" theme where the taskbar (Windows mode)
+ * can be Light while apps are Dark (or vice versa).
+ * The authoritative source is the registry key `SystemUsesLightTheme`:
+ * 1 = Light taskbar (requires dark tray icon)
+ * 0 = Dark taskbar (requires white tray icon)
+ *
+ * Falls back to `!nativeTheme.shouldUseDarkColors` on non-Windows or when unreadable.
+ */
+export function isTaskbarLightTheme(): boolean {
+ if (process.platform === 'win32') {
+ try {
+ const out = execFileSync(
+ 'reg',
+ ['query', 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize', '/v', 'SystemUsesLightTheme'],
+ { windowsHide: true, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }
+ )
+ const text = String(out ?? '')
+ const m = text.match(/SystemUsesLightTheme\s+REG_DWORD\s+(0x[0-9a-fA-F]+|\d+)/i)
+ if (m && m[1]) {
+ const val = parseInt(m[1], 16)
+ return val === 1
+ }
+ } catch {
+ // Best-effort registry check; fall back to nativeTheme below.
+ }
+ }
+ return !nativeTheme.shouldUseDarkColors
+}
+
+/** Resolves the appropriate 32x32 tray icon based on the current taskbar theme. */
+export function getTrayImage(): Electron.NativeImage {
+ const isLight = isTaskbarLightTheme()
+ const preferredPath = isLight ? PATHS.trayDarkIcon() : PATHS.trayIcon()
+ const fallbackPath = PATHS.trayIcon()
+
+ const resolvedPath = existsSync(preferredPath)
+ ? preferredPath
+ : (existsSync(fallbackPath) ? fallbackPath : null)
- if (existsSync(iconPath)) {
+ if (resolvedPath) {
// Load and resize to exactly 32x32. Windows system tray renders icons at 16x16
// logical pixels but uses 32x32 physical pixels on 2x DPI displays.
// Using a single 32x32 image with no scaleFactor trickery is the most reliable
// approach — the OS scales it automatically.
- image = nativeImage.createFromPath(iconPath).resize({ width: 32, height: 32, quality: 'best' })
- } else {
- image = fallbackIcon()
+ return nativeImage.createFromPath(resolvedPath).resize({ width: 32, height: 32, quality: 'best' })
}
+
+ return fallbackIcon()
+}
+
+/** Dynamically switches tray icon image when system/taskbar theme updates. */
+export function updateTrayIcon(): void {
+ if (!tray || tray.isDestroyed()) return
+ try {
+ const image = getTrayImage()
+ tray.setImage(image)
+ } catch (err) {
+ console.error('[Tray] Failed to update tray icon image:', err)
+ }
+}
+
+export function createTray(): Tray {
+ const image = getTrayImage()
tray = new Tray(image)
tray.setToolTip('Edge-Drop')
+ // Register system theme change listener once
+ if (!themeListenerRegistered) {
+ themeListenerRegistered = true
+ nativeTheme.on('updated', () => {
+ updateTrayIcon()
+ })
+ }
+
// Show welcome notification on first run
if (!existsSync(PATHS.indexFile())) {
try {
@@ -190,6 +253,7 @@ function getTrayText(settingsLang: string | undefined, key: keyof typeof en['tra
}
tray.on('click', () => {
+ updateTrayIcon()
console.log('[Main] Tray icon left-clicked')
const win = getMainWindow()
if (!win) return
@@ -199,6 +263,7 @@ function getTrayText(settingsLang: string | undefined, key: keyof typeof en['tra
// Rebuild menu dynamically right before showing to ensure displays & checkmarks are 100% current.
tray.on('right-click', () => {
+ updateTrayIcon()
rebuild()
tray?.popUpContextMenu()
})
diff --git a/electron/store/paths.ts b/electron/store/paths.ts
index d09c90b..6757e4a 100644
--- a/electron/store/paths.ts
+++ b/electron/store/paths.ts
@@ -35,8 +35,10 @@ export const PATHS = {
tempDir: () => join(root(), 'temp'),
/** App icon (used by window + native drag image). */
icon: () => join(app.getAppPath(), 'resources', 'icon.png'),
- /** Tray icon (pure white logo without background). */
- trayIcon: () => join(app.getAppPath(), 'resources', 'tray.png')
+ /** Tray icon (pure white logo without background for dark taskbar). */
+ trayIcon: () => join(app.getAppPath(), 'resources', 'tray.png'),
+ /** Tray icon (dark charcoal logo without background for light taskbar). */
+ trayDarkIcon: () => join(app.getAppPath(), 'resources', 'tray-dark.png')
} as const
/**
diff --git a/resources/tray-dark.png b/resources/tray-dark.png
new file mode 100644
index 0000000..f712e72
Binary files /dev/null and b/resources/tray-dark.png differ
diff --git a/scratch/generate_new_icons.js b/scratch/generate_new_icons.js
index 47e410a..200fdd4 100644
--- a/scratch/generate_new_icons.js
+++ b/scratch/generate_new_icons.js
@@ -54,7 +54,7 @@ dirEntry.writeUInt32LE(22, 12);
const icoBuffer = Buffer.concat([header, dirEntry, appPngBuffer]);
writeFileSync('resources/icon.ico', icoBuffer);
-// 2. Tray Icon — pure white logo, no background, fills the 256x256 canvas
+// 2. Tray Icon (White for dark taskbar) — pure white logo, no background, fills the 256x256 canvas
// Removed internal padding to make it as large as possible in the system tray
const trayIconSvg = `<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg">
<svg x="0" y="0" width="256" height="256" viewBox="378 387 1072 1072" preserveAspectRatio="xMidYMid meet">
@@ -67,4 +67,18 @@ const trayResvg = new Resvg(trayIconSvg, { fitTo: { mode: 'width', value: 256 }
const trayPngBuffer = trayResvg.render().asPng();
writeFileSync('resources/tray.png', trayPngBuffer);
+// 3. Tray Icon (Dark for light taskbar) — dark charcoal (#1A1A1A) logo for Windows Light Theme
+const darkLogo = rawLogo.replace(/fill="#FFFFFF"/gi, 'fill="#1A1A1A"');
+const trayDarkIconSvg = `<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg">
+ <svg x="0" y="0" width="256" height="256" viewBox="378 387 1072 1072" preserveAspectRatio="xMidYMid meet">
+ ${darkLogo.replace(/<svg[^>]*>/, '').replace('</svg>', '')}
+ </svg>
+</svg>`;
+
+console.log('Rendering Tray Icon (dark charcoal for light theme)...');
+const trayDarkResvg = new Resvg(trayDarkIconSvg, { fitTo: { mode: 'width', value: 256 } });
+const trayDarkPngBuffer = trayDarkResvg.render().asPng();
+writeFileSync('resources/tray-dark.png', trayDarkPngBuffer);
+
console.log('Icons successfully generated!');
+
diff --git a/tests/trayTheme.test.ts b/tests/trayTheme.test.ts
new file mode 100644
index 0000000..242c5f0
--- /dev/null
+++ b/tests/trayTheme.test.ts
@@ -0,0 +1,143 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { existsSync, readFileSync } from 'node:fs'
+
+const mocks = vi.hoisted(() => ({
+ execFileSync: vi.fn(),
+ shouldUseDarkColors: true
+}))
+
+vi.mock('node:child_process', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('node:child_process')>()
+ return {
+ ...actual,
+ execFileSync: (...args: unknown[]) => mocks.execFileSync(...args)
+ }
+})
+
+// Mock electron modules
+vi.mock('electron', () => {
+ const mockNativeImage = {
+ resize: vi.fn().mockReturnThis(),
+ toPNG: vi.fn().mockReturnValue(Buffer.from('mock-png'))
+ }
+ return {
+ app: {
+ getAppPath: vi.fn().mockReturnValue(process.cwd()),
+ getPreferredSystemLanguages: vi.fn().mockReturnValue(['en-US'])
+ },
+ nativeTheme: {
+ get shouldUseDarkColors() {
+ return mocks.shouldUseDarkColors
+ },
+ set shouldUseDarkColors(v: boolean) {
+ mocks.shouldUseDarkColors = v
+ },
+ on: vi.fn()
+ },
+ nativeImage: {
+ createFromPath: vi.fn().mockReturnValue(mockNativeImage),
+ createFromBuffer: vi.fn().mockReturnValue(mockNativeImage)
+ },
+ Tray: vi.fn().mockImplementation(() => ({
+ setImage: vi.fn(),
+ setToolTip: vi.fn(),
+ setContextMenu: vi.fn(),
+ on: vi.fn(),
+ isDestroyed: vi.fn().mockReturnValue(false)
+ })),
+ Menu: {
+ buildFromTemplate: vi.fn().mockReturnValue({})
+ },
+ Notification: {
+ isSupported: vi.fn().mockReturnValue(false)
+ },
+ screen: {
+ on: vi.fn()
+ }
+ }
+})
+
+import { PATHS } from '../electron/store/paths'
+import { isTaskbarLightTheme, getTrayImage } from '../electron/main/tray'
+import { nativeTheme } from 'electron'
+
+describe('Tray Icon Theme Adaptation (Issue #64)', () => {
+ beforeEach(() => {
+ vi.restoreAllMocks()
+ })
+
+ it('defines both PATHS.trayIcon and PATHS.trayDarkIcon', () => {
+ const whitePath = PATHS.trayIcon()
+ const darkPath = PATHS.trayDarkIcon()
+
+ expect(whitePath).toContain('tray.png')
+ expect(darkPath).toContain('tray-dark.png')
+ })
+
+ it('ensures both tray.png and tray-dark.png exist and are valid PNG files', () => {
+ const whitePath = PATHS.trayIcon()
+ const darkPath = PATHS.trayDarkIcon()
+
+ expect(existsSync(whitePath)).toBe(true)
+ expect(existsSync(darkPath)).toBe(true)
+
+ // PNG magic bytes: 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
+ const pngMagic = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+
+ const whiteBuf = readFileSync(whitePath)
+ const darkBuf = readFileSync(darkPath)
+
+ expect(whiteBuf.subarray(0, 8)).toEqual(pngMagic)
+ expect(darkBuf.subarray(0, 8)).toEqual(pngMagic)
+ })
+
+ it('detects Light taskbar when SystemUsesLightTheme is 0x1', () => {
+ mocks.execFileSync.mockReturnValue(
+ 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\r\n SystemUsesLightTheme REG_DWORD 0x1\r\n'
+ )
+
+ const isLight = isTaskbarLightTheme()
+ expect(isLight).toBe(true)
+ })
+
+ it('detects Dark taskbar when SystemUsesLightTheme is 0x0', () => {
+ mocks.execFileSync.mockReturnValue(
+ 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\r\n SystemUsesLightTheme REG_DWORD 0x0\r\n'
+ )
+
+ const isLight = isTaskbarLightTheme()
+ expect(isLight).toBe(false)
+ })
+
+ it('falls back to !nativeTheme.shouldUseDarkColors when registry query fails', () => {
+ mocks.execFileSync.mockImplementation(() => {
+ throw new Error('Registry query failed')
+ })
+
+ // In dark mode (shouldUseDarkColors = true), isTaskbarLightTheme should be false
+ nativeTheme.shouldUseDarkColors = true
+ expect(isTaskbarLightTheme()).toBe(false)
+
+ // In light mode (shouldUseDarkColors = false), isTaskbarLightTheme should be true
+ nativeTheme.shouldUseDarkColors = false
+ expect(isTaskbarLightTheme()).toBe(true)
+ })
+
+ it('loads the dark tray icon when taskbar is in light mode', () => {
+ mocks.execFileSync.mockReturnValue(
+ 'SystemUsesLightTheme REG_DWORD 0x1'
+ )
+
+ const image = getTrayImage()
+ expect(image).toBeDefined()
+ })
+
+ it('loads the white tray icon when taskbar is in dark mode', () => {
+ mocks.execFileSync.mockReturnValue(
+ 'SystemUsesLightTheme REG_DWORD 0x0'
+ )
+
+ const image = getTrayImage()
+ expect(image).toBeDefined()
+ })
+})