Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: ui/android/view_android.cc

Issue 2645353004: ViewRoot class for event forwarding on Android (Closed)
Patch Set: comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
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
98 children_.push_back(child); 99 // The new child goes to front of the list of siblings.
100 children_.push_front(child);
boliu 2017/02/05 19:20:23 I think we should keep this push_back to match cc:
Jinsuk Kim 2017/02/06 02:23:44 Done. Hit testing is reversed order is done in OnT
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
104 void ViewAndroid::RemoveFromParent() { 106 void ViewAndroid::RemoveFromParent() {
105 if (parent_) 107 if (parent_)
106 parent_->RemoveChild(this); 108 parent_->RemoveChild(this);
107 } 109 }
108 110
111 void ViewAndroid::SetLayout(const gfx::Rect& bounds, bool match_parent) {
112 bounds_.SetRect(bounds.x(), bounds.y(), bounds.width(), bounds.height());
113 match_parent_ = match_parent;
114 }
115
109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { 116 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 117 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
111 if (delegate.is_null()) 118 if (delegate.is_null())
112 return ViewAndroid::ScopedAnchorView(); 119 return ViewAndroid::ScopedAnchorView();
113 120
114 JNIEnv* env = base::android::AttachCurrentThread(); 121 JNIEnv* env = base::android::AttachCurrentThread();
115 return ViewAndroid::ScopedAnchorView( 122 return ViewAndroid::ScopedAnchorView(
116 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); 123 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate);
117 } 124 }
118 125
126 float ViewAndroid::GetDipScale() {
127 return display::Screen::GetScreen()
128 ->GetDisplayNearestWindow(this)
129 .device_scale_factor();
130 }
131
119 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, 132 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor,
120 const gfx::RectF& bounds) { 133 const gfx::RectF& bounds) {
121 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 134 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
122 if (delegate.is_null()) 135 if (delegate.is_null())
123 return; 136 return;
124 137
125 float scale = display::Screen::GetScreen() 138 float scale = GetDipScale();
126 ->GetDisplayNearestWindow(this)
127 .device_scale_factor();
128 int left_margin = std::round(bounds.x() * scale); 139 int left_margin = std::round(bounds.x() * scale);
129 int top_margin = std::round((content_offset().y() + bounds.y()) * scale); 140 int top_margin = std::round((content_offset().y() + bounds.y()) * scale);
130 JNIEnv* env = base::android::AttachCurrentThread(); 141 JNIEnv* env = base::android::AttachCurrentThread();
131 Java_ViewAndroidDelegate_setViewPosition( 142 Java_ViewAndroidDelegate_setViewPosition(
132 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), 143 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(),
133 bounds.height(), scale, left_margin, top_margin); 144 bounds.height(), scale, left_margin, top_margin);
134 } 145 }
135 146
136 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() { 147 ScopedJavaLocalRef<jobject> ViewAndroid::GetContainerView() {
137 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 148 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
(...skipping 30 matching lines...) Expand all
168 } 179 }
169 180
170 cc::Layer* ViewAndroid::GetLayer() const { 181 cc::Layer* ViewAndroid::GetLayer() const {
171 return layer_.get(); 182 return layer_.get();
172 } 183 }
173 184
174 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { 185 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
175 layer_ = layer; 186 layer_ = layer;
176 } 187 }
177 188
189 ViewAndroid* ViewAndroid::GetViewRoot() {
190 return parent_ ? parent_->GetViewRoot() : nullptr;
191 }
192
193 bool ViewAndroid::IsViewRoot() {
194 return GetViewRoot() == this;
195 }
196
178 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, 197 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
179 const JavaRef<jobject>& jimage) { 198 const JavaRef<jobject>& jimage) {
180 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); 199 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
181 if (delegate.is_null()) 200 if (delegate.is_null())
182 return false; 201 return false;
183 JNIEnv* env = base::android::AttachCurrentThread(); 202 JNIEnv* env = base::android::AttachCurrentThread();
184 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, 203 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext,
185 jimage); 204 jimage);
186 } 205 }
187 206
207 bool ViewAndroid::OnTouchEventInternal(const MotionEventData& event) {
208 if (!children_.empty()) {
209 gfx::Point delta = bounds_.origin();
210 const MotionEventData& e =
211 delta.IsOrigin() ? event : event.Offset(-delta.x(), -delta.y());
212
213 for (auto& child: children_) {
214 if (child->match_parent_ || child->bounds_.Contains(e.GetX(), e.GetY())) {
215 if (child->OnTouchEventInternal(e))
216 return true;
217 }
218 }
219 }
220
221 if (client_ && client_->OnTouchEvent(event))
222 return true;
223
224 return false;
225 }
226
188 } // namespace ui 227 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698