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 ViewAndroid::ViewAndroid() : ViewAndroid(nullptr) {} |
74 delegate.obj()) {} | |
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 (std::list<ViewAndroid*>::iterator it = children_.begin(); |
82 it != children_.end(); it++) { | 81 it != children_.end(); it++) { |
83 DCHECK_EQ((*it)->parent_, this); | 82 DCHECK_EQ((*it)->parent_, this); |
84 (*it)->parent_ = nullptr; | 83 (*it)->parent_ = nullptr; |
85 } | 84 } |
86 } | 85 } |
87 | 86 |
88 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { | 87 void ViewAndroid::SetDelegate(const JavaRef<jobject>& delegate) { |
88 // A ViewAndroid may have its own delegate or otherwise will | |
89 // use the next available parent's delegate. | |
89 JNIEnv* env = base::android::AttachCurrentThread(); | 90 JNIEnv* env = base::android::AttachCurrentThread(); |
90 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 91 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
91 } | 92 } |
92 | 93 |
93 void ViewAndroid::AddChild(ViewAndroid* child) { | 94 void ViewAndroid::AddChild(ViewAndroid* child) { |
94 DCHECK(child); | 95 DCHECK(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 children_.push_back(child); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 173 bool ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
173 const JavaRef<jobject>& jimage) { | 174 const JavaRef<jobject>& jimage) { |
174 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 175 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
175 if (delegate.is_null()) | 176 if (delegate.is_null()) |
176 return false; | 177 return false; |
177 JNIEnv* env = base::android::AttachCurrentThread(); | 178 JNIEnv* env = base::android::AttachCurrentThread(); |
178 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, | 179 return Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, |
179 jimage); | 180 jimage); |
180 } | 181 } |
181 | 182 |
183 int ViewAndroid::GetPhysicalBackingSizeWidthPix() { | |
184 if (!parent_ || parent_ == GetWindowAndroid()) | |
boliu
2016/12/06 00:34:14
should just return own size, not recurse
ie we sh
Jinsuk Kim
2016/12/06 07:35:07
Done.
| |
185 return physical_width_pix_; | |
186 else | |
187 return parent_->GetPhysicalBackingSizeWidthPix(); | |
188 } | |
189 | |
190 int ViewAndroid::GetPhysicalBackingSizeHeightPix() { | |
191 if (!parent_ || parent_ == GetWindowAndroid()) | |
192 return physical_height_pix_; | |
193 else | |
194 return parent_->GetPhysicalBackingSizeHeightPix(); | |
195 } | |
196 | |
197 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | |
198 return gfx::Size(GetPhysicalBackingSizeWidthPix(), | |
199 GetPhysicalBackingSizeHeightPix()); | |
200 } | |
201 | |
202 void ViewAndroid::SetPhysicalBackingSize(int width, int height) { | |
203 if (!parent_ || parent_ == GetWindowAndroid()) { | |
204 physical_width_pix_ = width; | |
205 physical_height_pix_ = height; | |
206 } else { | |
207 return parent_->SetPhysicalBackingSize(width, height); | |
boliu
2016/12/06 00:34:14
recursion in the wrong direction.. this value shou
Jinsuk Kim
2016/12/06 07:35:07
Done.
| |
208 } | |
209 } | |
210 | |
211 void ViewAndroid::UpdateLayerBounds() { | |
212 layer_->SetBounds(GetPhysicalBackingSize()); | |
boliu
2016/12/06 00:34:14
just do this inside SetPhyiscalBackingSize, then c
Jinsuk Kim
2016/12/06 07:35:07
Done.
| |
213 } | |
214 | |
215 bool ViewAndroid::OnPhysicalBackingSizeChanged(int width, int height) { | |
216 if (width == GetPhysicalBackingSizeWidthPix() | |
217 && height == GetPhysicalBackingSizeHeightPix()) | |
218 return true; | |
219 | |
220 if (client_ && client_->OnPhysicalBackingSizeChanged(width, height)) { | |
221 SetPhysicalBackingSize(width, height); | |
222 UpdateLayerBounds(); | |
223 return true; | |
224 } | |
225 for (auto it = children_.begin(); it != children_.end(); ++it) { | |
226 if ((*it)->OnPhysicalBackingSizeChanged(width, height)) | |
227 return true; | |
228 } | |
229 return false; | |
230 } | |
231 | |
182 } // namespace ui | 232 } // namespace ui |
OLD | NEW |