A full-featured download manager.
fix(window): preserve visibility on repeated login launches (#2348)
Dr_rOot6fecb48c040c
diff --git a/e2e/window-reopen.spec.ts b/e2e/window-reopen.spec.tsindex 0efe6b08..7ab52d3d 100644--- a/e2e/window-reopen.spec.ts+++ b/e2e/window-reopen.spec.ts@@ -10,11 +10,14 @@ import {waitForQueryHandlers,} from './fixtures/electron-app'-async function secondLaunch(app: ElectronApplication): Promise<void> {+async function secondLaunch(+ app: ElectronApplication,+ args: string[] = []+): Promise<void> {// Exercise the Windows/Linux desktop shortcut event, including on macOS CI.- await app.evaluate(({ app: electronApp }) => {- electronApp.emit('second-instance', {}, ['Motrix.exe'], '', {})- })+ await app.evaluate(({ app: electronApp }, argv) => {+ electronApp.emit('second-instance', {}, ['Motrix.exe', ...argv], '', {})+ }, args)}async function windowState(app: ElectronApplication, id = 'main') {@@ -79,6 +82,11 @@ for (const lightweightMode of [false, true]) {.poll(() => windowState(app)).toEqual(lightweightMode ? null : { visible: false, minimized: false })+ await secondLaunch(app, ['--opened-at-login=1'])+ expect(await windowState(app)).toEqual(+ lightweightMode ? null : { visible: false, minimized: false }+ )+await secondLaunch(app)await expect.poll(() => windowState(app))diff --git a/src/main/launcher.test.ts b/src/main/launcher.test.tsindex a7025fb8..0737553d 100644--- a/src/main/launcher.test.ts+++ b/src/main/launcher.test.ts@@ -206,6 +206,59 @@ describe('setupLauncher', () => {expect(callbacks.onShowWindow).toHaveBeenCalledTimes(2)})+ it.each([false, true])(+ 'detects login metadata on cold start: %s',+ (openedAtLogin) => {+ if (openedAtLogin) process.argv.push('--opened-at-login=1')+ expect(setupLauncher(callbacks).wasOpenedAtLogin).toBe(openedAtLogin)+ }+ )++ it.each([false, true])(+ 'keeps repeated login launches in the background when window readiness is %s',+ (ready) => {+ process.argv.push('--opened-at-login=1')+ const handle = setupLauncher(callbacks)+ if (ready) handle.markWindowReady()++ emitSecondInstance(['Motrix.exe', '--opened-at-login=1'])+ emitSecondInstance(['Motrix.exe', '--opened-at-login=1'])+ handle.markWindowReady()+ handle.flushDeferred()+ expect(callbacks.onShowWindow).not.toHaveBeenCalled()++ // A later desktop shortcut still represents an explicit show request.+ emitSecondInstance()+ expect(callbacks.onShowWindow).toHaveBeenCalledOnce()+ }+ )++ it('preserves pending manual activation across a login launch', () => {+ const handle = setupLauncher(callbacks)+ emitSecondInstance()+ emitSecondInstance(['Motrix.exe', '--opened-at-login=1'])+ handle.markWindowReady()+ expect(callbacks.onShowWindow).toHaveBeenCalledOnce()+ })++ it.each(['magnet:?xt=urn:btih:pending', 'C:/downloads/test.torrent'])(+ 'preserves download ingress on a login launch: %s',+ (input) => {+ const handle = setupLauncher(callbacks)+ emitSecondInstance(['Motrix.exe', '--opened-at-login=1', input])+ handle.markWindowReady()+ expect(callbacks.onShowWindow).not.toHaveBeenCalled()+ expect(callbacks.onProtocolUrl).not.toHaveBeenCalled()+ expect(callbacks.onTorrentFile).not.toHaveBeenCalled()++ handle.flushDeferred()+ const callback = input.startsWith('magnet:')+ ? callbacks.onProtocolUrl+ : callbacks.onTorrentFile+ expect(callback).toHaveBeenCalledExactlyOnceWith(input)+ }+ )+it('can reopen onboarding while download ingress remains deferred', () => {const handle = setupLauncher(callbacks)handle.markWindowReady()diff --git a/src/main/launcher.ts b/src/main/launcher.tsindex 19fb2197..315d487b 100644--- a/src/main/launcher.ts+++ b/src/main/launcher.ts@@ -129,13 +129,17 @@ export function setupLauncher(callbacks: LauncherCallbacks): LauncherHandle {// second-instance (Windows/Linux: second launch passes argv)app.on('second-instance', (_event, argv) => {log.info('second instance detected')- if (windowReady) {- callbacks.onShowWindow()- } else {- // A desktop shortcut can be opened while settings and windows are still- // initializing. Retain that intent instead of losing it to an absent- // WindowManager; repeated launches need only one eventual activation.- pendingShowWindow = true+ // A login item can run after another instance has already started. It is+ // not an explicit activation and must preserve that instance's visibility.+ if (!argv.includes('--opened-at-login=1')) {+ if (windowReady) {+ callbacks.onShowWindow()+ } else {+ // A desktop shortcut can be opened while settings and windows are still+ // initializing. Retain that intent instead of losing it to an absent+ // WindowManager; repeated launches need only one eventual activation.+ pendingShowWindow = true+ }}if (process.platform !== 'darwin' && argv.length > 1) {diff --git a/src/main/platform/auto-launch.test.ts b/src/main/platform/auto-launch.test.tsindex e9ae4b82..a848b59b 100644--- a/src/main/platform/auto-launch.test.ts+++ b/src/main/platform/auto-launch.test.ts@@ -18,33 +18,39 @@ describe('syncAutoLaunch', () => {vi.clearAllMocks()})- it('enables login item with --opened-at-login arg', () => {- const originalPlatform = process.platform- Object.defineProperty(process, 'platform', { value: 'darwin' })-- syncAutoLaunch(true)-- expect(mockSetLoginItemSettings).toHaveBeenCalledWith({- openAtLogin: true,- args: ['--opened-at-login=1'],- })-- Object.defineProperty(process, 'platform', { value: originalPlatform })- })-- it('disables login item with empty args', () => {- const originalPlatform = process.platform- Object.defineProperty(process, 'platform', { value: 'darwin' })-- syncAutoLaunch(false)-- expect(mockSetLoginItemSettings).toHaveBeenCalledWith({- openAtLogin: false,- args: [],- })-- Object.defineProperty(process, 'platform', { value: originalPlatform })- })+ it.each(['darwin', 'win32'])(+ 'enables %s login item with --opened-at-login arg',+ (platform) => {+ const originalPlatform = process.platform+ Object.defineProperty(process, 'platform', { value: platform })++ syncAutoLaunch(true)++ expect(mockSetLoginItemSettings).toHaveBeenCalledWith({+ openAtLogin: true,+ args: ['--opened-at-login=1'],+ })++ Object.defineProperty(process, 'platform', { value: originalPlatform })+ }+ )++ it.each(['darwin', 'win32'])(+ 'disables %s login item with empty args',+ (platform) => {+ const originalPlatform = process.platform+ Object.defineProperty(process, 'platform', { value: platform })++ syncAutoLaunch(false)++ expect(mockSetLoginItemSettings).toHaveBeenCalledWith({+ openAtLogin: false,+ args: [],+ })++ Object.defineProperty(process, 'platform', { value: originalPlatform })+ }+ )it('skips on linux', () => {const originalPlatform = process.platform