1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/view_targeter.h"
7 #include "ui/events/event_target.h"
8 #include "ui/views/focus/focus_manager.h"
9 #include "ui/views/view.h"
10 #include "ui/views/view_targeter_delegate.h"
14 ViewTargeter::ViewTargeter(ViewTargeterDelegate
* delegate
)
15 : delegate_(delegate
) {
19 ViewTargeter::~ViewTargeter() {}
21 bool ViewTargeter::DoesIntersectRect(const View
* target
,
22 const gfx::Rect
& rect
) const {
23 return delegate_
->DoesIntersectRect(target
, rect
);
26 View
* ViewTargeter::TargetForRect(View
* root
, const gfx::Rect
& rect
) const {
27 return delegate_
->TargetForRect(root
, rect
);
30 gfx::RectF
ViewTargeter::BoundsForEvent(const ui::LocatedEvent
& event
) const {
31 gfx::RectF
event_bounds(event
.location_f(), gfx::SizeF(1, 1));
32 if (event
.IsGestureEvent()) {
33 const ui::GestureEvent
& gesture
= *(event
.AsGestureEvent());
34 event_bounds
= gesture
.details().bounding_box_f();
40 ui::EventTarget
* ViewTargeter::FindTargetForEvent(ui::EventTarget
* root
,
42 View
* view
= static_cast<View
*>(root
);
43 if (event
->IsKeyEvent())
44 return FindTargetForKeyEvent(view
, *static_cast<ui::KeyEvent
*>(event
));
45 else if (event
->IsScrollEvent())
46 return EventTargeter::FindTargetForEvent(root
, event
);
48 NOTREACHED() << "ViewTargeter does not yet support this event type.";
52 ui::EventTarget
* ViewTargeter::FindNextBestTarget(
53 ui::EventTarget
* previous_target
,
55 return previous_target
->GetParentTarget();
58 bool ViewTargeter::SubtreeCanAcceptEvent(
59 ui::EventTarget
* target
,
60 const ui::LocatedEvent
& event
) const {
61 views::View
* view
= static_cast<views::View
*>(target
);
65 if (!view
->CanProcessEventsWithinSubtree())
71 bool ViewTargeter::EventLocationInsideBounds(
72 ui::EventTarget
* target
,
73 const ui::LocatedEvent
& event
) const {
74 views::View
* view
= static_cast<views::View
*>(target
);
75 gfx::Rect
rect(event
.location(), gfx::Size(1, 1));
76 gfx::RectF
rect_in_view_coords_f(rect
);
78 View::ConvertRectToTarget(view
->parent(), view
, &rect_in_view_coords_f
);
79 gfx::Rect rect_in_view_coords
= gfx::ToEnclosingRect(rect_in_view_coords_f
);
81 // TODO(tdanderson): Don't call into HitTestRect() directly here.
82 // See crbug.com/370579.
83 return view
->HitTestRect(rect_in_view_coords
);
86 View
* ViewTargeter::FindTargetForKeyEvent(View
* view
, const ui::KeyEvent
& key
) {
87 if (view
->GetFocusManager())
88 return view
->GetFocusManager()->GetFocusedView();