OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/renderer_host/input/content_gesture_provider.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/command_line.h" | |
9 #include "base/logging.h" | |
10 #include "content/public/common/content_switches.h" | |
11 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
12 #include "ui/events/gesture_detection/gesture_config_helper.h" | |
13 #include "ui/events/gesture_detection/motion_event.h" | |
14 | |
15 using blink::WebGestureEvent; | |
16 using blink::WebInputEvent; | |
17 | |
18 namespace content { | |
19 namespace { | |
20 | |
21 WebGestureEvent CreateGesture(const ui::GestureEventData& data, | |
22 float scale) { | |
23 WebGestureEvent gesture; | |
24 gesture.x = data.x * scale; | |
25 gesture.y = data.y * scale; | |
26 gesture.timeStampSeconds = (data.time - base::TimeTicks()).InSecondsF(); | |
27 gesture.sourceDevice = WebGestureEvent::Touchscreen; | |
28 | |
29 switch (data.type) { | |
30 case ui::GESTURE_SHOW_PRESS: | |
31 gesture.type = WebInputEvent::GestureShowPress; | |
32 gesture.data.showPress.width = data.details.show_press.width * scale; | |
33 gesture.data.showPress.width = data.details.show_press.width * scale; | |
34 break; | |
35 case ui::GESTURE_DOUBLE_TAP: | |
36 gesture.type = WebInputEvent::GestureDoubleTap; | |
37 break; | |
38 case ui::GESTURE_TAP: | |
39 gesture.type = WebInputEvent::GestureTap; | |
40 gesture.data.tap.tapCount = data.details.tap.tap_count; | |
41 gesture.data.tap.width = data.details.tap.width * scale; | |
42 gesture.data.tap.height = data.details.tap.height * scale; | |
43 break; | |
44 case ui::GESTURE_TAP_UNCONFIRMED: | |
45 gesture.type = WebInputEvent::GestureTapUnconfirmed; | |
46 gesture.data.tap.tapCount = data.details.tap.tap_count; | |
47 gesture.data.tap.width = data.details.tap.width * scale; | |
48 gesture.data.tap.height = data.details.tap.height * scale; | |
49 break; | |
50 case ui::GESTURE_LONG_PRESS: | |
51 gesture.type = WebInputEvent::GestureLongPress; | |
52 gesture.data.longPress.width = data.details.long_press.width * scale; | |
53 gesture.data.longPress.height = data.details.long_press.height * scale; | |
54 break; | |
55 case ui::GESTURE_LONG_TAP: | |
56 gesture.type = WebInputEvent::GestureLongTap; | |
57 gesture.data.longPress.width = data.details.long_press.width * scale; | |
58 gesture.data.longPress.height = data.details.long_press.height * scale; | |
59 break; | |
60 case ui::GESTURE_SCROLL_BEGIN: | |
61 gesture.type = WebInputEvent::GestureScrollBegin; | |
62 gesture.data.scrollBegin.deltaXHint = | |
63 data.details.scroll_begin.delta_x_hint * scale; | |
64 gesture.data.scrollBegin.deltaYHint = | |
65 data.details.scroll_begin.delta_y_hint * scale; | |
66 break; | |
67 case ui::GESTURE_SCROLL_UPDATE: | |
68 gesture.type = WebInputEvent::GestureScrollUpdate; | |
69 gesture.data.scrollUpdate.deltaX = | |
70 data.details.scroll_update.delta_x * scale; | |
71 gesture.data.scrollUpdate.deltaY = | |
72 data.details.scroll_update.delta_y * scale; | |
73 gesture.data.scrollUpdate.velocityX = | |
74 data.details.scroll_update.velocity_x * scale; | |
75 gesture.data.scrollUpdate.velocityY = | |
76 data.details.scroll_update.velocity_y * scale; | |
77 break; | |
78 case ui::GESTURE_SCROLL_END: | |
79 gesture.type = WebInputEvent::GestureScrollEnd; | |
80 break; | |
81 case ui::GESTURE_FLING_START: | |
82 gesture.type = WebInputEvent::GestureFlingStart; | |
83 gesture.data.flingStart.velocityX = | |
84 data.details.fling_start.velocity_x * scale; | |
85 gesture.data.flingStart.velocityY = | |
86 data.details.fling_start.velocity_y * scale; | |
87 break; | |
88 case ui::GESTURE_FLING_CANCEL: | |
89 gesture.type = WebInputEvent::GestureFlingCancel; | |
90 break; | |
91 case ui::GESTURE_PINCH_BEGIN: | |
92 gesture.type = WebInputEvent::GesturePinchBegin; | |
93 break; | |
94 case ui::GESTURE_PINCH_UPDATE: | |
95 gesture.type = WebInputEvent::GesturePinchUpdate; | |
96 gesture.data.pinchUpdate.scale = data.details.pinch_update.scale; | |
97 break; | |
98 case ui::GESTURE_PINCH_END: | |
99 gesture.type = WebInputEvent::GesturePinchEnd; | |
100 break; | |
101 case ui::GESTURE_TAP_CANCEL: | |
102 gesture.type = WebInputEvent::GestureTapCancel; | |
103 break; | |
104 case ui::GESTURE_TAP_DOWN: | |
105 gesture.type = WebInputEvent::GestureTapDown; | |
106 gesture.data.tapDown.width = data.details.tap_down.width * scale; | |
107 gesture.data.tapDown.height = data.details.tap_down.height * scale; | |
108 break; | |
109 case ui::GESTURE_TYPE_INVALID: | |
110 NOTREACHED() << "Invalid ui::GestureEventType provided."; | |
111 break; | |
112 } | |
113 | |
114 return gesture; | |
115 } | |
116 | |
117 ui::TouchDispositionGestureFilter::TouchEventAck | |
118 ToTouchDispositionGestureFilterAck(InputEventAckState ack_state) { | |
tdresser
2014/02/28 15:11:27
Looking at this more closely, I'm not convinced we
jdduke (slow)
2014/02/28 19:44:55
Sounds good. I think as this framework is adopted
| |
119 switch (ack_state) { | |
120 case INPUT_EVENT_ACK_STATE_CONSUMED: | |
121 return ui::TouchDispositionGestureFilter::CONSUMED; | |
122 case INPUT_EVENT_ACK_STATE_NOT_CONSUMED: | |
123 return ui::TouchDispositionGestureFilter::NOT_CONSUMED; | |
124 case INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS: | |
125 return ui::TouchDispositionGestureFilter::NO_CONSUMER_EXISTS; | |
126 case INPUT_EVENT_ACK_STATE_IGNORED: | |
127 return ui::TouchDispositionGestureFilter::CONSUMED; | |
128 case INPUT_EVENT_ACK_STATE_UNKNOWN: | |
129 break; | |
130 }; | |
131 NOTREACHED() << "Invalid ack state: " << ack_state; | |
132 return ui::TouchDispositionGestureFilter::NO_CONSUMER_EXISTS; | |
133 } | |
134 | |
135 ui::GestureProvider::Config GetGestureProviderConfig() { | |
136 ui::GestureProvider::Config config = ui::DefaultGestureProviderConfig(); | |
137 config.disable_click_delay = | |
138 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableClickDelay); | |
139 return config; | |
140 } | |
141 | |
142 } // namespace | |
143 | |
144 ContentGestureProvider::ContentGestureProvider( | |
145 ContentGestureProviderClient* client, | |
146 float touch_to_gesture_scale) | |
147 : client_(client), | |
148 touch_to_gesture_scale_(touch_to_gesture_scale), | |
149 gesture_provider_(GetGestureProviderConfig(), this), | |
150 gesture_filter_(this), | |
151 handling_event_(false) {} | |
152 | |
153 bool ContentGestureProvider::OnTouchEvent(const ui::MotionEvent& event) { | |
154 DCHECK(!handling_event_); | |
155 base::AutoReset<bool> handling_event(&handling_event_, true); | |
156 | |
157 pending_gesture_packet_ = ui::GestureEventDataPacket::FromTouch(event); | |
158 | |
159 if (!gesture_provider_.OnTouchEvent(event)) | |
160 return false; | |
161 | |
162 ui::TouchDispositionGestureFilter::PacketResult result = | |
163 gesture_filter_.OnGesturePacket(pending_gesture_packet_); | |
164 if (result != ui::TouchDispositionGestureFilter::SUCCESS) { | |
165 NOTREACHED() << "Invalid touch gesture sequence detected."; | |
166 return false; | |
167 } | |
168 | |
169 return true; | |
170 } | |
171 | |
172 void ContentGestureProvider::OnTouchEventAck(InputEventAckState ack_state) { | |
173 DCHECK_NE(INPUT_EVENT_ACK_STATE_UNKNOWN, ack_state); | |
174 gesture_filter_.OnTouchEventAck( | |
175 ToTouchDispositionGestureFilterAck(ack_state)); | |
176 } | |
177 | |
178 void ContentGestureProvider::ResetGestureDetectors() { | |
179 gesture_provider_.ResetGestureDetectors(); | |
180 } | |
181 | |
182 void ContentGestureProvider::CancelActiveTouchSequence() { | |
183 gesture_provider_.CancelActiveTouchSequence(); | |
184 } | |
185 | |
186 void ContentGestureProvider::SetMultiTouchSupportEnabled(bool enabled) { | |
187 gesture_provider_.SetMultiTouchSupportEnabled(enabled); | |
188 } | |
189 | |
190 void ContentGestureProvider::SetDoubleTapSupportForPlatformEnabled( | |
191 bool enabled) { | |
192 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(enabled); | |
193 } | |
194 | |
195 void ContentGestureProvider::SetDoubleTapSupportForPageEnabled(bool enabled) { | |
196 gesture_provider_.SetDoubleTapSupportForPageEnabled(enabled); | |
197 } | |
198 | |
199 void ContentGestureProvider::OnGestureEvent( | |
200 const ui::GestureEventData& event) { | |
201 if (handling_event_) { | |
202 pending_gesture_packet_.Push(event); | |
203 return; | |
204 } | |
205 | |
206 gesture_filter_.OnGesturePacket( | |
207 ui::GestureEventDataPacket::FromTouchTimeout(event)); | |
208 } | |
209 | |
210 void ContentGestureProvider::ForwardGestureEvent( | |
211 const ui::GestureEventData& event) { | |
212 client_->OnGestureEvent(CreateGesture(event, touch_to_gesture_scale_)); | |
213 } | |
214 | |
215 } // namespace content | |
OLD | NEW |