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(mojo::KeyboardCode keyboard_code
,
24 mojo::EventFlags flags
) {
25 accelerators_
.insert(Accelerator(keyboard_code
, flags
));
28 void EventDispatcher::RemoveAccelerator(mojo::KeyboardCode keyboard_code
,
29 mojo::EventFlags flags
) {
30 accelerators_
.erase(Accelerator(keyboard_code
, flags
));
33 void EventDispatcher::OnEvent(ServerView
* root
, mojo::EventPtr event
) {
34 if (event
->pointer_data
) {
35 const gfx::Point
root_point(static_cast<int>(event
->pointer_data
->x
),
36 static_cast<int>(event
->pointer_data
->y
));
37 ServerView
* target
= connection_manager_
->GetFocusedView();
38 if (event
->action
== mojo::EVENT_TYPE_POINTER_DOWN
|| !target
||
39 !root
->Contains(target
)) {
40 target
= FindDeepestVisibleView(root
, root_point
);
42 connection_manager_
->SetFocusedView(target
);
44 const gfx::PointF
local_point(ConvertPointFBetweenViews(
46 gfx::PointF(event
->pointer_data
->x
, event
->pointer_data
->y
)));
47 event
->pointer_data
->x
= local_point
.x();
48 event
->pointer_data
->y
= local_point
.y();
49 connection_manager_
->DispatchInputEventToView(target
, event
.Pass());
50 } else if (event
->action
== mojo::EVENT_TYPE_KEY_PRESSED
&&
51 accelerators_
.count(Accelerator(event
->key_data
->windows_key_code
,
53 connection_manager_
->OnAccelerator(root
, event
.Pass());
55 ServerView
* focused_view
= connection_manager_
->GetFocusedView();
57 connection_manager_
->DispatchInputEventToView(focused_view
, event
.Pass());
61 } // namespace view_manager