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

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: comments addressed, moved fuzzing utility functions to new file Created 7 years, 2 months 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
Index: ui/views/view.cc
diff --git a/ui/views/view.cc b/ui/views/view.cc
index ac3c4715ecdca5119e0f1f9689e2215292be9698..e13872ad053c87762e49cffc41ffb59e3d9d95f2 100644
--- a/ui/views/view.cc
+++ b/ui/views/view.cc
@@ -38,6 +38,7 @@
#include "ui/views/context_menu_controller.h"
#include "ui/views/drag_controller.h"
#include "ui/views/layout/layout_manager.h"
+#include "ui/views/rect_based_targeting_utils.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
@@ -68,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.6;
+
// Returns the top view in |view|'s hierarchy.
const views::View* GetHierarchyRoot(const views::View* view) {
const views::View* root = view;
@@ -681,6 +687,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 +839,63 @@ 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 (views::UsePointBasedTargeting(rect))
+ return cur_view;
+
+ gfx::Rect cur_view_bounds(cur_view->GetLocalBounds());
+ ConvertRectToTarget(cur_view, this, &cur_view_bounds);
+ 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;
+
+ return rect_view ? rect_view : point_view;
}
View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
@@ -1978,6 +2054,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() {

Powered by Google App Engine
This is Rietveld 408576698