Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / mus / event_dispatcher.cc
blobc505f2f668bdbf7c03dd39dc5b76e0a3b350ebdd
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/mus/event_dispatcher.h"
7 #include "components/mus/server_view.h"
8 #include "components/mus/view_coordinate_conversions.h"
9 #include "components/mus/view_locator.h"
10 #include "components/mus/view_tree_host_impl.h"
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/point_f.h"
14 namespace mus {
16 EventDispatcher::EventDispatcher(ViewTreeHostImpl* view_tree_host)
17 : view_tree_host_(view_tree_host) {}
19 EventDispatcher::~EventDispatcher() {}
21 void EventDispatcher::AddAccelerator(uint32_t id,
22 mojo::KeyboardCode keyboard_code,
23 mojo::EventFlags flags) {
24 #if !defined(NDEBUG)
25 for (const auto& pair : accelerators_) {
26 DCHECK(pair.first != id);
27 DCHECK(pair.second.keyboard_code != keyboard_code ||
28 pair.second.flags != flags);
30 #endif
31 accelerators_.insert(Entry(id, Accelerator(keyboard_code, flags)));
34 void EventDispatcher::RemoveAccelerator(uint32_t id) {
35 auto it = accelerators_.find(id);
36 DCHECK(it != accelerators_.end());
37 accelerators_.erase(it);
40 void EventDispatcher::OnEvent(mojo::EventPtr event) {
41 if (event->action == mojo::EVENT_TYPE_KEY_PRESSED &&
42 !event->key_data->is_char) {
43 uint32_t accelerator = 0u;
44 if (FindAccelerator(*event, &accelerator)) {
45 view_tree_host_->OnAccelerator(accelerator, event.Pass());
46 return;
50 ServerView* target = FindEventTarget(event.get());
51 if (target) {
52 // Update focus on pointer-down.
53 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN)
54 view_tree_host_->SetFocusedView(target);
55 view_tree_host_->DispatchInputEventToView(target, event.Pass());
59 bool EventDispatcher::FindAccelerator(const mojo::Event& event,
60 uint32_t* accelerator_id) {
61 DCHECK(event.key_data);
62 for (const auto& pair : accelerators_) {
63 if (pair.second.keyboard_code == event.key_data->windows_key_code &&
64 pair.second.flags == event.flags) {
65 *accelerator_id = pair.first;
66 return true;
69 return false;
72 ServerView* EventDispatcher::FindEventTarget(mojo::Event* event) {
73 ServerView* focused_view = view_tree_host_->GetFocusedView();
74 if (event->pointer_data) {
75 ServerView* root = view_tree_host_->root_view();
76 const gfx::Point root_point(
77 static_cast<int>(event->pointer_data->location->x),
78 static_cast<int>(event->pointer_data->location->y));
79 ServerView* target = focused_view;
80 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN || !target ||
81 !root->Contains(target)) {
82 target = FindDeepestVisibleView(root, root_point);
83 CHECK(target);
85 const gfx::PointF local_point(ConvertPointFBetweenViews(
86 root, target, gfx::PointF(event->pointer_data->location->x,
87 event->pointer_data->location->y)));
88 event->pointer_data->location->x = local_point.x();
89 event->pointer_data->location->y = local_point.y();
90 return target;
93 return focused_view;
96 } // namespace mus