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

Side by Side Diff: content/browser/renderer_host/input/touch_emulator.cc

Issue 2573073003: Collapse the API surface on WebInputEvent via accessor functions. (Closed)
Patch Set: Fix nits Created 3 years, 11 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/browser/renderer_host/input/touch_emulator.h" 5 #include "content/browser/renderer_host/input/touch_emulator.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 #include "content/browser/renderer_host/input/motion_event_web.h" 8 #include "content/browser/renderer_host/input/motion_event_web.h"
9 #include "content/common/input/web_touch_event_traits.h" 9 #include "content/common/input/web_touch_event_traits.h"
10 #include "content/grit/content_resources.h" 10 #include "content/grit/content_resources.h"
(...skipping 24 matching lines...) Expand all
35 ui::GetGestureProviderConfig(config_type); 35 ui::GetGestureProviderConfig(config_type);
36 config.gesture_begin_end_types_enabled = false; 36 config.gesture_begin_end_types_enabled = false;
37 config.gesture_detector_config.swipe_enabled = false; 37 config.gesture_detector_config.swipe_enabled = false;
38 config.gesture_detector_config.two_finger_tap_enabled = false; 38 config.gesture_detector_config.two_finger_tap_enabled = false;
39 return config; 39 return config;
40 } 40 }
41 41
42 int ModifiersWithoutMouseButtons(const WebInputEvent& event) { 42 int ModifiersWithoutMouseButtons(const WebInputEvent& event) {
43 const int all_buttons = WebInputEvent::LeftButtonDown | 43 const int all_buttons = WebInputEvent::LeftButtonDown |
44 WebInputEvent::MiddleButtonDown | WebInputEvent::RightButtonDown; 44 WebInputEvent::MiddleButtonDown | WebInputEvent::RightButtonDown;
45 return event.modifiers & ~all_buttons; 45 return event.modifiers() & ~all_buttons;
46 } 46 }
47 47
48 // Time between two consecutive mouse moves, during which second mouse move 48 // Time between two consecutive mouse moves, during which second mouse move
49 // is not converted to touch. 49 // is not converted to touch.
50 const double kMouseMoveDropIntervalSeconds = 5.f / 1000; 50 const double kMouseMoveDropIntervalSeconds = 5.f / 1000;
51 51
52 } // namespace 52 } // namespace
53 53
54 TouchEmulator::TouchEmulator(TouchEmulatorClient* client, 54 TouchEmulator::TouchEmulator(TouchEmulatorClient* client,
55 float device_scale_factor) 55 float device_scale_factor)
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 cursor->InitFromCursorInfo(cursor_info); 151 cursor->InitFromCursorInfo(cursor_info);
152 return gfx::ScaleSize(gfx::SizeF(cursor_image.Size()), 1.f / scale); 152 return gfx::ScaleSize(gfx::SizeF(cursor_image.Size()), 1.f / scale);
153 } 153 }
154 154
155 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) { 155 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) {
156 if (!enabled()) 156 if (!enabled())
157 return false; 157 return false;
158 158
159 if (mouse_event.button == WebMouseEvent::Button::Right && 159 if (mouse_event.button == WebMouseEvent::Button::Right &&
160 mouse_event.type == WebInputEvent::MouseDown) { 160 mouse_event.type() == WebInputEvent::MouseDown) {
161 client_->ShowContextMenuAtPoint(gfx::Point(mouse_event.x, mouse_event.y)); 161 client_->ShowContextMenuAtPoint(gfx::Point(mouse_event.x, mouse_event.y));
162 } 162 }
163 163
164 if (mouse_event.button != WebMouseEvent::Button::Left) 164 if (mouse_event.button != WebMouseEvent::Button::Left)
165 return true; 165 return true;
166 166
167 if (mouse_event.type == WebInputEvent::MouseMove) { 167 if (mouse_event.type() == WebInputEvent::MouseMove) {
168 if (last_mouse_event_was_move_ && 168 if (last_mouse_event_was_move_ &&
169 mouse_event.timeStampSeconds < last_mouse_move_timestamp_ + 169 mouse_event.timeStampSeconds() <
170 kMouseMoveDropIntervalSeconds) 170 last_mouse_move_timestamp_ + kMouseMoveDropIntervalSeconds)
171 return true; 171 return true;
172 172
173 last_mouse_event_was_move_ = true; 173 last_mouse_event_was_move_ = true;
174 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds; 174 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds();
175 } else { 175 } else {
176 last_mouse_event_was_move_ = false; 176 last_mouse_event_was_move_ = false;
177 } 177 }
178 178
179 if (mouse_event.type == WebInputEvent::MouseDown) 179 if (mouse_event.type() == WebInputEvent::MouseDown)
180 mouse_pressed_ = true; 180 mouse_pressed_ = true;
181 else if (mouse_event.type == WebInputEvent::MouseUp) 181 else if (mouse_event.type() == WebInputEvent::MouseUp)
182 mouse_pressed_ = false; 182 mouse_pressed_ = false;
183 183
184 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0); 184 UpdateShiftPressed((mouse_event.modifiers() & WebInputEvent::ShiftKey) != 0);
185 185
186 if (mouse_event.type != WebInputEvent::MouseDown && 186 if (mouse_event.type() != WebInputEvent::MouseDown &&
187 mouse_event.type != WebInputEvent::MouseMove && 187 mouse_event.type() != WebInputEvent::MouseMove &&
188 mouse_event.type != WebInputEvent::MouseUp) { 188 mouse_event.type() != WebInputEvent::MouseUp) {
189 return true; 189 return true;
190 } 190 }
191 191
192 FillTouchEventAndPoint(mouse_event); 192 FillTouchEventAndPoint(mouse_event);
193 HandleEmulatedTouchEvent(touch_event_); 193 HandleEmulatedTouchEvent(touch_event_);
194 194
195 // Do not pass mouse events to the renderer. 195 // Do not pass mouse events to the renderer.
196 return true; 196 return true;
197 } 197 }
198 198
199 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) { 199 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) {
200 if (!enabled()) 200 if (!enabled())
201 return false; 201 return false;
202 202
203 // Send mouse wheel for easy scrolling when there is no active touch. 203 // Send mouse wheel for easy scrolling when there is no active touch.
204 return emulated_stream_active_sequence_count_ > 0; 204 return emulated_stream_active_sequence_count_ > 0;
205 } 205 }
206 206
207 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) { 207 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) {
208 if (!enabled()) 208 if (!enabled())
209 return false; 209 return false;
210 210
211 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0)) 211 if (!UpdateShiftPressed((event.modifiers() & WebInputEvent::ShiftKey) != 0))
212 return false; 212 return false;
213 213
214 if (!mouse_pressed_) 214 if (!mouse_pressed_)
215 return false; 215 return false;
216 216
217 // Note: The necessary pinch events will be lazily inserted by 217 // Note: The necessary pinch events will be lazily inserted by
218 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the 218 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
219 // scroll stream as the event driver. 219 // scroll stream as the event driver.
220 if (shift_pressed_) { 220 if (shift_pressed_) {
221 // TODO(dgozman): Add secondary touch point and set anchor. 221 // TODO(dgozman): Add secondary touch point and set anchor.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 // middle of a sequence), so don't decrement sequence count below zero. 290 // middle of a sequence), so don't decrement sequence count below zero.
291 if (is_sequence_end && native_stream_active_sequence_count_) 291 if (is_sequence_end && native_stream_active_sequence_count_)
292 native_stream_active_sequence_count_--; 292 native_stream_active_sequence_count_--;
293 return false; 293 return false;
294 } 294 }
295 295
296 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { 296 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
297 WebGestureEvent gesture_event = 297 WebGestureEvent gesture_event =
298 ui::CreateWebGestureEventFromGestureEventData(gesture); 298 ui::CreateWebGestureEventFromGestureEventData(gesture);
299 299
300 switch (gesture_event.type) { 300 switch (gesture_event.type()) {
301 case WebInputEvent::Undefined: 301 case WebInputEvent::Undefined:
302 NOTREACHED() << "Undefined WebInputEvent type"; 302 NOTREACHED() << "Undefined WebInputEvent type";
303 // Bail without sending the junk event to the client. 303 // Bail without sending the junk event to the client.
304 return; 304 return;
305 305
306 case WebInputEvent::GestureScrollBegin: 306 case WebInputEvent::GestureScrollBegin:
307 client_->ForwardEmulatedGestureEvent(gesture_event); 307 client_->ForwardEmulatedGestureEvent(gesture_event);
308 // PinchBegin must always follow ScrollBegin. 308 // PinchBegin must always follow ScrollBegin.
309 if (InPinchGestureMode()) 309 if (InPinchGestureMode())
310 PinchBegin(gesture_event); 310 PinchBegin(gesture_event);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 DCHECK(pinch_gesture_active_); 412 DCHECK(pinch_gesture_active_);
413 pinch_gesture_active_ = false; 413 pinch_gesture_active_ = false;
414 WebGestureEvent pinch_event = 414 WebGestureEvent pinch_event =
415 GetPinchGestureEvent(WebInputEvent::GesturePinchEnd, event); 415 GetPinchGestureEvent(WebInputEvent::GesturePinchEnd, event);
416 client_->ForwardEmulatedGestureEvent(pinch_event); 416 client_->ForwardEmulatedGestureEvent(pinch_event);
417 } 417 }
418 418
419 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) { 419 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) {
420 WebGestureEvent scroll_event(WebInputEvent::GestureScrollEnd, 420 WebGestureEvent scroll_event(WebInputEvent::GestureScrollEnd,
421 ModifiersWithoutMouseButtons(event), 421 ModifiersWithoutMouseButtons(event),
422 event.timeStampSeconds); 422 event.timeStampSeconds());
423 scroll_event.sourceDevice = blink::WebGestureDeviceTouchscreen; 423 scroll_event.sourceDevice = blink::WebGestureDeviceTouchscreen;
424 client_->ForwardEmulatedGestureEvent(scroll_event); 424 client_->ForwardEmulatedGestureEvent(scroll_event);
425 } 425 }
426 426
427 WebGestureEvent TouchEmulator::GetPinchGestureEvent( 427 WebGestureEvent TouchEmulator::GetPinchGestureEvent(
428 WebInputEvent::Type type, 428 WebInputEvent::Type type,
429 const WebInputEvent& original_event) { 429 const WebInputEvent& original_event) {
430 WebGestureEvent event(type, ModifiersWithoutMouseButtons(original_event), 430 WebGestureEvent event(type, ModifiersWithoutMouseButtons(original_event),
431 original_event.timeStampSeconds); 431 original_event.timeStampSeconds());
432 event.sourceDevice = blink::WebGestureDeviceTouchscreen; 432 event.sourceDevice = blink::WebGestureDeviceTouchscreen;
433 event.x = pinch_anchor_.x(); 433 event.x = pinch_anchor_.x();
434 event.y = pinch_anchor_.y(); 434 event.y = pinch_anchor_.y();
435 return event; 435 return event;
436 } 436 }
437 437
438 void TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) { 438 void TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) {
439 WebInputEvent::Type eventType; 439 WebInputEvent::Type eventType;
440 switch (mouse_event.type) { 440 switch (mouse_event.type()) {
441 case WebInputEvent::MouseDown: 441 case WebInputEvent::MouseDown:
442 eventType = WebInputEvent::TouchStart; 442 eventType = WebInputEvent::TouchStart;
443 break; 443 break;
444 case WebInputEvent::MouseMove: 444 case WebInputEvent::MouseMove:
445 eventType = WebInputEvent::TouchMove; 445 eventType = WebInputEvent::TouchMove;
446 break; 446 break;
447 case WebInputEvent::MouseUp: 447 case WebInputEvent::MouseUp:
448 eventType = WebInputEvent::TouchEnd; 448 eventType = WebInputEvent::TouchEnd;
449 break; 449 break;
450 default: 450 default:
451 eventType = WebInputEvent::Undefined; 451 eventType = WebInputEvent::Undefined;
452 NOTREACHED() << "Invalid event for touch emulation: " << mouse_event.type; 452 NOTREACHED() << "Invalid event for touch emulation: "
453 << mouse_event.type();
453 } 454 }
454 touch_event_.touchesLength = 1; 455 touch_event_.touchesLength = 1;
455 touch_event_.setModifiers(ModifiersWithoutMouseButtons(mouse_event)); 456 touch_event_.setModifiers(ModifiersWithoutMouseButtons(mouse_event));
456 WebTouchEventTraits::ResetTypeAndTouchStates( 457 WebTouchEventTraits::ResetTypeAndTouchStates(
457 eventType, mouse_event.timeStampSeconds, &touch_event_); 458 eventType, mouse_event.timeStampSeconds(), &touch_event_);
458 WebTouchPoint& point = touch_event_.touches[0]; 459 WebTouchPoint& point = touch_event_.touches[0];
459 point.id = 0; 460 point.id = 0;
460 point.radiusX = 0.5f * cursor_size_.width(); 461 point.radiusX = 0.5f * cursor_size_.width();
461 point.radiusY = 0.5f * cursor_size_.height(); 462 point.radiusY = 0.5f * cursor_size_.height();
462 point.force = 1.f; 463 point.force = 1.f;
463 point.rotationAngle = 0.f; 464 point.rotationAngle = 0.f;
464 point.position.x = mouse_event.x; 465 point.position.x = mouse_event.x;
465 point.screenPosition.x = mouse_event.globalX; 466 point.screenPosition.x = mouse_event.globalX;
466 point.position.y = mouse_event.y; 467 point.position.y = mouse_event.y;
467 point.screenPosition.y = mouse_event.globalY; 468 point.screenPosition.y = mouse_event.globalY;
468 point.tiltX = 0; 469 point.tiltX = 0;
469 point.tiltY = 0; 470 point.tiltY = 0;
470 point.pointerType = blink::WebPointerProperties::PointerType::Touch; 471 point.pointerType = blink::WebPointerProperties::PointerType::Touch;
471 } 472 }
472 473
473 bool TouchEmulator::InPinchGestureMode() const { 474 bool TouchEmulator::InPinchGestureMode() const {
474 return shift_pressed_; 475 return shift_pressed_;
475 } 476 }
476 477
477 } // namespace content 478 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698