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

Unified Diff: ui/android/view_android.cc

Issue 2645353004: ViewRoot class for event forwarding on Android (Closed)
Patch Set: fix tests 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..78714b25da60364895ab8a66a42f05ac34cbbccf 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,49 @@ 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) {
+ rect_.SetRect(origin.x(), origin.y(), width, height);
+}
+
+bool ViewAndroid::Bounds::IsInBounds(const MotionEventData& event) {
+ bool width_matched = (rect_.width() == Bounds::kMatchParent) ?
boliu 2017/01/24 23:47:12 this only holds during the root to child recursion
Jinsuk Kim 2017/01/25 02:34:33 Removed the method and check it directly in the ca
+ true : rect_.x() <= event.GetX() && event.GetX() < rect_.right();
+ bool height_matched = (rect_.height() == Bounds::kMatchParent) ?
+ true : rect_.y() <= event.GetY() && event.GetY() < rect_.bottom();
+ return width_matched && height_matched;
+}
-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);
+ children_.push_front(child);
boliu 2017/01/24 23:47:12 why push front?
Jinsuk Kim 2017/01/25 02:34:33 This is for newly added view to go to front among
if (child->parent_)
child->RemoveFromParent();
child->parent_ = this;
@@ -106,6 +122,14 @@ void ViewAndroid::RemoveFromParent() {
parent_->RemoveChild(this);
}
+void ViewAndroid::SetBounds(const gfx::Point& origin, int width, int height) {
+ bounds_.SetBounds(origin, width, height);
+}
+
+bool ViewAndroid::IsInBounds(const MotionEventData& event) {
+ return bounds_.IsInBounds(event);
+}
+
ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
if (delegate.is_null())
@@ -116,15 +140,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 +203,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 +221,25 @@ bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
jimage);
}
+bool ViewAndroid::OnTouchEventInternal(const MotionEventData& event) {
+ if (!IsInBounds(event))
+ return false;
+
+ if (!children_.empty()) {
+ gfx::Point delta = bounds_.origin();
boliu 2017/01/24 23:47:12 this is not wrong, but just a bit odd while readin
Jinsuk Kim 2017/01/25 02:34:33 Would you elaborate? I *think* the event is transl
+ const MotionEventData& e =
boliu 2017/01/24 23:47:12 I'm blanking on whether this is safe c++.. if IsO
Jinsuk Kim 2017/01/25 02:34:33 Const guarantees its lifetime: https://herbsutter.
+ delta.IsOrigin() ? event : event.Offset(-delta.x(), -delta.y());
+
+ for (auto& child: children_) {
+ if (child->OnTouchEventInternal(e))
+ return true;
+ }
+ }
+
+ if (client_ && client_->OnTouchEvent(event))
boliu 2017/01/24 23:47:12 hmm, should this happen before looping over childr
Jinsuk Kim 2017/01/25 02:34:33 To the best of my knowledge, child views receives
+ return true;
+
+ return false;
+}
+
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698