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 |
19 using base::android::JavaParamRef; | |
18 using base::android::JavaRef; | 20 using base::android::JavaRef; |
19 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
20 | 22 |
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()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after 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::ViewAndroid(ViewClient* client) : parent_(nullptr), |
72 : parent_(nullptr) | 74 client_(client), |
73 , delegate_(base::android::AttachCurrentThread(), | 75 physical_width_pix_(0), |
74 delegate.obj()) {} | 76 physical_height_pix_(0), |
75 | 77 has_event_handler_(false) {} |
76 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 78 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
77 | 79 |
78 ViewAndroid::~ViewAndroid() { | 80 ViewAndroid::~ViewAndroid() { |
79 RemoveFromParent(); | 81 RemoveFromParent(); |
80 | 82 |
81 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 83 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
82 it != children_.end(); it++) { | 84 it != children_.end(); it++) { |
83 DCHECK_EQ((*it)->parent_, this); | 85 DCHECK_EQ((*it)->parent_, this); |
84 (*it)->parent_ = nullptr; | 86 (*it)->parent_ = nullptr; |
85 } | 87 } |
86 } | 88 } |
87 | 89 |
88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 90 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
91 // A ViewAndroid may have its own delegate or otherwise will | |
92 // use the next available parent's delegate. | |
89 JNIEnv* env = base::android::AttachCurrentThread(); | 93 JNIEnv* env = base::android::AttachCurrentThread(); |
90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 94 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
91 } | 95 } |
92 | 96 |
93 void ViewAndroid::AddChild(ViewAndroid* child) { | 97 void ViewAndroid::AddChild(ViewAndroid* child) { |
94 DCHECK(child); | 98 DCHECK(child); |
95 DCHECK(std::find(children_.begin(), children_.end(), child) == | 99 DCHECK(std::find(children_.begin(), children_.end(), child) == |
96 children_.end()); | 100 children_.end()); |
101 DCHECK(!HasEventHandlerInTreeHierarchy() || | |
102 !child->HasEventHandlerInSubtree()); | |
97 | 103 |
98 children_.push_back(child); | 104 children_.push_back(child); |
99 if (child->parent_) | 105 if (child->parent_) |
100 child->RemoveFromParent(); | 106 child->RemoveFromParent(); |
101 child->parent_ = this; | 107 child->parent_ = this; |
boliu
2016/12/06 23:15:46
should update child size here
Jinsuk Kim
2016/12/07 12:36:28
Done.
| |
102 } | 108 } |
103 | 109 |
104 void ViewAndroid::RemoveFromParent() { | 110 void ViewAndroid::RemoveFromParent() { |
105 if (parent_) | 111 if (parent_) |
106 parent_->RemoveChild(this); | 112 parent_->RemoveChild(this); |
107 } | 113 } |
108 | 114 |
109 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 115 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
110 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 116 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
111 if (delegate.is_null()) | 117 if (delegate.is_null()) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } | 168 } |
163 | 169 |
164 cc::Layer* ViewAndroid::GetLayer() const { | 170 cc::Layer* ViewAndroid::GetLayer() const { |
165 return layer_.get(); | 171 return layer_.get(); |
166 } | 172 } |
167 | 173 |
168 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 174 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
169 layer_ = layer; | 175 layer_ = layer; |
170 } | 176 } |
171 | 177 |
178 void ViewAndroid::OnSetEventHandler() { | |
179 DCHECK(!HasEventHandlerInTreeHierarchy()); | |
180 has_event_handler_ = true; | |
181 } | |
182 | |
183 bool ViewAndroid::HasEventHandlerInTreeHierarchy() { | |
184 ViewAndroid* view = parent_; | |
185 while (view) { | |
186 if (view->has_event_handler()) | |
187 return true; | |
188 view = view->parent_; | |
189 } | |
190 return HasEventHandlerInSubtree(); | |
191 } | |
192 | |
193 bool ViewAndroid::HasEventHandlerInSubtree() { | |
194 if (has_event_handler_) | |
195 return true; | |
196 for (auto it = children_.begin(); it != children_.end(); ++it) { | |
boliu
2016/12/06 23:15:46
style: you can use for-each style "for (auto& chil
Jinsuk Kim
2016/12/07 12:36:28
Done.
| |
197 if ((*it)->HasEventHandlerInSubtree()) | |
198 return true; | |
199 } | |
200 return false; | |
201 } | |
202 | |
172 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 203 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
173 const JavaRef<jobject>& jimage) { | 204 const JavaRef<jobject>& jimage) { |
174 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 205 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
175 if (delegate.is_null()) | 206 if (delegate.is_null()) |
176 return false; | 207 return false; |
177 JNIEnv* env = base::android::AttachCurrentThread(); | 208 JNIEnv* env = base::android::AttachCurrentThread(); |
178 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 209 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
179 jimage); | 210 jimage); |
180 } | 211 } |
181 | 212 |
213 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | |
214 return gfx::Size(physical_width_pix_, physical_height_pix_); | |
215 } | |
216 | |
217 void ViewAndroid::SetPhysicalBackingSize(int width, int height) { | |
boliu
2016/12/06 23:15:46
nit: maybe this can just be inlined, a bit confusi
Jinsuk Kim
2016/12/07 12:36:28
Done.
| |
218 physical_width_pix_ = width; | |
219 physical_height_pix_ = height; | |
220 UpdateLayerBounds(); | |
221 } | |
222 | |
223 void ViewAndroid::UpdateLayerBounds() { | |
224 layer_->SetBounds(GetPhysicalBackingSize()); | |
225 } | |
226 | |
227 void ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { | |
228 if (width == physical_width_pix_ && height == physical_height_pix_) | |
229 return; | |
230 | |
231 SetPhysicalBackingSize(width, height); | |
232 if (client_) | |
233 client_->OnPhysicalBackingSizeChanged(width, height); | |
234 | |
235 for (auto it = children_.begin(); it != children_.end(); ++it) | |
boliu
2016/12/06 23:15:46
ditto for-each loop
Jinsuk Kim
2016/12/07 12:36:28
Done.
| |
236 (*it)->OnPhysicalBackingSizeChanged(width, height); | |
237 } | |
238 | |
182 } // namespace ui | 239 } // namespace ui |
OLD | NEW |