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

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: tests added 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 3e0d55a4d070e112f2a6d946256a9be2e98477b1..b8ed96fb65c21d3c74dbf5a6babed366de559d23 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.6f;
+
// Returns the top view in |view|'s hierarchy.
const views::View* GetHierarchyRoot(const views::View* view) {
const views::View* root = view;
@@ -833,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());
tdanderson 2013/10/29 23:36:57 Lines 904-911 are a new addition since the last ti
+ 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) {
@@ -892,7 +957,10 @@ bool View::HitTestRect(const gfx::Rect& rect) const {
if (GetLocalBounds().Intersects(rect)) {
if (HasHitTestMask()) {
gfx::Path mask;
- GetHitTestMask(&mask);
+ HitTestSource source = HIT_TEST_SOURCE_MOUSE;
+ if (!views::UsePointBasedTargeting(rect))
+ source = HIT_TEST_SOURCE_TOUCH;
+ GetHitTestMask(&mask, source);
#if defined(USE_AURA)
// TODO: should we use this every where?
SkRegion clip_region;
@@ -1501,7 +1569,7 @@ bool View::HasHitTestMask() const {
return false;
}
-void View::GetHitTestMask(gfx::Path* mask) const {
+void View::GetHitTestMask(gfx::Path* mask, HitTestSource source) const {
DCHECK(mask);
}

Powered by Google App Engine
This is Rietveld 408576698