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 ui::EventTarget
* ViewTargeter::FindTargetForEvent(ui::EventTarget
* root
,
32 View
* view
= static_cast<View
*>(root
);
34 if (event
->IsKeyEvent())
35 return FindTargetForKeyEvent(view
, *static_cast<ui::KeyEvent
*>(event
));
37 if (event
->IsScrollEvent()) {
38 return FindTargetForScrollEvent(view
,
39 *static_cast<ui::ScrollEvent
*>(event
));
42 if (event
->IsGestureEvent())
43 return FindTargetForGestureEvent(view
, *(event
->AsGestureEvent()));
45 NOTREACHED() << "ViewTargeter does not yet support this event type.";
49 ui::EventTarget
* ViewTargeter::FindNextBestTarget(
50 ui::EventTarget
* previous_target
,
55 if (event
->IsGestureEvent()) {
56 return FindNextBestTargetForGestureEvent(previous_target
,
57 *(event
->AsGestureEvent()));
60 return previous_target
->GetParentTarget();
63 bool ViewTargeter::SubtreeCanAcceptEvent(
64 ui::EventTarget
* target
,
65 const ui::LocatedEvent
& event
) const {
70 bool ViewTargeter::EventLocationInsideBounds(
71 ui::EventTarget
* target
,
72 const ui::LocatedEvent
& event
) const {
77 View
* ViewTargeter::FindTargetForKeyEvent(View
* root
, const ui::KeyEvent
& key
) {
78 if (root
->GetFocusManager())
79 return root
->GetFocusManager()->GetFocusedView();
83 View
* ViewTargeter::FindTargetForScrollEvent(View
* root
,
84 const ui::ScrollEvent
& scroll
) {
85 gfx::Rect
rect(scroll
.location(), gfx::Size(1, 1));
86 return root
->GetEffectiveViewTargeter()->TargetForRect(root
, rect
);
89 View
* ViewTargeter::FindTargetForGestureEvent(View
* root
,
90 const ui::GestureEvent
& gesture
) {
91 // TODO(tdanderson): The only code path that performs targeting for gestures
92 // uses the ViewTargeter installed on the RootView (i.e.,
93 // a RootViewTargeter). Provide a default implementation
94 // here if we need to be able to perform gesture targeting
95 // starting at an arbitrary node in a Views tree.
100 ui::EventTarget
* ViewTargeter::FindNextBestTargetForGestureEvent(
101 ui::EventTarget
* previous_target
,
102 const ui::GestureEvent
& gesture
) {