| Index: ui/views/view.cc
|
| diff --git a/ui/views/view.cc b/ui/views/view.cc
|
| index 858248d64d7b457a18daf25d123a4d69cdfd878d..b3cb9a203e0c823bc617c7b720baa5d8e58b18c6 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);
|
| +}
|
| +
|
| +// static
|
| void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
|
| DCHECK(src);
|
| DCHECK(p);
|
| @@ -749,19 +807,66 @@ 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)),
|
| + EVENT_HANDLER_TYPE_MOUSE);
|
| +}
|
| +
|
| +View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
|
| + // TODO: Consider expanding small (single point) rectangles.
|
| + return GetEventHandler(rect, EVENT_HANDLER_TYPE_TOUCH);
|
| +}
|
| +
|
| +View* View::GetEventHandler(const gfx::Rect& rect, EventType type) {
|
| + View* closest_view = NULL;
|
| + 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);
|
| + 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 == EVENT_HANDLER_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) {
|
| + 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)
|
| + return closest_untouched_view;
|
| + return type == EVENT_HANDLER_TYPE_MOUSE ? this : NULL;
|
| }
|
|
|
| gfx::NativeCursor View::GetCursor(const ui::MouseEvent& event) {
|
|
|