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

Unified Diff: ui/android/view_android.cc

Issue 2595263002: Introduce ViewRoot forwarding input/view events to native (Closed)
Patch Set: rebased & ViewAndroud::Bounds 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 b219487915a8b1ebb59a3990e91e9e0e03d22cd8..04e5e0d608f0823c5e1cd937b74ca629d1ccf921 100644
--- a/ui/android/view_android.cc
+++ b/ui/android/view_android.cc
@@ -9,6 +9,7 @@
#include "base/android/jni_android.h"
#include "cc/layers/layer.h"
#include "jni/ViewAndroidDelegate_jni.h"
+#include "ui/android/view_client.h"
#include "ui/android/window_android.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
@@ -18,6 +19,7 @@ namespace ui {
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
+// ViewAndroid::ScopedAndroidView
ViewAndroid::ScopedAnchorView::ScopedAnchorView(
JNIEnv* env,
const JavaRef<jobject>& jview,
@@ -68,34 +70,53 @@ ViewAndroid::ScopedAnchorView::view() const {
return view_.get(env);
}
-ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate)
- : parent_(nullptr)
- , delegate_(base::android::AttachCurrentThread(),
- delegate.obj()) {}
+// ViewAndroid::Bounds
+void ViewAndroid::Bounds::SetBounds(const gfx::Point& origin,
+ int width,
+ int height) {
+ origin_.set_x(origin.x());
+ origin_.set_y(origin.y());
+ width_ = width;
+ height_ = height;
+}
+
+bool ViewAndroid::Bounds::IsInBounds(const MotionEventAndroid& event) {
+ bool width_matched = (width_ == Bounds::kMatchParent) ? true :
+ origin_.x() <= event.GetX(0) && event.GetX(0) < origin_.x() + width_;
+ bool height_matched = (height_ == Bounds::kMatchParent) ? true:
+ origin_.y() <= event.GetY(0) && event.GetY(0) < (origin_.y() + height_);
-ViewAndroid::ViewAndroid() : parent_(nullptr) {}
+ return width_matched && height_matched;
+}
+
+// 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);
+ children_.push_front(child);
if (child->parent_)
child->RemoveFromParent();
child->parent_ = this;
@@ -106,6 +127,17 @@ void ViewAndroid::RemoveFromParent() {
parent_->RemoveChild(this);
}
+void ViewAndroid::SetBounds(const gfx::Point& origin, int width, int height) {
+ bounds_.SetBounds(origin, width, height);
+ for (auto& child: children_) {
+ child->SetBounds(origin, Bounds::kMatchParent, Bounds::kMatchParent);
boliu 2017/01/17 16:17:28 this makes no sense to me, why are children automa
Jinsuk Kim 2017/01/17 23:32:47 VA for RWHVA is supposed to have the same layout a
+ }
+}
+
+bool ViewAndroid::IsInBounds(const MotionEventAndroid& event) {
+ return bounds_.IsInBounds(event);
+}
+
ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
if (delegate.is_null())
@@ -166,6 +198,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());
@@ -176,4 +216,18 @@ bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
jimage);
}
+bool ViewAndroid::OnTouchEventInternal(const MotionEventAndroid& event,
+ bool is_touch_handle_event) {
+ if (client_ && IsInBounds(event)) {
+ if (client_->OnTouchEvent(event, is_touch_handle_event))
+ return true;
+ }
+
+ for (auto& child: children_) {
+ if (child->OnTouchEventInternal(event, is_touch_handle_event))
boliu 2017/01/17 16:17:28 when recursing, need to translate the event by -or
Jinsuk Kim 2017/01/17 23:32:47 As I mentioned in the TODO, origin remains zero at
+ return true;
+ }
+ return false;
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698