| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 updateTouchPointerEventInit(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 pointerEventInit->setClientX(locationInFrameZoomed.x()); |
| 148 pointerEventInit->setClientY(locationInFrameZoomed.y()); |
| 149 |
| 150 pointerEventInit->setPressure(getPointerEventPressure( |
| 151 mouseEvent.pointerProperties().force, pointerEventInit->buttons())); |
| 152 pointerEventInit->setTiltX(mouseEvent.pointerProperties().tiltX); |
| 153 pointerEventInit->setTiltY(mouseEvent.pointerProperties().tiltY); |
| 154 } |
| 155 |
| 156 } // namespace |
| 157 |
| 158 const int PointerEventFactory::s_invalidId = 0; |
| 159 |
| 160 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons. |
| 161 const int PointerEventFactory::s_mouseId = 1; |
| 162 |
| 91 void PointerEventFactory::setIdTypeButtons( | 163 void PointerEventFactory::setIdTypeButtons( |
| 92 PointerEventInit& pointerEventInit, | 164 PointerEventInit& pointerEventInit, |
| 93 const WebPointerProperties& pointerProperties, | 165 const WebPointerProperties& pointerProperties, |
| 94 unsigned buttons) { | 166 unsigned buttons) { |
| 95 const WebPointerProperties::PointerType pointerType = | 167 const WebPointerProperties::PointerType pointerType = |
| 96 pointerProperties.pointerType; | 168 pointerProperties.pointerType; |
| 97 const IncomingId incomingId(pointerType, pointerProperties.id); | 169 const IncomingId incomingId(pointerType, pointerProperties.id); |
| 98 int pointerId = addIdAndActiveButtons(incomingId, buttons != 0); | 170 int pointerId = addIdAndActiveButtons(incomingId, buttons != 0); |
| 99 | 171 |
| 100 // Tweak the |buttons| to reflect pen eraser mode only if the pen is in | 172 // Tweak the |buttons| to reflect pen eraser mode only if the pen is in |
| (...skipping 20 matching lines...) Expand all Loading... |
| 121 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter && | 193 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter && |
| 122 type != EventTypeNames::pointerleave && | 194 type != EventTypeNames::pointerleave && |
| 123 type != EventTypeNames::pointercancel && | 195 type != EventTypeNames::pointercancel && |
| 124 type != EventTypeNames::gotpointercapture && | 196 type != EventTypeNames::gotpointercapture && |
| 125 type != EventTypeNames::lostpointercapture); | 197 type != EventTypeNames::lostpointercapture); |
| 126 | 198 |
| 127 pointerEventInit.setComposed(true); | 199 pointerEventInit.setComposed(true); |
| 128 pointerEventInit.setDetail(0); | 200 pointerEventInit.setDetail(0); |
| 129 } | 201 } |
| 130 | 202 |
| 131 PointerEvent* PointerEventFactory::create(const AtomicString& mouseEventName, | 203 PointerEvent* PointerEventFactory::create( |
| 132 const PlatformMouseEvent& mouseEvent, | 204 const AtomicString& mouseEventName, |
| 133 LocalDOMWindow* view) { | 205 const PlatformMouseEvent& mouseEvent, |
| 206 const Vector<PlatformMouseEvent>& coalescedMouseEvents, |
| 207 LocalDOMWindow* view) { |
| 134 DCHECK(mouseEventName == EventTypeNames::mousemove || | 208 DCHECK(mouseEventName == EventTypeNames::mousemove || |
| 135 mouseEventName == EventTypeNames::mousedown || | 209 mouseEventName == EventTypeNames::mousedown || |
| 136 mouseEventName == EventTypeNames::mouseup); | 210 mouseEventName == EventTypeNames::mouseup); |
| 137 | 211 |
| 138 AtomicString pointerEventName = | 212 AtomicString pointerEventName = |
| 139 pointerEventNameForMouseEventName(mouseEventName); | 213 pointerEventNameForMouseEventName(mouseEventName); |
| 214 DCHECK(pointerEventName == EventTypeNames::pointermove || |
| 215 coalescedMouseEvents.isEmpty()); |
| 216 |
| 140 unsigned buttons = | 217 unsigned buttons = |
| 141 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers()); | 218 MouseEvent::platformModifiersToButtons(mouseEvent.getModifiers()); |
| 142 PointerEventInit pointerEventInit; | 219 PointerEventInit pointerEventInit; |
| 143 | 220 |
| 144 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons); | 221 setIdTypeButtons(pointerEventInit, mouseEvent.pointerProperties(), buttons); |
| 145 setEventSpecificFields(pointerEventInit, pointerEventName); | 222 setEventSpecificFields(pointerEventInit, pointerEventName); |
| 146 | 223 |
| 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 || | 224 if (pointerEventName == EventTypeNames::pointerdown || |
| 166 pointerEventName == EventTypeNames::pointerup) { | 225 pointerEventName == EventTypeNames::pointerup) { |
| 167 WebPointerProperties::Button button = mouseEvent.pointerProperties().button; | 226 WebPointerProperties::Button button = mouseEvent.pointerProperties().button; |
| 168 // TODO(mustaq): Fix when the spec starts supporting hovering erasers. | 227 // TODO(mustaq): Fix when the spec starts supporting hovering erasers. |
| 169 if (mouseEvent.pointerProperties().pointerType == | 228 if (mouseEvent.pointerProperties().pointerType == |
| 170 WebPointerProperties::PointerType::Eraser && | 229 WebPointerProperties::PointerType::Eraser && |
| 171 button == WebPointerProperties::Button::Left) | 230 button == WebPointerProperties::Button::Left) |
| 172 button = WebPointerProperties::Button::Eraser; | 231 button = WebPointerProperties::Button::Eraser; |
| 173 pointerEventInit.setButton(static_cast<int>(button)); | 232 pointerEventInit.setButton(static_cast<int>(button)); |
| 174 } else { | 233 } else { |
| 175 DCHECK(pointerEventName == EventTypeNames::pointermove); | 234 DCHECK(pointerEventName == EventTypeNames::pointermove); |
| 176 pointerEventInit.setButton( | 235 pointerEventInit.setButton( |
| 177 static_cast<int>(WebPointerProperties::Button::NoButton)); | 236 static_cast<int>(WebPointerProperties::Button::NoButton)); |
| 178 } | 237 } |
| 179 pointerEventInit.setPressure(getPointerEventPressure( | |
| 180 mouseEvent.pointerProperties().force, pointerEventInit.buttons())); | |
| 181 pointerEventInit.setTiltX(mouseEvent.pointerProperties().tiltX); | |
| 182 pointerEventInit.setTiltY(mouseEvent.pointerProperties().tiltY); | |
| 183 | 238 |
| 184 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, | 239 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, |
| 185 mouseEvent.getModifiers()); | 240 mouseEvent.getModifiers()); |
| 186 | 241 |
| 187 // Make sure chorded buttons fire pointermove instead of pointerup/down. | 242 // Make sure chorded buttons fire pointermove instead of pointerup/down. |
| 188 if ((pointerEventName == EventTypeNames::pointerdown && | 243 if ((pointerEventName == EventTypeNames::pointerdown && |
| 189 (buttons & | 244 (buttons & |
| 190 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) != | 245 ~buttonToButtonsBitfield(mouseEvent.pointerProperties().button)) != |
| 191 0) || | 246 0) || |
| 192 (pointerEventName == EventTypeNames::pointerup && buttons != 0)) | 247 (pointerEventName == EventTypeNames::pointerup && buttons != 0)) |
| 193 pointerEventName = EventTypeNames::pointermove; | 248 pointerEventName = EventTypeNames::pointermove; |
| 194 | 249 |
| 195 pointerEventInit.setView(view); | 250 pointerEventInit.setView(view); |
| 196 | 251 |
| 252 updateMousePointerEventInit(mouseEvent, view, &pointerEventInit); |
| 253 |
| 254 // Created coalesced events init structure |
| 255 HeapVector<Member<PointerEvent>> coalescedPointerEvents; |
| 256 for (const auto& coalescedMouseEvent : coalescedMouseEvents) { |
| 257 PointerEventInit coalescedEventInit = pointerEventInit; |
| 258 updateMousePointerEventInit(coalescedMouseEvent, view, &coalescedEventInit); |
| 259 coalescedPointerEvents.append( |
| 260 PointerEvent::create(pointerEventName, coalescedEventInit)); |
| 261 } |
| 262 pointerEventInit.setCoalescedEvents(coalescedPointerEvents); |
| 263 |
| 197 return PointerEvent::create(pointerEventName, pointerEventInit); | 264 return PointerEvent::create(pointerEventName, pointerEventInit); |
| 198 } | 265 } |
| 199 | 266 |
| 200 PointerEvent* PointerEventFactory::create(const AtomicString& type, | 267 PointerEvent* PointerEventFactory::create( |
| 201 const PlatformTouchPoint& touchPoint, | 268 const PlatformTouchPoint& touchPoint, |
| 202 PlatformEvent::Modifiers modifiers, | 269 const Vector<PlatformTouchPoint>& coalescedPoints, |
| 203 const FloatSize& pointRadius, | 270 PlatformEvent::Modifiers modifiers, |
| 204 const FloatPoint& clientPoint, | 271 LocalFrame* targetFrame, |
| 205 DOMWindow* view) { | 272 DOMWindow* view) { |
| 206 const PlatformTouchPoint::TouchState pointState = touchPoint.state(); | 273 const PlatformTouchPoint::TouchState pointState = touchPoint.state(); |
| 274 const AtomicString& type = |
| 275 pointerEventNameForTouchPointState(touchPoint.state()); |
| 276 DCHECK(type == EventTypeNames::pointermove || coalescedPoints.isEmpty()); |
| 207 | 277 |
| 208 bool pointerReleasedOrCancelled = | 278 bool pointerReleasedOrCancelled = |
| 209 pointState == PlatformTouchPoint::TouchReleased || | 279 pointState == PlatformTouchPoint::TouchReleased || |
| 210 pointState == PlatformTouchPoint::TouchCancelled; | 280 pointState == PlatformTouchPoint::TouchCancelled; |
| 211 bool pointerPressedOrReleased = | 281 bool pointerPressedOrReleased = |
| 212 pointState == PlatformTouchPoint::TouchPressed || | 282 pointState == PlatformTouchPoint::TouchPressed || |
| 213 pointState == PlatformTouchPoint::TouchReleased; | 283 pointState == PlatformTouchPoint::TouchReleased; |
| 214 | 284 |
| 215 PointerEventInit pointerEventInit; | 285 PointerEventInit pointerEventInit; |
| 216 | 286 |
| 217 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), | 287 setIdTypeButtons(pointerEventInit, touchPoint.pointerProperties(), |
| 218 pointerReleasedOrCancelled ? 0 : 1); | 288 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>( | 289 pointerEventInit.setButton(static_cast<int>( |
| 227 pointerPressedOrReleased ? WebPointerProperties::Button::Left | 290 pointerPressedOrReleased ? WebPointerProperties::Button::Left |
| 228 : WebPointerProperties::Button::NoButton)); | 291 : WebPointerProperties::Button::NoButton)); |
| 229 pointerEventInit.setPressure( | 292 |
| 230 getPointerEventPressure(touchPoint.force(), pointerEventInit.buttons())); | |
| 231 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX); | |
| 232 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY); | |
| 233 pointerEventInit.setView(view); | 293 pointerEventInit.setView(view); |
| 294 updateTouchPointerEventInit(touchPoint, targetFrame, &pointerEventInit); |
| 234 | 295 |
| 235 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); | 296 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); |
| 236 | 297 |
| 237 setEventSpecificFields(pointerEventInit, type); | 298 setEventSpecificFields(pointerEventInit, type); |
| 238 | 299 |
| 300 // Created coalesced events init structure |
| 301 HeapVector<Member<PointerEvent>> coalescedPointerEvents; |
| 302 for (const auto& coalescedTouchPoint : coalescedPoints) { |
| 303 PointerEventInit coalescedEventInit = pointerEventInit; |
| 304 updateTouchPointerEventInit(coalescedTouchPoint, targetFrame, |
| 305 &coalescedEventInit); |
| 306 coalescedPointerEvents.append( |
| 307 PointerEvent::create(type, coalescedEventInit)); |
| 308 } |
| 309 pointerEventInit.setCoalescedEvents(coalescedPointerEvents); |
| 310 |
| 239 return PointerEvent::create(type, pointerEventInit); | 311 return PointerEvent::create(type, pointerEventInit); |
| 240 } | 312 } |
| 241 | 313 |
| 242 PointerEvent* PointerEventFactory::createPointerCancelEvent( | 314 PointerEvent* PointerEventFactory::createPointerCancelEvent( |
| 243 const int pointerId, | 315 const int pointerId, |
| 244 const WebPointerProperties::PointerType pointerType) { | 316 const WebPointerProperties::PointerType pointerType) { |
| 245 DCHECK(m_pointerIdMapping.contains(pointerId)); | 317 DCHECK(m_pointerIdMapping.contains(pointerId)); |
| 246 m_pointerIdMapping.set( | 318 m_pointerIdMapping.set( |
| 247 pointerId, | 319 pointerId, |
| 248 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false)); | 320 PointerAttributes(m_pointerIdMapping.get(pointerId).incomingId, false)); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 const WebPointerProperties& properties) const { | 495 const WebPointerProperties& properties) const { |
| 424 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) | 496 if (properties.pointerType == WebPointerProperties::PointerType::Mouse) |
| 425 return PointerEventFactory::s_mouseId; | 497 return PointerEventFactory::s_mouseId; |
| 426 IncomingId id(properties.pointerType, properties.id); | 498 IncomingId id(properties.pointerType, properties.id); |
| 427 if (m_pointerIncomingIdMapping.contains(id)) | 499 if (m_pointerIncomingIdMapping.contains(id)) |
| 428 return m_pointerIncomingIdMapping.get(id); | 500 return m_pointerIncomingIdMapping.get(id); |
| 429 return PointerEventFactory::s_invalidId; | 501 return PointerEventFactory::s_invalidId; |
| 430 } | 502 } |
| 431 | 503 |
| 432 } // namespace blink | 504 } // namespace blink |
| OLD | NEW |