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/view_client.h" | |
| 12 #include "ui/android/window_android.h" | 13 #include "ui/android/window_android.h" |
| 13 #include "ui/display/display.h" | 14 #include "ui/display/display.h" |
| 14 #include "ui/display/screen.h" | 15 #include "ui/display/screen.h" |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 | 18 |
| 18 using base::android::JavaRef; | 19 using base::android::JavaRef; |
| 19 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
| 20 | 21 |
| 22 // ViewAndroid::ScopedAndroidView | |
| 21 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 23 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 22 JNIEnv* env, | 24 JNIEnv* env, |
| 23 const JavaRef<jobject>& jview, | 25 const JavaRef<jobject>& jview, |
| 24 const JavaRef<jobject>& jdelegate) | 26 const JavaRef<jobject>& jdelegate) |
| 25 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { | 27 : view_(env, jview.obj()), delegate_(env, jdelegate.obj()) { |
| 26 // If there's a view, then we need a delegate to remove it. | 28 // If there's a view, then we need a delegate to remove it. |
| 27 DCHECK(!jdelegate.is_null() || jview.is_null()); | 29 DCHECK(!jdelegate.is_null() || jview.is_null()); |
| 28 } | 30 } |
| 29 | 31 |
| 30 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } | 32 ViewAndroid::ScopedAnchorView::ScopedAnchorView() { } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 61 view_.reset(); | 63 view_.reset(); |
| 62 delegate_.reset(); | 64 delegate_.reset(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 const base::android::ScopedJavaLocalRef<jobject> | 67 const base::android::ScopedJavaLocalRef<jobject> |
| 66 ViewAndroid::ScopedAnchorView::view() const { | 68 ViewAndroid::ScopedAnchorView::view() const { |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | 69 JNIEnv* env = base::android::AttachCurrentThread(); |
| 68 return view_.get(env); | 70 return view_.get(env); |
| 69 } | 71 } |
| 70 | 72 |
| 71 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 73 // ViewAndroid::Bounds |
| 72 : parent_(nullptr) | 74 void ViewAndroid::Bounds::SetBounds(const gfx::Point& origin, |
| 73 , delegate_(base::android::AttachCurrentThread(), | 75 int width, |
| 74 delegate.obj()) {} | 76 int height) { |
| 77 origin_.set_x(origin.x()); | |
| 78 origin_.set_y(origin.y()); | |
| 79 width_ = width; | |
| 80 height_ = height; | |
| 81 } | |
| 75 | 82 |
| 76 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 83 bool ViewAndroid::Bounds::IsInBounds(const MotionEventAndroid& event) { |
| 84 bool width_matched = (width_ == Bounds::kMatchParent) ? true : | |
| 85 origin_.x() <= event.GetX(0) && event.GetX(0) < origin_.x() + width_; | |
| 86 bool height_matched = (height_ == Bounds::kMatchParent) ? true: | |
| 87 origin_.y() <= event.GetY(0) && event.GetY(0) < (origin_.y() + height_); | |
| 88 | |
| 89 return width_matched && height_matched; | |
| 90 } | |
| 91 | |
| 92 // ViewAndroid | |
| 93 ViewAndroid::ViewAndroid(ViewClient* client) : parent_(nullptr), | |
| 94 client_(client) {} | |
| 95 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} | |
| 77 | 96 |
| 78 ViewAndroid::~ViewAndroid() { | 97 ViewAndroid::~ViewAndroid() { |
| 79 RemoveFromParent(); | 98 RemoveFromParent(); |
| 80 | 99 |
| 81 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 100 for (auto& child : children_) { |
| 82 it != children_.end(); it++) { | 101 DCHECK_EQ(child->parent_, this); |
| 83 DCHECK_EQ((*it)->parent_, this); | 102 child->parent_ = nullptr; |
| 84 (*it)->parent_ = nullptr; | |
| 85 } | 103 } |
| 86 } | 104 } |
| 87 | 105 |
| 88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 106 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
| 107 // A ViewAndroid may have its own delegate or otherwise will | |
| 108 // use the next available parent's delegate. | |
| 89 JNIEnv* env = base::android::AttachCurrentThread(); | 109 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 110 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 91 } | 111 } |
| 92 | 112 |
| 93 void ViewAndroid::AddChild(ViewAndroid* child) { | 113 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 94 DCHECK(child); | 114 DCHECK(child); |
| 115 DCHECK(!child->IsViewRoot()); // ViewRoot cannot be a child. | |
| 95 DCHECK(std::find(children_.begin(), children_.end(), child) == | 116 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 96 children_.end()); | 117 children_.end()); |
| 97 | 118 |
| 98 children_.push_back(child); | 119 children_.push_front(child); |
| 99 if (child->parent_) | 120 if (child->parent_) |
| 100 child->RemoveFromParent(); | 121 child->RemoveFromParent(); |
| 101 child->parent_ = this; | 122 child->parent_ = this; |
| 102 } | 123 } |
| 103 | 124 |
| 104 void ViewAndroid::RemoveFromParent() { | 125 void ViewAndroid::RemoveFromParent() { |
| 105 if (parent_) | 126 if (parent_) |
| 106 parent_->RemoveChild(this); | 127 parent_->RemoveChild(this); |
| 107 } | 128 } |
| 108 | 129 |
| 130 void ViewAndroid::SetBounds(const gfx::Point& origin, int width, int height) { | |
| 131 bounds_.SetBounds(origin, width, height); | |
| 132 for (auto& child: children_) { | |
| 133 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
| |
| 134 } | |
| 135 } | |
| 136 | |
| 137 bool ViewAndroid::IsInBounds(const MotionEventAndroid& event) { | |
| 138 return bounds_.IsInBounds(event); | |
| 139 } | |
| 140 | |
| 109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 141 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 142 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 111 if (delegate.is_null()) | 143 if (delegate.is_null()) |
| 112 return ViewAndroid::ScopedAnchorView(); | 144 return ViewAndroid::ScopedAnchorView(); |
| 113 | 145 |
| 114 JNIEnv* env = base::android::AttachCurrentThread(); | 146 JNIEnv* env = base::android::AttachCurrentThread(); |
| 115 return ViewAndroid::ScopedAnchorView( | 147 return ViewAndroid::ScopedAnchorView( |
| 116 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); | 148 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); |
| 117 } | 149 } |
| 118 | 150 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 } | 191 } |
| 160 | 192 |
| 161 cc::Layer* ViewAndroid::GetLayer() const { | 193 cc::Layer* ViewAndroid::GetLayer() const { |
| 162 return layer_.get(); | 194 return layer_.get(); |
| 163 } | 195 } |
| 164 | 196 |
| 165 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 197 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 166 layer_ = layer; | 198 layer_ = layer; |
| 167 } | 199 } |
| 168 | 200 |
| 201 ViewAndroid* ViewAndroid::GetViewRoot() { | |
| 202 return parent_ ? parent_->GetViewRoot() : nullptr; | |
| 203 } | |
| 204 | |
| 205 bool ViewAndroid::IsViewRoot() { | |
| 206 return GetViewRoot() == this; | |
| 207 } | |
| 208 | |
| 169 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 209 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 170 const JavaRef<jobject>& jimage) { | 210 const JavaRef<jobject>& jimage) { |
| 171 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 211 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 172 if (delegate.is_null()) | 212 if (delegate.is_null()) |
| 173 return false; | 213 return false; |
| 174 JNIEnv* env = base::android::AttachCurrentThread(); | 214 JNIEnv* env = base::android::AttachCurrentThread(); |
| 175 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 215 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
| 176 jimage); | 216 jimage); |
| 177 } | 217 } |
| 178 | 218 |
| 219 bool ViewAndroid::OnTouchEventInternal(const MotionEventAndroid& event, | |
| 220 bool is_touch_handle_event) { | |
| 221 if (client_ && IsInBounds(event)) { | |
| 222 if (client_->OnTouchEvent(event, is_touch_handle_event)) | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 for (auto& child: children_) { | |
| 227 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
| |
| 228 return true; | |
| 229 } | |
| 230 return false; | |
| 231 } | |
| 232 | |
| 179 } // namespace ui | 233 } // namespace ui |
| OLD | NEW |