|
| 1 | +/** |
| 2 | + * Window State Manager |
| 3 | + * |
| 4 | + * Persists and restores window position, size, and maximized state. |
| 5 | + * Handles multi-monitor setups and gracefully handles disconnected monitors. |
| 6 | + */ |
| 7 | + |
| 8 | +const { screen } = require('electron'); |
| 9 | +const path = require('path'); |
| 10 | +const fs = require('fs'); |
| 11 | +const { getAppDataDir } = require('./main-fs-ipc'); |
| 12 | + |
| 13 | +const STATE_FILE = 'window-state.json'; |
| 14 | + |
| 15 | +// Default window dimensions |
| 16 | +const DEFAULTS = { |
| 17 | + width: 1366, |
| 18 | + height: 900, |
| 19 | + minWidth: 800, |
| 20 | + minHeight: 600 |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Get the path to the window state file. |
| 25 | + */ |
| 26 | +function getStateFilePath() { |
| 27 | + return path.join(getAppDataDir(), STATE_FILE); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Load saved window state from disk. |
| 32 | + * Returns null if no saved state or file is corrupted. |
| 33 | + */ |
| 34 | +function loadWindowState() { |
| 35 | + try { |
| 36 | + const filePath = getStateFilePath(); |
| 37 | + if (!fs.existsSync(filePath)) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + const data = fs.readFileSync(filePath, 'utf8'); |
| 41 | + return JSON.parse(data); |
| 42 | + } catch (err) { |
| 43 | + console.warn('Failed to load window state:', err.message); |
| 44 | + return null; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Save window state to disk. |
| 50 | + */ |
| 51 | +function saveWindowState(state) { |
| 52 | + try { |
| 53 | + const filePath = getStateFilePath(); |
| 54 | + // Ensure directory exists |
| 55 | + const dir = path.dirname(filePath); |
| 56 | + if (!fs.existsSync(dir)) { |
| 57 | + fs.mkdirSync(dir, { recursive: true }); |
| 58 | + } |
| 59 | + fs.writeFileSync(filePath, JSON.stringify(state, null, 2)); |
| 60 | + } catch (err) { |
| 61 | + console.warn('Failed to save window state:', err.message); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Check if a rectangle is visible on any display. |
| 67 | + * Returns true if at least a portion of the window would be visible. |
| 68 | + */ |
| 69 | +function isVisibleOnAnyDisplay(bounds) { |
| 70 | + const displays = screen.getAllDisplays(); |
| 71 | + const minVisibleArea = 100; // At least 100px visible |
| 72 | + |
| 73 | + for (const display of displays) { |
| 74 | + const { x, y, width, height } = display.workArea; |
| 75 | + |
| 76 | + // Calculate overlap between window bounds and display work area |
| 77 | + const overlapX = Math.max(0, Math.min(bounds.x + bounds.width, x + width) - Math.max(bounds.x, x)); |
| 78 | + const overlapY = Math.max(0, Math.min(bounds.y + bounds.height, y + height) - Math.max(bounds.y, y)); |
| 79 | + const overlapArea = overlapX * overlapY; |
| 80 | + |
| 81 | + if (overlapArea >= minVisibleArea) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Get the display nearest to a point. |
| 90 | + */ |
| 91 | +function getNearestDisplay(x, y) { |
| 92 | + return screen.getDisplayNearestPoint({ x, y }); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Get window options with restored state or defaults. |
| 97 | + * Validates saved position is on a visible display. |
| 98 | + */ |
| 99 | +function getWindowOptions() { |
| 100 | + const savedState = loadWindowState(); |
| 101 | + |
| 102 | + const options = { |
| 103 | + width: DEFAULTS.width, |
| 104 | + height: DEFAULTS.height, |
| 105 | + minWidth: DEFAULTS.minWidth, |
| 106 | + minHeight: DEFAULTS.minHeight |
| 107 | + }; |
| 108 | + |
| 109 | + if (savedState) { |
| 110 | + // Restore size (clamped to minimums) |
| 111 | + options.width = Math.max(savedState.width || DEFAULTS.width, DEFAULTS.minWidth); |
| 112 | + options.height = Math.max(savedState.height || DEFAULTS.height, DEFAULTS.minHeight); |
| 113 | + |
| 114 | + // Check if saved position is visible on any current display |
| 115 | + if (savedState.x !== undefined && savedState.y !== undefined) { |
| 116 | + const bounds = { |
| 117 | + x: savedState.x, |
| 118 | + y: savedState.y, |
| 119 | + width: options.width, |
| 120 | + height: options.height |
| 121 | + }; |
| 122 | + |
| 123 | + if (isVisibleOnAnyDisplay(bounds)) { |
| 124 | + // Position is valid, use it |
| 125 | + options.x = savedState.x; |
| 126 | + options.y = savedState.y; |
| 127 | + } else { |
| 128 | + // Position is off-screen (monitor disconnected?), center on nearest display |
| 129 | + const nearestDisplay = getNearestDisplay(savedState.x, savedState.y); |
| 130 | + const { x, y, width, height } = nearestDisplay.workArea; |
| 131 | + options.x = x + Math.round((width - options.width) / 2); |
| 132 | + options.y = y + Math.round((height - options.height) / 2); |
| 133 | + console.log('Window position was off-screen, repositioned to nearest display'); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Track if we need to maximize after window is created |
| 138 | + options._wasMaximized = savedState.isMaximized || false; |
| 139 | + } |
| 140 | + |
| 141 | + return options; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Track window state changes and save on close. |
| 146 | + * Call this after creating the BrowserWindow. |
| 147 | + */ |
| 148 | +function trackWindowState(win) { |
| 149 | + let windowState = { |
| 150 | + width: DEFAULTS.width, |
| 151 | + height: DEFAULTS.height, |
| 152 | + x: undefined, |
| 153 | + y: undefined, |
| 154 | + isMaximized: false |
| 155 | + }; |
| 156 | + |
| 157 | + // Update state from current window bounds |
| 158 | + function updateState() { |
| 159 | + if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) { |
| 160 | + const bounds = win.getBounds(); |
| 161 | + windowState.width = bounds.width; |
| 162 | + windowState.height = bounds.height; |
| 163 | + windowState.x = bounds.x; |
| 164 | + windowState.y = bounds.y; |
| 165 | + } |
| 166 | + windowState.isMaximized = win.isMaximized(); |
| 167 | + } |
| 168 | + |
| 169 | + // Listen for state changes |
| 170 | + win.on('resize', updateState); |
| 171 | + win.on('move', updateState); |
| 172 | + win.on('maximize', updateState); |
| 173 | + win.on('unmaximize', updateState); |
| 174 | + |
| 175 | + // Save state before window closes |
| 176 | + win.on('close', () => { |
| 177 | + updateState(); |
| 178 | + saveWindowState(windowState); |
| 179 | + }); |
| 180 | + |
| 181 | + // Initialize state |
| 182 | + updateState(); |
| 183 | +} |
| 184 | + |
| 185 | +module.exports = { |
| 186 | + DEFAULTS, |
| 187 | + getWindowOptions, |
| 188 | + trackWindowState |
| 189 | +}; |
0 commit comments