Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: ui/events/mojo/event_struct_traits.cc

Issue 1939133002: StructTraits to map mus::mojom::Event to unique_ptr<ui::Event> (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed feedback. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/events/mojo/event_struct_traits.h ('k') | ui/events/mojo/typemaps.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 "ui/events/mojo/event_struct_traits.h"
6
7 #include "components/mus/public/interfaces/input_event_constants.mojom.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/events/keycodes/dom/keycode_converter.h"
11
12 namespace mojo {
13 namespace {
14
15 mus::mojom::EventType UIEventTypeToMojo(ui::EventType type) {
16 switch (type) {
17 case ui::ET_MOUSE_PRESSED:
18 case ui::ET_TOUCH_PRESSED:
19 case ui::ET_POINTER_DOWN:
20 return mus::mojom::EventType::POINTER_DOWN;
21
22 case ui::ET_MOUSE_DRAGGED:
23 case ui::ET_MOUSE_MOVED:
24 case ui::ET_MOUSE_ENTERED:
25 case ui::ET_TOUCH_MOVED:
26 case ui::ET_POINTER_MOVED:
27 return mus::mojom::EventType::POINTER_MOVE;
28
29 case ui::ET_MOUSE_EXITED:
30 case ui::ET_POINTER_EXITED:
31 return mus::mojom::EventType::MOUSE_EXIT;
32
33 case ui::ET_MOUSEWHEEL:
34 return mus::mojom::EventType::WHEEL;
35
36 case ui::ET_MOUSE_RELEASED:
37 case ui::ET_TOUCH_RELEASED:
38 case ui::ET_POINTER_UP:
39 return mus::mojom::EventType::POINTER_UP;
40
41 case ui::ET_TOUCH_CANCELLED:
42 case ui::ET_POINTER_CANCELLED:
43 return mus::mojom::EventType::POINTER_CANCEL;
44
45 case ui::ET_KEY_PRESSED:
46 return mus::mojom::EventType::KEY_PRESSED;
47
48 case ui::ET_KEY_RELEASED:
49 return mus::mojom::EventType::KEY_RELEASED;
50
51 default:
52 break;
53 }
54 return mus::mojom::EventType::UNKNOWN;
55 }
56
57 ui::EventType MojoMouseEventTypeToUIEvent(mus::mojom::EventType action,
58 int32_t flags) {
59 switch (action) {
60 case mus::mojom::EventType::POINTER_DOWN:
61 return ui::ET_MOUSE_PRESSED;
62
63 case mus::mojom::EventType::POINTER_UP:
64 return ui::ET_MOUSE_RELEASED;
65
66 case mus::mojom::EventType::POINTER_MOVE:
67 if (flags & (mus::mojom::kEventFlagLeftMouseButton |
68 mus::mojom::kEventFlagMiddleMouseButton |
69 mus::mojom::kEventFlagRightMouseButton)) {
70 return ui::ET_MOUSE_DRAGGED;
71 }
72 return ui::ET_MOUSE_MOVED;
73
74 case mus::mojom::EventType::MOUSE_EXIT:
75 return ui::ET_MOUSE_EXITED;
76
77 default:
78 NOTREACHED();
79 }
80
81 return ui::ET_MOUSE_RELEASED;
82 }
83
84 ui::EventType MojoTouchEventTypeToUIEvent(mus::mojom::EventType action) {
85 switch (action) {
86 case mus::mojom::EventType::POINTER_DOWN:
87 return ui::ET_TOUCH_PRESSED;
88
89 case mus::mojom::EventType::POINTER_UP:
90 return ui::ET_TOUCH_RELEASED;
91
92 case mus::mojom::EventType::POINTER_MOVE:
93 return ui::ET_TOUCH_MOVED;
94
95 case mus::mojom::EventType::POINTER_CANCEL:
96 return ui::ET_TOUCH_CANCELLED;
97
98 default:
99 NOTREACHED();
100 }
101
102 return ui::ET_TOUCH_CANCELLED;
103 }
104
105 } // namespace
106
107 static_assert(mus::mojom::kEventFlagNone == static_cast<int32_t>(ui::EF_NONE),
108 "EVENT_FLAGS must match");
109 static_assert(mus::mojom::kEventFlagIsSynthesized ==
110 static_cast<int32_t>(ui::EF_IS_SYNTHESIZED),
111 "EVENT_FLAGS must match");
112 static_assert(mus::mojom::kEventFlagShiftDown ==
113 static_cast<int32_t>(ui::EF_SHIFT_DOWN),
114 "EVENT_FLAGS must match");
115 static_assert(mus::mojom::kEventFlagControlDown ==
116 static_cast<int32_t>(ui::EF_CONTROL_DOWN),
117 "EVENT_FLAGS must match");
118 static_assert(mus::mojom::kEventFlagAltDown ==
119 static_cast<int32_t>(ui::EF_ALT_DOWN),
120 "EVENT_FLAGS must match");
121 static_assert(mus::mojom::kEventFlagCommandDown ==
122 static_cast<int32_t>(ui::EF_COMMAND_DOWN),
123 "EVENT_FLAGS must match");
124 static_assert(mus::mojom::kEventFlagAltgrDown ==
125 static_cast<int32_t>(ui::EF_ALTGR_DOWN),
126 "EVENT_FLAGS must match");
127 static_assert(mus::mojom::kEventFlagMod3Down ==
128 static_cast<int32_t>(ui::EF_MOD3_DOWN),
129 "EVENT_FLAGS must match");
130 static_assert(mus::mojom::kEventFlagNumLockOn ==
131 static_cast<int32_t>(ui::EF_NUM_LOCK_ON),
132 "EVENT_FLAGS must match");
133 static_assert(mus::mojom::kEventFlagCapsLockOn ==
134 static_cast<int32_t>(ui::EF_CAPS_LOCK_ON),
135 "EVENT_FLAGS must match");
136 static_assert(mus::mojom::kEventFlagScrollLockOn ==
137 static_cast<int32_t>(ui::EF_SCROLL_LOCK_ON),
138 "EVENT_FLAGS must match");
139 static_assert(mus::mojom::kEventFlagLeftMouseButton ==
140 static_cast<int32_t>(ui::EF_LEFT_MOUSE_BUTTON),
141 "EVENT_FLAGS must match");
142 static_assert(mus::mojom::kEventFlagMiddleMouseButton ==
143 static_cast<int32_t>(ui::EF_MIDDLE_MOUSE_BUTTON),
144 "EVENT_FLAGS must match");
145 static_assert(mus::mojom::kEventFlagRightMouseButton ==
146 static_cast<int32_t>(ui::EF_RIGHT_MOUSE_BUTTON),
147 "EVENT_FLAGS must match");
148 static_assert(mus::mojom::kEventFlagBackMouseButton ==
149 static_cast<int32_t>(ui::EF_BACK_MOUSE_BUTTON),
150 "EVENT_FLAGS must match");
151 static_assert(mus::mojom::kEventFlagForwardMouseButton ==
152 static_cast<int32_t>(ui::EF_FORWARD_MOUSE_BUTTON),
153 "EVENT_FLAGS must match");
154
155 int32_t StructTraits<mus::mojom::Event, EventUniquePtr>::action(
156 const EventUniquePtr& event) {
157 return static_cast<int32_t>(UIEventTypeToMojo(event->type()));
158 }
159
160 int32_t StructTraits<mus::mojom::Event, EventUniquePtr>::flags(
161 const EventUniquePtr& event) {
162 return event->flags();
163 }
164
165 int64_t StructTraits<mus::mojom::Event, EventUniquePtr>::time_stamp(
166 const EventUniquePtr& event) {
167 return event->time_stamp().ToInternalValue();
168 }
169
170 bool StructTraits<mus::mojom::Event, EventUniquePtr>::has_key_data(
171 const EventUniquePtr& event) {
172 return event->IsKeyEvent();
173 }
174
175 mus::mojom::KeyDataPtr
176 StructTraits<mus::mojom::Event, EventUniquePtr>::key_data(
177 const EventUniquePtr& event) {
178 if (!event->IsKeyEvent())
179 return nullptr;
180
181 const ui::KeyEvent* key_event = event->AsKeyEvent();
182 mus::mojom::KeyDataPtr key_data(mus::mojom::KeyData::New());
183 key_data->key_code = key_event->GetConflatedWindowsKeyCode();
184 key_data->native_key_code =
185 ui::KeycodeConverter::DomCodeToNativeKeycode(key_event->code());
186 key_data->is_char = key_event->is_char();
187 key_data->character = key_event->GetCharacter();
188 key_data->windows_key_code = static_cast<mus::mojom::KeyboardCode>(
189 key_event->GetLocatedWindowsKeyboardCode());
190 key_data->text = key_event->GetText();
191 key_data->unmodified_text = key_event->GetUnmodifiedText();
192
193 return key_data;
194 }
195
196 bool StructTraits<mus::mojom::Event, EventUniquePtr>::has_pointer_data(
197 const EventUniquePtr& event) {
198 return event->IsPointerEvent() || event->IsMouseWheelEvent();
199 }
200
201 mus::mojom::PointerDataPtr
202 StructTraits<mus::mojom::Event, EventUniquePtr>::pointer_data(
203 const EventUniquePtr& event) {
204 if (!has_pointer_data(event))
205 return nullptr;
206
207 mus::mojom::PointerDataPtr pointer_data(mus::mojom::PointerData::New());
208
209 const ui::PointerDetails* pointer_details = nullptr;
210 if (event->IsPointerEvent()) {
211 const ui::PointerEvent* pointer_event = event->AsPointerEvent();
212 pointer_data->pointer_id = pointer_event->pointer_id();
213 pointer_details = &pointer_event->pointer_details();
214 } else {
215 const ui::MouseWheelEvent* wheel_event = event->AsMouseWheelEvent();
216 pointer_data->pointer_id = ui::PointerEvent::kMousePointerId;
217 pointer_details = &wheel_event->pointer_details();
218 }
219
220 switch (pointer_details->pointer_type) {
221 case ui::EventPointerType::POINTER_TYPE_MOUSE:
222 pointer_data->kind = mus::mojom::PointerKind::MOUSE;
223 break;
224 case ui::EventPointerType::POINTER_TYPE_TOUCH:
225 pointer_data->kind = mus::mojom::PointerKind::TOUCH;
226 break;
227 default:
228 NOTREACHED();
229 }
230
231 mus::mojom::BrushDataPtr brush_data(mus::mojom::BrushData::New());
232 // TODO(rjk): this is in the wrong coordinate system
233 brush_data->width = pointer_details->radius_x;
234 brush_data->height = pointer_details->radius_y;
235 // TODO(rjk): update for touch_event->rotation_angle();
236 brush_data->pressure = pointer_details->force;
237 brush_data->tilt_x = pointer_details->tilt_x;
238 brush_data->tilt_y = pointer_details->tilt_y;
239 pointer_data->brush_data = std::move(brush_data);
240
241 // TODO(rjkroege): Plumb raw pointer events on windows.
242 // TODO(rjkroege): Handle force-touch on MacOS
243 // TODO(rjkroege): Adjust brush data appropriately for Android.
244
245 mus::mojom::LocationDataPtr location_data(mus::mojom::LocationData::New());
246 const ui::LocatedEvent* located_event = event->AsLocatedEvent();
247 location_data->x = located_event->location_f().x();
248 location_data->y = located_event->location_f().y();
249 location_data->screen_x = located_event->root_location_f().x();
250 location_data->screen_y = located_event->root_location_f().y();
251 pointer_data->location = std::move(location_data);
252
253 if (event->IsMouseWheelEvent()) {
254 const ui::MouseWheelEvent* wheel_event = event->AsMouseWheelEvent();
255
256 mus::mojom::WheelDataPtr wheel_data(mus::mojom::WheelData::New());
257
258 // TODO(rjkroege): Support page scrolling on windows by directly
259 // cracking into a mojo event when the native event is available.
260 wheel_data->mode = mus::mojom::WheelMode::LINE;
261 // TODO(rjkroege): Support precise scrolling deltas.
262
263 if ((event->flags() & ui::EF_SHIFT_DOWN) != 0 &&
264 wheel_event->x_offset() == 0) {
265 wheel_data->delta_x = wheel_event->y_offset();
266 wheel_data->delta_y = 0;
267 wheel_data->delta_z = 0;
268 } else {
269 // TODO(rjkroege): support z in ui::Events.
270 wheel_data->delta_x = wheel_event->x_offset();
271 wheel_data->delta_y = wheel_event->y_offset();
272 wheel_data->delta_z = 0;
273 }
274 pointer_data->wheel_data = std::move(wheel_data);
275 }
276
277 return pointer_data;
278 }
279
280 bool StructTraits<mus::mojom::Event, EventUniquePtr>::Read(
281 mus::mojom::EventDataView event,
282 EventUniquePtr* out) {
283 switch (event.action()) {
284 case mus::mojom::EventType::KEY_PRESSED:
285 case mus::mojom::EventType::KEY_RELEASED: {
286 mus::mojom::KeyDataPtr key_data;
287 if (!event.ReadKeyData<mus::mojom::KeyDataPtr>(&key_data))
288 return false;
289
290 if (key_data->is_char) {
291 out->reset(new ui::KeyEvent(
292 static_cast<base::char16>(key_data->character),
293 static_cast<ui::KeyboardCode>(key_data->key_code), event.flags()));
294 return true;
295 }
296 out->reset(new ui::KeyEvent(
297 event.action() == mus::mojom::EventType::KEY_PRESSED
298 ? ui::ET_KEY_PRESSED
299 : ui::ET_KEY_RELEASED,
300
301 static_cast<ui::KeyboardCode>(key_data->key_code), event.flags()));
302 return true;
303 }
304 case mus::mojom::EventType::POINTER_DOWN:
305 case mus::mojom::EventType::POINTER_UP:
306 case mus::mojom::EventType::POINTER_MOVE:
307 case mus::mojom::EventType::POINTER_CANCEL:
308 case mus::mojom::EventType::MOUSE_EXIT:
309 case mus::mojom::EventType::WHEEL: {
310 mus::mojom::PointerDataPtr pointer_data;
311 if (!event.ReadPointerData<mus::mojom::PointerDataPtr>(&pointer_data))
312 return false;
313
314 const gfx::Point location(pointer_data->location->x,
315 pointer_data->location->y);
316 const gfx::Point screen_location(pointer_data->location->screen_x,
317 pointer_data->location->screen_y);
318
319 switch (pointer_data->kind) {
320 case mus::mojom::PointerKind::MOUSE: {
321 if (event.action() == mus::mojom::EventType::WHEEL) {
322 out->reset(new ui::MouseWheelEvent(
323 gfx::Vector2d(
324 static_cast<int>(pointer_data->wheel_data->delta_x),
325 static_cast<int>(pointer_data->wheel_data->delta_y)),
326 location, screen_location, ui::EventTimeForNow(),
327 ui::EventFlags(event.flags()), ui::EventFlags(event.flags())));
328 return true;
329 }
330 // TODO(moshayedi): Construct pointer event directly.
331 ui::PointerEvent* mouse_event = new ui::PointerEvent(ui::MouseEvent(
332 MojoMouseEventTypeToUIEvent(event.action(), event.flags()),
333 location, screen_location, ui::EventTimeForNow(),
334 ui::EventFlags(event.flags()), ui::EventFlags(event.flags())));
335 out->reset(mouse_event);
336 return true;
337 }
338 case mus::mojom::PointerKind::TOUCH: {
339 // TODO(moshayedi): Construct pointer event directly.
340 ui::PointerEvent* pointer_event = new ui::PointerEvent(ui::TouchEvent(
341 MojoTouchEventTypeToUIEvent(event.action()), gfx::Point(),
342 ui::EventFlags(event.flags()), pointer_data->pointer_id,
343 base::TimeDelta::FromInternalValue(event.time_stamp()),
344 pointer_data->brush_data->width, pointer_data->brush_data->height,
345 0, pointer_data->brush_data->pressure));
346 pointer_event->set_location(location);
347 pointer_event->set_root_location(screen_location);
348 out->reset(pointer_event);
349 return true;
350 }
351 case mus::mojom::PointerKind::PEN:
352 NOTIMPLEMENTED();
353 return false;
354 }
355 }
356 case mus::mojom::EventType::UNKNOWN:
357 return false;
358 }
359
360 return false;
361 }
362
363 } // namespace mojo
OLDNEW
« no previous file with comments | « ui/events/mojo/event_struct_traits.h ('k') | ui/events/mojo/typemaps.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698