OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "cc/surfaces/surface_hittest.h" | 9 #include "cc/surfaces/surface_hittest.h" |
10 #include "components/mus/surfaces/surfaces_state.h" | 10 #include "components/mus/surfaces/surfaces_state.h" |
11 #include "components/mus/ws/event_dispatcher_delegate.h" | 11 #include "components/mus/ws/event_dispatcher_delegate.h" |
12 #include "components/mus/ws/server_window.h" | 12 #include "components/mus/ws/server_window.h" |
13 #include "components/mus/ws/server_window_delegate.h" | 13 #include "components/mus/ws/server_window_delegate.h" |
14 #include "components/mus/ws/window_coordinate_conversions.h" | 14 #include "components/mus/ws/window_coordinate_conversions.h" |
15 #include "components/mus/ws/window_finder.h" | 15 #include "components/mus/ws/window_finder.h" |
16 #include "components/mus/ws/window_tree_host_impl.h" | 16 #include "components/mus/ws/window_tree_host_impl.h" |
17 #include "mojo/converters/geometry/geometry_type_converters.h" | 17 #include "mojo/converters/geometry/geometry_type_converters.h" |
18 #include "ui/gfx/geometry/point.h" | 18 #include "ui/gfx/geometry/point.h" |
19 #include "ui/gfx/geometry/point_conversions.h" | 19 #include "ui/gfx/geometry/point_conversions.h" |
20 | 20 |
21 namespace mus { | 21 namespace mus { |
22 namespace ws { | 22 namespace ws { |
23 namespace { | 23 namespace { |
24 | 24 |
25 bool IsOnlyOneMouseButtonDown(mojom::EventFlags flags) { | 25 bool IsOnlyOneMouseButtonDown(int flags) { |
26 const uint32_t mouse_only_flags = | 26 const uint32_t mouse_only_flags = |
27 flags & (mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON | | 27 flags & |
28 mojom::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | | 28 (mojom::kEventFlagLeftMouseButton | mojom::kEventFlagMiddleMouseButton | |
29 mojom::EVENT_FLAGS_RIGHT_MOUSE_BUTTON); | 29 mojom::kEventFlagRightMouseButton); |
30 return mouse_only_flags == mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON || | 30 return mouse_only_flags == mojom::kEventFlagLeftMouseButton || |
31 mouse_only_flags == mojom::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON || | 31 mouse_only_flags == mojom::kEventFlagMiddleMouseButton || |
32 mouse_only_flags == mojom::EVENT_FLAGS_RIGHT_MOUSE_BUTTON; | 32 mouse_only_flags == mojom::kEventFlagRightMouseButton; |
33 } | 33 } |
34 | 34 |
35 bool IsLocationInNonclientArea(const ServerWindow* target, | 35 bool IsLocationInNonclientArea(const ServerWindow* target, |
36 const gfx::Point& location) { | 36 const gfx::Point& location) { |
37 if (!target->parent()) | 37 if (!target->parent()) |
38 return false; | 38 return false; |
39 | 39 |
40 gfx::Rect client_area(target->bounds().size()); | 40 gfx::Rect client_area(target->bounds().size()); |
41 client_area.Inset(target->client_area()); | 41 client_area.Inset(target->client_area()); |
42 if (client_area.Contains(location)) | 42 if (client_area.Contains(location)) |
(...skipping 11 matching lines...) Expand all Loading... |
54 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, | 54 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, |
55 event.pointer_data->location->y)); | 55 event.pointer_data->location->y)); |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 class EventMatcher { | 60 class EventMatcher { |
61 public: | 61 public: |
62 explicit EventMatcher(const mojom::EventMatcher& matcher) | 62 explicit EventMatcher(const mojom::EventMatcher& matcher) |
63 : fields_to_match_(NONE), | 63 : fields_to_match_(NONE), |
64 event_type_(mojom::EVENT_TYPE_UNKNOWN), | 64 event_type_(mojom::EventType::UNKNOWN), |
65 event_flags_(mojom::EVENT_FLAGS_NONE), | 65 event_flags_(mojom::kEventFlagNone), |
66 ignore_event_flags_(mojom::EVENT_FLAGS_NONE), | 66 ignore_event_flags_(mojom::kEventFlagNone), |
67 keyboard_code_(mojom::KEYBOARD_CODE_UNKNOWN), | 67 keyboard_code_(mojom::KeyboardCode::UNKNOWN), |
68 pointer_kind_(mojom::POINTER_KIND_MOUSE) { | 68 pointer_kind_(mojom::PointerKind::MOUSE) { |
69 if (matcher.type_matcher) { | 69 if (matcher.type_matcher) { |
70 fields_to_match_ |= TYPE; | 70 fields_to_match_ |= TYPE; |
71 event_type_ = matcher.type_matcher->type; | 71 event_type_ = matcher.type_matcher->type; |
72 } | 72 } |
73 if (matcher.flags_matcher) { | 73 if (matcher.flags_matcher) { |
74 fields_to_match_ |= FLAGS; | 74 fields_to_match_ |= FLAGS; |
75 event_flags_ = matcher.flags_matcher->flags; | 75 event_flags_ = matcher.flags_matcher->flags; |
76 if (matcher.ignore_flags_matcher) | 76 if (matcher.ignore_flags_matcher) |
77 ignore_event_flags_ = matcher.ignore_flags_matcher->flags; | 77 ignore_event_flags_ = matcher.ignore_flags_matcher->flags; |
78 } | 78 } |
(...skipping 10 matching lines...) Expand all Loading... |
89 pointer_region_ = | 89 pointer_region_ = |
90 matcher.pointer_location_matcher->region.To<gfx::RectF>(); | 90 matcher.pointer_location_matcher->region.To<gfx::RectF>(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 ~EventMatcher() {} | 94 ~EventMatcher() {} |
95 | 95 |
96 bool MatchesEvent(const mojom::Event& event) const { | 96 bool MatchesEvent(const mojom::Event& event) const { |
97 if ((fields_to_match_ & TYPE) && event.action != event_type_) | 97 if ((fields_to_match_ & TYPE) && event.action != event_type_) |
98 return false; | 98 return false; |
99 mojom::EventFlags flags = | 99 int flags = event.flags & ~ignore_event_flags_; |
100 static_cast<mojom::EventFlags>(event.flags & ~ignore_event_flags_); | |
101 if ((fields_to_match_ & FLAGS) && flags != event_flags_) | 100 if ((fields_to_match_ & FLAGS) && flags != event_flags_) |
102 return false; | 101 return false; |
103 if (fields_to_match_ & KEYBOARD_CODE) { | 102 if (fields_to_match_ & KEYBOARD_CODE) { |
104 if (!event.key_data) | 103 if (!event.key_data) |
105 return false; | 104 return false; |
106 if (keyboard_code_ != event.key_data->key_code) | 105 if (static_cast<int32_t>(keyboard_code_) != event.key_data->key_code) |
107 return false; | 106 return false; |
108 } | 107 } |
109 if (fields_to_match_ & POINTER_KIND) { | 108 if (fields_to_match_ & POINTER_KIND) { |
110 if (!event.pointer_data) | 109 if (!event.pointer_data) |
111 return false; | 110 return false; |
112 if (pointer_kind_ != event.pointer_data->kind) | 111 if (pointer_kind_ != event.pointer_data->kind) |
113 return false; | 112 return false; |
114 } | 113 } |
115 if (fields_to_match_ & POINTER_LOCATION) { | 114 if (fields_to_match_ & POINTER_LOCATION) { |
116 // TODO(sad): The tricky part here is to make sure the same coord-space is | 115 // TODO(sad): The tricky part here is to make sure the same coord-space is |
(...skipping 20 matching lines...) Expand all Loading... |
137 NONE = 0, | 136 NONE = 0, |
138 TYPE = 1 << 0, | 137 TYPE = 1 << 0, |
139 FLAGS = 1 << 1, | 138 FLAGS = 1 << 1, |
140 KEYBOARD_CODE = 1 << 2, | 139 KEYBOARD_CODE = 1 << 2, |
141 POINTER_KIND = 1 << 3, | 140 POINTER_KIND = 1 << 3, |
142 POINTER_LOCATION = 1 << 4, | 141 POINTER_LOCATION = 1 << 4, |
143 }; | 142 }; |
144 | 143 |
145 uint32_t fields_to_match_; | 144 uint32_t fields_to_match_; |
146 mojom::EventType event_type_; | 145 mojom::EventType event_type_; |
147 mojom::EventFlags event_flags_; | 146 int event_flags_; |
148 mojom::EventFlags ignore_event_flags_; | 147 int ignore_event_flags_; |
149 mojom::KeyboardCode keyboard_code_; | 148 mojom::KeyboardCode keyboard_code_; |
150 mojom::PointerKind pointer_kind_; | 149 mojom::PointerKind pointer_kind_; |
151 gfx::RectF pointer_region_; | 150 gfx::RectF pointer_region_; |
152 }; | 151 }; |
153 | 152 |
154 //////////////////////////////////////////////////////////////////////////////// | 153 //////////////////////////////////////////////////////////////////////////////// |
155 | 154 |
156 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) | 155 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) |
157 : delegate_(delegate), | 156 : delegate_(delegate), |
158 root_(nullptr), | 157 root_(nullptr), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 void EventDispatcher::RemoveAccelerator(uint32_t id) { | 191 void EventDispatcher::RemoveAccelerator(uint32_t id) { |
193 auto it = accelerators_.find(id); | 192 auto it = accelerators_.find(id); |
194 DCHECK(it != accelerators_.end()); | 193 DCHECK(it != accelerators_.end()); |
195 accelerators_.erase(it); | 194 accelerators_.erase(it); |
196 } | 195 } |
197 | 196 |
198 void EventDispatcher::OnEvent(mojom::EventPtr event) { | 197 void EventDispatcher::OnEvent(mojom::EventPtr event) { |
199 if (!root_) | 198 if (!root_) |
200 return; | 199 return; |
201 | 200 |
202 if (event->action == mojom::EVENT_TYPE_KEY_PRESSED && | 201 if (event->action == mojom::EventType::KEY_PRESSED && |
203 !event->key_data->is_char) { | 202 !event->key_data->is_char) { |
204 uint32_t accelerator = 0u; | 203 uint32_t accelerator = 0u; |
205 if (FindAccelerator(*event, &accelerator)) { | 204 if (FindAccelerator(*event, &accelerator)) { |
206 delegate_->OnAccelerator(accelerator, std::move(event)); | 205 delegate_->OnAccelerator(accelerator, std::move(event)); |
207 return; | 206 return; |
208 } | 207 } |
209 } | 208 } |
210 | 209 |
211 if (event->key_data) { | 210 if (event->key_data) { |
212 ProcessKeyEvent(std::move(event)); | 211 ProcessKeyEvent(std::move(event)); |
(...skipping 10 matching lines...) Expand all Loading... |
223 | 222 |
224 void EventDispatcher::ProcessKeyEvent(mojom::EventPtr event) { | 223 void EventDispatcher::ProcessKeyEvent(mojom::EventPtr event) { |
225 ServerWindow* focused_window = | 224 ServerWindow* focused_window = |
226 delegate_->GetFocusedWindowForEventDispatcher(); | 225 delegate_->GetFocusedWindowForEventDispatcher(); |
227 if (focused_window) | 226 if (focused_window) |
228 delegate_->DispatchInputEventToWindow(focused_window, false, | 227 delegate_->DispatchInputEventToWindow(focused_window, false, |
229 std::move(event)); | 228 std::move(event)); |
230 } | 229 } |
231 | 230 |
232 void EventDispatcher::ProcessPointerEvent(mojom::EventPtr event) { | 231 void EventDispatcher::ProcessPointerEvent(mojom::EventPtr event) { |
233 bool is_mouse_event = | 232 bool is_mouse_event = event->pointer_data && |
234 event->pointer_data && | 233 event->pointer_data->kind == mojom::PointerKind::MOUSE; |
235 event->pointer_data->kind == mojom::PointerKind::POINTER_KIND_MOUSE; | |
236 | 234 |
237 if (is_mouse_event) | 235 if (is_mouse_event) |
238 mouse_pointer_last_location_ = EventLocationToPoint(*event); | 236 mouse_pointer_last_location_ = EventLocationToPoint(*event); |
239 | 237 |
240 const int32_t pointer_id = event->pointer_data->pointer_id; | 238 const int32_t pointer_id = event->pointer_data->pointer_id; |
241 if (event->action == mojom::EVENT_TYPE_WHEEL || | 239 if (event->action == mojom::EventType::WHEEL || |
242 (event->action == mojom::EVENT_TYPE_POINTER_MOVE && | 240 (event->action == mojom::EventType::POINTER_MOVE && |
243 pointer_targets_.count(pointer_id) == 0)) { | 241 pointer_targets_.count(pointer_id) == 0)) { |
244 PointerTarget pointer_target; | 242 PointerTarget pointer_target; |
245 if (pointer_targets_.count(pointer_id) != 0) { | 243 if (pointer_targets_.count(pointer_id) != 0) { |
246 pointer_target = pointer_targets_[pointer_id]; | 244 pointer_target = pointer_targets_[pointer_id]; |
247 } else { | 245 } else { |
248 gfx::Point location(EventLocationToPoint(*event)); | 246 gfx::Point location(EventLocationToPoint(*event)); |
249 pointer_target.window = | 247 pointer_target.window = |
250 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); | 248 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); |
251 pointer_target.in_nonclient_area = | 249 pointer_target.in_nonclient_area = |
252 IsLocationInNonclientArea(pointer_target.window, location); | 250 IsLocationInNonclientArea(pointer_target.window, location); |
253 } | 251 } |
254 if (is_mouse_event && !mouse_button_down_) | 252 if (is_mouse_event && !mouse_button_down_) |
255 mouse_cursor_source_window_ = pointer_target.window; | 253 mouse_cursor_source_window_ = pointer_target.window; |
256 DispatchToPointerTarget(pointer_target, std::move(event)); | 254 DispatchToPointerTarget(pointer_target, std::move(event)); |
257 return; | 255 return; |
258 } | 256 } |
259 | 257 |
260 // Pointer down implicitly captures. | 258 // Pointer down implicitly captures. |
261 if (pointer_targets_.count(pointer_id) == 0) { | 259 if (pointer_targets_.count(pointer_id) == 0) { |
262 DCHECK(event->action == mojom::EVENT_TYPE_POINTER_DOWN); | 260 DCHECK(event->action == mojom::EventType::POINTER_DOWN); |
263 const bool is_first_pointer_down = pointer_targets_.empty(); | 261 const bool is_first_pointer_down = pointer_targets_.empty(); |
264 gfx::Point location(EventLocationToPoint(*event)); | 262 gfx::Point location(EventLocationToPoint(*event)); |
265 ServerWindow* target = | 263 ServerWindow* target = |
266 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); | 264 FindDeepestVisibleWindowForEvents(root_, surface_id_, &location); |
267 DCHECK(target); | 265 DCHECK(target); |
268 if (!IsObservingWindow(target)) | 266 if (!IsObservingWindow(target)) |
269 target->AddObserver(this); | 267 target->AddObserver(this); |
270 | 268 |
271 if (is_mouse_event) { | 269 if (is_mouse_event) { |
272 mouse_button_down_ = true; | 270 mouse_button_down_ = true; |
273 mouse_cursor_source_window_ = target; | 271 mouse_cursor_source_window_ = target; |
274 } | 272 } |
275 | 273 |
276 pointer_targets_[pointer_id].window = target; | 274 pointer_targets_[pointer_id].window = target; |
277 pointer_targets_[pointer_id].in_nonclient_area = | 275 pointer_targets_[pointer_id].in_nonclient_area = |
278 IsLocationInNonclientArea(target, location); | 276 IsLocationInNonclientArea(target, location); |
279 | 277 |
280 if (is_first_pointer_down) | 278 if (is_first_pointer_down) |
281 delegate_->SetFocusedWindowFromEventDispatcher(target); | 279 delegate_->SetFocusedWindowFromEventDispatcher(target); |
282 } | 280 } |
283 | 281 |
284 // Release capture on pointer up. For mouse we only release if there are | 282 // Release capture on pointer up. For mouse we only release if there are |
285 // no buttons down. | 283 // no buttons down. |
286 const bool should_reset_target = | 284 const bool should_reset_target = |
287 (event->action == mojom::EVENT_TYPE_POINTER_UP || | 285 (event->action == mojom::EventType::POINTER_UP || |
288 event->action == mojom::EVENT_TYPE_POINTER_CANCEL) && | 286 event->action == mojom::EventType::POINTER_CANCEL) && |
289 (event->pointer_data->kind != mojom::POINTER_KIND_MOUSE || | 287 (event->pointer_data->kind != mojom::PointerKind::MOUSE || |
290 IsOnlyOneMouseButtonDown(event->flags)); | 288 IsOnlyOneMouseButtonDown(event->flags)); |
291 | 289 |
292 if (should_reset_target && is_mouse_event) { | 290 if (should_reset_target && is_mouse_event) { |
293 // When we release the mouse button, we want the cursor to be sourced from | 291 // When we release the mouse button, we want the cursor to be sourced from |
294 // the window under the mouse pointer, even though we're sending the button | 292 // the window under the mouse pointer, even though we're sending the button |
295 // up event to the window that had implicit capture. We have to set this | 293 // up event to the window that had implicit capture. We have to set this |
296 // before we perform dispatch because the Delegate is going to read this | 294 // before we perform dispatch because the Delegate is going to read this |
297 // information from us. | 295 // information from us. |
298 mouse_button_down_ = false; | 296 mouse_button_down_ = false; |
299 gfx::Point location(EventLocationToPoint(*event)); | 297 gfx::Point location(EventLocationToPoint(*event)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 | 364 |
367 void EventDispatcher::OnWindowDestroyed(ServerWindow* window) { | 365 void EventDispatcher::OnWindowDestroyed(ServerWindow* window) { |
368 CancelPointerEventsToTarget(window); | 366 CancelPointerEventsToTarget(window); |
369 | 367 |
370 if (mouse_cursor_source_window_ == window) | 368 if (mouse_cursor_source_window_ == window) |
371 mouse_cursor_source_window_ = nullptr; | 369 mouse_cursor_source_window_ = nullptr; |
372 } | 370 } |
373 | 371 |
374 } // namespace ws | 372 } // namespace ws |
375 } // namespace mus | 373 } // namespace mus |
OLD | NEW |