@@ -159,6 +159,7 @@ NWWindow.prototype.on = function (event, callback, record) {
159
159
function wrap ( cb ) {
160
160
var fn = ( cb || callback ) . bind ( self ) ;
161
161
fn . listener = callback ;
162
+ callback . __nw_cb = fn ;
162
163
return fn ;
163
164
}
164
165
@@ -168,10 +169,23 @@ NWWindow.prototype.on = function (event, callback, record) {
168
169
}
169
170
switch ( event ) {
170
171
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 ) ;
172
178
break ;
173
179
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 ) ;
175
189
break ;
176
190
case 'loaded' :
177
191
var g = wrap ( function ( tabId , changeInfo , tab ) {
@@ -234,14 +248,14 @@ NWWindow.prototype.on = function (event, callback, record) {
234
248
return this ; //return early
235
249
break ;
236
250
}
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
+ // }
245
259
return this ;
246
260
} ;
247
261
NWWindow . prototype . removeListener = function ( event , callback ) {
@@ -271,12 +285,8 @@ NWWindow.prototype.removeListener = function (event, callback) {
271
285
}
272
286
switch ( event ) {
273
287
case 'focus' :
274
- if ( this . appWindow . contentWindow . onfocus && this . appWindow . contentWindow . onfocus . listener === callback )
275
- this . appWindow . contentWindow . onfocus = null ;
276
- break ;
277
288
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 ) ;
280
290
break ;
281
291
}
282
292
return this ;
0 commit comments