#include "ui/views/view_targeter_delegate.h"
#include <limits.h>
#include "base/containers/adapters.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/views/rect_based_targeting_utils.h"
#include "ui/views/view.h"
namespace {
static const float kRectTargetOverlap = 0.6f;
}
namespace views {
bool ViewTargeterDelegate::DoesIntersectRect(const View* target,
const gfx::Rect& rect) const {
return target->GetLocalBounds().Intersects(rect);
}
View* ViewTargeterDelegate::TargetForRect(View* root, const gfx::Rect& rect) {
View* rect_view = nullptr;
int rect_view_distance = INT_MAX;
View* point_view = nullptr;
View::Views children = root->GetChildrenInZOrder();
DCHECK_EQ(root->children().size(), children.size());
for (auto* child : base::Reversed(children)) {
if (!child->GetCanProcessEventsWithinSubtree() || !child->GetEnabled())
continue;
if (!child->GetVisible())
continue;
gfx::RectF rect_in_child_coords_f(rect);
View::ConvertRectToTarget(root, child, &rect_in_child_coords_f);
gfx::Rect rect_in_child_coords =
gfx::ToEnclosingRect(rect_in_child_coords_f);
if (!child->HitTestRect(rect_in_child_coords))
continue;
View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
if (views::UsePointBasedTargeting(rect))
return cur_view;
gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
View::ConvertRectToTarget(cur_view, root, &cur_view_bounds_f);
gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(cur_view_bounds_f);
if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
gfx::Point touch_center(rect.CenterPoint());
int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
cur_view_bounds);
if (!rect_view || cur_dist < rect_view_distance) {
rect_view = cur_view;
rect_view_distance = cur_dist;
}
} else if (!rect_view && !point_view) {
gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint());
if (child->HitTestPoint(point_in_child_coords))
point_view = child->GetEventHandlerForPoint(point_in_child_coords);
}
}
if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
return root;
return rect_view ? rect_view : point_view;
}
}