Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Unified Diff: ui/android/view_android.cc

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: rebased Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/android/view_android.cc
diff --git a/ui/android/view_android.cc b/ui/android/view_android.cc
index 1d5b146653458a83b93c58810f743f6df78e5c46..9b1e47a797a77b9e5fa8257a2b0bfb74ee17ff53 100644
--- a/ui/android/view_android.cc
+++ b/ui/android/view_android.cc
@@ -10,9 +10,10 @@
#include "base/android/jni_string.h"
#include "cc/layers/layer.h"
#include "jni/ViewAndroidDelegate_jni.h"
+#include "ui/android/event_handler.h"
+#include "ui/android/view_client.h"
#include "ui/android/window_android.h"
-#include "ui/display/display.h"
-#include "ui/display/screen.h"
+#include "ui/base/layout.h"
#include "url/gurl.h"
namespace ui {
@@ -71,11 +72,12 @@ ViewAndroid::ScopedAnchorView::view() const {
return view_.get(env);
}
-ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate)
+ViewAndroid::ViewAndroid(ViewClient* view_client)
: parent_(nullptr),
- delegate_(base::android::AttachCurrentThread(), delegate.obj()) {}
+ client_(view_client),
+ dip_scale_(ui::GetScaleFactorForNativeView(this)) {}
-ViewAndroid::ViewAndroid() : parent_(nullptr) {}
+ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {}
ViewAndroid::~ViewAndroid() {
RemoveFromParent();
@@ -88,21 +90,48 @@ ViewAndroid::~ViewAndroid() {
}
void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) {
+ // A ViewAndroid may have its own delegate or otherwise will
David Trainor- moved to gerrit 2017/02/28 01:31:23 comment looks wrapped too early.
Jinsuk Kim 2017/02/28 06:56:05 Done.
+ // use the next available parent's delegate.
JNIEnv* env = base::android::AttachCurrentThread();
delegate_ = JavaObjectWeakGlobalRef(env, delegate);
}
+float ViewAndroid::GetDipScale() {
David Trainor- moved to gerrit 2017/02/28 01:31:23 dip_scale() in header?
Jinsuk Kim 2017/02/28 06:56:05 Turned to not caching the scale.
+ return dip_scale_;
+}
+
+EventHandler* ViewAndroid::CreateEventHandler() {
+ DCHECK(!parent_ || parent_ == GetWindowAndroid())
+ << "Only WebContentViewAndroid's ViewAndroid can have event handler.";
+ DCHECK(!event_handler_) << "EventHandler is already created";
+
+ event_handler_.reset(EventHandler::Create(this));
+ return event_handler_.get();
+}
+
void ViewAndroid::AddChild(ViewAndroid* child) {
DCHECK(child);
DCHECK(std::find(children_.begin(), children_.end(), child) ==
children_.end());
+ DCHECK(!ViewTreeHasEventHandler(child)) << "Child cannot have event handler";
+ // The new child goes to the top, which is the end of the list.
children_.push_back(child);
if (child->parent_)
child->RemoveFromParent();
child->parent_ = this;
}
+bool ViewAndroid::ViewTreeHasEventHandler(ViewAndroid* view) {
David Trainor- moved to gerrit 2017/02/28 01:31:23 "// static" above this.
Jinsuk Kim 2017/02/28 06:56:05 Done.
+ if (view->has_event_handler())
+ return true;
+ for (auto& child : view->children_) {
+ if (child->has_event_handler())
+ return true;
+ }
+ return false;
+}
+
void ViewAndroid::RemoveFromParent() {
if (parent_)
parent_->RemoveChild(this);
@@ -124,15 +153,12 @@ void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor,
if (delegate.is_null())
return;
- float scale = display::Screen::GetScreen()
- ->GetDisplayNearestWindow(this)
- .device_scale_factor();
- int left_margin = std::round(bounds.x() * scale);
- int top_margin = std::round((content_offset().y() + bounds.y()) * scale);
+ int left_margin = std::round(bounds.x() * dip_scale_);
+ int top_margin = std::round((content_offset().y() + bounds.y()) * dip_scale_);
JNIEnv* env = base::android::AttachCurrentThread();
Java_ViewAndroidDelegate_setViewPosition(
env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(),
- bounds.height(), scale, left_margin, top_margin);
+ bounds.height(), dip_scale_, left_margin, top_margin);
}
ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() {
@@ -227,4 +253,30 @@ void ViewAndroid::StartContentIntent(const GURL& content_url,
is_main_frame);
}
+bool ViewAndroid::OnTouchEvent(const MotionEventAndroid& event,
+ bool is_touch_handle_event) {
+ if (client_ && client_->OnTouchEvent(event, is_touch_handle_event))
+ return true;
+
+ for (auto it = children_.rbegin(); it != children_.rend(); ++it) {
+ if ((*it)->OnTouchEvent(event, is_touch_handle_event))
+ return true;
+ }
+
+ return false;
+}
+
+bool ViewAndroid::OnMouseEvent(const MotionEventAndroid& event,
+ int action_button) {
+ if (client_ && client_->OnMouseEvent(event, action_button))
+ return true;
+
+ for (auto it = children_.rbegin(); it != children_.rend(); ++it) {
+ if ((*it)->OnMouseEvent(event, action_button))
+ return true;
+ }
+
+ return false;
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698