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