* Register a window with the manager and assign a stable ID * Returns the stable window ID to use for localStorage namespacing
(window: BrowserWindow)
| 17 | * Returns the stable window ID to use for localStorage namespacing |
| 18 | */ |
| 19 | register(window: BrowserWindow): string { |
| 20 | const electronId = window.id |
| 21 | this.windows.set(electronId, window) |
| 22 | |
| 23 | // Assign stable ID |
| 24 | let stableId: string |
| 25 | if (this.mainWindowId === null) { |
| 26 | // First window ever registered becomes the "main" window |
| 27 | this.mainWindowId = electronId |
| 28 | stableId = "main" |
| 29 | } else { |
| 30 | // Secondary windows get incrementing IDs |
| 31 | stableId = `window-${this.nextSecondaryId++}` |
| 32 | } |
| 33 | this.windowIdMap.set(electronId, stableId) |
| 34 | |
| 35 | // Track focus |
| 36 | window.on("focus", () => { |
| 37 | this.focusedWindowId = electronId |
| 38 | }) |
| 39 | |
| 40 | // Clean up on close |
| 41 | // Note: Electron automatically removes all listeners when a window is destroyed, |
| 42 | // so we only need to clean up our internal tracking here |
| 43 | window.on("closed", () => { |
| 44 | // Cleanup git watcher subscriptions for this window to prevent memory leaks |
| 45 | cleanupWindowSubscriptions(electronId) |
| 46 | // Release any chat ownership held by this window |
| 47 | this.releaseAllChats(electronId) |
| 48 | |
| 49 | this.windows.delete(electronId) |
| 50 | this.windowIdMap.delete(electronId) |
| 51 | if (this.focusedWindowId === electronId) { |
| 52 | this.focusedWindowId = null |
| 53 | } |
| 54 | // If main window is closed, update mainWindowId for internal tracking |
| 55 | // but DON'T change the stable ID of remaining windows - they keep their localStorage namespace |
| 56 | if (this.mainWindowId === electronId) { |
| 57 | const remainingWindows = Array.from(this.windows.keys()) |
| 58 | this.mainWindowId = remainingWindows.length > 0 ? remainingWindows[0] : null |
| 59 | // Note: We intentionally keep the existing stable ID (e.g., "window-2") |
| 60 | // Changing it to "main" would orphan the window's localStorage data |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | // Set as focused if it's the only window |
| 65 | if (this.windows.size === 1) { |
| 66 | this.focusedWindowId = electronId |
| 67 | } |
| 68 | |
| 69 | return stableId |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the stable ID for a window |
no test coverage detected