Chromium Code Reviews| Index: ui/android/view_android.cc |
| diff --git a/ui/android/view_android.cc b/ui/android/view_android.cc |
| index 1d5b146653458a83b93c58810f743f6df78e5c46..2f15751db6aba04bccf24ba576736ebac451c0b6 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,10 @@ ViewAndroid::ScopedAnchorView::view() const { |
| return view_.get(env); |
| } |
| -ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) |
| - : parent_(nullptr), |
| - delegate_(base::android::AttachCurrentThread(), delegate.obj()) {} |
| +ViewAndroid::ViewAndroid(ViewClient* view_client) |
| + : parent_(nullptr), client_(view_client) {} |
| -ViewAndroid::ViewAndroid() : parent_(nullptr) {} |
| +ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| ViewAndroid::~ViewAndroid() { |
| RemoveFromParent(); |
| @@ -88,21 +88,66 @@ ViewAndroid::~ViewAndroid() { |
| } |
| 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); |
| } |
| +float ViewAndroid::GetDipScale() { |
| + return ui::GetScaleFactorForNativeView(this); |
| +} |
| + |
| +EventHandler* ViewAndroid::CreateEventHandler() { |
| + DCHECK(!ViewTreeHasEventHandler(GetViewRoot())) |
|
Khushal
2017/03/02 21:46:15
That feels like a lot of unnecessary complexity ju
boliu
2017/03/02 22:02:16
I asked for these checks. Imo it's a correctness g
Khushal
2017/03/02 22:08:59
What 2 sources are we worried about? The event met
boliu
2017/03/02 22:17:41
I want to make sure nothing can be receiving event
Khushal
2017/03/02 22:34:34
Is there a good reason for why we need to have a c
boliu
2017/03/02 22:41:50
Can keep doing that *within* ViewAndroids/ViewClie
Khushal
2017/03/02 23:11:36
So here is where my understanding of source. We ar
boliu
2017/03/02 23:16:06
Totally disagree with that assessment. Having any
Khushal
2017/03/03 00:37:55
Okay, if you know this has caused pain in the past
Jinsuk Kim
2017/03/03 06:29:21
Hid native EventHandler instance (owned by ViewAnd
|
| + << "Root of the ViewAndroid can have at most one handler."; |
| + event_handler_.reset(EventHandler::Create(this)); |
| + return event_handler_.get(); |
| +} |
| + |
| +ViewAndroid* ViewAndroid::GetViewRoot() { |
|
boliu
2017/03/02 20:58:14
So.. viewroot is the immediate children of windowa
Jinsuk Kim
2017/03/03 06:29:21
Removed GetViewRoot(). It was used for |ViewTreeHa
|
| + DCHECK(parent_) << "View is not attached to a tree yet."; |
|
Khushal
2017/03/02 21:46:15
nit: This doesn't really need to be a DCHECK. Just
Jinsuk Kim
2017/03/03 06:29:22
Removed this method.
|
| + |
| + // The root of VA tree is that of WCVA for now. |
| + return parent_ == GetWindowAndroid() ? this : nullptr; |
| +} |
| + |
| void ViewAndroid::AddChild(ViewAndroid* child) { |
| DCHECK(child); |
| DCHECK(std::find(children_.begin(), children_.end(), child) == |
| children_.end()); |
| + DCHECK(!ViewTreeHasEventHandler(child) || |
| + !ViewTreeHasEventHandler(GetViewRoot())) |
| + << "Only one event handler is allowed."; |
| + // 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; |
| } |
| +// static |
| +bool ViewAndroid::ViewTreeHasEventHandler(ViewAndroid* view) { |
| + if (view->has_event_handler()) |
| + return true; |
| + for (auto& child : view->children_) { |
| + if (child->has_event_handler()) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +void ViewAndroid::MoveToTop(ViewAndroid* child) { |
|
David Trainor- moved to gerrit
2017/03/03 23:04:34
MoveToFront feels more clear
Jinsuk Kim
2017/03/06 04:07:33
Done.
|
| + DCHECK(child); |
| + auto it = std::find(children_.begin(), children_.end(), child); |
| + DCHECK(it != children_.end()); |
| + |
| + // Top element is placed at the end of the list. |
| + if (*it != children_.back()) |
| + children_.splice(children_.rbegin().base(), children_, it); |
|
Khushal
2017/03/02 21:46:15
nit: children_.end() instead of children_.rbegin()
Jinsuk Kim
2017/03/03 06:29:22
Done.
|
| +} |
| + |
| void ViewAndroid::RemoveFromParent() { |
| if (parent_) |
| parent_->RemoveChild(this); |
| @@ -124,15 +169,13 @@ 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); |
| + float dip_scale = GetDipScale(); |
| + 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() { |
| @@ -177,6 +220,18 @@ void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| layer_ = layer; |
| } |
| +void ViewAndroid::SetLayout(int x, |
|
Khushal
2017/03/02 21:46:15
Do you want to split this into 2 methods: SetBound
Jinsuk Kim
2017/03/03 06:29:22
I haven't found a case that needs the splitting ye
boliu
2017/03/03 19:46:18
Actually, should not do split this. This is our ve
David Trainor- moved to gerrit
2017/03/03 23:04:34
The behavior of this feels a bit weird to me. mat
boliu
2017/03/03 23:10:27
We certainly do the same thing. Make a struct with
Khushal
2017/03/03 23:36:06
It looked a bit awkward for me to see an API where
Jinsuk Kim
2017/03/06 04:07:33
Added a struct to make match_parent and other para
|
| + int y, |
| + int width, |
| + int height, |
| + bool match_parent) { |
| + DCHECK(!match_parent || (x == 0 && y == 0 && width == 0 && height == 0)) |
| + << "Should give empty dimension when view layout matches its parent."; |
|
boliu
2017/03/02 20:58:14
this can be better still. like if I trigger this D
Jinsuk Kim
2017/03/03 06:29:21
Logged all the parameters.
|
| + origin_.SetPoint(x, y); |
| + size_.SetSize(width, height); |
| + match_parent_ = match_parent; |
| +} |
| + |
| bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| const JavaRef<jobject>& jimage) { |
| ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| @@ -227,4 +282,48 @@ void ViewAndroid::StartContentIntent(const GURL& content_url, |
| is_main_frame); |
| } |
| +bool ViewAndroid::OnTouchEvent(const MotionEventData& event) { |
| + if (!children_.empty()) { |
| + const MotionEventData& e = |
| + origin_.IsOrigin() ? event : event.Offset(-origin_.x(), -origin_.y()); |
| + |
| + for (auto it = children_.rbegin(); it != children_.rend(); ++it) { |
|
Khushal
2017/03/02 21:46:15
nit: for (auto* child : children_)?
boliu
2017/03/02 22:02:16
back to forward order is desired here. we modeled
Khushal
2017/03/02 22:08:59
Oh Duh, didn't notice that. Yeah, a comment for th
Jinsuk Kim
2017/03/03 06:29:22
Thanks for the suggestion. Reversed looks nicer. D
|
| + bool matched = (*it)->match_parent_; |
| + if (!matched) { |
| + gfx::Rect bound((*it)->origin_, (*it)->size_); |
| + matched = bound.Contains(e.GetX(), e.GetY()); |
|
boliu
2017/03/02 20:58:14
can we abstract out hit testing into its own metho
Jinsuk Kim
2017/03/03 06:29:22
Good idea. Done.
|
| + } |
| + if (matched && (*it)->OnTouchEvent(e)) |
| + return true; |
| + } |
| + } |
| + |
| + if (client_ && client_->OnTouchEvent(event)) |
|
Khushal
2017/03/02 21:46:15
nit: return client_ ? client_->OnTouchEvent(event)
Jinsuk Kim
2017/03/03 06:29:22
Done.
|
| + return true; |
| + |
| + return false; |
| +} |
| + |
| +bool ViewAndroid::OnMouseEvent(const MotionEventData& event) { |
| + if (!children_.empty()) { |
| + const MotionEventData& e = |
| + origin_.IsOrigin() ? event : event.Offset(-origin_.x(), -origin_.y()); |
| + |
| + for (auto it = children_.rbegin(); it != children_.rend(); ++it) { |
| + bool matched = (*it)->match_parent_; |
| + if (!matched) { |
| + gfx::Rect bound((*it)->origin_, (*it)->size_); |
| + matched = bound.Contains(e.GetX(), e.GetY()); |
| + } |
| + if (matched && (*it)->OnMouseEvent(e)) |
| + return true; |
| + } |
| + } |
| + |
| + if (client_ && client_->OnMouseEvent(event)) |
| + return true; |
| + |
| + return false; |
| +} |
| + |
| } // namespace ui |