A full-featured download manager.
fix(tray): restore window toggle on left click (#1885)
Taha Zarife733394ec6a1
diff --git a/src/main/index.ts b/src/main/index.tsindex c4072111..d6129d4a 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -2241,6 +2241,7 @@ async function initializeMainProcess(): Promise<void> {menuManager: activeMenuManager,protocolManager,extraResourceDir: platform.extraResourceDir,+ toggleMainWindow: () => windowManager.toggle('main'),})launcher.flushDeferred()diff --git a/src/main/platform/tray.test.ts b/src/main/platform/tray.test.tsindex 051d02dd..21161038 100644--- a/src/main/platform/tray.test.ts+++ b/src/main/platform/tray.test.ts@@ -71,6 +71,8 @@ import { RunMode } from '@shared/constants'import { setupTray, type TrayDeps } from './tray'const originalPlatform = process.platform+const trayMenu = { kind: 'tray-menu' }+const toggleMainWindow = vi.fn()function createDeps(): TrayDeps {return {@@ -85,7 +87,7 @@ function createDeps(): TrayDeps {}),},menuManager: {- getTrayMenu: () => null,+ getTrayMenu: () => trayMenu,onTrayRebuilt: vi.fn(),},protocolManager: {@@ -93,9 +95,19 @@ function createDeps(): TrayDeps {handleTorrentFile: vi.fn(),},extraResourceDir: path.join(process.cwd(), 'extra'),+ toggleMainWindow,} as unknown as TrayDeps}+function getTrayHandler(eventName: string): () => void {+ const handler = trayInstance.on.mock.calls.find(+ ([event]) => event === eventName+ )?.[1]++ expect(handler).toBeTypeOf('function')+ return handler as () => void+}+describe('setupTray', () => {beforeEach(() => {vi.clearAllMocks()@@ -118,6 +130,7 @@ describe('setupTray', () => {'493f17b6-d4ac-48d3-8723-c3ac490b14cf')})+handle.destroy()})@@ -129,6 +142,78 @@ describe('setupTray', () => {await vi.waitFor(() => {expect(trayConstructor).toHaveBeenCalledWith(icon)})++ handle.destroy()+ })++ it('toggles the main window without opening the menu on Windows left click', async () => {+ Object.defineProperty(process, 'platform', { value: 'win32' })++ const handle = setupTray(createDeps())++ await vi.waitFor(() => {+ expect(trayInstance.on).toHaveBeenCalledWith(+ 'click',+ expect.any(Function)+ )+ })++ getTrayHandler('click')()++ expect(toggleMainWindow).toHaveBeenCalledOnce()+ expect(trayInstance.popUpContextMenu).not.toHaveBeenCalled()++ handle.destroy()+ })++ it('opens the menu without toggling the window on Windows right click', async () => {+ Object.defineProperty(process, 'platform', { value: 'win32' })++ const handle = setupTray(createDeps())++ await vi.waitFor(() => {+ expect(trayInstance.on).toHaveBeenCalledWith(+ 'right-click',+ expect.any(Function)+ )+ })++ getTrayHandler('right-click')()++ expect(trayInstance.popUpContextMenu).toHaveBeenCalledExactlyOnceWith(+ trayMenu+ )+ expect(toggleMainWindow).not.toHaveBeenCalled()+handle.destroy()})++ it.each(['darwin', 'linux'] as const)(+ 'preserves the tray-menu behavior on %s left click',+ async (platform) => {+ Object.defineProperty(process, 'platform', { value: platform })++ const handle = setupTray(createDeps())++ await vi.waitFor(() => {+ expect(trayInstance.on).toHaveBeenCalledWith(+ 'click',+ expect.any(Function)+ )+ })++ getTrayHandler('click')()++ expect(trayInstance.popUpContextMenu).toHaveBeenCalledExactlyOnceWith(+ trayMenu+ )+ expect(toggleMainWindow).not.toHaveBeenCalled()++ if (platform === 'linux') {+ expect(trayInstance.setContextMenu).toHaveBeenCalledWith(trayMenu)+ }++ handle.destroy()+ }+ )})diff --git a/src/main/platform/tray.ts b/src/main/platform/tray.tsindex 4c770530..35727bfc 100644--- a/src/main/platform/tray.ts+++ b/src/main/platform/tray.ts@@ -35,6 +35,7 @@ export interface TrayDeps {menuManager: MenuManagerprotocolManager: ReturnType<typeof createProtocolManager>extraResourceDir: string+ toggleMainWindow: () => void}export interface TrayHandle {@@ -49,6 +50,7 @@ export function setupTray(deps: TrayDeps): TrayHandle {menuManager,protocolManager,extraResourceDir,+ toggleMainWindow,} = deps// Resolve asset paths — all tray assets live in extra/tray/@@ -109,6 +111,11 @@ export function setupTray(deps: TrayDeps): TrayHandle {// Eventstray.on('click', () => {+ if (process.platform === 'win32') {+ toggleMainWindow()+ return+ }+if (currentMenu) tray?.popUpContextMenu(currentMenu)})