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

Side by Side Diff: third_party/WebKit/Source/core/events/PointerEventFactory.cpp

Issue 2510133002: Add getCoalescedEvents API to PointerEvent (Closed)
Patch Set: Fix build.gn and add an extra test Created 4 years, 1 month 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 2016 The Chromium Authors. All rights reserved. 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 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 "core/events/PointerEventFactory.h" 5 #include "core/events/PointerEventFactory.h"
6 6
7 #include "core/frame/FrameView.h" 7 #include "core/frame/FrameView.h"
8 #include "platform/geometry/FloatSize.h" 8 #include "platform/geometry/FloatSize.h"
9 9
10 namespace blink { 10 namespace blink {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 CASE_BUTTON_TO_BUTTONS(X2); 68 CASE_BUTTON_TO_BUTTONS(X2);
69 CASE_BUTTON_TO_BUTTONS(Eraser); 69 CASE_BUTTON_TO_BUTTONS(Eraser);
70 } 70 }
71 71
72 #undef CASE_BUTTON_TO_BUTTONS 72 #undef CASE_BUTTON_TO_BUTTONS
73 73
74 NOTREACHED(); 74 NOTREACHED();
75 return 0; 75 return 0;
76 } 76 }
77 77
78 } // namespace 78 const AtomicString& pointerEventNameForTouchPointState(
79 79 PlatformTouchPoint::TouchState state) {
80 const int PointerEventFactory::s_invalidId = 0; 80 switch (state) {
81 81 case PlatformTouchPoint::TouchReleased:
82 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons. 82 return EventTypeNames::pointerup;
83 const int PointerEventFactory::s_mouseId = 1; 83 case PlatformTouchPoint::TouchCancelled:
84 return EventTypeNames::pointercancel;
85 case PlatformTouchPoint::TouchPressed:
86 return EventTypeNames::pointerdown;
87 case PlatformTouchPoint::TouchMoved:
88 return EventTypeNames::pointermove;
89 case PlatformTouchPoint::TouchStationary:
90 // Fall through to default
91 default:
92 NOTREACHED();
93 return emptyAtom;
94 }
95 }
84 96
85 float getPointerEventPressure(float force, int buttons) { 97 float getPointerEventPressure(float force, int buttons) {
86 if (std::isnan(force)) 98 if (std::isnan(force))
87 return buttons ? 0.5 : 0; 99 return buttons ? 0.5 : 0;
88 return force; 100 return force;
89 } 101 }
90 102
103 void updateTouchPointPointerEventInit(const PlatformTouchPoint& touchPoint,
104 LocalFrame* targetFrame,
105 PointerEventInit* pointerEventInit) {
106 if (targetFrame) {
107 FloatPoint pagePoint =
108 targetFrame->view()->rootFrameToContents(touchPoint.pos());
109 float scaleFactor = 1.0f / targetFrame->pageZoomFactor();
110 FloatPoint scrollPosition(targetFrame->view()->scrollOffset());
111 FloatPoint clientPoint = pagePoint.scaledBy(scaleFactor);
112 clientPoint.moveBy(scrollPosition.scaledBy(-scaleFactor));
113
114 pointerEventInit->setClientX(clientPoint.x());
115 pointerEventInit->setClientY(clientPoint.y());
116
117 FloatSize pointRadius = touchPoint.radius().scaledBy(scaleFactor);
118 pointerEventInit->setWidth(pointRadius.width());
119 pointerEventInit->setHeight(pointRadius.height());
120 }
121
122 pointerEventInit->setScreenX(touchPoint.screenPos().x());
123 pointerEventInit->setScreenY(touchPoint.screenPos().y());
124 pointerEventInit->setPressure(
125 getPointerEventPressure(touchPoint.force(), pointerEventInit->buttons()));
126 pointerEventInit->setTiltX(touchPoint.pointerProperties().tiltX);
127 pointerEventInit->setTiltY(touchPoint.pointerProperties().tiltY);
128 }
129
130 void updateMousePointerEventInit(const PlatformMouseEvent& mouseEvent,
131 LocalDOMWindow* view,
132 PointerEventInit* pointerEventInit) {
133 pointerEventInit->setScreenX(mouseEvent.globalPosition().x());
134 pointerEventInit->setScreenY(mouseEvent.globalPosition().y());
135
136 IntPoint locationInFrameZoomed;
137 if (view && view->frame() && view->frame()->view()) {
138 LocalFrame* frame = view->frame();
139 FrameView* frameView = frame->view();
140 IntPoint locationInContents =
141 frameView->rootFrameToContents(mouseEvent.position());
142 locationInFrameZoomed = frameView->contentsToFrame(locationInContents);
143 float scaleFactor = 1 / frame->pageZoomFactor();
144 locationInFrameZoomed.scale(scaleFactor, scaleFactor);
145 }
146
147 // Set up initial values for coordinates.
148 pointerEventInit->setClientX(locationInFrameZoomed.x());
149 pointerEventInit->setClientY(locationInFrameZoomed.y());
150
151 pointerEventInit->setPressure(getPointerEventPressure(
152 mouseEvent.pointerProperties().force, pointerEventInit->buttons()));
153 pointerEventInit->setTiltX(mouseEvent.pointerProperties().tiltX);
154 pointerEventInit->setTiltY(mouseEvent.pointerProperties().tiltY);
155 }
156
157 } // namespace
158
159 const int PointerEventFactory::s_invalidId = 0;
160
161 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons.
162 const int PointerEventFactory::s_mouseId = 1;
163
91 void PointerEventFactory::setIdTypeButtons( 164 void PointerEventFactory::setIdTypeButtons(
92 PointerEventInit& pointerEventInit, 165 PointerEventInit& pointerEventInit,
93 const WebPointerProperties& pointerProperties, 166 const WebPointerProperties& pointerProperties,
94 unsigned buttons) { 167 unsigned buttons) {
95 const WebPointerProperties::PointerType pointerType = 168 const WebPointerProperties::PointerType pointerType =
96 pointerProperties.pointerType; 169 pointerProperties.pointerType;
97 const IncomingId incomingId(pointerType, pointerProperties.id); 170 const IncomingId incomingId(pointerType, pointerProperties.id);
98 int pointerId = addIdAndActiveButtons(incomingId, buttons != 0); 171 int pointerId = addIdAndActiveButtons(incomingId, buttons != 0);
99 172
100 // Tweak the |buttons| to reflect pen eraser mode only if the pen is in 173 // Tweak the |buttons| to reflect pen eraser mode only if the pen is in
(...skipping 20 matching lines...) Expand all
121 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter && 194 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter &&
122 type != EventTypeNames::pointerleave && 195 type != EventTypeNames::pointerleave &&
123 type != EventTypeNames::pointercancel && 196 type != EventTypeNames::pointercancel &&
124 type != EventTypeNames::gotpointercapture && 197 type != EventTypeNames::gotpointercapture &&
125 type != EventTypeNames::lostpointercapture); 198 type != EventTypeNames::lostpointercapture);
126 199
127 pointerEventInit.setComposed(true); 200 pointerEventInit.setComposed(true);
128 pointerEventInit.setDetail(0); 201 pointerEventInit.setDetail(0);
129 } 202 }
130 203
131 PointerEvent* PointerEventFactory::create(const AtomicString& mouseEventName, 204 PointerEvent* PointerEventFactory::create(
132 const PlatformMouseEvent& mouseEvent, 205 const AtomicString& mouseEventName,
133 LocalDOMWindow* view) { 206 const PlatformMouseEvent& mouseEvent,
207 const Vector<PlatformMouseEvent>& coalescedMouseEvents,
208 LocalDOMWindow* view) {
134 DCHECK(mouseEventName == EventTypeNames::mousemove || 209 DCHECK(mouseEventName == EventTypeNames::mousemove ||
135 mouseEventName == EventTypeNames::mousedown || 210 mouseEventName == EventTypeNames::mousedown ||
136 mouseEventName == EventTypeNames::mouseup); 211 mouseEventName == EventTypeNames::mouseup);
137 212
138 AtomicString pointerEventName = 213 AtomicString pointerEventName =
139 pointerEventNameForMouseEventName(mouseEventName); 214 pointerEventNameForMouseEventName(mouseEventName);
140 unsigned buttons = 215 unsigned buttons =
141 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers()); 216 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers());
142 PointerEventInit pointerEventInit; 217 PointerEventInit pointerEventInit;
143 218
144 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons); 219 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons);
145 setEventSpecificFields(pointerEventInit, pointerEventName); 220 setEventSpecificFields(pointerEventInit, pointerEventName);
146 221
147 pointerEventInit.setScreenX(mouseEvent.globalPosition().x());
148 pointerEventInit.setScreenY(mouseEvent.globalPosition().y());
149
150 IntPoint locationInFrameZoomed;
151 if (view && view->frame() && view->frame()->view()) {
152 LocalFrame* frame = view->frame();
153 FrameView* frameView = frame->view();
154 IntPoint locationInContents =
155 frameView->rootFrameToContents(mouseEvent.position());
156 locationInFrameZoomed = frameView->contentsToFrame(locationInContents);
157 float scaleFactor = 1 / frame->pageZoomFactor();
158 locationInFrameZoomed.scale(scaleFactor, scaleFactor);
159 }
160
161 // Set up initial values for coordinates.
162 pointerEventInit.setClientX(locationInFrameZoomed.x());
163 pointerEventInit.setClientY(locationInFrameZoomed.y());
164
165 if (pointerEventName == EventTypeNames::pointerdown || 222 if (pointerEventName == EventTypeNames::pointerdown ||
166 pointerEventName == EventTypeNames::pointerup) { 223 pointerEventName == EventTypeNames::pointerup) {
167 WebPointerProperties::Button button = mouseEvent.pointerProperties().button; 224 WebPointerProperties::Button button = mouseEvent.pointerProperties().button;
168 // TODO(mustaq): Fix when the spec starts supporting hovering erasers. 225 // TODO(mustaq): Fix when the spec starts supporting hovering erasers.
169 if (mouseEvent.pointerProperties().pointerType == 226 if (mouseEvent.pointerProperties().pointerType ==
170 WebPointerProperties::PointerType::Eraser && 227 WebPointerProperties::PointerType::Eraser &&
171 button == WebPointerProperties::Button::Left) 228 button == WebPointerProperties::Button::Left)
172 button = WebPointerProperties::Button::Eraser; 229 button = WebPointerProperties::Button::Eraser;
173 pointerEventInit.setButton(static_cast<int>(button)); 230 pointerEventInit.setButton(static_cast<int>(button));
174 } else { 231 } else {
175 DCHECK(pointerEventName == EventTypeNames::pointermove); 232 DCHECK(pointerEventName == EventTypeNames::pointermove);
176 pointerEventInit.setButton( 233 pointerEventInit.setButton(
177 static_cast<int>(WebPointerProperties::Button::NoButton)); 234 static_cast<int>(WebPointerProperties::Button::NoButton));
178 } 235 }
179 pointerEventInit.setPressure(getPointerEventPressure(
180 mouseEvent.pointerProperties().force, pointerEventInit.buttons()));
181 pointerEventInit.setTiltX(mouseEvent.pointerProperties().tiltX);
182 pointerEventInit.setTiltY(mouseEvent.pointerProperties().tiltY);
183 236
184 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, 237 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit,
185 mouseEvent.getModifiers()); 238 mouseEvent.getModifiers());
186 239
187 // Make sure chorded buttons fire pointermove instead of pointerup/down. 240 // Make sure chorded buttons fire pointermove instead of pointerup/down.
188 if ((pointerEventName == EventTypeNames::pointerdown && 241 if ((pointerEventName == EventTypeNames::pointerdown &&
189 (buttons & 242 (buttons &
190 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) != 243 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) !=
191 0) || 244 0) ||
192 (pointerEventName == EventTypeNames::pointerup && buttons != 0)) 245 (pointerEventName == EventTypeNames::pointerup && buttons != 0))
193 pointerEventName = EventTypeNames::pointermove; 246 pointerEventName = EventTypeNames::pointermove;
194 247
195 pointerEventInit.setView(view); 248 pointerEventInit.setView(view);
196 249
250 updateMousePointerEventInit(mouseEvent, view, &pointerEventInit);
251
252 // Created coalesced events init structure
253 HeapVector<Member<PointerEvent>> coalescedPointerEvents;
254 for (const auto& coalescedMouseEvent : coalescedMouseEvents) {
255 PointerEventInit coalescedEventInit = pointerEventInit;
256 updateMousePointerEventInit(coalescedMouseEvent, view, &coalescedEventInit);
257 coalescedPointerEvents.append(
258 PointerEvent::create(pointerEventName, coalescedEventInit));
259 }
260 pointerEventInit.setCoalescedEvents(coalescedPointerEvents);
dtapuska 2016/11/21 15:51:56 can we DCHECK that coalescedPointerEvents is size
Navid Zolghadr 2016/11/21 19:35:11 Done.
261
197 return PointerEvent::create(pointerEventName, pointerEventInit); 262 return PointerEvent::create(pointerEventName, pointerEventInit);
198 } 263 }
199 264
200 PointerEvent* PointerEventFactory::create(const AtomicString& type, 265 PointerEvent* PointerEventFactory::create(
201 const PlatformTouchPoint& touchPoint, 266 const PlatformTouchPoint& touchPoint,
202 PlatformEvent::Modifiers modifiers, 267 const Vector<PlatformTouchPoint>& coalescedPoints,
203 const FloatSize& pointRadius, 268 PlatformEvent::Modifiers modifiers,
204 const FloatPoint& clientPoint, 269 LocalFrame* targetFrame,
205 DOMWindow* view) { 270 DOMWindow* view) {
206 const PlatformTouchPoint::TouchState pointState = touchPoint.state(); 271 const PlatformTouchPoint::TouchState pointState = touchPoint.state();
272 const AtomicString& type =
273 pointerEventNameForTouchPointState(touchPoint.state());
207 274
208 bool pointerReleasedOrCancelled = 275 bool pointerReleasedOrCancelled =
209 pointState == PlatformTouchPoint::TouchReleased || 276 pointState == PlatformTouchPoint::TouchReleased ||
210 pointState == PlatformTouchPoint::TouchCancelled; 277 pointState == PlatformTouchPoint::TouchCancelled;
211 bool pointerPressedOrReleased = 278 bool pointerPressedOrReleased =
212 pointState == PlatformTouchPoint::TouchPressed || 279 pointState == PlatformTouchPoint::TouchPressed ||
213 pointState == PlatformTouchPoint::TouchReleased; 280 pointState == PlatformTouchPoint::TouchReleased;
214 281
215 PointerEventInit pointerEventInit; 282 PointerEventInit pointerEventInit;
216 283
217 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), 284 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(),
218 pointerReleasedOrCancelled ? 0 : 1); 285 pointerReleasedOrCancelled ? 0 : 1);
219
220 pointerEventInit.setWidth(pointRadius.width());
221 pointerEventInit.setHeight(pointRadius.height());
222 pointerEventInit.setScreenX(touchPoint.screenPos().x());
223 pointerEventInit.setScreenY(touchPoint.screenPos().y());
224 pointerEventInit.setClientX(clientPoint.x());
225 pointerEventInit.setClientY(clientPoint.y());
226 pointerEventInit.setButton(static_cast<int>( 286 pointerEventInit.setButton(static_cast<int>(
227 pointerPressedOrReleased ? WebPointerProperties::Button::Left 287 pointerPressedOrReleased ? WebPointerProperties::Button::Left
228 : WebPointerProperties::Button::NoButton)); 288 : WebPointerProperties::Button::NoButton));
229 pointerEventInit.setPressure( 289
230 getPointerEventPressure(touchPoint.force(), pointerEventInit.buttons()));
231 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX);
232 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY);
233 pointerEventInit.setView(view); 290 pointerEventInit.setView(view);
291 updateTouchPointPointerEventInit(touchPoint, targetFrame, &pointerEventInit);
234 292
235 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); 293 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers);
236 294
237 setEventSpecificFields(pointerEventInit, type); 295 setEventSpecificFields(pointerEventInit, type);
238 296
297 // Created coalesced events init structure
298 HeapVector<Member<PointerEvent>> coalescedPointerEvents;
299 for (const auto& coalescedTouchPoint : coalescedPoints) {
300 PointerEventInit coalescedEventInit = pointerEventInit;
301 updateTouchPointPointerEventInit(coalescedTouchPoint, targetFrame,
302 &coalescedEventInit);
303 coalescedPointerEvents.append(
304 PointerEvent::create(type, coalescedEventInit));
305 }
306 pointerEventInit.setCoalescedEvents(coalescedPointerEvents);
dtapuska 2016/11/21 15:51:56 can we DCHECK that coalescedPoitnerEvents is size
Navid Zolghadr 2016/11/21 19:35:11 Done.
307
239 return PointerEvent::create(type, pointerEventInit); 308 return PointerEvent::create(type, pointerEventInit);
240 } 309 }
241 310
242 PointerEvent* PointerEventFactory::createPointerCancelEvent( 311 PointerEvent* PointerEventFactory::createPointerCancelEvent(
243 const int pointerId, 312 const int pointerId,
244 const WebPointerProperties::PointerType pointerType) { 313 const WebPointerProperties::PointerType pointerType) {
245 DCHECK(m_pointerIdMapping.contains(pointerId)); 314 DCHECK(m_pointerIdMapping.contains(pointerId));
246 m_pointerIdMapping.set( 315 m_pointerIdMapping.set(
247 pointerId, 316 pointerId,
248 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false)); 317 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false));
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 const WebPointerProperties& properties) const { 492 const WebPointerProperties& properties) const {
424 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) 493 if (properties.pointerType == WebPointerProperties::PointerType::Mouse)
425 return PointerEventFactory::s_mouseId; 494 return PointerEventFactory::s_mouseId;
426 IncomingId id(properties.pointerType, properties.id); 495 IncomingId id(properties.pointerType, properties.id);
427 if (m_pointerIncomingIdMapping.contains(id)) 496 if (m_pointerIncomingIdMapping.contains(id))
428 return m_pointerIncomingIdMapping.get(id); 497 return m_pointerIncomingIdMapping.get(id);
429 return PointerEventFactory::s_invalidId; 498 return PointerEventFactory::s_invalidId;
430 } 499 }
431 500
432 } // namespace blink 501 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698