-
Notifications
You must be signed in to change notification settings - Fork 2
/
electron-window-state.js
191 lines (166 loc) · 5.09 KB
/
electron-window-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Based on https://github.com/poursal/electron-window-state/blob/feature-center-reset/index.js
'use strict';
const path = require('path');
const jsonfile = require('jsonfile');
const mkdirp = require('mkdirp');
module.exports = function (options, app, screen) {
let state;
let winRef;
let stateChangeTimer;
const eventHandlingDelay = 100;
const config = Object.assign({
file: 'window-state.json',
path: app.getPath('userData'),
maximize: true,
fullScreen: true,
resetToCenter: false
}, options);
const fullStoreFileName = path.join(config.path, config.file);
function isNormal(win) {
return !win.isMaximized() && !win.isMinimized() && !win.isFullScreen();
}
function hasBounds() {
return state &&
Number.isInteger(state.x) &&
Number.isInteger(state.y) &&
Number.isInteger(state.width) && state.width > 0 &&
Number.isInteger(state.height) && state.height > 0;
}
function resetStateToDefault() {
// Reset window position to default on the primary display
state.displayBounds = screen.getPrimaryDisplay().bounds;
state.x = config.resetToCenter ? Math.floor((state.displayBounds.width - state.width) / 2) : 0;
state.y = config.resetToCenter ? Math.floor((state.displayBounds.height - state.height) / 2) : 0;
// If window still outside of bounds, then reset size as well
if (!windowWithinBounds(state.displayBounds)) {
state.width = config.defaultWidth || 800;
state.height = config.defaultHeight || 600;
if (config.resetToCenter) {
state.x = Math.floor((state.displayBounds.width - state.width) / 2);
state.y = Math.floor((state.displayBounds.height - state.height) / 2);
}
}
}
function windowWithinBounds(bounds) {
return (
state.x >= bounds.x &&
state.y >= bounds.y &&
state.x + state.width <= bounds.x + bounds.width &&
state.y + state.height <= bounds.y + bounds.height
);
}
function ensureWindowVisibleOnSomeDisplay() {
const visible = screen.getAllDisplays().some(display => {
return windowWithinBounds(display.bounds);
});
if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetStateToDefault();
}
}
function validateState() {
const isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
if (!isValid) {
state = null;
return;
}
if (hasBounds() && state.displayBounds) {
ensureWindowVisibleOnSomeDisplay();
}
}
function updateState(win) {
win = win || winRef;
if (!win) {
return;
}
// Don't throw an error when window was closed
try {
const winBounds = win.getBounds();
if (isNormal(win)) {
state.x = winBounds.x;
state.y = winBounds.y;
state.width = winBounds.width;
state.height = winBounds.height;
}
state.isMaximized = win.isMaximized();
state.isFullScreen = win.isFullScreen();
state.displayBounds = screen.getDisplayMatching(winBounds).bounds;
} catch (err) {}
}
function saveState(win) {
// Update window state only if it was provided
if (win) {
updateState(win);
}
// Save state
try {
mkdirp.sync(path.dirname(fullStoreFileName));
jsonfile.writeFileSync(fullStoreFileName, state);
} catch (err) {
// Don't care
}
}
function stateChangeHandler() {
// Handles both 'resize' and 'move'
clearTimeout(stateChangeTimer);
stateChangeTimer = setTimeout(updateState, eventHandlingDelay);
}
function closeHandler() {
updateState();
}
function closedHandler() {
// Unregister listeners and save state
unmanage();
saveState();
}
function manage(win) {
if (config.maximize && state.isMaximized) {
win.maximize();
}
if (config.fullScreen && state.isFullScreen) {
win.setFullScreen(true);
}
win.on('resize', stateChangeHandler);
win.on('move', stateChangeHandler);
win.on('close', closeHandler);
win.on('closed', closedHandler);
winRef = win;
}
function unmanage() {
if (winRef) {
winRef.removeListener('resize', stateChangeHandler);
winRef.removeListener('move', stateChangeHandler);
clearTimeout(stateChangeTimer);
winRef.removeListener('close', closeHandler);
winRef.removeListener('closed', closedHandler);
winRef = null;
}
}
// Load previous state
try {
state = jsonfile.readFileSync(fullStoreFileName);
} catch (err) {
// Don't care
}
// Check state validity
validateState();
// Set state fallback values
state = Object.assign({
width: config.defaultWidth || 800,
height: config.defaultHeight || 600
}, state);
return {
get x() { return state.x; },
get y() { return state.y; },
get width() { return state.width; },
get height() { return state.height; },
get displayBounds() { return state.displayBounds; },
get isMaximized() { return state.isMaximized; },
get isFullScreen() { return state.isFullScreen; },
saveState,
unmanage,
manage,
resetStateToDefault
};
};