From fcc1f4d7ed1fd0978e68f72c405c8ee5f179e519 Mon Sep 17 00:00:00 2001 From: Arek Sredzki Date: Tue, 8 Mar 2016 11:11:17 -0800 Subject: [PATCH] Finalized browser-window show & hide events, added tests & fixed os x implementation --- atom/browser/native_window.cc | 8 +++++ atom/browser/native_window_mac.mm | 15 ++++++++++ lib/browser/api/browser-window.js | 10 +++---- spec/api-browser-window-spec.js | 49 ++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 98b3d725a9a..56cfdea54c5 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -428,6 +428,14 @@ void NativeWindow::NotifyWindowFocus() { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowFocus()); } +void NativeWindow::NotifyWindowShow() { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowShow()); +} + +void NativeWindow::NotifyWindowHide() { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowHide()); +} + void NativeWindow::NotifyWindowMaximize() { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowMaximize()); } diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 6bbd9fd5001..a8f7bc5c168 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -79,6 +79,21 @@ bool ScopedDisableResize::disable_resize_ = false; return self; } +- (void)windowDidChangeOcclusionState:(NSNotification *)notification { + // notification.object is the window that changed its state. + // It's safe to use self.window instead if you don't assign one delegate to many windows + NSWindow *window = notification.object; + + // check occlusion binary flag + if (window.occlusionState & NSWindowOcclusionStateVisible) { + // The app is visible + shell_->NotifyWindowShow(); + } else { + // The app is not visible + shell_->NotifyWindowHide(); + } +} + - (void)windowDidBecomeMain:(NSNotification*)notification { content::WebContents* web_contents = shell_->web_contents(); if (!web_contents) diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 139659c2ea2..1d2f665e5d0 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -32,7 +32,7 @@ BrowserWindow.prototype._init = function() { // window.resizeTo(...) // window.moveTo(...) - this.webContents.on('move', (event, title) => { + this.webContents.on('move', (event, size) => { return this.setBounds(size); }); @@ -80,16 +80,16 @@ BrowserWindow.prototype._init = function() { // Evented visibilityState changes this.on('show', () => { - return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', true, this.isMinimized()); + return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized()); }); this.on('hide', () => { - return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', false, this.isMinimized()); + return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized()); }); this.on('minimize', () => { - return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), true); + return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized()); }); this.on('restore', () => { - return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), false); + return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized()); }); // Notify the creation of the window. diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 731ae1cb86f..9a6be6865da 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -120,14 +120,55 @@ describe('browser-window module', function() { }); describe('BrowserWindow.show()', function() { - it('should focus on window', function() { - if (isCI) { - return; - } + if (isCI) { + return; + } + it('should focus on window', function() { w.show(); assert(w.isFocused()); }); + + it('should make the window visible', function() { + w.show(); + assert(w.isVisible()); + }); + + it('emits when window is shown', function(done) { + this.timeout(10000); + w.once('show', function() { + assert.equal(w.isVisible(), true); + done(); + }); + w.show(); + }); + }); + + describe('BrowserWindow.hide()', function() { + if (isCI) { + return; + } + + it('should defocus on window', function() { + w.hide(); + assert(!w.isFocused()); + }); + + it('should make the window not visible', function() { + w.show(); + w.hide(); + assert(!w.isVisible()); + }); + + it('emits when window is hidden', function(done) { + this.timeout(10000); + w.show(); + w.once('hide', function() { + assert.equal(w.isVisible(), false); + done(); + }); + w.hide(); + }); }); describe('BrowserWindow.showInactive()', function() {