OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "config.h" |
| 6 #include "core/events/PointerEventFactory.h" |
| 7 |
| 8 namespace blink { |
| 9 |
| 10 namespace { |
| 11 |
| 12 inline int toInt(WebPointerProperties::PointerType t) { return static_cast<int>(
t); } |
| 13 |
| 14 const char* pointerTypeNameForWebPointPointerType(WebPointerProperties::PointerT
ype type) |
| 15 { |
| 16 switch (type) { |
| 17 case WebPointerProperties::PointerType::Unknown: |
| 18 return ""; |
| 19 case WebPointerProperties::PointerType::Touch: |
| 20 return "touch"; |
| 21 case WebPointerProperties::PointerType::Pen: |
| 22 return "pen"; |
| 23 case WebPointerProperties::PointerType::Mouse: |
| 24 return "mouse"; |
| 25 } |
| 26 ASSERT_NOT_REACHED(); |
| 27 return ""; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 const PointerEventFactory::MappedId PointerEventFactory::s_invalidId = 0; |
| 33 |
| 34 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons. |
| 35 const PointerEventFactory::MappedId PointerEventFactory::s_mouseId = 1; |
| 36 |
| 37 |
| 38 |
| 39 void PointerEventFactory::setIdAndType(PointerEventInit &pointerEventInit, |
| 40 const WebPointerProperties &pointerProperties) |
| 41 { |
| 42 const WebPointerProperties::PointerType pointerType = pointerProperties.poin
terType; |
| 43 MappedId pointerId = add(PointerEventFactory::IncomingId(toInt(pointerType),
pointerProperties.id)); |
| 44 pointerEventInit.setPointerId(pointerId); |
| 45 pointerEventInit.setPointerType(pointerTypeNameForWebPointPointerType(pointe
rType)); |
| 46 pointerEventInit.setIsPrimary(isPrimary(pointerId)); |
| 47 } |
| 48 |
| 49 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr
ing& type, |
| 50 const PlatformMouseEvent& mouseEvent, |
| 51 PassRefPtrWillBeRawPtr<Node> relatedTarget, |
| 52 PassRefPtrWillBeRawPtr<AbstractView> view) |
| 53 { |
| 54 PointerEventInit pointerEventInit; |
| 55 |
| 56 setIdAndType(pointerEventInit, mouseEvent.pointerProperties()); |
| 57 |
| 58 pointerEventInit.setScreenX(mouseEvent.globalPosition().x()); |
| 59 pointerEventInit.setScreenY(mouseEvent.globalPosition().y()); |
| 60 pointerEventInit.setClientX(mouseEvent.position().x()); |
| 61 pointerEventInit.setClientY(mouseEvent.position().y()); |
| 62 |
| 63 pointerEventInit.setButton(mouseEvent.button()); |
| 64 pointerEventInit.setButtons(MouseEvent::platformModifiersToButtons(mouseEven
t.modifiers())); |
| 65 |
| 66 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, mouseEvent.m
odifiers()); |
| 67 |
| 68 pointerEventInit.setBubbles(type != EventTypeNames::pointerenter |
| 69 && type != EventTypeNames::pointerleave); |
| 70 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter |
| 71 && type != EventTypeNames::pointerleave && type != EventTypeNames::point
ercancel); |
| 72 |
| 73 pointerEventInit.setView(view); |
| 74 if (relatedTarget) |
| 75 pointerEventInit.setRelatedTarget(relatedTarget); |
| 76 |
| 77 return PointerEvent::create(type, pointerEventInit); |
| 78 } |
| 79 |
| 80 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr
ing& type, |
| 81 const PlatformTouchPoint& touchPoint, PlatformEvent::Modifiers modifiers, |
| 82 const double width, const double height, |
| 83 const double clientX, const double clientY) |
| 84 { |
| 85 const PlatformTouchPoint::State pointState = touchPoint.state(); |
| 86 |
| 87 bool pointerReleasedOrCancelled = pointState == PlatformTouchPoint::TouchRel
eased |
| 88 || pointState == PlatformTouchPoint::TouchCancelled; |
| 89 |
| 90 bool isEnterOrLeave = false; |
| 91 |
| 92 PointerEventInit pointerEventInit; |
| 93 |
| 94 setIdAndType(pointerEventInit, touchPoint.pointerProperties()); |
| 95 |
| 96 pointerEventInit.setWidth(width); |
| 97 pointerEventInit.setHeight(height); |
| 98 pointerEventInit.setPressure(touchPoint.force()); |
| 99 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX); |
| 100 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY); |
| 101 pointerEventInit.setScreenX(touchPoint.screenPos().x()); |
| 102 pointerEventInit.setScreenY(touchPoint.screenPos().y()); |
| 103 pointerEventInit.setClientX(clientX); |
| 104 pointerEventInit.setClientY(clientY); |
| 105 pointerEventInit.setButton(0); |
| 106 pointerEventInit.setButtons(pointerReleasedOrCancelled ? 0 : 1); |
| 107 |
| 108 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); |
| 109 |
| 110 pointerEventInit.setBubbles(!isEnterOrLeave); |
| 111 pointerEventInit.setCancelable(!isEnterOrLeave && pointState != PlatformTouc
hPoint::TouchCancelled); |
| 112 |
| 113 return PointerEvent::create(type, pointerEventInit); |
| 114 } |
| 115 |
| 116 |
| 117 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::createPointerCancel(co
nst PlatformTouchPoint& touchPoint) |
| 118 { |
| 119 PointerEventInit pointerEventInit; |
| 120 |
| 121 setIdAndType(pointerEventInit, touchPoint.pointerProperties()); |
| 122 |
| 123 pointerEventInit.setBubbles(true); |
| 124 pointerEventInit.setCancelable(false); |
| 125 |
| 126 return PointerEvent::create(EventTypeNames::pointercancel, pointerEventInit)
; |
| 127 } |
| 128 |
| 129 PointerEventFactory::PointerEventFactory() |
| 130 { |
| 131 clear(); |
| 132 } |
| 133 |
| 134 PointerEventFactory::~PointerEventFactory() |
| 135 { |
| 136 clear(); |
| 137 } |
| 138 |
| 139 void PointerEventFactory::clear() |
| 140 { |
| 141 for (int type = 0; type <= toInt(WebPointerProperties::PointerType::LastEntr
y); type++) { |
| 142 m_primaryId[type] = PointerEventFactory::s_invalidId; |
| 143 m_idCount[type] = 0; |
| 144 } |
| 145 m_idMapping.clear(); |
| 146 m_idReverseMapping.clear(); |
| 147 |
| 148 // Always add mouse pointer in initialization and never remove it. |
| 149 // No need to add it to m_idMapping as it is not going to be used with the e
xisting APIs |
| 150 m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = s_mouseId; |
| 151 m_idReverseMapping.add(s_mouseId, IncomingId(toInt(WebPointerProperties::Poi
nterType::Mouse), 0)); |
| 152 |
| 153 m_currentId = PointerEventFactory::s_mouseId+1; |
| 154 } |
| 155 |
| 156 PointerEventFactory::MappedId PointerEventFactory::add(const IncomingId p) |
| 157 { |
| 158 // Do not add extra mouse pointer as it was added in initialization |
| 159 if (p.first == toInt(WebPointerProperties::PointerType::Mouse)) |
| 160 return s_mouseId; |
| 161 |
| 162 int type = p.first; |
| 163 if (m_idMapping.contains(p)) |
| 164 return m_idMapping.get(p); |
| 165 // We do not handle the overflow of m_currentId as it should be very rare |
| 166 MappedId mappedId = m_currentId++; |
| 167 if (!m_idCount[type]) |
| 168 m_primaryId[type] = mappedId; |
| 169 m_idCount[type]++; |
| 170 m_idMapping.add(p, mappedId); |
| 171 m_idReverseMapping.add(mappedId, p); |
| 172 return static_cast<PointerEventFactory::MappedId>(mappedId); |
| 173 } |
| 174 |
| 175 void PointerEventFactory::remove(const PassRefPtrWillBeRawPtr<PointerEvent> poin
terEvent) |
| 176 { |
| 177 MappedId mappedId = pointerEvent->pointerId(); |
| 178 // Do not remove mouse pointer id as it should always be there |
| 179 if (mappedId == s_mouseId || !m_idReverseMapping.contains(mappedId)) |
| 180 return; |
| 181 |
| 182 IncomingId p = m_idReverseMapping.get(mappedId); |
| 183 int type = p.first; |
| 184 m_idReverseMapping.remove(mappedId); |
| 185 m_idMapping.remove(p); |
| 186 if (m_primaryId[type] == mappedId) |
| 187 m_primaryId[type] = PointerEventFactory::s_invalidId; |
| 188 m_idCount[type]--; |
| 189 } |
| 190 |
| 191 bool PointerEventFactory::isPrimary(PointerEventFactory::MappedId mappedId) cons
t |
| 192 { |
| 193 if (!m_idReverseMapping.contains(mappedId)) |
| 194 return false; |
| 195 |
| 196 IncomingId p = m_idReverseMapping.get(mappedId); |
| 197 int type = p.first; |
| 198 return m_primaryId[type] == mappedId; |
| 199 } |
| 200 |
| 201 |
| 202 } // namespace blink |
OLD | NEW |