Skip to content

Commit 9543046

Browse files
committed
newwin: focus and blur event support
1 parent e0552b7 commit 9543046

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/resources/api_nw_newwin.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ NWWindow.prototype.on = function (event, callback, record) {
159159
function wrap(cb) {
160160
var fn = (cb || callback).bind(self);
161161
fn.listener = callback;
162+
callback.__nw_cb = fn;
162163
return fn;
163164
}
164165

@@ -168,10 +169,23 @@ NWWindow.prototype.on = function (event, callback, record) {
168169
}
169170
switch (event) {
170171
case 'focus':
171-
this.appWindow.contentWindow.onfocus = wrap();
172+
var cbf = wrap(function(windowId) {
173+
if (self.cWindow.id !== windowId)
174+
return;
175+
callback.call(self);
176+
});
177+
chrome.windows.onFocusChanged.addListener(cbf);
172178
break;
173179
case 'blur':
174-
this.appWindow.contentWindow.onblur = wrap();
180+
this.cWindow = currentNWWindowInternal.getCurrent({'populate': true});
181+
var cbf = wrap(function(windowId) {
182+
if (self.cWindow.id === windowId)
183+
return;
184+
if (!self.cWindow.focused)
185+
return;
186+
callback.call(self);
187+
});
188+
chrome.windows.onFocusChanged.addListener(cbf);
175189
break;
176190
case 'loaded':
177191
var g = wrap(function(tabId, changeInfo, tab) {
@@ -234,14 +248,14 @@ NWWindow.prototype.on = function (event, callback, record) {
234248
return this; //return early
235249
break;
236250
}
237-
if (appWinEventsMap.hasOwnProperty(event)) {
238-
this.appWindow[appWinEventsMap[event]].addListener(wrap());
239-
return this;
240-
}
241-
if (nwWinEventsMap.hasOwnProperty(event)) {
242-
this[nwWinEventsMap[event]].addListener(wrap());
243-
return this;
244-
}
251+
//if (appWinEventsMap.hasOwnProperty(event)) {
252+
// this.appWindow[appWinEventsMap[event]].addListener(wrap());
253+
// return this;
254+
//}
255+
//if (nwWinEventsMap.hasOwnProperty(event)) {
256+
// this[nwWinEventsMap[event]].addListener(wrap());
257+
// return this;
258+
//}
245259
return this;
246260
};
247261
NWWindow.prototype.removeListener = function (event, callback) {
@@ -271,12 +285,8 @@ NWWindow.prototype.removeListener = function (event, callback) {
271285
}
272286
switch (event) {
273287
case 'focus':
274-
if (this.appWindow.contentWindow.onfocus && this.appWindow.contentWindow.onfocus.listener === callback)
275-
this.appWindow.contentWindow.onfocus = null;
276-
break;
277288
case 'blur':
278-
if (this.appWindow.contentWindow.onblur && this.appWindow.contentWindow.onblur.listener === callback)
279-
this.appWindow.contentWindow.onblur = null;
289+
chrome.windows.onFocusChanged.removeListener(callback.__nw_cb);
280290
break;
281291
}
282292
return this;

0 commit comments

Comments
 (0)