OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/mus/event_dispatcher.h" | |
6 | |
7 #include "cc/surfaces/surface_id.h" | |
8 #include "components/mus/connection_manager.h" | |
9 #include "components/mus/server_view.h" | |
10 #include "components/mus/server_view_delegate.h" | |
11 #include "components/mus/surfaces/surfaces_state.h" | |
12 #include "components/mus/view_coordinate_conversions.h" | |
13 #include "components/mus/view_tree_host_impl.h" | |
14 #include "ui/gfx/geometry/point.h" | |
15 #include "ui/gfx/geometry/point_f.h" | |
16 | |
17 namespace mus { | |
18 | |
19 EventDispatcher::EventDispatcher(ViewTreeHostImpl* view_tree_host) | |
20 : view_tree_host_(view_tree_host) {} | |
21 | |
22 EventDispatcher::~EventDispatcher() {} | |
23 | |
24 void EventDispatcher::AddAccelerator(uint32_t id, | |
25 mojo::KeyboardCode keyboard_code, | |
26 mojo::EventFlags flags) { | |
27 #if !defined(NDEBUG) | |
28 for (const auto& pair : accelerators_) { | |
29 DCHECK(pair.first != id); | |
30 DCHECK(pair.second.keyboard_code != keyboard_code || | |
31 pair.second.flags != flags); | |
32 } | |
33 #endif | |
34 accelerators_.insert(Entry(id, Accelerator(keyboard_code, flags))); | |
35 } | |
36 | |
37 void EventDispatcher::RemoveAccelerator(uint32_t id) { | |
38 auto it = accelerators_.find(id); | |
39 DCHECK(it != accelerators_.end()); | |
40 accelerators_.erase(it); | |
41 } | |
42 | |
43 void EventDispatcher::OnEvent(mojo::EventPtr event) { | |
44 if (event->action == mojo::EVENT_TYPE_KEY_PRESSED && | |
45 !event->key_data->is_char) { | |
46 uint32_t accelerator = 0u; | |
47 if (FindAccelerator(*event, &accelerator)) { | |
48 view_tree_host_->OnAccelerator(accelerator, event.Pass()); | |
49 return; | |
50 } | |
51 } | |
52 | |
53 ServerView* target = FindEventTarget(event.get()); | |
54 if (target) { | |
55 // Update focus on pointer-down. | |
56 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN) | |
57 view_tree_host_->SetFocusedView(target); | |
58 view_tree_host_->DispatchInputEventToView(target, event.Pass()); | |
59 } | |
60 } | |
61 | |
62 bool EventDispatcher::FindAccelerator(const mojo::Event& event, | |
63 uint32_t* accelerator_id) { | |
64 DCHECK(event.key_data); | |
65 for (const auto& pair : accelerators_) { | |
66 if (pair.second.keyboard_code == event.key_data->windows_key_code && | |
67 pair.second.flags == event.flags) { | |
68 *accelerator_id = pair.first; | |
69 return true; | |
70 } | |
71 } | |
72 return false; | |
73 } | |
74 | |
75 ServerView* EventDispatcher::FindEventTarget(mojo::Event* event) { | |
76 ServerView* focused_view = view_tree_host_->GetFocusedView(); | |
77 if (event->key_data) | |
78 return focused_view; | |
79 | |
80 DCHECK(event->pointer_data || event->wheel_data) << "Unknown event type: " | |
81 << event->action; | |
82 | |
83 mojo::LocationData* event_location = nullptr; | |
84 if (event->pointer_data) | |
85 event_location = event->pointer_data->location.get(); | |
86 else if (event->wheel_data) | |
87 event_location = event->wheel_data->location.get(); | |
88 DCHECK(event_location); | |
89 gfx::Point location(static_cast<int>(event_location->x), | |
90 static_cast<int>(event_location->y)); | |
91 ServerView* target = focused_view; | |
92 ServerView* root = view_tree_host_->root_view(); | |
93 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN || !target || | |
94 !root->Contains(target)) { | |
95 target = FindDeepestVisibleViewFromSurface(&location); | |
96 // Surface-based hit-testing will not return a valid target if no | |
97 // compositor-frame have been submitted (e.g. in unit-tests). | |
98 if (!target) | |
99 target = FindDeepestVisibleView(root, &location); | |
100 CHECK(target); | |
101 } else { | |
102 gfx::Point old_point = location; | |
103 location = ConvertPointBetweenViews(root, target, location); | |
104 } | |
105 | |
106 event_location->x = location.x(); | |
107 event_location->y = location.y(); | |
108 return target; | |
109 } | |
110 | |
111 ServerView* EventDispatcher::FindDeepestVisibleView(ServerView* view, | |
112 gfx::Point* location) { | |
113 for (ServerView* child : view->GetChildren()) { | |
114 if (!child->visible()) | |
115 continue; | |
116 | |
117 // TODO(sky): support transform. | |
118 gfx::Point child_location(location->x() - child->bounds().x(), | |
119 location->y() - child->bounds().y()); | |
120 if (child_location.x() >= 0 && child_location.y() >= 0 && | |
121 child_location.x() < child->bounds().width() && | |
122 child_location.y() < child->bounds().height()) { | |
123 *location = child_location; | |
124 return FindDeepestVisibleView(child, location); | |
125 } | |
126 } | |
127 return view; | |
128 } | |
129 | |
130 ServerView* EventDispatcher::FindDeepestVisibleViewFromSurface( | |
131 gfx::Point* location) { | |
132 if (view_tree_host_->surface_id().is_null()) | |
133 return nullptr; | |
134 | |
135 gfx::Transform transform_to_target_surface; | |
136 cc::SurfaceId target_surface = | |
137 view_tree_host_->root_view()->delegate() | |
138 ->GetSurfacesState() | |
139 ->hit_tester() | |
140 ->GetTargetSurfaceAtPoint(view_tree_host_->surface_id(), *location, | |
141 &transform_to_target_surface); | |
142 ViewId id = ViewIdFromTransportId( | |
143 cc::SurfaceIdAllocator::NamespaceForId(target_surface)); | |
144 ServerView* target = view_tree_host_->connection_manager()->GetView(id); | |
145 // TODO(fsamuel): This should be a DCHECK but currently we use stale | |
146 // information to decide where to route input events. This should be fixed | |
147 // once we implement a UI scheduler. | |
148 if (target) { | |
149 transform_to_target_surface.TransformPoint(location); | |
150 return target; | |
151 } | |
152 return nullptr; | |
153 } | |
154 | |
155 } // namespace mus | |
OLD | NEW |