From: Cheng Zhao Date: Fri, 24 May 2013 09:51:15 +0000 (+0800) Subject: Add BrowserWindow.focusOnWebView and blurWebView. X-Git-Tag: v0.1.0~139 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=edf2e84895720cc4b6e0d39e01696b50df78e580;p=electron.git Add BrowserWindow.focusOnWebView and blurWebView. --- diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 7e131f29..df6bac08 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 83fa056a..62864cdc 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 a4effd97..6f402d79 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 fafdda0b..41db8968 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 ec34dde1..ea20ef92 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 f4091d22..e91e6e29 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 f21ca0df..e5bb0471 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