| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture_ne
w.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 #include "content/common/input/input_event.h" | |
| 10 #include "ui/events/latency_info.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 SyntheticSmoothScrollGestureNew::SyntheticSmoothScrollGestureNew( | |
| 15 const SyntheticSmoothScrollGestureParams& params) | |
| 16 : params_(params), | |
| 17 current_y_(params_.anchor.y()) {} | |
| 18 | |
| 19 SyntheticSmoothScrollGestureNew::~SyntheticSmoothScrollGestureNew() {} | |
| 20 | |
| 21 SyntheticGestureNew::Result SyntheticSmoothScrollGestureNew::ForwardInputEvents( | |
| 22 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | |
| 23 | |
| 24 SyntheticGestureParams::GestureSourceType source = | |
| 25 params_.gesture_source_type; | |
| 26 if (source == SyntheticGestureParams::DEFAULT_INPUT) | |
| 27 source = target->GetDefaultSyntheticGestureSourceType(); | |
| 28 | |
| 29 if (!target->SupportsSyntheticGestureSourceType(source)) | |
| 30 return SyntheticGestureNew::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM; | |
| 31 | |
| 32 if (source == SyntheticGestureParams::TOUCH_INPUT) | |
| 33 return ForwardTouchInputEvents(interval, target); | |
| 34 else if (source == SyntheticGestureParams::MOUSE_INPUT) | |
| 35 return ForwardMouseInputEvents(interval, target); | |
| 36 else | |
| 37 return SyntheticGestureNew::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED; | |
| 38 } | |
| 39 | |
| 40 SyntheticGestureNew::Result | |
| 41 SyntheticSmoothScrollGestureNew::ForwardTouchInputEvents( | |
| 42 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | |
| 43 if (HasFinished()) | |
| 44 return SyntheticGestureNew::GESTURE_FINISHED; | |
| 45 | |
| 46 if (current_y_ == params_.anchor.y()) { | |
| 47 touch_event_.PressPoint(params_.anchor.x(), current_y_); | |
| 48 ForwardTouchEvent(target); | |
| 49 } | |
| 50 | |
| 51 current_y_ += GetPositionDelta(interval); | |
| 52 touch_event_.MovePoint(0, params_.anchor.x(), current_y_); | |
| 53 ForwardTouchEvent(target); | |
| 54 | |
| 55 if (HasFinished()) { | |
| 56 touch_event_.ReleasePoint(0); | |
| 57 ForwardTouchEvent(target); | |
| 58 return SyntheticGestureNew::GESTURE_FINISHED; | |
| 59 } | |
| 60 else { | |
| 61 return SyntheticGestureNew::GESTURE_RUNNING; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 SyntheticGestureNew::Result | |
| 66 SyntheticSmoothScrollGestureNew::ForwardMouseInputEvents( | |
| 67 const base::TimeDelta& interval, SyntheticGestureTarget* target) { | |
| 68 if (HasFinished()) | |
| 69 return SyntheticGestureNew::GESTURE_FINISHED; | |
| 70 | |
| 71 float delta = GetPositionDelta(interval); | |
| 72 current_y_ += delta; | |
| 73 ForwardMouseWheelEvent(target, delta); | |
| 74 | |
| 75 if (HasFinished()) | |
| 76 return SyntheticGestureNew::GESTURE_FINISHED; | |
| 77 else | |
| 78 return SyntheticGestureNew::GESTURE_RUNNING; | |
| 79 } | |
| 80 | |
| 81 void SyntheticSmoothScrollGestureNew::ForwardTouchEvent( | |
| 82 SyntheticGestureTarget* target) { | |
| 83 target->DispatchInputEventToPlatform( | |
| 84 InputEvent(touch_event_, ui::LatencyInfo(), false)); | |
| 85 } | |
| 86 | |
| 87 void SyntheticSmoothScrollGestureNew::ForwardMouseWheelEvent( | |
| 88 SyntheticGestureTarget* target, | |
| 89 float delta) { | |
| 90 blink::WebMouseWheelEvent mouse_wheel_event = | |
| 91 SyntheticWebMouseWheelEventBuilder::Build(0, delta, 0, false); | |
| 92 | |
| 93 target->DispatchInputEventToPlatform( | |
| 94 InputEvent(mouse_wheel_event, ui::LatencyInfo(), false)); | |
| 95 } | |
| 96 | |
| 97 float SyntheticSmoothScrollGestureNew::GetPositionDelta( | |
| 98 const base::TimeDelta& interval) { | |
| 99 float delta = params_.speed_in_pixels_s * interval.InSecondsF(); | |
| 100 // A positive value indicates scrolling down, which means the touch pointer | |
| 101 // moves up or the scroll wheel moves down. In either case, the delta is | |
| 102 // negative when scrolling down and positive when scrolling up. | |
| 103 return (params_.distance > 0) ? -delta : delta; | |
| 104 } | |
| 105 | |
| 106 bool SyntheticSmoothScrollGestureNew::HasFinished() { | |
| 107 return abs(current_y_ - params_.anchor.y()) >= abs(params_.distance); | |
| 108 } | |
| 109 | |
| 110 } // namespace content | |
| OLD | NEW |