view_targeter_delegate.cc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/views/view_targeter_delegate.h"
  5. #include <limits.h>
  6. #include "base/containers/adapters.h"
  7. #include "ui/gfx/geometry/rect_conversions.h"
  8. #include "ui/views/rect_based_targeting_utils.h"
  9. #include "ui/views/view.h"
  10. namespace {
  11. // The minimum percentage of a view's area that needs to be covered by a rect
  12. // representing a touch region in order for that view to be considered by the
  13. // rect-based targeting algorithm.
  14. static const float kRectTargetOverlap = 0.6f;
  15. } // namespace
  16. namespace views {
  17. // TODO(tdanderson): Move the contents of rect_based_targeting_utils.(h|cc)
  18. // into here.
  19. bool ViewTargeterDelegate::DoesIntersectRect(const View* target,
  20. const gfx::Rect& rect) const {
  21. return target->GetLocalBounds().Intersects(rect);
  22. }
  23. View* ViewTargeterDelegate::TargetForRect(View* root, const gfx::Rect& rect) {
  24. // |rect_view| represents the current best candidate to return
  25. // if rect-based targeting (i.e., fuzzing) is used.
  26. // |rect_view_distance| is used to keep track of the distance
  27. // between the center point of |rect_view| and the center
  28. // point of |rect|.
  29. View* rect_view = nullptr;
  30. int rect_view_distance = INT_MAX;
  31. // |point_view| represents the view that would have been returned
  32. // from this function call if point-based targeting were used.
  33. View* point_view = nullptr;
  34. View::Views children = root->GetChildrenInZOrder();
  35. DCHECK_EQ(root->children().size(), children.size());
  36. for (auto* child : base::Reversed(children)) {
  37. if (!child->GetCanProcessEventsWithinSubtree() || !child->GetEnabled())
  38. continue;
  39. // Ignore any children which are invisible or do not intersect |rect|.
  40. if (!child->GetVisible())
  41. continue;
  42. gfx::RectF rect_in_child_coords_f(rect);
  43. View::ConvertRectToTarget(root, child, &rect_in_child_coords_f);
  44. gfx::Rect rect_in_child_coords =
  45. gfx::ToEnclosingRect(rect_in_child_coords_f);
  46. if (!child->HitTestRect(rect_in_child_coords))
  47. continue;
  48. View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
  49. if (views::UsePointBasedTargeting(rect))
  50. return cur_view;
  51. gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
  52. View::ConvertRectToTarget(cur_view, root, &cur_view_bounds_f);
  53. gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(cur_view_bounds_f);
  54. if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
  55. // |cur_view| is a suitable candidate for rect-based targeting.
  56. // Check to see if it is the closest suitable candidate so far.
  57. gfx::Point touch_center(rect.CenterPoint());
  58. int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
  59. cur_view_bounds);
  60. if (!rect_view || cur_dist < rect_view_distance) {
  61. rect_view = cur_view;
  62. rect_view_distance = cur_dist;
  63. }
  64. } else if (!rect_view && !point_view) {
  65. // Rect-based targeting has not yielded any candidates so far. Check
  66. // if point-based targeting would have selected |cur_view|.
  67. gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint());
  68. if (child->HitTestPoint(point_in_child_coords))
  69. point_view = child->GetEventHandlerForPoint(point_in_child_coords);
  70. }
  71. }
  72. if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
  73. return root;
  74. return rect_view ? rect_view : point_view;
  75. }
  76. } // namespace views