Skip to content

Commit

Permalink
docs: update comments in util
Browse files Browse the repository at this point in the history
  • Loading branch information
psychedelicious committed Dec 21, 2024
1 parent 722d94b commit b6efd85
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ export const isDevelopment = (): boolean => process.env.NODE_ENV === 'developmen

//#region Window mgmt

/**
* Checks if the given rect exceeds the screen bounds
*/
const exceedsScreenBounds = (bounds: Electron.Rectangle): boolean => {
const screenArea = screen.getDisplayMatching(bounds).workArea;
return (
Expand All @@ -288,13 +291,27 @@ const exceedsScreenBounds = (bounds: Electron.Rectangle): boolean => {
);
};

/**
* Manages a window's size:
* - Restores the window to its previous size and position, maximizing or fullscreening it if necessary
* - Saves the window's size and position when it is closed
* - If provided, uses the initialProps to set the window's size and position
*
* The window will not be set to the stored/initial bounds if it exceeds the current screen bounds.
*
* @param window The window to manage
* @param windowProps The stored window properties
* @param setWindowProps The function to call to save the window properties
* @param initialProps The initial window properties to use if there are no stored properties
*/
export const manageWindowSize = (
window: BrowserWindow,
windowProps: WindowProps | undefined,
setWindowProps: (windowProps: WindowProps) => void,
initialProps?: Partial<WindowProps>
): void => {
if (windowProps) {
// Restore window size and position
const { bounds, isMaximized, isFullScreen } = windowProps;
if (!exceedsScreenBounds(bounds)) {
window.setBounds(bounds);
Expand All @@ -306,6 +323,7 @@ export const manageWindowSize = (
window.setFullScreen(true);
}
} else if (initialProps) {
// No stored properties, use initial properties if they exist
const { bounds, isMaximized, isFullScreen } = initialProps;
if (bounds && !exceedsScreenBounds(bounds)) {
window.setBounds(bounds);
Expand All @@ -318,6 +336,7 @@ export const manageWindowSize = (
}
}

// Save window size and position when it is closed and clear the event listener
const handleClose = () => {
setWindowProps({
bounds: window.getBounds(),
Expand Down

0 comments on commit b6efd85

Please sign in to comment.