]> khamidou — GitHub mirrors - electron.git/commitdiff
Add BrowserWindow.focusOnWebView and blurWebView.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 24 May 2013 09:51:15 +0000 (17:51 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 24 May 2013 09:51:15 +0000 (17:51 +0800)
browser/api/atom_api_window.cc
browser/api/atom_api_window.h
browser/native_window.cc
browser/native_window.h
browser/native_window_mac.h
browser/native_window_mac.mm
browser/native_window_observer.h

index 7e131f29c8c5a683f1102b5cb1178778eb596f89..df6bac08be587bb5eba036bc81a95ac32dad66ed 100644 (file)
@@ -401,6 +401,24 @@ v8::Handle<v8::Value> Window::CloseDevTools(const v8::Arguments &args) {
   return v8::Undefined();
 }
 
+// static
+v8::Handle<v8::Value> Window::FocusOnWebView(const v8::Arguments &args) {
+  UNWRAP_WINDOW_AND_CHECK;
+
+  self->window_->FocusOnWebView();
+
+  return v8::Undefined();
+}
+
+// static
+v8::Handle<v8::Value> Window::BlurWebView(const v8::Arguments &args) {
+  UNWRAP_WINDOW_AND_CHECK;
+
+  self->window_->BlurWebView();
+
+  return v8::Undefined();
+}
+
 // static
 v8::Handle<v8::Value> Window::GetPageTitle(const v8::Arguments &args) {
   UNWRAP_WINDOW_AND_CHECK;
@@ -626,6 +644,8 @@ void Window::Initialize(v8::Handle<v8::Object> 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);
index 83fa056a6279affc87f6f7ee92a57df465a4e536..62864cdc5b68641b229bd5f822a29ef85cbc14cc 100644 (file)
@@ -74,6 +74,8 @@ class Window : public EventEmitter,
   static v8::Handle<v8::Value> IsKiosk(const v8::Arguments &args);
   static v8::Handle<v8::Value> OpenDevTools(const v8::Arguments &args);
   static v8::Handle<v8::Value> CloseDevTools(const v8::Arguments &args);
+  static v8::Handle<v8::Value> FocusOnWebView(const v8::Arguments &args);
+  static v8::Handle<v8::Value> BlurWebView(const v8::Arguments &args);
 
   // APIs for WebContents.
   static v8::Handle<v8::Value> GetPageTitle(const v8::Arguments &args);
index a4effd97234bd26f6f899b9f3ebfc6fdec1ca0df..6f402d79b039b38b97271b36ce7450f20d5556de 100644 (file)
@@ -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,
index fafdda0b32030427cc45b1501eb367af65970143..41db89680c4296b8a76f15da1ae6449cb0f6b37e 100644 (file)
@@ -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,
index ec34dde1ab8fda6d626270e5b89e8f3738a135bc..ea20ef92ee03060ef372f624a0b094efad35c746 100644 (file)
@@ -54,6 +54,8 @@ class NativeWindowMac : public NativeWindow {
 
   NSWindow*& window() { return window_; }
 
+  void NotifyWindowBlur() { NativeWindow::NotifyWindowBlur(); }
+
  protected:
   void SetNonLionFullscreen(bool fullscreen);
 
index f4091d2238d597849b8d3fc33d941412cebcd41f..e91e6e298fd2843845afc7aba8640f13e16bc8a9 100644 (file)
@@ -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<NSWindowDelegate> {
  @private
   return self;
 }
 
-- (void)windowWillClose:(NSNotification *)notification {
+- (void)windowDidResignKey:(NSNotification*)notification {
+  shell_->NotifyWindowBlur();
+}
+
+- (void)windowWillClose:(NSNotification*)notification {
   shell_->window() = nil;
   [self autorelease];
 }
index f21ca0dff29d8444fdec504ae18a5763971f3c20..e5bb0471622564d1e83c3b2e78dd4a85662c97ee 100644 (file)
@@ -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