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/connection_manager.h"
8 #include "components/view_manager/server_view.h"
9 #include "components/view_manager/view_coordinate_conversions.h"
10 #include "components/view_manager/view_locator.h"
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/point_f.h"
14 namespace view_manager
{
16 EventDispatcher::EventDispatcher(ConnectionManager
* connection_manager
)
17 : connection_manager_(connection_manager
) {
20 EventDispatcher::~EventDispatcher() {
23 void EventDispatcher::AddAccelerator(uint32_t id
,
24 mojo::KeyboardCode keyboard_code
,
25 mojo::EventFlags flags
) {
27 std::for_each(accelerators_
.begin(), accelerators_
.end(),
28 [id
, keyboard_code
, flags
](const Entry
& entry
) {
29 DCHECK(entry
.first
!= id
);
30 DCHECK(entry
.second
.keyboard_code
!= keyboard_code
||
31 entry
.second
.flags
!= flags
);
34 accelerators_
.insert(Entry(id
, Accelerator(keyboard_code
, flags
)));
37 void EventDispatcher::RemoveAccelerator(uint32_t id
) {
38 auto it
= accelerators_
.find(id
);
39 DCHECK(it
!= accelerators_
.end());
40 accelerators_
.erase(it
);
43 void EventDispatcher::OnEvent(ServerView
* root
, mojo::EventPtr event
) {
44 if (event
->pointer_data
) {
45 const gfx::Point
root_point(static_cast<int>(event
->pointer_data
->x
),
46 static_cast<int>(event
->pointer_data
->y
));
47 ServerView
* target
= connection_manager_
->GetFocusedView();
48 if (event
->action
== mojo::EVENT_TYPE_POINTER_DOWN
|| !target
||
49 !root
->Contains(target
)) {
50 target
= FindDeepestVisibleView(root
, root_point
);
52 connection_manager_
->SetFocusedView(target
);
54 const gfx::PointF
local_point(ConvertPointFBetweenViews(
56 gfx::PointF(event
->pointer_data
->x
, event
->pointer_data
->y
)));
57 event
->pointer_data
->x
= local_point
.x();
58 event
->pointer_data
->y
= local_point
.y();
59 connection_manager_
->DispatchInputEventToView(target
, event
.Pass());
61 uint32_t accelerator_id
= 0;
62 if (event
->action
== mojo::EVENT_TYPE_KEY_PRESSED
&&
63 HandleAccelerator(event
->key_data
->windows_key_code
, event
->flags
,
65 // For accelerators, normal event dispatch is bypassed.
66 connection_manager_
->OnAccelerator(root
, accelerator_id
, event
.Pass());
69 ServerView
* focused_view
= connection_manager_
->GetFocusedView();
71 connection_manager_
->DispatchInputEventToView(focused_view
, event
.Pass());
75 bool EventDispatcher::HandleAccelerator(mojo::KeyboardCode keyboard_code
,
76 mojo::EventFlags flags
,
77 uint32_t* accelerator_id
) {
78 auto it
= std::find_if(accelerators_
.begin(), accelerators_
.end(),
79 [keyboard_code
, flags
](const Entry
& entry
) {
80 return entry
.second
.keyboard_code
== keyboard_code
&&
81 entry
.second
.flags
== flags
;
83 bool found
= it
!= accelerators_
.end();
85 *accelerator_id
= it
->first
;
89 } // namespace view_manager