From c611eb061db4dadaac8d9e68a207ed0bf08d2524 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 22 Feb 2018 17:07:08 +0900 Subject: [PATCH] Move InputEventObserver to api::BrowserWindow --- atom/browser/api/atom_api_browser_window.cc | 28 ++++++++++++++--- atom/browser/api/atom_api_browser_window.h | 8 ++++- atom/browser/native_window.cc | 5 --- atom/browser/native_window.h | 1 - atom/browser/native_window_mac.h | 14 +-------- atom/browser/native_window_mac.mm | 34 --------------------- atom/browser/native_window_observer.h | 1 - 7 files changed, 32 insertions(+), 59 deletions(-) diff --git a/atom/browser/api/atom_api_browser_window.cc b/atom/browser/api/atom_api_browser_window.cc index caaaf8bfe156..0554a9b97aa6 100644 --- a/atom/browser/api/atom_api_browser_window.cc +++ b/atom/browser/api/atom_api_browser_window.cc @@ -172,6 +172,10 @@ void BrowserWindow::Init(v8::Isolate* isolate, // window's JS wrapper gets initialized. if (!parent.IsEmpty()) parent->child_windows_.Set(isolate, ID(), wrapper); + + auto* host = web_contents->web_contents()->GetRenderViewHost(); + if (host) + host->GetWidget()->AddInputEventObserver(this); } BrowserWindow::~BrowserWindow() { @@ -185,6 +189,26 @@ BrowserWindow::~BrowserWindow() { base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release()); } +void BrowserWindow::OnInputEvent(const blink::WebInputEvent& event) { + switch (event.GetType()) { + case blink::WebInputEvent::kGestureScrollBegin: + case blink::WebInputEvent::kGestureScrollUpdate: + case blink::WebInputEvent::kGestureScrollEnd: + Emit("scroll-touch-edge"); + break; + default: + break; + } +} + +void BrowserWindow::RenderViewHostChanged(content::RenderViewHost* old_host, + content::RenderViewHost* new_host) { + if (old_host) + old_host->GetWidget()->RemoveInputEventObserver(this); + if (new_host) + new_host->GetWidget()->AddInputEventObserver(this); +} + void BrowserWindow::RenderViewCreated( content::RenderViewHost* render_view_host) { if (!window_->transparent()) @@ -386,10 +410,6 @@ void BrowserWindow::OnWindowScrollTouchEnd() { Emit("scroll-touch-end"); } -void BrowserWindow::OnWindowScrollTouchEdge() { - Emit("scroll-touch-edge"); -} - void BrowserWindow::OnWindowSwipe(const std::string& direction) { Emit("swipe", direction); } diff --git a/atom/browser/api/atom_api_browser_window.h b/atom/browser/api/atom_api_browser_window.h index 6c4335079ca6..f5ba1c587c28 100644 --- a/atom/browser/api/atom_api_browser_window.h +++ b/atom/browser/api/atom_api_browser_window.h @@ -17,6 +17,7 @@ #include "atom/common/key_weak_map.h" #include "base/cancelable_callback.h" #include "base/memory/weak_ptr.h" +#include "content/public/browser/render_widget_host.h" #include "native_mate/persistent_dictionary.h" class GURL; @@ -37,6 +38,7 @@ class NativeWindow; namespace api { class BrowserWindow : public mate::TrackableObject, + public content::RenderWidgetHost::InputEventObserver, public content::WebContentsObserver, public ExtendedWebContentsObserver, public NativeWindowObserver { @@ -60,7 +62,12 @@ class BrowserWindow : public mate::TrackableObject, const mate::Dictionary& options); ~BrowserWindow() override; + // content::RenderWidgetHost::InputEventObserver: + void OnInputEvent(const blink::WebInputEvent& event) override; + // content::WebContentsObserver: + void RenderViewHostChanged(content::RenderViewHost* old_host, + content::RenderViewHost* new_host) override; void RenderViewCreated(content::RenderViewHost* render_view_host) override; void DidFirstVisuallyNonEmptyPaint() override; void BeforeUnloadDialogCancelled() override; @@ -90,7 +97,6 @@ class BrowserWindow : public mate::TrackableObject, void OnWindowMoved() override; void OnWindowScrollTouchBegin() override; void OnWindowScrollTouchEnd() override; - void OnWindowScrollTouchEdge() override; void OnWindowSwipe(const std::string& direction) override; void OnWindowSheetBegin() override; void OnWindowSheetEnd() override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 157a5bdd9702..ea11b91ee69d 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -530,11 +530,6 @@ void NativeWindow::NotifyWindowScrollTouchEnd() { observer.OnWindowScrollTouchEnd(); } -void NativeWindow::NotifyWindowScrollTouchEdge() { - for (NativeWindowObserver& observer : observers_) - observer.OnWindowScrollTouchEdge(); -} - void NativeWindow::NotifyWindowSwipe(const std::string& direction) { for (NativeWindowObserver& observer : observers_) observer.OnWindowSwipe(direction); diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 5a34e7dd9b86..5661f6237d17 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -259,7 +259,6 @@ class NativeWindow : public base::SupportsUserData, void NotifyWindowMoved(); void NotifyWindowScrollTouchBegin(); void NotifyWindowScrollTouchEnd(); - void NotifyWindowScrollTouchEdge(); void NotifyWindowSwipe(const std::string& direction); void NotifyWindowSheetBegin(); void NotifyWindowSheetEnd(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 64a816086b2c..a54e24b4668c 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -12,7 +12,6 @@ #include "atom/browser/native_window.h" #include "base/mac/scoped_nsobject.h" -#include "content/public/browser/render_widget_host.h" @class AtomNSWindow; @class AtomNSWindowDelegate; @@ -20,8 +19,7 @@ namespace atom { -class NativeWindowMac : public NativeWindow, - public content::RenderWidgetHost::InputEventObserver { +class NativeWindowMac : public NativeWindow { public: NativeWindowMac(brightray::InspectableWebContents* inspectable_web_contents, const mate::Dictionary& options, @@ -124,13 +122,6 @@ class NativeWindowMac : public NativeWindow, void UpdateDraggableRegions( const std::vector& regions) override; - // content::RenderWidgetHost::InputEventObserver: - void OnInputEvent(const blink::WebInputEvent& event) override; - - // content::WebContentsObserver: - void RenderViewHostChanged(content::RenderViewHost* old_host, - content::RenderViewHost* new_host) override; - // Set the attribute of NSWindow while work around a bug of zoom button. void SetStyleMask(bool on, NSUInteger flag); void SetCollectionBehavior(bool on, NSUInteger flag); @@ -163,9 +154,6 @@ class NativeWindowMac : public NativeWindow, void InstallView(); void UninstallView(); - void RegisterInputEventObserver(content::RenderViewHost* host); - void UnregisterInputEventObserver(content::RenderViewHost* host); - void SetRenderWidgetHostOpaque(bool opaque); base::scoped_nsobject window_; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index ed51d7219edb..72dd45728f15 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -1038,9 +1038,6 @@ NativeWindowMac::NativeWindowMac( // Set maximizable state last to ensure zoom button does not get reset // by calls to other APIs. SetMaximizable(maximizable); - - RegisterInputEventObserver( - web_contents->GetWebContents()->GetRenderViewHost()); } NativeWindowMac::~NativeWindowMac() { @@ -1869,25 +1866,6 @@ void NativeWindowMac::UpdateDraggableRegions( [window_ setMovableByWindowBackground:YES]; } -void NativeWindowMac::OnInputEvent(const blink::WebInputEvent& event) { - switch (event.GetType()) { - case blink::WebInputEvent::kGestureScrollBegin: - case blink::WebInputEvent::kGestureScrollUpdate: - case blink::WebInputEvent::kGestureScrollEnd: - this->NotifyWindowScrollTouchEdge(); - break; - default: - break; - } -} - -void NativeWindowMac::RenderViewHostChanged( - content::RenderViewHost* old_host, - content::RenderViewHost* new_host) { - UnregisterInputEventObserver(old_host); - RegisterInputEventObserver(new_host); -} - std::vector NativeWindowMac::CalculateNonDraggableRegions( const std::vector& regions, int width, int height) { std::vector result; @@ -2016,18 +1994,6 @@ void NativeWindowMac::SetCollectionBehavior(bool on, NSUInteger flag) { SetMaximizable(was_maximizable); } -void NativeWindowMac::RegisterInputEventObserver( - content::RenderViewHost* host) { - if (host) - host->GetWidget()->AddInputEventObserver(this); -} - -void NativeWindowMac::UnregisterInputEventObserver( - content::RenderViewHost* host) { - if (host) - host->GetWidget()->RemoveInputEventObserver(this); -} - // static NativeWindow* NativeWindow::Create( brightray::InspectableWebContents* inspectable_web_contents, diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index d3c34cbe2212..27f5cb92f8a3 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -65,7 +65,6 @@ class NativeWindowObserver { virtual void OnWindowMoved() {} virtual void OnWindowScrollTouchBegin() {} virtual void OnWindowScrollTouchEnd() {} - virtual void OnWindowScrollTouchEdge() {} virtual void OnWindowSwipe(const std::string& direction) {} virtual void OnWindowSheetBegin() {} virtual void OnWindowSheetEnd() {}