Chromium Code Reviews| Index: ui/views/view.cc |
| diff --git a/ui/views/view.cc b/ui/views/view.cc |
| index 406ad42914754c13fa73df1b85c722969147748d..c7771f76bcc8e25f1df2727915d5b3f1ee08bf15 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; |
|
sky
2012/08/02 15:52:08
Is it possible for rect_a_area to be 0?
tdanderson
2012/08/02 18:11:37
I don't believe this is possible given our impleme
|
| +} |
| + |
| +int DistanceToInterval(int pos, int start, int end) { |
|
sky
2012/08/02 15:52:08
Add description.
tdanderson
2012/08/02 18:11:37
Done.
|
| + if (pos < start) |
| + return start - pos; |
| + if (pos > end) |
| + return end - pos; |
| + 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 { |
| @@ -763,6 +802,42 @@ View* View::GetEventHandlerForPoint(const gfx::Point& point) { |
| return this; |
| } |
| +View* View::GetEventHandlerForRect(const gfx::Rect& touch_rect) { |
| + if (!child_count()) { |
|
sky
2012/08/02 15:52:08
The structure of this should roughly match that of
tdanderson
2012/08/02 18:11:37
Done.
|
| + gfx::Rect view_rect(GetBoundsInScreen()); |
| + if (PercentCoveredBy(view_rect, touch_rect) >= kFuzzingOverlapPercentage) |
| + return this; |
| + return NULL; |
| + } |
| + |
| + 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->GetBoundsInScreen()); |
| + if (!child_rect.Intersects(touch_rect)) |
| + continue; |
| + |
| + View* cur_view = child->GetEventHandlerForRect(touch_rect); |
| + if (!cur_view) |
| + continue; |
| + |
| + gfx::Point touch_center(touch_rect.CenterPoint()); |
| + gfx::Rect cur_rect(cur_view->GetBoundsInScreen()); |
| + int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center, |
| + cur_rect); |
| + if (!closest_view || cur_dist < best_dist_so_far) { |
| + closest_view = cur_view; |
| + best_dist_so_far = cur_dist; |
| + } |
| + } |
| + |
| + return closest_view; |
| +} |
| + |
| gfx::NativeCursor View::GetCursor(const MouseEvent& event) { |
| #if defined(OS_WIN) && !defined(USE_AURA) |
| static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); |