OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/gfx/rect_conversions.h" |
| 6 #include "ui/views/rect_based_targeting_utils.h" |
5 #include "ui/views/view.h" | 7 #include "ui/views/view.h" |
6 #include "ui/views/view_targeter_delegate.h" | 8 #include "ui/views/view_targeter_delegate.h" |
7 | 9 |
| 10 // Terry - move rect_based_targeting_utils into here? |
| 11 |
| 12 namespace { |
| 13 |
| 14 // The minimum percentage of a view's area that needs to be covered by a rect |
| 15 // representing a touch region in order for that view to be considered by the |
| 16 // rect-based targeting algorithm. |
| 17 static const float kRectTargetOverlap = 0.6f; |
| 18 |
| 19 } // namespace |
| 20 |
8 namespace views { | 21 namespace views { |
9 | 22 |
10 bool ViewTargeterDelegate::DoesIntersectRect(const View* target, | 23 bool ViewTargeterDelegate::DoesIntersectRect(const View* target, |
11 const gfx::Rect& rect) const { | 24 const gfx::Rect& rect) const { |
12 return target->GetLocalBounds().Intersects(rect); | 25 return target->GetLocalBounds().Intersects(rect); |
13 } | 26 } |
14 | 27 |
| 28 View* ViewTargeterDelegate::TargetForRect(View* root, |
| 29 const gfx::Rect& rect) { |
| 30 // Terry - updates to doc? |
| 31 |
| 32 // |rect_view| represents the current best candidate to return |
| 33 // if rect-based targeting (i.e., fuzzing) is used. |
| 34 // |rect_view_distance| is used to keep track of the distance |
| 35 // between the center point of |rect_view| and the center |
| 36 // point of |rect|. |
| 37 View* rect_view = NULL; |
| 38 int rect_view_distance = INT_MAX; |
| 39 |
| 40 // |point_view| represents the view that would have been returned |
| 41 // from this function call if point-based targeting were used. |
| 42 View* point_view = NULL; |
| 43 |
| 44 for (int i = root->child_count() - 1; i >= 0; --i) { |
| 45 View* child = root->child_at(i); |
| 46 |
| 47 if (!child->CanProcessEventsWithinSubtree()) |
| 48 continue; |
| 49 |
| 50 // Ignore any children which are invisible or do not intersect |rect|. |
| 51 if (!child->visible()) |
| 52 continue; |
| 53 gfx::RectF rect_in_child_coords_f(rect); |
| 54 View::ConvertRectToTarget(root, child, &rect_in_child_coords_f); |
| 55 gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect( |
| 56 rect_in_child_coords_f); |
| 57 if (!child->HitTestRect(rect_in_child_coords)) |
| 58 continue; |
| 59 |
| 60 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords); |
| 61 |
| 62 if (views::UsePointBasedTargeting(rect)) |
| 63 return cur_view; |
| 64 |
| 65 gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds()); |
| 66 View::ConvertRectToTarget(cur_view, root, &cur_view_bounds_f); |
| 67 gfx::Rect cur_view_bounds = gfx::ToEnclosingRect( |
| 68 cur_view_bounds_f); |
| 69 if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) { |
| 70 // |cur_view| is a suitable candidate for rect-based targeting. |
| 71 // Check to see if it is the closest suitable candidate so far. |
| 72 gfx::Point touch_center(rect.CenterPoint()); |
| 73 int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center, |
| 74 cur_view_bounds); |
| 75 if (!rect_view || cur_dist < rect_view_distance) { |
| 76 rect_view = cur_view; |
| 77 rect_view_distance = cur_dist; |
| 78 } |
| 79 } else if (!rect_view && !point_view) { |
| 80 // Rect-based targeting has not yielded any candidates so far. Check |
| 81 // if point-based targeting would have selected |cur_view|. |
| 82 gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint()); |
| 83 if (child->HitTestPoint(point_in_child_coords)) |
| 84 point_view = child->GetEventHandlerForPoint(point_in_child_coords); |
| 85 } |
| 86 } |
| 87 |
| 88 if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view)) |
| 89 return root; |
| 90 |
| 91 // If |this| is a suitable candidate for rect-based targeting, check to |
| 92 // see if it is closer than the current best suitable candidate so far. |
| 93 gfx::Rect local_bounds(root->GetLocalBounds()); |
| 94 if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) { |
| 95 gfx::Point touch_center(rect.CenterPoint()); |
| 96 int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center, |
| 97 local_bounds); |
| 98 if (!rect_view || cur_dist < rect_view_distance) |
| 99 rect_view = root; |
| 100 } |
| 101 |
| 102 return rect_view ? rect_view : point_view; |
| 103 } |
| 104 |
15 } // namespace views | 105 } // namespace views |
OLD | NEW |