[desktop] Add a default window size (#1977)

This commit is contained in:
Manav Rathi
2024-06-03 16:26:07 +05:30
committed by GitHub
2 changed files with 38 additions and 14 deletions

View File

@@ -220,12 +220,27 @@ const createMainWindow = () => {
};
/**
* The position and size of the window the last time it was closed.
* The position and size to use when showing the main window.
*
* The return value of `undefined` is taken to mean that the app's main window
* should be maximized.
* The return value is `undefined` if the app's window was maximized the last
* time around, and so if we should restore it to the maximized state.
*
* Otherwise it returns the position and size of the window the last time the
* app quit.
*
* If there is no such saved value (or if it is the first time the user is
* running the app), return a default size.
*/
const windowBounds = () => userPreferences.get("windowBounds");
const windowBounds = () => {
if (userPreferences.get("isWindowMaximized")) return undefined;
const bounds = userPreferences.get("windowBounds");
if (bounds) return bounds;
// Default size. Picked arbitrarily as something that should look good on
// first launch. We don't provide a position to let Electron center the app.
return { width: 1170, height: 710 };
};
/**
* If for some reason {@link windowBounds} is outside the screen's bounds (e.g.
@@ -233,10 +248,11 @@ const windowBounds = () => userPreferences.get("windowBounds");
* bounds might not be appropriate.
*
* Luckily, if we try to set an x/y position that is outside the screen's
* bounds, then Electron automatically clamps them to the screen's available
* space, and we do not need to tackle it specifically.
* bounds, then Electron automatically clamps x + width and y + height to lie
* within the screen's available space, and we do not need to tackle such out of
* bounds cases specifically.
*
* However, there is no minimum window size the Electron enforces by default. As
* However there is no minimum window size the Electron enforces by default. As
* a safety valve, provide an (arbitrary) minimum size so that the user can
* resize it back to sanity if something I cannot currently anticipate happens.
*/
@@ -247,8 +263,13 @@ const minimumWindowSize = () => ({ minWidth: 200, minHeight: 200 });
* details.
*/
const saveWindowBounds = (window: BrowserWindow) => {
if (window.isMaximized()) userPreferences.delete("windowBounds");
else userPreferences.set("windowBounds", window.getBounds());
if (window.isMaximized()) {
userPreferences.set("isWindowMaximized", true);
userPreferences.delete("windowBounds");
} else {
userPreferences.delete("isWindowMaximized");
userPreferences.set("windowBounds", window.getBounds());
}
};
/**

View File

@@ -13,11 +13,9 @@ interface UserPreferences {
* The last position and size of our app's window.
*
* This value is saved when the app is about to quit, and is used to restore
* the window to the previous state when it restarts.
*
* If the user maximizes the window then this value is cleared and instead
* we just re-maximize the window on restart. This is also the behaviour if
* no previously saved `windowRect` is found.
* the window to the previous state when it restarts. It is only saved if
* the app is not maximized (when the app was maximized when it was being
* quit then {@link isWindowMaximized} will be set instead).
*/
windowBounds?: {
x: number;
@@ -25,6 +23,10 @@ interface UserPreferences {
width: number;
height: number;
};
/**
* `true` if the app's main window is maximized the last time it was closed.
*/
isWindowMaximized?: boolean;
}
const userPreferencesSchema: Schema<UserPreferences> = {
@@ -39,6 +41,7 @@ const userPreferencesSchema: Schema<UserPreferences> = {
height: { type: "number" },
},
},
isWindowMaximized: { type: "boolean" },
};
export const userPreferences = new Store({