OLD | NEW |
---|---|
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 } | 95 } |
96 | 96 |
97 float getPointerEventPressure(float force, int buttons) { | 97 float getPointerEventPressure(float force, int buttons) { |
98 if (std::isnan(force)) | 98 if (std::isnan(force)) |
99 return buttons ? 0.5 : 0; | 99 return buttons ? 0.5 : 0; |
100 return force; | 100 return force; |
101 } | 101 } |
102 | 102 |
103 void updateTouchPointerEventInit(const PlatformTouchPoint& touchPoint, | 103 void updateTouchPointerEventInit(const PlatformTouchPoint& touchPoint, |
104 LocalFrame* targetFrame, | 104 LocalFrame* targetFrame, |
105 double lastClientX, | |
106 double lastClientY, | |
105 PointerEventInit* pointerEventInit) { | 107 PointerEventInit* pointerEventInit) { |
106 // This function should not update attributes like pointerId, isPrimary, | 108 // This function should not update attributes like pointerId, isPrimary, |
107 // and pointerType which is the same among the coalesced events and the | 109 // and pointerType which is the same among the coalesced events and the |
108 // dispatched event. | 110 // dispatched event. |
109 | 111 |
110 if (targetFrame) { | 112 if (targetFrame) { |
111 FloatPoint pagePoint = | 113 FloatPoint pagePoint = |
112 targetFrame->view()->rootFrameToContents(touchPoint.pos()); | 114 targetFrame->view()->rootFrameToContents(touchPoint.pos()); |
113 float scaleFactor = 1.0f / targetFrame->pageZoomFactor(); | 115 float scaleFactor = 1.0f / targetFrame->pageZoomFactor(); |
114 FloatPoint scrollPosition(targetFrame->view()->getScrollOffset()); | 116 FloatPoint scrollPosition(targetFrame->view()->getScrollOffset()); |
115 FloatPoint clientPoint = pagePoint.scaledBy(scaleFactor); | 117 FloatPoint clientPoint = pagePoint.scaledBy(scaleFactor); |
116 clientPoint.moveBy(scrollPosition.scaledBy(-scaleFactor)); | 118 clientPoint.moveBy(scrollPosition.scaledBy(-scaleFactor)); |
117 | 119 |
118 pointerEventInit->setClientX(clientPoint.x()); | 120 pointerEventInit->setClientX(clientPoint.x()); |
119 pointerEventInit->setClientY(clientPoint.y()); | 121 pointerEventInit->setClientY(clientPoint.y()); |
120 | 122 |
123 // We leave movementX/Y to be zero for anything except pointerdown and | |
124 // pointermove. | |
125 if (touchPoint.state() == PlatformTouchPoint::TouchMoved || | |
126 touchPoint.state() == PlatformTouchPoint::TouchPressed) { | |
mustaq
2017/01/11 19:55:16
Did you mean TouchReleased instead?
Navid Zolghadr
2017/01/16 18:57:23
I did mean release. But after the issue that I fil
| |
127 pointerEventInit->setMovementX(clientPoint.x() - lastClientX); | |
128 pointerEventInit->setMovementY(clientPoint.y() - lastClientY); | |
129 } | |
130 | |
121 FloatSize pointRadius = touchPoint.radius().scaledBy(scaleFactor); | 131 FloatSize pointRadius = touchPoint.radius().scaledBy(scaleFactor); |
122 pointerEventInit->setWidth(pointRadius.width()); | 132 pointerEventInit->setWidth(pointRadius.width()); |
123 pointerEventInit->setHeight(pointRadius.height()); | 133 pointerEventInit->setHeight(pointRadius.height()); |
124 } | 134 } |
125 | 135 |
126 pointerEventInit->setScreenX(touchPoint.screenPos().x()); | 136 pointerEventInit->setScreenX(touchPoint.screenPos().x()); |
127 pointerEventInit->setScreenY(touchPoint.screenPos().y()); | 137 pointerEventInit->setScreenY(touchPoint.screenPos().y()); |
128 pointerEventInit->setPressure( | 138 pointerEventInit->setPressure( |
129 getPointerEventPressure(touchPoint.force(), pointerEventInit->buttons())); | 139 getPointerEventPressure(touchPoint.force(), pointerEventInit->buttons())); |
130 pointerEventInit->setTiltX(touchPoint.pointerProperties().tiltX); | 140 pointerEventInit->setTiltX(touchPoint.pointerProperties().tiltX); |
(...skipping 17 matching lines...) Expand all Loading... | |
148 IntPoint locationInContents = | 158 IntPoint locationInContents = |
149 frameView->rootFrameToContents(mouseEvent.position()); | 159 frameView->rootFrameToContents(mouseEvent.position()); |
150 locationInFrameZoomed = frameView->contentsToFrame(locationInContents); | 160 locationInFrameZoomed = frameView->contentsToFrame(locationInContents); |
151 float scaleFactor = 1 / frame->pageZoomFactor(); | 161 float scaleFactor = 1 / frame->pageZoomFactor(); |
152 locationInFrameZoomed.scale(scaleFactor, scaleFactor); | 162 locationInFrameZoomed.scale(scaleFactor, scaleFactor); |
153 } | 163 } |
154 | 164 |
155 pointerEventInit->setClientX(locationInFrameZoomed.x()); | 165 pointerEventInit->setClientX(locationInFrameZoomed.x()); |
156 pointerEventInit->setClientY(locationInFrameZoomed.y()); | 166 pointerEventInit->setClientY(locationInFrameZoomed.y()); |
157 | 167 |
168 pointerEventInit->setMovementX(mouseEvent.movementDelta().x()); | |
169 pointerEventInit->setMovementY(mouseEvent.movementDelta().y()); | |
170 | |
158 pointerEventInit->setPressure(getPointerEventPressure( | 171 pointerEventInit->setPressure(getPointerEventPressure( |
159 mouseEvent.pointerProperties().force, pointerEventInit->buttons())); | 172 mouseEvent.pointerProperties().force, pointerEventInit->buttons())); |
160 pointerEventInit->setTiltX(mouseEvent.pointerProperties().tiltX); | 173 pointerEventInit->setTiltX(mouseEvent.pointerProperties().tiltX); |
161 pointerEventInit->setTiltY(mouseEvent.pointerProperties().tiltY); | 174 pointerEventInit->setTiltY(mouseEvent.pointerProperties().tiltY); |
162 } | 175 } |
163 | 176 |
164 } // namespace | 177 } // namespace |
165 | 178 |
166 const int PointerEventFactory::s_invalidId = 0; | 179 const int PointerEventFactory::s_invalidId = 0; |
167 | 180 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 | 309 |
297 PointerEventInit pointerEventInit; | 310 PointerEventInit pointerEventInit; |
298 | 311 |
299 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), | 312 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), |
300 pointerReleasedOrCancelled ? 0 : 1); | 313 pointerReleasedOrCancelled ? 0 : 1); |
301 pointerEventInit.setButton(static_cast<int>( | 314 pointerEventInit.setButton(static_cast<int>( |
302 pointerPressedOrReleased ? WebPointerProperties::Button::Left | 315 pointerPressedOrReleased ? WebPointerProperties::Button::Left |
303 : WebPointerProperties::Button::NoButton)); | 316 : WebPointerProperties::Button::NoButton)); |
304 | 317 |
305 pointerEventInit.setView(view); | 318 pointerEventInit.setView(view); |
306 updateTouchPointerEventInit(touchPoint, targetFrame, &pointerEventInit); | 319 PointerAttributes lastPointerAttributes = |
320 m_pointerIdMapping.get(pointerEventInit.pointerId()); | |
321 double lastClientX = lastPointerAttributes.clientX; | |
322 double lastClientY = lastPointerAttributes.clientY; | |
323 updateTouchPointerEventInit(touchPoint, targetFrame, lastClientX, lastClientY, | |
324 &pointerEventInit); | |
325 lastPointerAttributes.clientX = pointerEventInit.clientX(); | |
326 lastPointerAttributes.clientY = pointerEventInit.clientY(); | |
327 m_pointerIdMapping.set(pointerEventInit.pointerId(), lastPointerAttributes); | |
307 | 328 |
308 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); | 329 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); |
309 | 330 |
310 setEventSpecificFields(pointerEventInit, type); | 331 setEventSpecificFields(pointerEventInit, type); |
311 | 332 |
312 if (type == EventTypeNames::pointermove) { | 333 if (type == EventTypeNames::pointermove) { |
313 HeapVector<Member<PointerEvent>> coalescedPointerEvents; | 334 HeapVector<Member<PointerEvent>> coalescedPointerEvents; |
314 for (const auto& coalescedTouchPoint : coalescedPoints) { | 335 for (const auto& coalescedTouchPoint : coalescedPoints) { |
315 DCHECK_EQ(touchPoint.state(), coalescedTouchPoint.state()); | 336 DCHECK_EQ(touchPoint.state(), coalescedTouchPoint.state()); |
316 DCHECK_EQ(touchPoint.pointerProperties().id, | 337 DCHECK_EQ(touchPoint.pointerProperties().id, |
317 coalescedTouchPoint.pointerProperties().id); | 338 coalescedTouchPoint.pointerProperties().id); |
318 DCHECK_EQ(touchPoint.pointerProperties().pointerType, | 339 DCHECK_EQ(touchPoint.pointerProperties().pointerType, |
319 coalescedTouchPoint.pointerProperties().pointerType); | 340 coalescedTouchPoint.pointerProperties().pointerType); |
320 PointerEventInit coalescedEventInit = pointerEventInit; | 341 PointerEventInit coalescedEventInit = pointerEventInit; |
321 updateTouchPointerEventInit(coalescedTouchPoint, targetFrame, | 342 updateTouchPointerEventInit(coalescedTouchPoint, targetFrame, lastClientX, |
322 &coalescedEventInit); | 343 lastClientY, &coalescedEventInit); |
344 lastClientX = pointerEventInit.clientX(); | |
345 lastClientY = pointerEventInit.clientY(); | |
323 coalescedPointerEvents.push_back( | 346 coalescedPointerEvents.push_back( |
324 PointerEvent::create(type, coalescedEventInit)); | 347 PointerEvent::create(type, coalescedEventInit)); |
325 } | 348 } |
326 pointerEventInit.setCoalescedEvents(coalescedPointerEvents); | 349 pointerEventInit.setCoalescedEvents(coalescedPointerEvents); |
327 } | 350 } |
328 | 351 |
329 return PointerEvent::create(type, pointerEventInit); | 352 return PointerEvent::create(type, pointerEventInit); |
330 } | 353 } |
331 | 354 |
332 PointerEvent* PointerEventFactory::createPointerCancelEvent( | 355 PointerEvent* PointerEventFactory::createPointerCancelEvent( |
333 const int pointerId, | 356 const int pointerId, |
334 const WebPointerProperties::PointerType pointerType) { | 357 const WebPointerProperties::PointerType pointerType) { |
335 DCHECK(m_pointerIdMapping.contains(pointerId)); | 358 DCHECK(m_pointerIdMapping.contains(pointerId)); |
336 m_pointerIdMapping.set( | 359 m_pointerIdMapping.set( |
337 pointerId, | 360 pointerId, PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, |
338 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false)); | 361 false, 0, 0)); |
339 | 362 |
340 PointerEventInit pointerEventInit; | 363 PointerEventInit pointerEventInit; |
341 | 364 |
342 pointerEventInit.setPointerId(pointerId); | 365 pointerEventInit.setPointerId(pointerId); |
343 pointerEventInit.setPointerType( | 366 pointerEventInit.setPointerType( |
344 pointerTypeNameForWebPointPointerType(pointerType)); | 367 pointerTypeNameForWebPointPointerType(pointerType)); |
345 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | 368 pointerEventInit.setIsPrimary(isPrimary(pointerId)); |
346 | 369 |
347 setEventSpecificFields(pointerEventInit, EventTypeNames::pointercancel); | 370 setEventSpecificFields(pointerEventInit, EventTypeNames::pointercancel); |
348 | 371 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 for (int type = 0; | 436 for (int type = 0; |
414 type <= toInt(WebPointerProperties::PointerType::LastEntry); type++) { | 437 type <= toInt(WebPointerProperties::PointerType::LastEntry); type++) { |
415 m_primaryId[type] = PointerEventFactory::s_invalidId; | 438 m_primaryId[type] = PointerEventFactory::s_invalidId; |
416 m_idCount[type] = 0; | 439 m_idCount[type] = 0; |
417 } | 440 } |
418 m_pointerIncomingIdMapping.clear(); | 441 m_pointerIncomingIdMapping.clear(); |
419 m_pointerIdMapping.clear(); | 442 m_pointerIdMapping.clear(); |
420 | 443 |
421 // Always add mouse pointer in initialization and never remove it. | 444 // Always add mouse pointer in initialization and never remove it. |
422 // No need to add it to m_pointerIncomingIdMapping as it is not going to be | 445 // No need to add it to m_pointerIncomingIdMapping as it is not going to be |
423 // used with the existing APIs | 446 // used with the existing APIs. |
447 // We don't need to set clientX/Y for the mouse as that will only be used to | |
448 // set movementX/Y which for mouse it comes from the browser. See Pointer Lock | |
449 // spec: https://www.w3.org/TR/pointerlock/. | |
424 m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = s_mouseId; | 450 m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = s_mouseId; |
425 m_pointerIdMapping.add( | 451 m_pointerIdMapping.add( |
426 s_mouseId, | 452 s_mouseId, |
427 PointerAttributes(IncomingId(WebPointerProperties::PointerType::Mouse, 0), | 453 PointerAttributes(IncomingId(WebPointerProperties::PointerType::Mouse, 0), |
428 0)); | 454 0, 0, 0)); |
429 | 455 |
430 m_currentId = PointerEventFactory::s_mouseId + 1; | 456 m_currentId = PointerEventFactory::s_mouseId + 1; |
431 } | 457 } |
432 | 458 |
433 int PointerEventFactory::addIdAndActiveButtons(const IncomingId p, | 459 int PointerEventFactory::addIdAndActiveButtons(const IncomingId p, |
434 bool isActiveButtons) { | 460 bool isActiveButtons) { |
435 // Do not add extra mouse pointer as it was added in initialization | 461 // Do not add extra mouse pointer as it was added in initialization |
436 if (p.pointerType() == WebPointerProperties::PointerType::Mouse) { | 462 if (p.pointerType() == WebPointerProperties::PointerType::Mouse) { |
437 m_pointerIdMapping.set(s_mouseId, PointerAttributes(p, isActiveButtons)); | 463 m_pointerIdMapping.set(s_mouseId, |
464 PointerAttributes(p, isActiveButtons, 0, 0)); | |
438 return s_mouseId; | 465 return s_mouseId; |
439 } | 466 } |
440 | 467 |
441 if (m_pointerIncomingIdMapping.contains(p)) { | 468 if (m_pointerIncomingIdMapping.contains(p)) { |
442 int mappedId = m_pointerIncomingIdMapping.get(p); | 469 int mappedId = m_pointerIncomingIdMapping.get(p); |
443 m_pointerIdMapping.set(mappedId, PointerAttributes(p, isActiveButtons)); | 470 PointerAttributes lastPointerAttributes = m_pointerIdMapping.get(mappedId); |
471 m_pointerIdMapping.set( | |
472 mappedId, | |
473 PointerAttributes(p, isActiveButtons, lastPointerAttributes.clientX, | |
474 lastPointerAttributes.clientY)); | |
444 return mappedId; | 475 return mappedId; |
445 } | 476 } |
446 int typeInt = p.pointerTypeInt(); | 477 int typeInt = p.pointerTypeInt(); |
447 // We do not handle the overflow of m_currentId as it should be very rare | 478 // We do not handle the overflow of m_currentId as it should be very rare |
448 int mappedId = m_currentId++; | 479 int mappedId = m_currentId++; |
449 if (!m_idCount[typeInt]) | 480 if (!m_idCount[typeInt]) |
450 m_primaryId[typeInt] = mappedId; | 481 m_primaryId[typeInt] = mappedId; |
451 m_idCount[typeInt]++; | 482 m_idCount[typeInt]++; |
452 m_pointerIncomingIdMapping.add(p, mappedId); | 483 m_pointerIncomingIdMapping.add(p, mappedId); |
453 m_pointerIdMapping.add(mappedId, PointerAttributes(p, isActiveButtons)); | 484 |
485 // Zero initialization should be fine as we only set the movementX/Y for | |
486 // pointermove and pointerup. | |
487 m_pointerIdMapping.add(mappedId, PointerAttributes(p, isActiveButtons, 0, 0)); | |
454 return mappedId; | 488 return mappedId; |
455 } | 489 } |
456 | 490 |
457 bool PointerEventFactory::remove(const int mappedId) { | 491 bool PointerEventFactory::remove(const int mappedId) { |
458 // Do not remove mouse pointer id as it should always be there | 492 // Do not remove mouse pointer id as it should always be there |
459 if (mappedId == s_mouseId || !m_pointerIdMapping.contains(mappedId)) | 493 if (mappedId == s_mouseId || !m_pointerIdMapping.contains(mappedId)) |
460 return false; | 494 return false; |
461 | 495 |
462 IncomingId p = m_pointerIdMapping.get(mappedId).incomingId; | 496 IncomingId p = m_pointerIdMapping.get(mappedId).incomingId; |
463 int typeInt = p.pointerTypeInt(); | 497 int typeInt = p.pointerTypeInt(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 const WebPointerProperties& properties) const { | 547 const WebPointerProperties& properties) const { |
514 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) | 548 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) |
515 return PointerEventFactory::s_mouseId; | 549 return PointerEventFactory::s_mouseId; |
516 IncomingId id(properties.pointerType, properties.id); | 550 IncomingId id(properties.pointerType, properties.id); |
517 if (m_pointerIncomingIdMapping.contains(id)) | 551 if (m_pointerIncomingIdMapping.contains(id)) |
518 return m_pointerIncomingIdMapping.get(id); | 552 return m_pointerIncomingIdMapping.get(id); |
519 return PointerEventFactory::s_invalidId; | 553 return PointerEventFactory::s_invalidId; |
520 } | 554 } |
521 | 555 |
522 } // namespace blink | 556 } // namespace blink |
OLD | NEW |