Skip to content

Commit

Permalink
feat(electron): save window state (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
mofazhe committed Jul 8, 2024
1 parent 395b158 commit 2f6fb9c
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 8 deletions.
8 changes: 3 additions & 5 deletions packages/electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"electron-squirrel-startup": "^1.0.0",
"open": "^10.1.0",
"undici": "^6.19.2",
"update-electron-app": "^2.0.1"
"update-electron-app": "^2.0.1",
"jsonfile": "^4.0.0",
"mkdirp": "^0.5.1"
},
"config": {
"forge": "./forge.config.js"
Expand Down
21 changes: 19 additions & 2 deletions packages/electron/src/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const { app, BrowserWindow, ipcMain, dialog, screen } = require('electron')
const { resolve } = require('path')
const contextMenu = require('electron-context-menu')
const log = require('electron-log/main')
Expand All @@ -8,6 +8,7 @@ const db = require('./db')
const helpers = require('./helpers')
const TaskQueue = require('./task-queue')
const updateElectronApp = require('update-electron-app')
const windowStateKeeper = require('./utils/window-state')

Object.assign(console, log.functions)

Expand All @@ -28,7 +29,20 @@ if (process.platform === 'linux' && process.env.SNAP) {
function createWindow() {
const autoHideMenuBar = true

const workAreaSize = screen.getPrimaryDisplay().workAreaSize
// console.log("workAreaSize: width: " + workAreaSize.width + ", height: " + workAreaSize.height)
const winStateOptions = {
defaultWidth: parseInt(workAreaSize.width * 0.75),
defaultHeight: parseInt(workAreaSize.height * 0.75),
defaultMaximize: true,
};
const winState = windowStateKeeper(winStateOptions);

const win = new BrowserWindow({
x: winState.x,
y: winState.y,
width: winState.width,
height: winState.height,
show: false,
autoHideMenuBar,
webPreferences: {
Expand All @@ -39,7 +53,10 @@ function createWindow() {
icon: resolve(__dirname, '../ui/favicon.png'),
})
win.on('show', () => { win.focus() })
win.maximize()
// win.maximize()
// Please do not set useContentSize to true at creating BrowserWindow instance because it changes how to calculate window size.
// https://github.com/mawie81/electron-window-state?tab=readme-ov-file#usage
winState.manage(win)
win.show()

if (!app.isPackaged) {
Expand Down
56 changes: 56 additions & 0 deletions packages/electron/src/utils/window-state.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as Electron from "electron";

declare function windowStateKeeper(
opts: windowStateKeeper.Options
): windowStateKeeper.State;

declare namespace windowStateKeeper {
interface Options {
/** The height that should be returned if no file exists yet. Defaults to `600`. */
defaultHeight?: number;
/** The width that should be returned if no file exists yet. Defaults to `800`. */
defaultWidth?: number;
/** The maximize that should be returned if no file exists yet. Defaults to `false`.
* If true will ignore defaultHeight and defaultWidth.
*/
defaultMaximize?: boolean;
/** The fullScreen that should be returned if no file exists yet. Defaults to `false`.
* If true will ignore defaultHeight, defaultWidth and defaultMaximize.
*/
defaultFullScreen?: boolean;
fullScreen?: boolean;
/** The path where the state file should be written to. Defaults to `app.getPath('userData')`. */
path?: string;
/** The name of file. Defaults to `window-state.json`. */
file?: string;
/** Should we automatically maximize the window, if it was last closed maximized. Defaults to `true`. */
maximize?: boolean;
}

interface State {
displayBounds: {
height: number;
width: number;
};
/** The saved height of loaded state. `defaultHeight` if the state has not been saved yet. */
height: number;
/** true if the window state was saved while the window was in full screen mode. `undefined` if the state has not been saved yet. */
isFullScreen: boolean;
/** `true` if the window state was saved while the window was maximized. `undefined` if the state has not been saved yet. */
isMaximized: boolean;
/** Register listeners on the given `BrowserWindow` for events that are related to size or position changes (resize, move). It will also restore the window's maximized or full screen state. When the window is closed we automatically remove the listeners and save the state. */
manage: (window: Electron.BrowserWindow) => void;
/** Saves the current state of the given `BrowserWindow`. This exists mostly for legacy purposes, and in most cases it's better to just use `manage()`. */
saveState: (window: Electron.BrowserWindow) => void;
/** Removes all listeners of the managed `BrowserWindow` in case it does not need to be managed anymore. */
unmanage: () => void;
/** The saved width of loaded state. `defaultWidth` if the state has not been saved yet. */
width: number;
/** The saved x coordinate of the loaded state. `undefined` if the state has not been saved yet. */
x: number;
/** The saved y coordinate of the loaded state. `undefined` if the state has not been saved yet. */
y: number;
}
}

export = windowStateKeeper;
Loading

0 comments on commit 2f6fb9c

Please sign in to comment.