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" |
11 #include "base/containers/adapters.h" | 11 #include "base/containers/adapters.h" |
12 #include "cc/layers/layer.h" | 12 #include "cc/layers/layer.h" |
13 #include "jni/ViewAndroidDelegate_jni.h" | 13 #include "jni/ViewAndroidDelegate_jni.h" |
14 #include "ui/android/event_forwarder.h" | 14 #include "ui/android/event_forwarder.h" |
15 #include "ui/android/view_client.h" | 15 #include "ui/android/view_client.h" |
16 #include "ui/android/window_android.h" | 16 #include "ui/android/window_android.h" |
17 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
18 #include "ui/events/android/drag_event_android.h" | |
18 #include "ui/events/android/motion_event_android.h" | 19 #include "ui/events/android/motion_event_android.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace ui { | 22 namespace ui { |
22 | 23 |
23 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
24 using base::android::JavaRef; | 25 using base::android::JavaRef; |
25 using base::android::ScopedJavaLocalRef; | 26 using base::android::ScopedJavaLocalRef; |
26 | 27 |
27 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | 28 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 // A ViewAndroid may have its own delegate or otherwise will use the next | 96 // A ViewAndroid may have its own delegate or otherwise will use the next |
96 // available parent's delegate. | 97 // available parent's delegate. |
97 JNIEnv* env = base::android::AttachCurrentThread(); | 98 JNIEnv* env = base::android::AttachCurrentThread(); |
98 delegate_ = JavaObjectWeakGlobalRef(env, delegate); | 99 delegate_ = JavaObjectWeakGlobalRef(env, delegate); |
99 } | 100 } |
100 | 101 |
101 float ViewAndroid::GetDipScale() { | 102 float ViewAndroid::GetDipScale() { |
102 return ui::GetScaleFactorForNativeView(this); | 103 return ui::GetScaleFactorForNativeView(this); |
103 } | 104 } |
104 | 105 |
106 bool ViewAndroid::HitBy(const gfx::Point& point) { | |
boliu
2017/05/24 22:54:02
just inline this one line method with a single cal
Jinsuk Kim
2017/05/25 03:18:18
Done.
| |
107 return layout_params_.HitBy(point); | |
108 } | |
109 | |
105 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() { | 110 ScopedJavaLocalRef<jobject> ViewAndroid::GetEventForwarder() { |
106 if (!event_forwarder_) { | 111 if (!event_forwarder_) { |
107 DCHECK(!RootPathHasEventForwarder(parent_)) | 112 DCHECK(!RootPathHasEventForwarder(parent_)) |
108 << "The view tree path already has an event forwarder."; | 113 << "The view tree path already has an event forwarder."; |
109 DCHECK(!SubtreeHasEventForwarder(this)) | 114 DCHECK(!SubtreeHasEventForwarder(this)) |
110 << "The view tree path already has an event forwarder."; | 115 << "The view tree path already has an event forwarder."; |
111 event_forwarder_.reset(new EventForwarder(this)); | 116 event_forwarder_.reset(new EventForwarder(this)); |
112 } | 117 } |
113 return event_forwarder_->GetJavaObject(); | 118 return event_forwarder_->GetJavaObject(); |
114 } | 119 } |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 client_->OnPhysicalBackingSizeChanged(); | 300 client_->OnPhysicalBackingSizeChanged(); |
296 | 301 |
297 for (auto* child : children_) | 302 for (auto* child : children_) |
298 child->OnPhysicalBackingSizeChanged(size); | 303 child->OnPhysicalBackingSizeChanged(size); |
299 } | 304 } |
300 | 305 |
301 gfx::Size ViewAndroid::GetPhysicalBackingSize() { | 306 gfx::Size ViewAndroid::GetPhysicalBackingSize() { |
302 return physical_size_; | 307 return physical_size_; |
303 } | 308 } |
304 | 309 |
310 bool ViewAndroid::OnDragEvent(const DragEventAndroid& event) { | |
311 return HitTest(base::Bind(&ViewAndroid::SendDragEventToClient), event, | |
312 event.location()); | |
313 } | |
314 | |
315 // static | |
316 bool ViewAndroid::SendDragEventToClient(ViewClient* client, | |
317 const DragEventAndroid& event, | |
318 const gfx::Point& point) { | |
319 std::unique_ptr<DragEventAndroid> e = event.CreateFor(point); | |
320 return client->OnDragEvent(*e); | |
321 } | |
322 | |
305 bool ViewAndroid::OnTouchEvent(const MotionEventAndroid& event, | 323 bool ViewAndroid::OnTouchEvent(const MotionEventAndroid& event, |
306 bool for_touch_handle) { | 324 bool for_touch_handle) { |
307 return HitTest( | 325 return HitTest( |
308 base::Bind(&ViewAndroid::SendTouchEventToClient, for_touch_handle), | 326 base::Bind(&ViewAndroid::SendTouchEventToClient, for_touch_handle), event, |
309 event); | 327 event.GetPoint()); |
310 } | 328 } |
311 | 329 |
330 // static | |
312 bool ViewAndroid::SendTouchEventToClient(bool for_touch_handle, | 331 bool ViewAndroid::SendTouchEventToClient(bool for_touch_handle, |
313 ViewClient* client, | 332 ViewClient* client, |
314 const MotionEventAndroid& event) { | 333 const MotionEventAndroid& event, |
315 return client->OnTouchEvent(event, for_touch_handle); | 334 const gfx::Point& point) { |
335 std::unique_ptr<MotionEventAndroid> e(event.CreateFor(point)); | |
336 return client->OnTouchEvent(*e, for_touch_handle); | |
316 } | 337 } |
317 | 338 |
318 bool ViewAndroid::OnMouseEvent(const MotionEventAndroid& event) { | 339 bool ViewAndroid::OnMouseEvent(const MotionEventAndroid& event) { |
319 return HitTest(base::Bind(&ViewAndroid::SendMouseEventToClient), event); | 340 return HitTest(base::Bind(&ViewAndroid::SendMouseEventToClient), event, |
341 event.GetPoint()); | |
320 } | 342 } |
321 | 343 |
322 // static | 344 // static |
323 bool ViewAndroid::SendMouseEventToClient(ViewClient* client, | 345 bool ViewAndroid::SendMouseEventToClient(ViewClient* client, |
324 const MotionEventAndroid& event) { | 346 const MotionEventAndroid& event, |
325 return client->OnMouseEvent(event); | 347 const gfx::Point& point) { |
348 std::unique_ptr<MotionEventAndroid> e(event.CreateFor(point)); | |
349 return client->OnMouseEvent(*e); | |
326 } | 350 } |
327 | 351 |
328 // static | |
329 bool ViewAndroid::OnMouseWheelEvent(const MotionEventAndroid& event) { | 352 bool ViewAndroid::OnMouseWheelEvent(const MotionEventAndroid& event) { |
330 return HitTest(base::Bind(&ViewAndroid::SendMouseWheelEventToClient), event); | 353 return HitTest(base::Bind(&ViewAndroid::SendMouseWheelEventToClient), event, |
354 event.GetPoint()); | |
331 } | 355 } |
332 | 356 |
333 // static | 357 // static |
334 bool ViewAndroid::SendMouseWheelEventToClient(ViewClient* client, | 358 bool ViewAndroid::SendMouseWheelEventToClient(ViewClient* client, |
335 const MotionEventAndroid& event) { | 359 const MotionEventAndroid& event, |
336 return client->OnMouseWheelEvent(event); | 360 const gfx::Point& point) { |
361 std::unique_ptr<MotionEventAndroid> e(event.CreateFor(point)); | |
362 return client->OnMouseWheelEvent(*e); | |
337 } | 363 } |
338 | 364 |
339 bool ViewAndroid::HitTest(ViewClientCallback send_to_client, | 365 template <typename E, typename T> |
340 const MotionEventAndroid& event) { | 366 bool ViewAndroid::HitTest(T send_to_client, |
boliu
2017/05/24 22:54:02
I think T doesn't need to be a template, just decl
Jinsuk Kim
2017/05/25 03:18:18
Done.
| |
341 if (client_ && send_to_client.Run(client_, event)) | 367 const E& event, |
368 const gfx::Point& point) { | |
369 if (client_ && send_to_client.Run(client_, event, point)) | |
342 return true; | 370 return true; |
343 | 371 |
344 if (!children_.empty()) { | 372 if (!children_.empty()) { |
345 std::unique_ptr<MotionEventAndroid> e( | 373 gfx::Point offset_point(point); |
346 event.Offset(-layout_params_.x, -layout_params_.y)); | 374 offset_point.Offset(-layout_params_.x, -layout_params_.y); |
347 | 375 |
348 // Match from back to front for hit testing. | 376 // Match from back to front for hit testing. |
349 for (auto* child : base::Reversed(children_)) { | 377 for (auto* child : base::Reversed(children_)) { |
350 bool matched = child->layout_params_.match_parent; | 378 if (child->HitBy(offset_point) && |
351 if (!matched) { | 379 child->HitTest(send_to_client, event, offset_point)) |
352 gfx::Rect bound(child->layout_params_.x, child->layout_params_.y, | |
353 child->layout_params_.width, | |
354 child->layout_params_.height); | |
355 matched = bound.Contains(e->GetX(0), e->GetY(0)); | |
356 } | |
357 if (matched && child->HitTest(send_to_client, *e)) | |
358 return true; | 380 return true; |
359 } | 381 } |
360 } | 382 } |
361 return false; | 383 return false; |
362 } | 384 } |
363 | 385 |
364 } // namespace ui | 386 } // namespace ui |
OLD | NEW |