diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 401cc66c73cc..869ce7571fab 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -95,6 +95,10 @@ void Window::OnWindowBlur() { Emit("blur"); } +void Window::OnWindowFocus() { + Emit("focus"); +} + void Window::OnRendererUnresponsive() { Emit("unresponsive"); } diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 239673672d2b..be74f14b95a3 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -53,6 +53,7 @@ class Window : public mate::EventEmitter, virtual void WillCloseWindow(bool* prevent_default) OVERRIDE; virtual void OnWindowClosed() OVERRIDE; virtual void OnWindowBlur() OVERRIDE; + virtual void OnWindowFocus() OVERRIDE; virtual void OnRendererUnresponsive() OVERRIDE; virtual void OnRendererResponsive() OVERRIDE; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 558778029e5b..3fe3cb4d380a 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -368,6 +368,10 @@ void NativeWindow::NotifyWindowBlur() { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur()); } +void NativeWindow::NotifyWindowFocus() { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowFocus()); +} + // In atom-shell all reloads and navigations started by renderer process would // be redirected to this method, so we can have precise control of how we // would open the url (in our case, is to restart the renderer process). See diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 7c2b60806713..26fe94abd1a3 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -177,6 +177,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // related notifications. void NotifyWindowClosed(); void NotifyWindowBlur(); + void NotifyWindowFocus(); void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); diff --git a/atom/browser/native_window_gtk.cc b/atom/browser/native_window_gtk.cc index 0b49b0420c7f..364947ec493f 100644 --- a/atom/browser/native_window_gtk.cc +++ b/atom/browser/native_window_gtk.cc @@ -492,6 +492,11 @@ gboolean NativeWindowGtk::OnWindowDeleteEvent(GtkWidget* widget, return TRUE; } +gboolean NativeWindowGtk::OnFocusIn(GtkWidget* window, GdkEventFocus*) { + NotifyWindowFocus(); + return FALSE; +} + gboolean NativeWindowGtk::OnFocusOut(GtkWidget* window, GdkEventFocus*) { NotifyWindowBlur(); return FALSE; diff --git a/atom/browser/native_window_gtk.h b/atom/browser/native_window_gtk.h index d73381de3382..be5ff6914c0b 100644 --- a/atom/browser/native_window_gtk.h +++ b/atom/browser/native_window_gtk.h @@ -101,6 +101,7 @@ class NativeWindowGtk : public NativeWindow, CHROMEGTK_CALLBACK_1(NativeWindowGtk, gboolean, OnWindowDeleteEvent, GdkEvent*); + CHROMEGTK_CALLBACK_1(NativeWindowGtk, gboolean, OnFocusIn, GdkEventFocus*); CHROMEGTK_CALLBACK_1(NativeWindowGtk, gboolean, OnFocusOut, GdkEventFocus*); CHROMEGTK_CALLBACK_1(NativeWindowGtk, gboolean, OnWindowState, GdkEventWindowState*); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index e8da6b523e11..a34ec9c7df9c 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -48,6 +48,10 @@ static const CGFloat kAtomWindowCornerRadius = 4.0; acceptsFirstMouse_ = accept; } +- (void)windowDidBecomeMain:(NSNotification*)notification { + shell_->NotifyWindowFocus(); +} + - (void)windowDidResignMain:(NSNotification*)notification { shell_->NotifyWindowBlur(); } diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 422d7d72577e..33a1505a99b3 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -26,6 +26,9 @@ class NativeWindowObserver { // Called when window loses focus. virtual void OnWindowBlur() {} + // Called when window gains focus. + virtual void OnWindowFocus() {} + // Called when renderer is hung. virtual void OnRendererUnresponsive() {} diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 384b6e2a7129..f29e21848810 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -134,6 +134,10 @@ Emitted when the unresponsive web page becomes responsive again. Emitted when window loses focus. +### Event: 'focus' + +Emitted when window gains focus. + ### Class Method: BrowserWindow.getAllWindows() Returns an array of all opened browser windows.