Chromium Code Reviews| Index: ui/views/view.cc |
| diff --git a/ui/views/view.cc b/ui/views/view.cc |
| index f0745545e08ec15d1035c744286d9a9084792981..af9fe4905f7e55480df9954f7deacf8eaabdacf2 100644 |
| --- a/ui/views/view.cc |
| +++ b/ui/views/view.cc |
| @@ -28,6 +28,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" |
| @@ -81,6 +82,54 @@ const views::View* GetHierarchyRoot(const views::View* view) { |
| return root; |
| } |
| +// Returns a ratio of the area of intersection compared to the smaller of |
| +// the two rectangles. |
| +double OverlapProportion(const gfx::Rect& rect_one, |
| + const gfx::Rect& rect_two) { |
| + gfx::Rect intersection = rect_one.Intersect(rect_two); |
| + double rect_one_area = rect_one.size().GetArea();; |
| + double rect_two_area = rect_two.size().GetArea();; |
| + double intersection_area = intersection.size().GetArea();; |
| + if ((rect_one_area <= 0) || (rect_two_area <= 0)) |
| + return 0.0; |
| + if (rect_one_area > rect_two_area) |
| + return intersection_area / rect_two_area; |
| + else |
| + return intersection_area / rect_one_area; |
| +} |
| + |
| +// 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 { |
| @@ -627,6 +676,15 @@ void View::ConvertPointToTarget(const View* source, |
| } |
| // static |
| +void View::ConvertRectToTarget(const View* source, |
| + const View* target, |
| + gfx::Rect* rect) { |
| + gfx::Point rect_location(rect->x(), rect->y()); |
| + View::ConvertPointToTarget(source, target, &rect_location); |
| + rect->set_origin(rect_location); |
|
sadrul
2012/09/18 17:29:22
Just converting the origin isn't sufficient. You w
|
| +} |
| + |
| +// static |
| void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
| DCHECK(src); |
| DCHECK(p); |
| @@ -749,19 +807,65 @@ 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 GetEventHandler(gfx::Rect(point, gfx::Size(1, 1)), MOUSE); |
| +} |
| + |
| +View* View::GetEventHandlerForRect(const gfx::Rect& rect) { |
|
sadrul
2012/09/18 17:29:22
Without changes in RootView, it is difficult to un
|
| + // TODO: Consider expanding small (single point) rectangles. |
|
tdanderson
2012/09/18 19:19:02
What do you mean by "expanding" small rectangles?
|
| + return GetEventHandler(rect, TOUCH); |
| +} |
| + |
| +View* View::GetEventHandler(const gfx::Rect& rect, EventType type) { |
|
sky
2012/09/18 19:51:41
ordering doesn't match header.
|
| + View* closest_view = NULL; |
|
tdanderson
2012/09/18 19:19:02
I think calling this |closest_view| in your curren
|
| + View* closest_untouched_view = NULL; |
| + double max_proportion = 0; |
| + int closest_distance = INT_MAX; |
| for (int i = child_count() - 1; i >= 0; --i) { |
| View* child = child_at(i); |
| if (!child->visible()) |
| 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); |
|
sadrul
2012/09/18 17:29:22
It is important to continue to use the hit-testing
|
| + gfx::Rect child_rect(child->bounds()); |
| + |
| + if (!child_rect.Intersects(rect)) |
| + continue; |
| + |
| + gfx::Rect rect_in_child_coords(rect); |
| + View::ConvertRectToTarget(this, child, &rect_in_child_coords); |
| + View* cur_view = child->GetEventHandler(rect_in_child_coords, type); |
| + if (!cur_view) |
| + continue; |
| + |
| + if (type == MOUSE) |
| + return cur_view; |
| + |
| + gfx::Point touch_center(rect.CenterPoint()); |
| + gfx::Rect cur_rect(cur_view->GetLocalBounds()); |
| + View::ConvertRectToTarget(cur_view, this, &cur_rect); |
| + int distance = DistanceSquaredFromCenterLineToPoint(touch_center, |
| + cur_rect); |
| + double proportion = OverlapProportion(cur_rect, rect); |
| + if (proportion == 0) { |
| + if (distance < closest_distance) { |
| + closest_untouched_view = cur_view; |
| + closest_distance = distance; |
| + } |
| + } else if (!closest_view || proportion > max_proportion) { |
|
tdanderson
2012/09/18 19:19:02
I am worried about the case where there are >=2 vi
|
| + closest_view = cur_view; |
| + max_proportion = proportion; |
| + } |
| } |
| - return this; |
| + |
| + if (closest_view) |
| + return closest_view; |
| + |
| + gfx::Rect view_rect(GetLocalBounds()); |
| + double proportion = OverlapProportion(view_rect, rect); |
| + if (proportion > max_proportion) |
| + return this; |
| + if (closest_untouched_view) |
|
tdanderson
2012/09/18 19:19:02
It seems this is only reached if a touch region do
|
| + return closest_untouched_view; |
| + return type == MOUSE ? this : NULL; |
| } |
| gfx::NativeCursor View::GetCursor(const ui::MouseEvent& event) { |