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..8351d43aa0c11fd76fb223de51e65b674ccf688a 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,56 @@ 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())) |
| + << "Root of the ViewAndroid can have at most one handler."; |
| + event_handler_.reset(EventHandler::Create(this)); |
| + return event_handler_.get(); |
| +} |
| + |
| +ViewAndroid* ViewAndroid::GetViewRoot() { |
| + DCHECK(parent_) << "View is not attached to a tree yet."; |
| + |
| + // 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::RemoveFromParent() { |
| if (parent_) |
| parent_->RemoveChild(this); |
| @@ -124,15 +159,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 +210,17 @@ void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| layer_ = layer; |
| } |
| +void ViewAndroid::SetLayout(int x, |
| + int y, |
| + int width, |
| + int height, |
| + bool match_parent) { |
| + DCHECK(!match_parent || (x == 0 && y == 0 && width == 0 && height == 0)); |
|
boliu
2017/02/28 22:44:54
can you write this in a way such that when it fail
Jinsuk Kim
2017/03/02 04:08:35
Added a message about the condition when |match_pa
|
| + 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 +271,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) { |
| + bool matched = (*it)->match_parent_; |
| + if (!matched) { |
| + gfx::Rect bound((*it)->origin_, (*it)->size_); |
| + matched = bound.Contains(e.GetX(), e.GetY()); |
| + } |
| + if (matched && (*it)->OnTouchEvent(e)) |
| + return true; |
| + } |
| + } |
| + |
| + if (client_ && client_->OnTouchEvent(event)) |
| + 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 |