-
Notifications
You must be signed in to change notification settings - Fork 233
feat: implement window bounds persistence COMPASS-6561 #7388
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
base: main
Are you sure you want to change the base?
Changes from all commits
2bad280
54c08fb
ad278b4
6924373
8d13821
ebeede5
d198c68
1b40f27
65171d9
54f712f
02836d7
df9f359
32f90ca
3dff406
8ed328c
c7d854f
ed70691
2e4b687
2750310
8bdb6c6
1b86957
da94013
f3688b6
637f305
a7b69fe
9f9acc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import type { | |
import { app as electronApp, shell, BrowserWindow } from 'electron'; | ||
import { enable } from '@electron/remote/main'; | ||
|
||
import { createLogger } from '@mongodb-js/compass-logging'; | ||
import { createLogger, mongoLogId } from '@mongodb-js/compass-logging'; | ||
import COMPASS_ICON from './icon'; | ||
import type { CompassApplication } from './application'; | ||
import { | ||
|
@@ -23,7 +23,7 @@ import { | |
registerConnectionIdForBrowserWindow, | ||
} from './auto-connect'; | ||
|
||
const { debug } = createLogger('COMPASS-WINDOW-MANAGER'); | ||
const { debug, log } = createLogger('COMPASS-WINDOW-MANAGER'); | ||
|
||
const earlyOpenUrls: string[] = []; | ||
function earlyOpenUrlListener( | ||
|
@@ -68,6 +68,7 @@ const DEFAULT_HEIGHT = (() => { | |
// change significantly at widths of 1024 and less. | ||
const MIN_WIDTH = process.env.COMPASS_MIN_WIDTH ?? 1025; | ||
const MIN_HEIGHT = process.env.COMPASS_MIN_HEIGHT ?? 640; | ||
const SAVE_DEBOUNCE_DELAY = 500; // 500ms delay for save operations | ||
|
||
/** | ||
* The app's HTML shell which is the output of `./src/index.html` | ||
|
@@ -77,11 +78,73 @@ const DEFAULT_URL = | |
process.env.COMPASS_INDEX_RENDERER_URL || | ||
pathToFileURL(path.join(__dirname, 'index.html')).toString(); | ||
|
||
async function showWindowWhenReady(bw: BrowserWindow) { | ||
async function showWindowWhenReady( | ||
bw: BrowserWindow, | ||
isMaximized?: boolean, | ||
isFullScreen?: boolean | ||
) { | ||
await once(bw, 'ready-to-show'); | ||
if (isMaximized) { | ||
// win.maximize() maximizes the window. | ||
// This will also show (but not focus) the window if it isn't being displayed already. | ||
bw.maximize(); | ||
bw.focus(); | ||
return; | ||
} | ||
|
||
if (isFullScreen) { | ||
bw.setFullScreen(true); | ||
} | ||
|
||
bw.show(); | ||
} | ||
|
||
/** | ||
* Save window bounds to preferences | ||
*/ | ||
async function saveWindowBounds( | ||
window: BrowserWindow, | ||
compassApp: typeof CompassApplication | ||
) { | ||
try { | ||
const bounds = window.getBounds(); | ||
const isMaximized = window.isMaximized(); | ||
const isFullScreen = window.isFullScreen(); | ||
|
||
await compassApp.preferences.savePreferences({ | ||
windowBounds: { | ||
x: bounds.x, | ||
y: bounds.y, | ||
width: bounds.width, | ||
height: bounds.height, | ||
isMaximized, | ||
isFullScreen, | ||
}, | ||
}); | ||
} catch (err) { | ||
log.warn( | ||
mongoLogId(1_001_000_377), | ||
'Window Manager', | ||
'Failed to save window bounds', | ||
{ message: (err as Error).message } | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Get saved window bounds from preferences | ||
*/ | ||
function getSavedWindowBounds(compassApp: typeof CompassApplication) { | ||
const windowBounds = | ||
compassApp.preferences.getPreferences().windowBounds ?? {}; | ||
const { width, height, ...rest } = windowBounds; | ||
return { | ||
...rest, | ||
width: width ?? DEFAULT_WIDTH, | ||
height: height ?? DEFAULT_HEIGHT, | ||
}; | ||
} | ||
|
||
/** | ||
* Call me instead of using `new BrowserWindow()` directly because i'll: | ||
* | ||
|
@@ -109,9 +172,11 @@ function showConnectWindow( | |
} | ||
> = {} | ||
): BrowserWindow { | ||
// Get saved window bounds | ||
const { isMaximized, isFullScreen, ...bounds } = | ||
getSavedWindowBounds(compassApp); | ||
const windowOpts = { | ||
width: Number(DEFAULT_WIDTH), | ||
height: Number(DEFAULT_HEIGHT), | ||
...bounds, | ||
minWidth: Number(MIN_WIDTH), | ||
minHeight: Number(MIN_HEIGHT), | ||
/** | ||
|
@@ -156,8 +221,35 @@ function showConnectWindow( | |
|
||
compassApp.emit('new-window', window); | ||
|
||
// Set up window state persistence | ||
let saveTimeoutId: NodeJS.Timeout | null = null; | ||
const debouncedSaveWindowBounds = () => { | ||
if (saveTimeoutId) { | ||
clearTimeout(saveTimeoutId); | ||
} | ||
saveTimeoutId = setTimeout(() => { | ||
if (window && !window.isDestroyed()) { | ||
void saveWindowBounds(window, compassApp); | ||
} | ||
saveTimeoutId = null; | ||
}, SAVE_DEBOUNCE_DELAY); // Debounce to avoid too frequent saves | ||
}; | ||
|
||
// Save window bounds when moved or resized | ||
window.on('move', debouncedSaveWindowBounds); | ||
window.on('moved', debouncedSaveWindowBounds); | ||
window.on('resize', debouncedSaveWindowBounds); | ||
window.on('resized', debouncedSaveWindowBounds); | ||
window.on('maximize', debouncedSaveWindowBounds); | ||
window.on('unmaximize', debouncedSaveWindowBounds); | ||
Comment on lines
+243
to
+244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me time to realize this is not full screen maximize. To have more consistent behaviour, should we also handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn’t fully considered the fullscreen-related behavior. commit: da94013 |
||
window.on('enter-full-screen', debouncedSaveWindowBounds); | ||
window.on('leave-full-screen', debouncedSaveWindowBounds); | ||
|
||
const onWindowClosed = () => { | ||
debug('Window closed. Dereferencing.'); | ||
if (saveTimeoutId) { | ||
clearTimeout(saveTimeoutId); | ||
} | ||
window = null; | ||
void unsubscribeProxyListenerPromise.then((unsubscribe) => unsubscribe()); | ||
}; | ||
|
@@ -166,7 +258,7 @@ function showConnectWindow( | |
|
||
debug(`Loading page ${rendererUrl} in main window`); | ||
|
||
void showWindowWhenReady(window); | ||
void showWindowWhenReady(window, isMaximized, isFullScreen); | ||
|
||
void window.loadURL(rendererUrl); | ||
|
||
|
@@ -243,6 +335,8 @@ class CompassWindowManager { | |
if (first) { | ||
debug('sending `app:quit` msg'); | ||
first.webContents.send('app:quit'); | ||
// Save window bounds before quitting | ||
void saveWindowBounds(first, compassApp); | ||
} | ||
}); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does electron gracefully handle if a user tries to open a window that's larger than their screen size or out of bounds of their current screen? I'm wondering if we need to account for situations where a user goes from a larger monitor to a smaller one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are the results I observed on macOS (v15.6.1).
I wasn’t able to test on Windows or Linux since I don’t have access to those machines.
When the window size was larger than the display or even slightly out of bounds, macOS automatically adjusted it.
If the window exceeded the display size, it was resized to fit within the screen.
If it was positioned partially outside the display, it was automatically moved so that the entire window remained visible.
I checked the Electron documentation but couldn’t find any mention of this behavior.
It might be handled by the operating system rather than Electron itself, so if possible, could you also test it on Windows and Linux?
2025-10-06.1.19.03.mov