Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit 650bb8b

Browse files
authored
Merge pull request #40 from skellock/window-state-tracking
Tracks window coordinates & saves to disk.
2 parents 32072e3 + 7c20a73 commit 650bb8b

File tree

5 files changed

+107
-24
lines changed

5 files changed

+107
-24
lines changed

docs/stack.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ A few quality-of-life utilities for working in Electron.
118118
119119
Persist JSON to the file system.
120120

121+
> **electron-window-state-manager**
122+
123+
Persist window coordinates & dimensions to disk to survive restarts.
124+
121125

122126
## Bundler
123127

package-lock.json

Lines changed: 39 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"electron-log": "2.2.7",
4242
"electron-store": "1.2.0",
4343
"electron-updater": "2.8.2",
44+
"electron-window-state-manager": "0.3.2",
4445
"glamor": "2.20.39",
4546
"mobx": "3.2.2",
4647
"mobx-react": "4.2.2",

src/main/main-window/main-window.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { app, BrowserWindow } from 'electron'
22
import { join } from 'path'
33
import { format } from 'url'
4+
import WindowStateManager = require('electron-window-state-manager')
45

56
// default dimensions
67
const DIMENSIONS = { width: 600, height: 500, minWidth: 450, minHeight: 450 }
@@ -12,8 +13,20 @@ const DIMENSIONS = { width: 600, height: 500, minWidth: 450, minHeight: 450 }
1213
* @return The main BrowserWindow.
1314
*/
1415
export function createMainWindow(appPath: string) {
16+
// persistent window state manager
17+
const windowState = new WindowStateManager('main', {
18+
defaultWidth: DIMENSIONS.width,
19+
defaultHeight: DIMENSIONS.height,
20+
})
21+
22+
// create our main window
1523
const window = new BrowserWindow({
16-
...DIMENSIONS,
24+
minWidth: DIMENSIONS.minWidth,
25+
minHeight: DIMENSIONS.minHeight,
26+
width: windowState.width,
27+
height: windowState.height,
28+
x: windowState.x,
29+
y: windowState.y,
1730
show: false,
1831
useContentSize: true,
1932
titleBarStyle: 'hidden',
@@ -22,6 +35,16 @@ export function createMainWindow(appPath: string) {
2235
title: app.getName(),
2336
})
2437

38+
// maximize if we did before
39+
if (windowState.maximized) {
40+
window.maximize()
41+
}
42+
43+
// trap movement events
44+
window.on('close', () => windowState.saveState(window))
45+
window.on('move', () => windowState.saveState(window))
46+
window.on('resize', () => windowState.saveState(window))
47+
2548
// load entry html page in the renderer.
2649
window.loadURL(
2750
format({
@@ -33,8 +56,10 @@ export function createMainWindow(appPath: string) {
3356

3457
// only appear once we've loaded
3558
window.webContents.on('did-finish-load', () => {
36-
window.show()
37-
window.focus()
59+
setTimeout(() => {
60+
window.show()
61+
window.focus()
62+
}, 100)
3863
})
3964

4065
return window
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
declare module 'electron-window-state-manager' {
2+
interface ElectronStateManagerOptions {
3+
/** The default width for this window. */
4+
defaultWidth?: number
5+
/** The default height for this window. */
6+
defaultHeight?: number
7+
}
8+
9+
/**
10+
* Manages an Electron.BrowserWindow's dimensions and stores it to disk.
11+
*/
12+
class ElectronStateManager {
13+
/**
14+
* Creates a manager that loads/saves a window's coordinates & dimensions.
15+
*
16+
* @param name The name of the window.
17+
* @param options Some default options.
18+
*/
19+
constructor(name: string, options?: ElectronStateManagerOptions)
20+
/** Save this window's dimensions */
21+
saveState(browserWindow: Electron.BrowserWindow): void
22+
/** The window width. */
23+
width: number
24+
/** The window height. */
25+
height: number
26+
/** the window left position. */
27+
x: number
28+
/** The window right position. */
29+
y: number
30+
/** Is this maximized? */
31+
maximized: boolean
32+
}
33+
34+
export = ElectronStateManager
35+
}

0 commit comments

Comments
 (0)