-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
161 lines (136 loc) · 4.62 KB
/
index.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
'use strict'
/**
* Cross-platform window state preservation.
* Yes this code is quite complicated, but this is the best I came up with for
* current state of node-webkit Window API (v0.7.3 and later).
*
* Known issues:
* - Unmaximization not always sets the window (x, y) in the lastly used coordinates.
* - Unmaximization animation sometimes looks wierd.
* - Extra height added to window, at least in linux x64 gnome-shell env. It seems that
* when we read height then it returns it with window frame, but if we resize window
* then it applies dimensions only to internal document without external frame.
* Need to test in other environments with different visual themes.
*
* Change log:
* 2013-12-01
* - Workaround of extra height in gnome-shell added.
*
* 2014-03-22
* - Repared workaround (from 2013-12-01) behaviour when use frameless window.
* Now it works correctly.
* 2014-10-02
* - Fixed cannot set windowState of null error when attempting to set localStorage
*
*/
var gui = require('nw.gui');
var win = gui.Window.get();
var winState;
var currWinMode;
var resizeTimeout;
var isMaximizationEvent = false;
// extra height added in linux x64 gnome-shell env, use it as workaround
var deltaHeight = (function () {
// use deltaHeight only in windows with frame enabled
if (gui.App && gui.App.manifest && gui.App.manifest.window && gui.App.manifest.window.frame) return true; else return 'disabled';
})();
function initWindowState() {
winState = JSON.parse(localStorage.windowState || 'null');
if (winState) {
currWinMode = winState.mode;
if (currWinMode === 'maximized') {
win.maximize();
} else {
restoreWindowState();
}
} else {
currWinMode = 'normal';
if (deltaHeight !== 'disabled') deltaHeight = 0;
dumpWindowState();
}
win.show();
}
function dumpWindowState() {
if (!winState) {
winState = {};
}
// we don't want to save minimized state, only maximized or normal
if (currWinMode === 'maximized') {
winState.mode = 'maximized';
} else {
winState.mode = 'normal';
}
// when window is maximized you want to preserve normal
// window dimensions to restore them later (even between sessions)
if (currWinMode === 'normal') {
winState.x = win.x;
winState.y = win.y;
winState.width = win.width;
winState.height = win.height;
// save delta only of it is not zero
if (deltaHeight !== 'disabled' && deltaHeight !== 0 && currWinMode !== 'maximized') {
winState.deltaHeight = deltaHeight;
}
}
}
function restoreWindowState() {
// deltaHeight already saved, so just restore it and adjust window height
if (deltaHeight !== 'disabled' && typeof winState.deltaHeight !== 'undefined') {
deltaHeight = winState.deltaHeight
winState.height = winState.height - deltaHeight
}
win.resizeTo(winState.width, winState.height);
win.moveTo(winState.x, winState.y);
}
function saveWindowState() {
dumpWindowState();
localStorage['windowState'] = JSON.stringify(winState);
}
initWindowState();
win.on('maximize', function () {
isMaximizationEvent = true;
currWinMode = 'maximized';
});
win.on('unmaximize', function () {
currWinMode = 'normal';
restoreWindowState();
});
win.on('minimize', function () {
currWinMode = 'minimized';
});
win.on('restore', function () {
currWinMode = 'normal';
});
win.window.addEventListener('resize', function () {
// resize event is fired many times on one resize action,
// this hack with setTiemout forces it to fire only once
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(function () {
// on MacOS you can resize maximized window, so it's no longer maximized
if (isMaximizationEvent) {
// first resize after maximization event should be ignored
isMaximizationEvent = false;
} else {
if (currWinMode === 'maximized') {
currWinMode = 'normal';
}
}
// there is no deltaHeight yet, calculate it and adjust window size
if (deltaHeight !== 'disabled' && deltaHeight === false) {
deltaHeight = win.height - winState.height;
// set correct size
if (deltaHeight !== 0) {
win.resizeTo(winState.width, win.height - deltaHeight);
}
}
dumpWindowState();
}, 500);
}, false);
win.on('close', function () {
try {
saveWindowState();
} catch(err) {
console.log("winstateError: " + err);
}
this.close(true);
});