Repository navigation
fix: preserve macOS app lifecycle on window close #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/desktop-electron/src/main/window-lifecycle.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { expect, test } from "bun:test" | ||
|
|
||
| import { | ||
| registerWindowLifecycle, | ||
| selectCommandWindow, | ||
| selectNextMainWindow, | ||
| shouldCreateWindowOnActivate, | ||
| shouldOpenWindowForExternalEvent, | ||
| shouldQueueDeepLinks, | ||
| shouldQuitWhenAllWindowsClosed, | ||
| } from "./window-lifecycle" | ||
|
|
||
| function createFakeApp() { | ||
| const listeners = new Map<string, () => void>() | ||
| let quitCount = 0 | ||
|
|
||
| return { | ||
| on(event: string, listener: () => void) { | ||
| listeners.set(event, listener) | ||
| }, | ||
| quit() { | ||
| quitCount++ | ||
| }, | ||
| emit(event: string) { | ||
| listeners.get(event)?.() | ||
| }, | ||
| quitCount() { | ||
| return quitCount | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| test("macOS keeps the app running when the last window closes", () => { | ||
| expect(shouldQuitWhenAllWindowsClosed("darwin")).toBe(false) | ||
| }) | ||
|
|
||
| test("non-macOS platforms keep the existing quit-on-last-window behavior", () => { | ||
| expect(shouldQuitWhenAllWindowsClosed("win32")).toBe(true) | ||
| expect(shouldQuitWhenAllWindowsClosed("linux")).toBe(true) | ||
| }) | ||
|
|
||
| test("macOS recreates a window on activate only when no windows are open", () => { | ||
| expect(shouldCreateWindowOnActivate("darwin", 0)).toBe(true) | ||
| expect(shouldCreateWindowOnActivate("darwin", 1)).toBe(false) | ||
| }) | ||
|
|
||
| test("non-macOS activate does not create a window through macOS lifecycle rules", () => { | ||
| expect(shouldCreateWindowOnActivate("win32", 0)).toBe(false) | ||
| expect(shouldCreateWindowOnActivate("linux", 0)).toBe(false) | ||
| }) | ||
|
|
||
| test("registered macOS lifecycle keeps the app alive and reopens a window on activate", () => { | ||
| const fake = createFakeApp() | ||
| let openCount = 0 | ||
|
|
||
| registerWindowLifecycle({ | ||
| onWindowAllClosed: (listener) => fake.on("window-all-closed", listener), | ||
| onActivate: (listener) => fake.on("activate", listener), | ||
| quit: () => fake.quit(), | ||
| getWindowCount: () => 0, | ||
| openWindow: () => { | ||
| openCount++ | ||
| }, | ||
| platform: "darwin", | ||
| }) | ||
|
|
||
| fake.emit("window-all-closed") | ||
| fake.emit("activate") | ||
|
|
||
| expect(fake.quitCount()).toBe(0) | ||
| expect(openCount).toBe(1) | ||
| }) | ||
|
|
||
| test("registered non-macOS lifecycle quits when all windows close", () => { | ||
| const fake = createFakeApp() | ||
| let openCount = 0 | ||
|
|
||
| registerWindowLifecycle({ | ||
| onWindowAllClosed: (listener) => fake.on("window-all-closed", listener), | ||
| onActivate: (listener) => fake.on("activate", listener), | ||
| quit: () => fake.quit(), | ||
| getWindowCount: () => 0, | ||
| openWindow: () => { | ||
| openCount++ | ||
| }, | ||
| platform: "win32", | ||
| }) | ||
|
|
||
| fake.emit("window-all-closed") | ||
| fake.emit("activate") | ||
|
|
||
| expect(fake.quitCount()).toBe(1) | ||
| expect(openCount).toBe(0) | ||
| }) | ||
|
|
||
| test("main window fallback keeps an older open window as the command target", () => { | ||
| const olderWindow = { isDestroyed: () => false } | ||
| const closingWindow = { isDestroyed: () => true } | ||
|
|
||
| expect(selectNextMainWindow(closingWindow, [olderWindow])).toBe(olderWindow) | ||
| }) | ||
|
|
||
| test("main window fallback ignores destroyed windows", () => { | ||
| const closingWindow = { isDestroyed: () => true } | ||
| const destroyedWindow = { isDestroyed: () => true } | ||
|
|
||
| expect(selectNextMainWindow(closingWindow, [destroyedWindow])).toBeNull() | ||
| }) | ||
|
|
||
| test("deep links are queued until a current window is ready to receive them", () => { | ||
| expect(shouldQueueDeepLinks(false, false)).toBe(true) | ||
| expect(shouldQueueDeepLinks(true, false)).toBe(true) | ||
| expect(shouldQueueDeepLinks(true, true)).toBe(false) | ||
| }) | ||
|
|
||
| test("headless external events reopen a window only after initialization is done", () => { | ||
| expect(shouldOpenWindowForExternalEvent(false, true)).toBe(true) | ||
| expect(shouldOpenWindowForExternalEvent(false, false)).toBe(false) | ||
| expect(shouldOpenWindowForExternalEvent(true, true)).toBe(false) | ||
| }) | ||
|
|
||
| test("menu commands prefer the focused window over the newest tracked window", () => { | ||
| const focusedWindow = { isDestroyed: () => false } | ||
| const currentWindow = { isDestroyed: () => false } | ||
|
|
||
| expect(selectCommandWindow(focusedWindow, currentWindow)).toBe(focusedWindow) | ||
| }) | ||
|
|
||
| test("menu commands fall back to the tracked window when there is no focused window", () => { | ||
| const currentWindow = { isDestroyed: () => false } | ||
|
|
||
| expect(selectCommandWindow(null, currentWindow)).toBe(currentWindow) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export function shouldQuitWhenAllWindowsClosed(platform: NodeJS.Platform) { | ||
| return platform !== "darwin" | ||
| } | ||
|
|
||
| export function shouldCreateWindowOnActivate(platform: NodeJS.Platform, windowCount: number) { | ||
| return platform === "darwin" && windowCount === 0 | ||
| } | ||
|
|
||
| type RegisterWindowLifecycleOptions = { | ||
| onWindowAllClosed: (listener: () => void) => void | ||
| onActivate: (listener: () => void) => void | ||
| quit: () => void | ||
| getWindowCount: () => number | ||
| openWindow: () => void | ||
| platform: NodeJS.Platform | ||
| } | ||
|
|
||
| export function registerWindowLifecycle(options: RegisterWindowLifecycleOptions) { | ||
| options.onWindowAllClosed(() => { | ||
| if (shouldQuitWhenAllWindowsClosed(options.platform)) options.quit() | ||
| }) | ||
|
|
||
| options.onActivate(() => { | ||
| if (shouldCreateWindowOnActivate(options.platform, options.getWindowCount())) options.openWindow() | ||
| }) | ||
| } | ||
|
|
||
| type WindowLike = { | ||
| isDestroyed: () => boolean | ||
| } | ||
|
|
||
| export function selectNextMainWindow<T extends WindowLike>(closedWindow: T, windows: T[]) { | ||
| return windows.find((win) => win !== closedWindow && !win.isDestroyed()) ?? null | ||
| } | ||
|
|
||
| export function shouldQueueDeepLinks(hasWindow: boolean, windowReady: boolean) { | ||
| return !hasWindow || !windowReady | ||
| } | ||
|
|
||
| export function shouldOpenWindowForExternalEvent(hasWindow: boolean, initialized: boolean) { | ||
| return !hasWindow && initialized | ||
| } | ||
|
|
||
| export function selectCommandWindow<T extends WindowLike>(focusedWindow: T | null, currentWindow: T | null) { | ||
| if (focusedWindow && !focusedWindow.isDestroyed()) return focusedWindow | ||
| if (currentWindow && !currentWindow.isDestroyed()) return currentWindow | ||
| return null | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.