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

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

Issue 2479123003: WIP Add getCoalescedEvents API using vector of WebInputEvents (Closed)
Patch Set: 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 17 matching lines...) Expand all
118 const AtomicString& type) { 191 const AtomicString& type) {
119 pointerEventInit.setBubbles(type != EventTypeNames::pointerenter && 192 pointerEventInit.setBubbles(type != EventTypeNames::pointerenter &&
120 type != EventTypeNames::pointerleave); 193 type != EventTypeNames::pointerleave);
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 200
128 PointerEvent* PointerEventFactory::create(const AtomicString& mouseEventName, 201 PointerEvent* PointerEventFactory::create(
129 const PlatformMouseEvent& mouseEvent, 202 const AtomicString& mouseEventName,
130 LocalDOMWindow* view) { 203 const PlatformMouseEvent& mouseEvent,
204 const Vector<PlatformMouseEvent>& coalescedEvents,
205 LocalDOMWindow* view) {
131 DCHECK(mouseEventName == EventTypeNames::mousemove || 206 DCHECK(mouseEventName == EventTypeNames::mousemove ||
132 mouseEventName == EventTypeNames::mousedown || 207 mouseEventName == EventTypeNames::mousedown ||
133 mouseEventName == EventTypeNames::mouseup); 208 mouseEventName == EventTypeNames::mouseup);
134 209
135 AtomicString pointerEventName = 210 AtomicString pointerEventName =
136 pointerEventNameForMouseEventName(mouseEventName); 211 pointerEventNameForMouseEventName(mouseEventName);
137 unsigned buttons = 212 unsigned buttons =
138 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers()); 213 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers());
139 PointerEventInit pointerEventInit; 214 PointerEventInit pointerEventInit;
140 215
141 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons); 216 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons);
142 setBubblesAndCancelable(pointerEventInit, pointerEventName); 217 setBubblesAndCancelable(pointerEventInit, pointerEventName);
143 218
144 pointerEventInit.setScreenX(mouseEvent.globalPosition().x());
145 pointerEventInit.setScreenY(mouseEvent.globalPosition().y());
146
147 IntPoint locationInFrameZoomed;
148 if (view && view->frame() && view->frame()->view()) {
149 LocalFrame* frame = view->frame();
150 FrameView* frameView = frame->view();
151 IntPoint locationInContents =
152 frameView->rootFrameToContents(mouseEvent.position());
153 locationInFrameZoomed = frameView->contentsToFrame(locationInContents);
154 float scaleFactor = 1 / frame->pageZoomFactor();
155 locationInFrameZoomed.scale(scaleFactor, scaleFactor);
156 }
157
158 // Set up initial values for coordinates.
159 pointerEventInit.setClientX(locationInFrameZoomed.x());
160 pointerEventInit.setClientY(locationInFrameZoomed.y());
161
162 if (pointerEventName == EventTypeNames::pointerdown || 219 if (pointerEventName == EventTypeNames::pointerdown ||
163 pointerEventName == EventTypeNames::pointerup) { 220 pointerEventName == EventTypeNames::pointerup) {
164 WebPointerProperties::Button button = mouseEvent.pointerProperties().button; 221 WebPointerProperties::Button button = mouseEvent.pointerProperties().button;
165 // TODO(mustaq): Fix when the spec starts supporting hovering erasers. 222 // TODO(mustaq): Fix when the spec starts supporting hovering erasers.
166 if (mouseEvent.pointerProperties().pointerType == 223 if (mouseEvent.pointerProperties().pointerType ==
167 WebPointerProperties::PointerType::Eraser && 224 WebPointerProperties::PointerType::Eraser &&
168 button == WebPointerProperties::Button::Left) 225 button == WebPointerProperties::Button::Left)
169 button = WebPointerProperties::Button::Eraser; 226 button = WebPointerProperties::Button::Eraser;
170 pointerEventInit.setButton(static_cast<int>(button)); 227 pointerEventInit.setButton(static_cast<int>(button));
171 } else { 228 } else {
172 DCHECK(pointerEventName == EventTypeNames::pointermove); 229 DCHECK(pointerEventName == EventTypeNames::pointermove);
173 pointerEventInit.setButton( 230 pointerEventInit.setButton(
174 static_cast<int>(WebPointerProperties::Button::NoButton)); 231 static_cast<int>(WebPointerProperties::Button::NoButton));
175 } 232 }
176 pointerEventInit.setPressure(getPointerEventPressure(
177 mouseEvent.pointerProperties().force, pointerEventInit.buttons()));
178 pointerEventInit.setTiltX(mouseEvent.pointerProperties().tiltX);
179 pointerEventInit.setTiltY(mouseEvent.pointerProperties().tiltY);
180 233
181 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, 234 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit,
182 mouseEvent.getModifiers()); 235 mouseEvent.getModifiers());
183 236
184 // Make sure chorded buttons fire pointermove instead of pointerup/down. 237 // Make sure chorded buttons fire pointermove instead of pointerup/down.
185 if ((pointerEventName == EventTypeNames::pointerdown && 238 if ((pointerEventName == EventTypeNames::pointerdown &&
186 (buttons & 239 (buttons &
187 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) != 240 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) !=
188 0) || 241 0) ||
189 (pointerEventName == EventTypeNames::pointerup && buttons != 0)) 242 (pointerEventName == EventTypeNames::pointerup && buttons != 0))
190 pointerEventName = EventTypeNames::pointermove; 243 pointerEventName = EventTypeNames::pointermove;
191 244
192 pointerEventInit.setView(view); 245 pointerEventInit.setView(view);
193 246
247 updateMousePointerEventInit(mouseEvent, view, &pointerEventInit);
248
249 // Created coalesced events init structure
250 HeapVector<PointerEventInit> coalescedEventInits;
251 for (const auto& coalescedMouseEvent : coalescedEvents) {
252 PointerEventInit coalescedEventInit = pointerEventInit;
253 updateMousePointerEventInit(coalescedMouseEvent, view, &coalescedEventInit);
254 coalescedEventInits.append(coalescedEventInit);
255 }
256 pointerEventInit.setCoalescedEventInits(coalescedEventInits);
257
194 return PointerEvent::create(pointerEventName, pointerEventInit); 258 return PointerEvent::create(pointerEventName, pointerEventInit);
195 } 259 }
196 260
197 PointerEvent* PointerEventFactory::create(const AtomicString& type, 261 PointerEvent* PointerEventFactory::create(
198 const PlatformTouchPoint& touchPoint, 262 const PlatformTouchPoint& touchPoint,
199 PlatformEvent::Modifiers modifiers, 263 const Vector<PlatformTouchPoint>& coalescedPoints,
200 const FloatSize& pointRadius, 264 PlatformEvent::Modifiers modifiers,
201 const FloatPoint& clientPoint, 265 LocalFrame* targetFrame,
202 DOMWindow* view) { 266 // const float scaleFactor,
267 // const FloatPoint& clientPoint,
268 DOMWindow* view) {
203 const PlatformTouchPoint::TouchState pointState = touchPoint.state(); 269 const PlatformTouchPoint::TouchState pointState = touchPoint.state();
204 270
205 bool pointerReleasedOrCancelled = 271 bool pointerReleasedOrCancelled =
206 pointState == PlatformTouchPoint::TouchReleased || 272 pointState == PlatformTouchPoint::TouchReleased ||
207 pointState == PlatformTouchPoint::TouchCancelled; 273 pointState == PlatformTouchPoint::TouchCancelled;
208 bool pointerPressedOrReleased = 274 bool pointerPressedOrReleased =
209 pointState == PlatformTouchPoint::TouchPressed || 275 pointState == PlatformTouchPoint::TouchPressed ||
210 pointState == PlatformTouchPoint::TouchReleased; 276 pointState == PlatformTouchPoint::TouchReleased;
211 277
212 bool isEnterOrLeave = false; 278 bool isEnterOrLeave = false;
213 279
214 PointerEventInit pointerEventInit; 280 PointerEventInit pointerEventInit;
215 281
216 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), 282 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(),
217 pointerReleasedOrCancelled ? 0 : 1); 283 pointerReleasedOrCancelled ? 0 : 1);
218
219 pointerEventInit.setWidth(pointRadius.width());
220 pointerEventInit.setHeight(pointRadius.height());
221 pointerEventInit.setScreenX(touchPoint.screenPos().x());
222 pointerEventInit.setScreenY(touchPoint.screenPos().y());
223 pointerEventInit.setClientX(clientPoint.x());
224 pointerEventInit.setClientY(clientPoint.y());
225 pointerEventInit.setButton(static_cast<int>( 284 pointerEventInit.setButton(static_cast<int>(
226 pointerPressedOrReleased ? WebPointerProperties::Button::Left 285 pointerPressedOrReleased ? WebPointerProperties::Button::Left
227 : WebPointerProperties::Button::NoButton)); 286 : WebPointerProperties::Button::NoButton));
228 pointerEventInit.setPressure( 287
229 getPointerEventPressure(touchPoint.force(), pointerEventInit.buttons()));
230 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX);
231 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY);
232 pointerEventInit.setView(view); 288 pointerEventInit.setView(view);
289 updateTouchPointPointerEventInit(touchPoint, targetFrame, &pointerEventInit);
233 290
234 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); 291 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers);
235 292
236 pointerEventInit.setBubbles(!isEnterOrLeave); 293 pointerEventInit.setBubbles(!isEnterOrLeave);
237 pointerEventInit.setCancelable( 294 pointerEventInit.setCancelable(
238 !isEnterOrLeave && pointState != PlatformTouchPoint::TouchCancelled); 295 !isEnterOrLeave && pointState != PlatformTouchPoint::TouchCancelled);
239 296
240 return PointerEvent::create(type, pointerEventInit); 297 // Created coalesced events init structure
298 HeapVector<PointerEventInit> coalescedEventInits;
299 for (const auto& coalescedTouchPoint : coalescedPoints) {
300 PointerEventInit coalescedEventInit = pointerEventInit;
301 updateTouchPointPointerEventInit(coalescedTouchPoint, targetFrame,
302 &coalescedEventInit);
303 coalescedEventInits.append(coalescedEventInit);
304 }
305 pointerEventInit.setCoalescedEventInits(coalescedEventInits);
306
307 return PointerEvent::create(
308 pointerEventNameForTouchPointState(touchPoint.state()), pointerEventInit);
241 } 309 }
242 310
243 PointerEvent* PointerEventFactory::createPointerCancelEvent( 311 PointerEvent* PointerEventFactory::createPointerCancelEvent(
244 const int pointerId, 312 const int pointerId,
245 const WebPointerProperties::PointerType pointerType) { 313 const WebPointerProperties::PointerType pointerType) {
246 DCHECK(m_pointerIdMapping.contains(pointerId)); 314 DCHECK(m_pointerIdMapping.contains(pointerId));
247 m_pointerIdMapping.set( 315 m_pointerIdMapping.set(
248 pointerId, 316 pointerId,
249 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false)); 317 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false));
250 318
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 const WebPointerProperties& properties) const { 492 const WebPointerProperties& properties) const {
425 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) 493 if (properties.pointerType == WebPointerProperties::PointerType::Mouse)
426 return PointerEventFactory::s_mouseId; 494 return PointerEventFactory::s_mouseId;
427 IncomingId id(properties.pointerType, properties.id); 495 IncomingId id(properties.pointerType, properties.id);
428 if (m_pointerIncomingIdMapping.contains(id)) 496 if (m_pointerIncomingIdMapping.contains(id))
429 return m_pointerIncomingIdMapping.get(id); 497 return m_pointerIncomingIdMapping.get(id);
430 return PointerEventFactory::s_invalidId; 498 return PointerEventFactory::s_invalidId;
431 } 499 }
432 500
433 } // namespace blink 501 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698