diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 7e131f29c8c..df6bac08be5 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -401,6 +401,24 @@ v8::Handle Window::CloseDevTools(const v8::Arguments &args) { return v8::Undefined(); } +// static +v8::Handle Window::FocusOnWebView(const v8::Arguments &args) { + UNWRAP_WINDOW_AND_CHECK; + + self->window_->FocusOnWebView(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::BlurWebView(const v8::Arguments &args) { + UNWRAP_WINDOW_AND_CHECK; + + self->window_->BlurWebView(); + + return v8::Undefined(); +} + // static v8::Handle Window::GetPageTitle(const v8::Arguments &args) { UNWRAP_WINDOW_AND_CHECK; @@ -626,6 +644,8 @@ void Window::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "isKiosk", IsKiosk); NODE_SET_PROTOTYPE_METHOD(t, "openDevTools", OpenDevTools); NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools", CloseDevTools); + NODE_SET_PROTOTYPE_METHOD(t, "focusOnWebView", FocusOnWebView); + NODE_SET_PROTOTYPE_METHOD(t, "blurWebView", BlurWebView); NODE_SET_PROTOTYPE_METHOD(t, "getPageTitle", GetPageTitle); NODE_SET_PROTOTYPE_METHOD(t, "isLoading", IsLoading); diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index 83fa056a627..62864cdc5b6 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -74,6 +74,8 @@ class Window : public EventEmitter, static v8::Handle IsKiosk(const v8::Arguments &args); static v8::Handle OpenDevTools(const v8::Arguments &args); static v8::Handle CloseDevTools(const v8::Arguments &args); + static v8::Handle FocusOnWebView(const v8::Arguments &args); + static v8::Handle BlurWebView(const v8::Arguments &args); // APIs for WebContents. static v8::Handle GetPageTitle(const v8::Arguments &args); diff --git a/browser/native_window.cc b/browser/native_window.cc index a4effd97234..6f402d79b03 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -132,6 +132,14 @@ void NativeWindow::CloseDevTools() { inspectable_web_contents()->GetView()->CloseDevTools(); } +void NativeWindow::FocusOnWebView() { + GetWebContents()->GetRenderViewHost()->Focus(); +} + +void NativeWindow::BlurWebView() { + GetWebContents()->GetRenderViewHost()->Blur(); +} + void NativeWindow::CloseWebContents() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, @@ -162,6 +170,10 @@ void NativeWindow::NotifyWindowClosed() { WindowList::RemoveWindow(this); } +void NativeWindow::NotifyWindowBlur() { + FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur()); +} + // Window opened by window.open. void NativeWindow::WebContentsCreated( content::WebContents* source_contents, @@ -207,11 +219,11 @@ bool NativeWindow::CanOverscrollContent() const { } void NativeWindow::ActivateContents(content::WebContents* contents) { - GetWebContents()->GetRenderViewHost()->Focus(); + FocusOnWebView(); } void NativeWindow::DeactivateContents(content::WebContents* contents) { - GetWebContents()->GetRenderViewHost()->Blur(); + BlurWebView(); } void NativeWindow::MoveContents(content::WebContents* source, diff --git a/browser/native_window.h b/browser/native_window.h index fafdda0b320..41db89680c4 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -95,6 +95,8 @@ class NativeWindow : public content::WebContentsDelegate, virtual bool IsClosed() const { return is_closed_; } virtual void OpenDevTools(); virtual void CloseDevTools(); + virtual void FocusOnWebView(); + virtual void BlurWebView(); // The same with closing a tab in a real browser. // @@ -120,6 +122,7 @@ class NativeWindow : public content::WebContentsDelegate, } void NotifyWindowClosed(); + void NotifyWindowBlur(); // Implementations of content::WebContentsDelegate. virtual void WebContentsCreated(content::WebContents* source_contents, diff --git a/browser/native_window_mac.h b/browser/native_window_mac.h index ec34dde1ab8..ea20ef92ee0 100644 --- a/browser/native_window_mac.h +++ b/browser/native_window_mac.h @@ -54,6 +54,8 @@ class NativeWindowMac : public NativeWindow { NSWindow*& window() { return window_; } + void NotifyWindowBlur() { NativeWindow::NotifyWindowBlur(); } + protected: void SetNonLionFullscreen(bool fullscreen); diff --git a/browser/native_window_mac.mm b/browser/native_window_mac.mm index f4091d2238d..e91e6e298fd 100644 --- a/browser/native_window_mac.mm +++ b/browser/native_window_mac.mm @@ -20,6 +20,7 @@ #include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" +#include "content/public/browser/render_view_host.h" @interface AtomNSWindowDelegate : NSObject { @private @@ -36,7 +37,11 @@ return self; } -- (void)windowWillClose:(NSNotification *)notification { +- (void)windowDidResignKey:(NSNotification*)notification { + shell_->NotifyWindowBlur(); +} + +- (void)windowWillClose:(NSNotification*)notification { shell_->window() = nil; [self autorelease]; } diff --git a/browser/native_window_observer.h b/browser/native_window_observer.h index f21ca0dff29..e5bb0471622 100644 --- a/browser/native_window_observer.h +++ b/browser/native_window_observer.h @@ -22,6 +22,9 @@ class NativeWindowObserver { // Called when the window is closed. virtual void OnWindowClosed() {} + + // Called when window loses focus. + virtual void OnWindowBlur() {} }; } // namespace atom