Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/android/view_android.h" | 5 #include "ui/android/view_android.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
| 11 #include "jni/ViewAndroidDelegate_jni.h" | 11 #include "jni/ViewAndroidDelegate_jni.h" |
| 12 #include "ui/android/window_android.h" | 12 #include "ui/android/window_android.h" |
| 13 #include "ui/display/display.h" | 13 #include "ui/display/display.h" |
| 14 #include "ui/display/screen.h" | 14 #include "ui/display/screen.h" |
| 15 | 15 |
| 16 namespace ui { | 16 namespace ui { |
| 17 | 17 |
| 18 using base::android::JavaRef; | 18 using base::android::JavaRef; |
| 19 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 20 | 20 |
| 21 // ViewAndroid::ScopedAndroidView | |
| 21 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 22 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 22 JNIEnv* env, | 23 JNIEnv* env, |
| 23 const JavaRef<jobject>& jview, | 24 const JavaRef<jobject>& jview, |
| 24 const JavaRef<jobject>& jdelegate) | 25 const JavaRef<jobject>& jdelegate) |
| 25 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { | 26 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { |
| 26 // If there's a view, then we need a delegate to remove it. | 27 // If there's a view, then we need a delegate to remove it. |
| 27 DCHECK(!jdelegate.is_null() || jview.is_null()); | 28 DCHECK(!jdelegate.is_null() || jview.is_null()); |
| 28 } | 29 } |
| 29 | 30 |
| 30 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } | 31 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 61 view_.reset(); | 62 view_.reset(); |
| 62 delegate_.reset(); | 63 delegate_.reset(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 const base::android::ScopedJavaLocalRef<jobject> | 66 const base::android::ScopedJavaLocalRef<jobject> |
| 66 ViewAndroid::ScopedAnchorView::view() const { | 67 ViewAndroid::ScopedAnchorView::view() const { |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | 68 JNIEnv* env = base::android::AttachCurrentThread(); |
| 68 return view_.get(env); | 69 return view_.get(env); |
| 69 } | 70 } |
| 70 | 71 |
| 71 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 72 // ViewAndroid |
| 72 : parent_(nullptr) | 73 ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr), |
| 73 , delegate_(base::android::AttachCurrentThread(), | 74 client_(client) {} |
| 74 delegate.obj()) {} | 75 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
| 75 | |
| 76 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | |
| 77 | 76 |
| 78 ViewAndroid::~ViewAndroid() { | 77 ViewAndroid::~ViewAndroid() { |
| 79 RemoveFromParent(); | 78 RemoveFromParent(); |
| 80 | 79 |
| 81 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 80 for (auto& child : children_) { |
| 82 it != children_.end(); it++) { | 81 DCHECK_EQ(child->parent_, this); |
| 83 DCHECK_EQ((*it)->parent_, this); | 82 child->parent_ = nullptr; |
|
boliu
2017/02/06 18:29:26
call RemoveChild to avoid code duplication, though
Jinsuk Kim
2017/02/06 23:51:33
Done.
| |
| 84 (*it)->parent_ = nullptr; | |
| 85 } | 83 } |
| 86 } | 84 } |
| 87 | 85 |
| 88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 86 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 87 // A ViewAndroid may have its own delegate or otherwise will | |
| 88 // use the next available parent's delegate. | |
| 89 JNIEnv* env = base::android::AttachCurrentThread(); | 89 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void ViewAndroid::AddChild(ViewAndroid* child) { | 93 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 94 DCHECK(child); | 94 DCHECK(child); |
| 95 DCHECK(!child->IsViewRoot()); // ViewRoot cannot be a child. | |
| 95 DCHECK(std::find(children_.begin(), children_.end(), child) == | 96 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 96 children_.end()); | 97 children_.end()); |
| 97 | 98 |
| 99 // The new child goes to the top of the list, which is the end of the list. | |
| 98 children_.push_back(child); | 100 children_.push_back(child); |
| 99 if (child->parent_) | 101 if (child->parent_) |
| 100 child->RemoveFromParent(); | 102 child->RemoveFromParent(); |
| 101 child->parent_ = this; | 103 child->parent_ = this; |
| 102 } | 104 } |
| 103 | 105 |
| 106 void ViewAndroid::MoveToTop(ViewAndroid* child) { | |
| 107 DCHECK(child); | |
| 108 auto it = std::find(children_.begin(), children_.end(), child); | |
| 109 DCHECK(it != children_.end()); | |
| 110 | |
| 111 // Top element is placed at the end of the list. | |
| 112 if (*it != children_.back()) | |
| 113 children_.splice(children_.rbegin().base(), children_, it); | |
| 114 } | |
| 115 | |
| 104 void ViewAndroid::RemoveFromParent() { | 116 void ViewAndroid::RemoveFromParent() { |
| 105 if (parent_) | 117 if (parent_) |
| 106 parent_->RemoveChild(this); | 118 parent_->RemoveChild(this); |
| 107 } | 119 } |
| 108 | 120 |
| 121 void ViewAndroid::SetLayout(int x, | |
| 122 int y, | |
| 123 int width, | |
| 124 int height, | |
| 125 bool match_parent) { | |
| 126 origin_.SetPoint(x, y); | |
|
boliu
2017/02/06 18:29:26
specify what origin and size are supposed to be wh
Jinsuk Kim
2017/02/06 23:51:33
Chose 1). Used Point.IsOrigin() and Size.IsEmpty()
| |
| 127 size_.SetSize(width, height); | |
| 128 match_parent_ = match_parent; | |
| 129 } | |
| 130 | |
| 109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 131 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 132 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 111 if (delegate.is_null()) | 133 if (delegate.is_null()) |
| 112 return ViewAndroid::ScopedAnchorView(); | 134 return ViewAndroid::ScopedAnchorView(); |
| 113 | 135 |
| 114 JNIEnv* env = base::android::AttachCurrentThread(); | 136 JNIEnv* env = base::android::AttachCurrentThread(); |
| 115 return ViewAndroid::ScopedAnchorView( | 137 return ViewAndroid::ScopedAnchorView( |
| 116 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); | 138 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); |
| 117 } | 139 } |
| 118 | 140 |
| 141 float ViewAndroid::GetDipScale() { | |
| 142 return display::Screen::GetScreen() | |
| 143 ->GetDisplayNearestWindow(this) | |
| 144 .device_scale_factor(); | |
| 145 } | |
| 146 | |
| 119 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | 147 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, |
| 120 const gfx::RectF& bounds) { | 148 const gfx::RectF& bounds) { |
| 121 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 149 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 122 if (delegate.is_null()) | 150 if (delegate.is_null()) |
| 123 return; | 151 return; |
| 124 | 152 |
| 125 float scale = display::Screen::GetScreen() | 153 float scale = GetDipScale(); |
| 126 ->GetDisplayNearestWindow(this) | |
| 127 .device_scale_factor(); | |
| 128 int left_margin = std::round(bounds.x() * scale); | 154 int left_margin = std::round(bounds.x() * scale); |
| 129 int top_margin = std::round((content_offset().y() + bounds.y()) * scale); | 155 int top_margin = std::round((content_offset().y() + bounds.y()) * scale); |
| 130 JNIEnv* env = base::android::AttachCurrentThread(); | 156 JNIEnv* env = base::android::AttachCurrentThread(); |
| 131 Java_ViewAndroidDelegate_setViewPosition( | 157 Java_ViewAndroidDelegate_setViewPosition( |
| 132 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), | 158 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), |
| 133 bounds.height(), scale, left_margin, top_margin); | 159 bounds.height(), scale, left_margin, top_margin); |
| 134 } | 160 } |
| 135 | 161 |
| 136 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() { | 162 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() { |
| 137 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 163 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 168 } | 194 } |
| 169 | 195 |
| 170 cc::Layer* ViewAndroid::GetLayer() const { | 196 cc::Layer* ViewAndroid::GetLayer() const { |
| 171 return layer_.get(); | 197 return layer_.get(); |
| 172 } | 198 } |
| 173 | 199 |
| 174 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 200 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 175 layer_ = layer; | 201 layer_ = layer; |
| 176 } | 202 } |
| 177 | 203 |
| 204 ViewAndroid* ViewAndroid::GetViewRoot() { | |
| 205 return parent_ ? parent_->GetViewRoot() : nullptr; | |
| 206 } | |
| 207 | |
| 208 bool ViewAndroid::IsViewRoot() { | |
| 209 return GetViewRoot() == this; | |
| 210 } | |
| 211 | |
| 178 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 212 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 179 const JavaRef<jobject>& jimage) { | 213 const JavaRef<jobject>& jimage) { |
| 180 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 214 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 181 if (delegate.is_null()) | 215 if (delegate.is_null()) |
| 182 return false; | 216 return false; |
| 183 JNIEnv* env = base::android::AttachCurrentThread(); | 217 JNIEnv* env = base::android::AttachCurrentThread(); |
| 184 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 218 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 185 jimage); | 219 jimage); |
| 186 } | 220 } |
| 187 | 221 |
| 222 bool ViewAndroid::OnTouchEventInternal(const MotionEventData& event) { | |
| 223 if (!children_.empty()) { | |
| 224 const MotionEventData& e = | |
| 225 origin_.IsOrigin() ? event : event.Offset(-origin_.x(), -origin_.y()); | |
| 226 | |
| 227 for (auto it = children_.rbegin(); it != children_.rend(); ++it) { | |
| 228 bool matched = (*it)->match_parent_; | |
| 229 if (!matched) { | |
| 230 gfx::Rect bound((*it)->origin_, (*it)->size_); | |
| 231 matched = bound.Contains(e.GetX(), e.GetY()); | |
| 232 } | |
| 233 if (matched && (*it)->OnTouchEventInternal(e)) | |
| 234 return true; | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 if (client_ && client_->OnTouchEvent(event)) | |
| 239 return true; | |
| 240 | |
| 241 return false; | |
| 242 } | |
| 243 | |
| 188 } // namespace ui | 244 } // namespace ui |
| OLD | NEW |