Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: ui/views/view.cc

Issue 22891016: Add support for rect-based event targeting in views (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/view.cc
diff --git a/ui/views/view.cc b/ui/views/view.cc
index 3d6b8f68471d663ccf96044961dce8794732ecee..2e2cc44f23eca7d61bd2512d7b58dda4f7c34b50 100644
--- a/ui/views/view.cc
+++ b/ui/views/view.cc
@@ -69,6 +69,11 @@ const bool kContextMenuOnMousePress = false;
const bool kContextMenuOnMousePress = true;
#endif
+// The minimum percentage of a view's area that needs to be covered by a rect
+// representing a touch region in order for that view to be considered by the
+// rect-based targeting algorithm.
+static const float kRectTargetOverlap = 0.6f;
+
// Returns the top view in |view|'s hierarchy.
const views::View* GetHierarchyRoot(const views::View* view) {
const views::View* root = view;
@@ -834,19 +839,78 @@ bool View::get_use_acceleration_when_possible() {
// Input -----------------------------------------------------------------------
View* View::GetEventHandlerForPoint(const gfx::Point& point) {
- // Walk the child Views recursively looking for the View that most
- // tightly encloses the specified point.
+ return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1)));
+}
+
+View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
+ // |rect_view| represents the current best candidate to return
+ // if rect-based targeting (i.e., fuzzing) is used.
+ // |rect_view_distance| is used to keep track of the distance
+ // between the center point of |rect_view| and the center
+ // point of |rect|.
+ View* rect_view = NULL;
+ int rect_view_distance = INT_MAX;
+
+ // |point_view| represents the view that would have been returned
+ // from this function call if point-based targeting were used.
+ View* point_view = NULL;
+
for (int i = child_count() - 1; i >= 0; --i) {
View* child = child_at(i);
+
+ // Ignore any children which are invisible or do not intersect |rect|.
if (!child->visible())
continue;
+ gfx::RectF rect_in_child_coords_f(rect);
+ ConvertRectToTarget(this, child, &rect_in_child_coords_f);
+ gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
+ rect_in_child_coords_f);
+ if (!child->HitTestRect(rect_in_child_coords))
+ continue;
- gfx::Point point_in_child_coords(point);
- ConvertPointToTarget(this, child, &point_in_child_coords);
- if (child->HitTestPoint(point_in_child_coords))
- return child->GetEventHandlerForPoint(point_in_child_coords);
+ View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
+
+ if (views::UsePointBasedTargeting(rect))
+ return cur_view;
+
+ gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
+ ConvertRectToTarget(cur_view, this, &cur_view_bounds_f);
+ gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(
+ cur_view_bounds_f);
+ if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
+ // |cur_view| is a suitable candidate for rect-based targeting.
+ // Check to see if it is the closest suitable candidate so far.
+ gfx::Point touch_center(rect.CenterPoint());
+ int cur_dist = views::DistanceSquaredFromCenterLineToPoint(
+ touch_center, cur_view_bounds);
+ if (!rect_view || cur_dist < rect_view_distance) {
+ rect_view = cur_view;
+ rect_view_distance = cur_dist;
+ }
+ } else if (!rect_view && !point_view) {
+ // Rect-based targeting has not yielded any candidates so far. Check
+ // if point-based targeting would have selected |cur_view|.
+ gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint());
+ if (child->HitTestPoint(point_in_child_coords))
+ point_view = child->GetEventHandlerForPoint(point_in_child_coords);
+ }
}
- return this;
+
+ if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
+ return this;
+
+ // If |this| is a suitable candidate for rect-based targeting, check to
+ // see if it is closer than the current best suitable candidate so far.
+ gfx::Rect local_bounds(GetLocalBounds());
+ if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) {
+ gfx::Point touch_center(rect.CenterPoint());
+ int cur_dist = views::DistanceSquaredFromCenterLineToPoint(touch_center,
+ local_bounds);
+ if (!rect_view || cur_dist < rect_view_distance)
+ rect_view = this;
+ }
+
+ return rect_view ? rect_view : point_view;
}
View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698