Skip to content

Commit 515901b

Browse files
committed
newwin: support more events for window size
1 parent 6098672 commit 515901b

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/resources/api_nw_newwin.js

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ function getPlatform() {
5151

5252
var canSetVisibleOnAllWorkspaces = /(mac|linux)/.exec(getPlatform());
5353
var appWinEventsMap = {
54-
'minimize': 'onMinimized',
55-
'maximize': 'onMaximized',
56-
'restore': 'onRestored',
57-
'enter-fullscreen': 'onFullscreened',
5854
'closed': 'onClosed'
5955
};
6056

6157
var nwWinEventsMap = {
58+
'minimize': 'onMinimized',
59+
'maximize': 'onMaximized',
60+
'restore': 'onRestore',
61+
'enter-fullscreen': 'onFullscreen',
6262
'zoom': 'onZoom',
63-
'close': 'onClose'
63+
'close': 'onClose',
64+
'resize': 'onResized'
6465
};
6566

6667
var nwWrapEventsMap = {
@@ -70,7 +71,6 @@ var nwWrapEventsMap = {
7071
};
7172

7273
var wrapEventsMapNewWin = {
73-
'resize': 'onResize',
7474
'move': 'onMove',
7575
'focus': 'onFocusChanged',
7676
'blur': 'onFocusChanged'
@@ -110,6 +110,7 @@ nw_internal.registerCustomHook(function(bindingsAPI) {
110110
var currentNWWindowInternal = nw_internal.generate();
111111

112112
function NWWindow(cWindow) {
113+
var self = this;
113114
if (cWindow)
114115
this.cWindow = cWindow;
115116
else {
@@ -118,8 +119,33 @@ function NWWindow(cWindow) {
118119
console.error('The JavaScript context calling ' +
119120
'nw.Window.get() has no associated Browser window.');
120121
}
122+
123+
function updateWindowAttributes(w) {
124+
if (w.id !== self.cWindow.id)
125+
return;
126+
var oldState = self.cWindow.state;
127+
var oldWidth = self.cWindow.width;
128+
var oldHeight = self.cWindow.height;
129+
130+
self.cWindow.state = w.state;
131+
self.cWindow.width = w.width;
132+
self.cWindow.height = w.height;
133+
134+
if (oldState != 'minimized' && w.state == 'minimized') {
135+
dispatchEventIfExists(self, 'onMinimized');
136+
} else if (oldState != 'maximized' && w.state == 'maximized') {
137+
dispatchEventIfExists(self, 'onMaximized');
138+
} else if (oldState != 'fullscreen' && w.state == 'fullscreen') {
139+
dispatchEventIfExists(self, 'onFullscreen');
140+
} else if (oldState != 'normal' && w.state == 'normal') {
141+
dispatchEventIfExists(self, 'onRestore');
142+
} else if (oldWidth != w.width || oldHeight != w.height) {
143+
dispatchEventIfExists(self, 'onResized', [w.width, w.height]);
144+
}
145+
}
121146
console.log("cWindow id: " + this.cWindow.id);
122147
privates(this).menu = null;
148+
chrome.windows.onWindowChanged.addListener(updateWindowAttributes);
123149
}
124150

125151
forEach(currentNWWindowInternal, function(key, value) {
@@ -134,6 +160,11 @@ NWWindow.prototype.onDocumentStart = new Event("nw.Window.onDocumentStart");
134160
NWWindow.prototype.onDocumentEnd = new Event("nw.Window.onDocumentEnd");
135161
NWWindow.prototype.onZoom = new Event();
136162
NWWindow.prototype.onClose = new Event("nw.Window.onClose", undefined, {supportsFilters: true});
163+
NWWindow.prototype.onMinimized = new Event();
164+
NWWindow.prototype.onMaximized = new Event();
165+
NWWindow.prototype.onFullscreen = new Event();
166+
NWWindow.prototype.onResized = new Event();
167+
NWWindow.prototype.onRestore = new Event();
137168

138169
NWWindow.prototype.close = function (force) {
139170
currentNWWindowInternal.close(force, this.cWindow.id);
@@ -247,35 +278,14 @@ NWWindow.prototype.on = function (event, callback, record) {
247278
chrome.windows.onMove.addListener(k);
248279
return this; //return early
249280
break;
250-
case 'resize':
251-
var l = wrap(function(w) {
252-
if (w.id != self.cWindow.id)
253-
return;
254-
callback.call(self, w.width, w.height);
255-
});
256-
chrome.windows.onResize.addListener(l);
257-
return this; //return early
258-
break;
259281
}
260-
//if (appWinEventsMap.hasOwnProperty(event)) {
261-
// this.appWindow[appWinEventsMap[event]].addListener(wrap());
262-
// return this;
263-
//}
264-
//if (nwWinEventsMap.hasOwnProperty(event)) {
265-
// this[nwWinEventsMap[event]].addListener(wrap());
266-
// return this;
267-
//}
282+
if (nwWinEventsMap.hasOwnProperty(event)) {
283+
this[nwWinEventsMap[event]].addListener(wrap());
284+
return this;
285+
}
268286
return this;
269287
};
270288
NWWindow.prototype.removeListener = function (event, callback) {
271-
if (appWinEventsMap.hasOwnProperty(event)) {
272-
for (let l of this.appWindow[appWinEventsMap[event]].getListeners()) {
273-
if (l.callback.listener && l.callback.listener === callback) {
274-
this.appWindow[appWinEventsMap[event]].removeListener(l.callback);
275-
return this;
276-
}
277-
}
278-
}
279289
if (nwWinEventsMap.hasOwnProperty(event)) {
280290
for (let l of this[nwWinEventsMap[event]].getListeners()) {
281291
if (l.callback.listener && l.callback.listener === callback) {
@@ -305,20 +315,14 @@ NWWindow.prototype.removeListener = function (event, callback) {
305315

306316
NWWindow.prototype.removeAllListeners = function (event) {
307317
if (arguments.length === 0) {
308-
var obj = Object.assign({}, appWinEventsMap, nwWinEventsMap, nwWrapEventsMap);
318+
var obj = Object.assign({}, nwWinEventsMap, nwWrapEventsMap);
309319
var keys = Object.keys(obj);
310320
for (var i = 0, key; i < keys.length; ++i) {
311321
key = keys[i];
312322
this.removeAllListeners(key);
313323
}
314324
return this;
315325
}
316-
if (appWinEventsMap.hasOwnProperty(event)) {
317-
for (let l of this.appWindow[appWinEventsMap[event]].getListeners()) {
318-
this.appWindow[appWinEventsMap[event]].removeListener(l.callback);
319-
}
320-
return this;
321-
}
322326
if (nwWinEventsMap.hasOwnProperty(event)) {
323327
for (let l of this[nwWinEventsMap[event]].getListeners()) {
324328
this[nwWinEventsMap[event]].removeListener(l.callback);

0 commit comments

Comments
 (0)