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 "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 JNIEnv* env = base::android::AttachCurrentThread(); | 97 JNIEnv* env = base::android::AttachCurrentThread(); |
| 98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
| 99 } | 99 } |
| 100 | 100 |
| 101 float ViewAndroid::GetDipScale() { | 101 float ViewAndroid::GetDipScale() { |
| 102 return ui::GetScaleFactorForNativeView(this); | 102 return ui::GetScaleFactorForNativeView(this); |
| 103 } | 103 } |
| 104 | 104 |
| 105 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() { | 105 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() { |
| 106 if (!event_forwarder_) { | 106 if (!event_forwarder_) { |
| 107 DCHECK(!ViewTreeHasEventForwarder(this)) | 107 DCHECK(!SubtreeOrParentsHaveEventForwarder(this)) |
| 108 << "Root of the ViewAndroid can have at most one handler."; | 108 << "The view tree path already has an event forwarder."; |
| 109 event_forwarder_.reset(new EventForwarder(this)); | 109 event_forwarder_.reset(new EventForwarder(this)); |
| 110 } | 110 } |
| 111 return event_forwarder_->GetJavaObject(); | 111 return event_forwarder_->GetJavaObject(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void ViewAndroid::AddChild(ViewAndroid* child) { | 114 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 115 DCHECK(child); | 115 DCHECK(child); |
| 116 DCHECK(std::find(children_.begin(), children_.end(), child) == | 116 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 117 children_.end()); | 117 children_.end()); |
| 118 DCHECK(!SubtreeHasEventForwarder(child) || !ViewTreeHasEventForwarder(this)) | 118 DCHECK(!SubtreeOrParentsHaveEventForwarder(child) || !has_event_forwarder()) |
|
boliu
2017/05/10 17:19:16
has_event_forwarder is not enough. you need to che
Jinsuk Kim
2017/05/10 21:57:57
|has_event_forwarder| only checks this node, and S
boliu
2017/05/10 22:09:40
so what if both are true:
this->parent_->has_event
Jinsuk Kim
2017/05/10 22:21:24
I'm afraid you don't get the change right. See the
boliu
2017/05/10 22:24:34
Oh yeah. That's just confusing. Rewrite it.
| |
| 119 << "Only one event handler is allowed."; | 119 << "Some view tree path will have more than one event forwarder " |
| 120 "if the child is added."; | |
| 120 | 121 |
| 121 // The new child goes to the top, which is the end of the list. | 122 // The new child goes to the top, which is the end of the list. |
| 122 children_.push_back(child); | 123 children_.push_back(child); |
| 123 if (child->parent_) | 124 if (child->parent_) |
| 124 child->RemoveFromParent(); | 125 child->RemoveFromParent(); |
| 125 child->parent_ = this; | 126 child->parent_ = this; |
| 126 } | 127 } |
| 127 | 128 |
| 128 // static | 129 bool ViewAndroid::SubtreeOrParentsHaveEventForwarder(ViewAndroid* tree) { |
| 129 bool ViewAndroid::ViewTreeHasEventForwarder(ViewAndroid* view) { | 130 // Checks from |parent_| to the root node if there's event forwarder. |
| 130 ViewAndroid* v = view; | 131 ViewAndroid* view = parent_; |
| 131 do { | 132 while (view) { |
| 132 if (v->has_event_forwarder()) | 133 if (view->has_event_forwarder()) |
| 133 return true; | 134 return true; |
| 134 v = v->parent_; | 135 view = view->parent_; |
| 135 } while (v); | 136 } |
| 136 return SubtreeHasEventForwarder(view); | 137 return SubtreeHasEventForwarder(tree); |
| 137 } | 138 } |
| 138 | 139 |
| 139 // static | 140 // static |
| 140 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) { | 141 bool ViewAndroid::SubtreeHasEventForwarder(ViewAndroid* view) { |
| 141 if (view->has_event_forwarder()) | 142 if (view->has_event_forwarder()) |
| 142 return true; | 143 return true; |
| 144 | |
| 143 for (auto* child : view->children_) { | 145 for (auto* child : view->children_) { |
| 144 if (SubtreeHasEventForwarder(child)) | 146 if (SubtreeHasEventForwarder(child)) |
| 145 return true; | 147 return true; |
| 146 } | 148 } |
| 147 return false; | 149 return false; |
| 148 } | 150 } |
| 149 | 151 |
| 150 void ViewAndroid::MoveToFront(ViewAndroid* child) { | 152 void ViewAndroid::MoveToFront(ViewAndroid* child) { |
| 151 DCHECK(child); | 153 DCHECK(child); |
| 152 auto it = std::find(children_.begin(), children_.end(), child); | 154 auto it = std::find(children_.begin(), children_.end(), child); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 matched = bound.Contains(e->GetX(0), e->GetY(0)); | 334 matched = bound.Contains(e->GetX(0), e->GetY(0)); |
| 333 } | 335 } |
| 334 if (matched && child->HitTest(send_to_client, *e)) | 336 if (matched && child->HitTest(send_to_client, *e)) |
| 335 return true; | 337 return true; |
| 336 } | 338 } |
| 337 } | 339 } |
| 338 return false; | 340 return false; |
| 339 } | 341 } |
| 340 | 342 |
| 341 } // namespace ui | 343 } // namespace ui |
| OLD | NEW |