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

Unified Diff: ui/views/view.cc

Issue 10790019: Add gesture target fuzzing to views (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: New approach using GetEventHandlerForRect Created 8 years, 5 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 d200d03aeed93b2d6edcee1cc1873257d2eb3d84..f5ebf6f1390b68a7028c98f0f547d6d35d21df1d 100644
--- a/ui/views/view.cc
+++ b/ui/views/view.cc
@@ -27,6 +27,7 @@
#include "ui/views/context_menu_controller.h"
#include "ui/views/drag_controller.h"
#include "ui/views/layout/layout_manager.h"
+#include "ui/views/view_constants.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
@@ -80,6 +81,44 @@ const views::View* GetHierarchyRoot(const views::View* view) {
return root;
}
+// Returns the percentage of |rect_1|'s area that is covered by |rect_2|.
+float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) {
+ gfx::Rect intersection(rect_1.Intersect(rect_2));
+ float intersection_area = intersection.size().GetArea();
+ float rect_1_area = rect_1.size().GetArea();
+ return intersection_area / rect_1_area;
+}
+
+int DistanceToInterval(int pos, int start, int end) {
+ if (pos < start)
+ return start - pos;
+ if (pos > end)
+ return end - pos;
+ return 0;
+}
+
+// Returns the distance from |point| to the center line of |target_rect|.
rjkroege 2012/07/19 00:51:57 square of the distance -- comment not consistent w
tdanderson 2012/07/25 23:08:57 Done.
+// The center line of a rectangle is obtained by repeatedly stripping away
+// 1px borders around the rectangle until a line remains.
+int 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);
+}
+
} // namespace
namespace views {
@@ -764,6 +803,48 @@ View* View::GetEventHandlerForPoint(const gfx::Point& point) {
return this;
}
+View* View::GetEventHandlerForRect(const gfx::Rect& touch_rect) {
+ if (!child_count()) {
+ gfx::Rect view_rect(GetScreenBounds());
+ if (PercentCoveredBy(view_rect, touch_rect) >= kFuzzingOverlapPercentage)
+ return this;
+ return NULL;
sadrul 2012/07/18 23:42:31 I think as long as the intersection is non-empty,
tdanderson 2012/07/25 23:08:57 This is the behavior that I want since only small
+ }
+
+ View* closest_view = NULL;
+ for (int i = child_count() - 1; i >= 0; --i) {
+ View* child = child_at(i);
+
+ if (!child->visible())
+ continue;
+
+ gfx::Rect child_rect(child->GetScreenBounds());
+ if (!child_rect.Intersects(touch_rect))
+ continue;
+
+ View* cur_view = child->GetEventHandlerForRect(touch_rect);
+
+ if (!closest_view)
sadrul 2012/07/18 23:42:31 braces (since else has them)
tdanderson 2012/07/25 23:08:57 Done.
+ closest_view = cur_view;
+ else if (cur_view) {
+ gfx::Point touch_center(touch_rect.CenterPoint());
+ gfx::Rect closest_view_rect(closest_view->GetScreenBounds());
+ gfx::Rect cur_rect(cur_view->GetScreenBounds());
+
+ int best_dist_so_far = DistanceSquaredFromCenterLineToPoint(
+ touch_center,
+ closest_view_rect);
+ int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center,
+ cur_rect);
+
+ if (cur_dist < best_dist_so_far)
+ closest_view = cur_view;
sadrul 2012/07/18 23:42:31 Can you remember best_dist_so_far instead of compu
tdanderson 2012/07/25 23:08:57 Done.
+ }
+ }
+
+ return closest_view;
+}
+
gfx::NativeCursor View::GetCursor(const MouseEvent& event) {
#if defined(OS_WIN) && !defined(USE_AURA)
static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW);

Powered by Google App Engine
This is Rietveld 408576698