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

Unified Diff: ui/android/view_android.cc

Issue 2645353004: ViewRoot class for event forwarding on Android (Closed)
Patch Set: comments Created 3 years, 11 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 6c55a7bf9df724ce0eea47170c138cd146ba2d85..e8c9e6ed9c0bf246b1dbfb5269094ec2b9ba2d54 100644
--- a/ui/android/view_android.cc
+++ b/ui/android/view_android.cc
@@ -18,6 +18,7 @@ namespace ui {
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
+// ViewAndroid::ScopedAndroidView
ViewAndroid::ScopedAnchorView::ScopedAnchorView(
JNIEnv* env,
const JavaRef<jobject>& jview,
@@ -68,34 +69,35 @@ ViewAndroid::ScopedAnchorView::view() const {
return view_.get(env);
}
-ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate)
- : parent_(nullptr)
- , delegate_(base::android::AttachCurrentThread(),
- delegate.obj()) {}
-
-ViewAndroid::ViewAndroid() : parent_(nullptr) {}
+// ViewAndroid
+ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr),
+ client_(client) {}
+ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {}
ViewAndroid::~ViewAndroid() {
RemoveFromParent();
- for (std::list<ViewAndroid*>::iterator it = children_.begin();
- it != children_.end(); it++) {
- DCHECK_EQ((*it)->parent_, this);
- (*it)->parent_ = nullptr;
+ for (auto& child : children_) {
+ DCHECK_EQ(child->parent_, this);
+ child->parent_ = nullptr;
}
}
void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) {
+ // A ViewAndroid may have its own delegate or otherwise will
+ // use the next available parent's delegate.
JNIEnv* env = base::android::AttachCurrentThread();
delegate_ = JavaObjectWeakGlobalRef(env, delegate);
}
void ViewAndroid::AddChild(ViewAndroid* child) {
DCHECK(child);
+ DCHECK(!child->IsViewRoot()); // ViewRoot cannot be a child.
DCHECK(std::find(children_.begin(), children_.end(), child) ==
children_.end());
- children_.push_back(child);
+ // The new child goes to front of the list of siblings.
+ children_.push_front(child);
boliu 2017/02/05 19:20:23 I think we should keep this push_back to match cc:
Jinsuk Kim 2017/02/06 02:23:44 Done. Hit testing is reversed order is done in OnT
if (child->parent_)
child->RemoveFromParent();
child->parent_ = this;
@@ -106,6 +108,11 @@ void ViewAndroid::RemoveFromParent() {
parent_->RemoveChild(this);
}
+void ViewAndroid::SetLayout(const gfx::Rect& bounds, bool match_parent) {
+ bounds_.SetRect(bounds.x(), bounds.y(), bounds.width(), bounds.height());
+ match_parent_ = match_parent;
+}
+
ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
if (delegate.is_null())
@@ -116,15 +123,19 @@ ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate);
}
+float ViewAndroid::GetDipScale() {
+ return display::Screen::GetScreen()
+ ->GetDisplayNearestWindow(this)
+ .device_scale_factor();
+}
+
void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor,
const gfx::RectF& bounds) {
ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
if (delegate.is_null())
return;
- float scale = display::Screen::GetScreen()
- ->GetDisplayNearestWindow(this)
- .device_scale_factor();
+ float scale = GetDipScale();
int left_margin = std::round(bounds.x() * scale);
int top_margin = std::round((content_offset().y() + bounds.y()) * scale);
JNIEnv* env = base::android::AttachCurrentThread();
@@ -175,6 +186,14 @@ void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
layer_ = layer;
}
+ViewAndroid* ViewAndroid::GetViewRoot() {
+ return parent_ ? parent_->GetViewRoot() : nullptr;
+}
+
+bool ViewAndroid::IsViewRoot() {
+ return GetViewRoot() == this;
+}
+
bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
const JavaRef<jobject>& jimage) {
ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
@@ -185,4 +204,24 @@ bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
jimage);
}
+bool ViewAndroid::OnTouchEventInternal(const MotionEventData& event) {
+ if (!children_.empty()) {
+ gfx::Point delta = bounds_.origin();
+ const MotionEventData& e =
+ delta.IsOrigin() ? event : event.Offset(-delta.x(), -delta.y());
+
+ for (auto& child: children_) {
+ if (child->match_parent_ || child->bounds_.Contains(e.GetX(), e.GetY())) {
+ if (child->OnTouchEventInternal(e))
+ return true;
+ }
+ }
+ }
+
+ if (client_ && client_->OnTouchEvent(event))
+ return true;
+
+ return false;
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698