1 // Copyright 2015 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 "components/view_manager/event_dispatcher.h"
7 #include "components/view_manager/server_view.h"
8 #include "components/view_manager/view_coordinate_conversions.h"
9 #include "components/view_manager/view_locator.h"
10 #include "components/view_manager/view_tree_host_impl.h"
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/point_f.h"
14 namespace view_manager
{
16 EventDispatcher::EventDispatcher(ViewTreeHostImpl
* view_tree_host
)
17 : view_tree_host_(view_tree_host
) {
20 EventDispatcher::~EventDispatcher() {
23 void EventDispatcher::AddAccelerator(uint32_t id
,
24 mojo::KeyboardCode keyboard_code
,
25 mojo::EventFlags flags
) {
27 for (const auto& pair
: accelerators_
) {
28 DCHECK(pair
.first
!= id
);
29 DCHECK(pair
.second
.keyboard_code
!= keyboard_code
||
30 pair
.second
.flags
!= flags
);
33 accelerators_
.insert(Entry(id
, Accelerator(keyboard_code
, flags
)));
36 void EventDispatcher::RemoveAccelerator(uint32_t id
) {
37 auto it
= accelerators_
.find(id
);
38 DCHECK(it
!= accelerators_
.end());
39 accelerators_
.erase(it
);
42 void EventDispatcher::OnEvent(mojo::EventPtr event
) {
43 if (event
->action
== mojo::EVENT_TYPE_KEY_PRESSED
&&
44 !event
->key_data
->is_char
) {
45 uint32_t accelerator
= 0u;
46 if (FindAccelerator(*event
, &accelerator
)) {
47 view_tree_host_
->OnAccelerator(accelerator
, event
.Pass());
52 ServerView
* target
= FindEventTarget(event
.get());
54 // Update focus on pointer-down.
55 if (event
->action
== mojo::EVENT_TYPE_POINTER_DOWN
)
56 view_tree_host_
->SetFocusedView(target
);
57 view_tree_host_
->DispatchInputEventToView(target
, event
.Pass());
61 bool EventDispatcher::FindAccelerator(const mojo::Event
& event
,
62 uint32_t* accelerator_id
) {
63 DCHECK(event
.key_data
);
64 for (const auto& pair
: accelerators_
) {
65 if (pair
.second
.keyboard_code
== event
.key_data
->windows_key_code
&&
66 pair
.second
.flags
== event
.flags
) {
67 *accelerator_id
= pair
.first
;
74 ServerView
* EventDispatcher::FindEventTarget(mojo::Event
* event
) {
75 ServerView
* focused_view
= view_tree_host_
->GetFocusedView();
76 if (event
->pointer_data
) {
77 ServerView
* root
= view_tree_host_
->root_view();
78 const gfx::Point
root_point(static_cast<int>(event
->pointer_data
->x
),
79 static_cast<int>(event
->pointer_data
->y
));
80 ServerView
* target
= focused_view
;
81 if (event
->action
== mojo::EVENT_TYPE_POINTER_DOWN
|| !target
||
82 !root
->Contains(target
)) {
83 target
= FindDeepestVisibleView(root
, root_point
);
86 const gfx::PointF
local_point(ConvertPointFBetweenViews(
88 gfx::PointF(event
->pointer_data
->x
, event
->pointer_data
->y
)));
89 event
->pointer_data
->x
= local_point
.x();
90 event
->pointer_data
->y
= local_point
.y();
97 } // namespace view_manager