Index: ui/views/view.cc |
diff --git a/ui/views/view.cc b/ui/views/view.cc |
index ac3c4715ecdca5119e0f1f9689e2215292be9698..a936a4d7566d4d578180817403007c34848a5f47 100644 |
--- a/ui/views/view.cc |
+++ b/ui/views/view.cc |
@@ -68,6 +68,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 |
+// views fuzzing algorithm. |
+static const float kViewsFuzzingOverlap = 0.6; |
+ |
// Returns the top view in |view|'s hierarchy. |
const views::View* GetHierarchyRoot(const views::View* view) { |
const views::View* root = view; |
@@ -681,6 +686,32 @@ void View::ConvertPointToTarget(const View* source, |
} |
// static |
+void View::ConvertRectToTarget(const View* source, |
+ const View* target, |
+ gfx::Rect* rect) { |
+ if (source == target) |
+ return; |
+ |
+ // |source| can be NULL. |
+ const View* root = GetHierarchyRoot(target); |
+ if (source) { |
+ CHECK_EQ(GetHierarchyRoot(source), root); |
+ |
+ if (source != root) |
+ source->ConvertRectForAncestor(root, rect); |
+ } |
+ |
+ if (target != root) |
+ target->ConvertRectFromAncestor(root, rect); |
+ |
+ // API defines NULL |source| as returning the point in screen coordinates. |
+ if (!source) { |
+ rect->set_origin(rect->origin() - |
+ root->GetWidget()->GetClientAreaBoundsInScreen().OffsetFromOrigin()); |
+ } |
+} |
+ |
+// static |
void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
DCHECK(src); |
DCHECK(p); |
@@ -807,19 +838,65 @@ 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::Rect rect_in_child_coords(rect); |
+ ConvertRectToTarget(this, child, &rect_in_child_coords); |
+ 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 (UsePointBasedTargeting(rect)) |
+ return cur_view; |
+ |
+ gfx::Rect cur_rect(cur_view->GetLocalBounds()); |
sky
2013/10/04 17:03:49
cur_rect -> cur_view_bounds
tdanderson
2013/10/07 21:35:21
Done.
|
+ ConvertRectToTarget(cur_view, this, &cur_rect); |
+ if (PercentCoveredBy(cur_rect, rect) >= kViewsFuzzingOverlap) { |
+ // |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()); |
+ // Terry: change this to just use distance between center points. |
sky
2013/10/04 17:03:49
TODO(tdanderson):
tdanderson
2013/10/07 21:35:21
On second thought, I'm going to leave this as is s
|
+ int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center, |
+ cur_rect); |
+ 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.CenterPoint()); |
sky
2013/10/04 17:03:49
Can you use the center point of rect_in_child_coor
tdanderson
2013/10/07 21:35:21
Done.
|
+ ConvertPointToTarget(this, child, &point_in_child_coords); |
+ if (child->HitTestPoint(point_in_child_coords)) |
+ point_view = child->GetEventHandlerForPoint(point_in_child_coords); |
+ } |
} |
- return this; |
+ |
+ if (UsePointBasedTargeting(rect) || (!rect_view && !point_view)) |
+ return this; |
+ |
+ return rect_view ? rect_view : point_view; |
} |
View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { |
@@ -1250,6 +1327,52 @@ int View::GetLineScrollIncrement(ScrollView* scroll_view, |
return 0; |
} |
+// Views fuzzing --------------------------------------------------------------- |
+ |
+// static |
+bool View::UsePointBasedTargeting(const gfx::Rect& rect) { |
+ return rect.width() == 1 && rect.height() == 1; |
+} |
+ |
+// static |
+float View::PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) { |
+ gfx::Rect intersection(rect_1); |
+ intersection.Intersect(rect_2); |
+ float intersection_area = intersection.size().GetArea(); |
sky
2013/10/04 17:03:49
Should this and 1342 be kept as ints and then case
tdanderson
2013/10/07 21:35:21
Done.
|
+ float rect_1_area = rect_1.size().GetArea(); |
+ return rect_1_area ? intersection_area / rect_1_area : 0; |
+} |
+ |
+// The positive distance from |pos| to the nearest endpoint of the interval |
+// [start, end] is returned if |pos| lies within the interval, otherwise |
+// 0 is returned. |
+int DistanceToInterval(int pos, int start, int end) { |
+ if (pos < start) |
+ return start - pos; |
+ if (pos > end) |
+ return pos - end; |
+ return 0; |
+} |
+ |
+// static |
+int View::DistanceSquaredFromCenterLineToPoint(const gfx::Point& point, |
+ const gfx::Rect& target_rect) { |
+ gfx::Point center_point = target_rect.CenterPoint(); |
+ int dx = center_point.x() - point.x(); |
+ int dy = center_point.y() - point.y(); |
+ |
+ if (target_rect.width() > target_rect.height()) { |
+ dx = DistanceToInterval(point.x(), |
+ target_rect.x() + (target_rect.height() / 2), |
+ target_rect.right() - (target_rect.height() / 2)); |
+ } else { |
+ dy = DistanceToInterval(point.y(), |
+ target_rect.y() + (target_rect.width() / 2), |
+ target_rect.bottom() - (target_rect.width() / 2)); |
+ } |
+ return (dx * dx) + (dy * dy); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// View, protected: |
@@ -1978,6 +2101,27 @@ bool View::ConvertPointFromAncestor(const View* ancestor, |
return result; |
} |
+bool View::ConvertRectForAncestor(const View* ancestor, |
+ gfx::Rect* rect) const { |
+ gfx::Transform trans; |
+ // TODO(sad): Have some way of caching the transformation results. |
+ bool result = GetTransformRelativeTo(ancestor, &trans); |
+ gfx::RectF r(*rect); |
+ trans.TransformRect(&r); |
+ *rect = gfx::ToEnclosingRect(r); |
+ return result; |
+} |
+ |
+bool View::ConvertRectFromAncestor(const View* ancestor, |
+ gfx::Rect* rect) const { |
+ gfx::Transform trans; |
+ bool result = GetTransformRelativeTo(ancestor, &trans); |
+ gfx::RectF r(*rect); |
+ trans.TransformRectReverse(&r); |
+ *rect = gfx::ToEnclosingRect(r); |
+ return result; |
+} |
+ |
// Accelerated painting -------------------------------------------------------- |
void View::CreateLayer() { |