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

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: No longer using screen coordinates Created 8 years, 4 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
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_constants.h » ('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 80bd221359589cc850369a3bb9de561f8537f722..c9f31c3f394d5d5a11c1755ef6e84b256ebe79af 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,46 @@ 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 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;
+}
+
+// Returns the square of the distance from |point| to the center line of
+// |target_rect|. 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 {
@@ -626,6 +667,15 @@ void View::ConvertPointToView(const View* source,
}
// static
+void View::ConvertRectToView(const View* source,
+ const View* target,
+ gfx::Rect* rect) {
+ gfx::Point rect_location(rect->x(), rect->y());
+ View::ConvertPointToView(source, target, &rect_location);
+ rect->set_origin(rect_location);
+}
+
+// static
void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
@@ -763,6 +813,44 @@ View* View::GetEventHandlerForPoint(const gfx::Point& point) {
return this;
}
+View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
sky 2012/08/03 23:46:36 I'm hoping its possible to combine this and GetEve
sky 2012/08/03 23:52:57 Gah, I guess I can't nuke my comments. Here's my s
+ View* closest_view = NULL;
+ int best_dist_so_far = INT_MAX;
+ for (int i = child_count() - 1; i >= 0; --i) {
+ View* child = child_at(i);
+ if (!child->visible())
+ continue;
+
+ gfx::Rect child_rect(child->bounds());
+ if (!child_rect.Intersects(rect))
+ continue;
+
+ gfx::Rect rect_in_child_coords(rect);
+ View::ConvertRectToView(this, child, &rect_in_child_coords);
+ View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
+ if (!cur_view)
sky 2012/08/03 23:46:36 between 830-831 something like: if (rect.IsEmpty(
sky 2012/08/03 23:52:57 if (type == MOUSE) return cur_view;
+ continue;
+
+ gfx::Point touch_center(rect.CenterPoint());
+ gfx::Rect cur_rect(cur_view->GetLocalBounds());
+ View::ConvertRectToView(cur_view, this, &cur_rect);
+ int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center,
sky 2012/08/03 23:46:36 If GetEventHandlerForRect returns a view, don't we
+ cur_rect);
+ if (!closest_view || cur_dist < best_dist_so_far) {
+ closest_view = cur_view;
sky 2012/08/03 23:46:36 result->best_view = closest_view;
+ best_dist_so_far = cur_dist;
+ }
+ }
+
sky 2012/08/03 23:46:36 if (!result->view_at_center && bounds().contains(c
+ if (closest_view)
+ return closest_view;
sky 2012/08/03 23:46:36 This would become return true.
+
+ gfx::Rect view_rect(GetLocalBounds());
+ if (PercentCoveredBy(view_rect, rect) >= kFuzzingOverlapPercentage)
+ return this;
sky 2012/08/03 23:46:36 result->best_view = this; return true;
+ return NULL;
sky 2012/08/03 23:46:36 return false;
sky 2012/08/03 23:52:57 return type == MOUSE ? this : NULL;
+}
+
gfx::NativeCursor View::GetCursor(const MouseEvent& event) {
#if defined(OS_WIN) && !defined(USE_AURA)
static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW);
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698