Chromium Code Reviews| 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 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr ing& type, | |
| 38 const PlatformMouseEvent& mouseEvent, | |
| 39 PassRefPtrWillBeRawPtr<Node> relatedTarget, | |
| 40 PassRefPtrWillBeRawPtr<AbstractView> view) | |
| 41 { | |
| 42 PointerEventInit pointerEventInit; | |
| 43 | |
| 44 const WebPointerProperties::PointerType pointerType = mouseEvent.pointerProp erties().pointerType; | |
| 45 MappedId pointerId = add(PointerEventFactory::IncomingId(toInt(pointerType), mouseEvent.pointerProperties().id)); | |
| 46 | |
| 47 pointerEventInit.setPointerId(pointerId); | |
|
mustaq
2015/12/02 15:53:46
In all create*() methods, the 4 lines dealing with
Navid Zolghadr
2015/12/02 16:49:07
Done.
| |
| 48 pointerEventInit.setPointerType(pointerTypeNameForWebPointPointerType(pointe rType)); | |
| 49 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | |
| 50 | |
| 51 pointerEventInit.setScreenX(mouseEvent.globalPosition().x()); | |
| 52 pointerEventInit.setScreenY(mouseEvent.globalPosition().y()); | |
| 53 pointerEventInit.setClientX(mouseEvent.position().x()); | |
| 54 pointerEventInit.setClientY(mouseEvent.position().y()); | |
| 55 | |
| 56 pointerEventInit.setButton(mouseEvent.button()); | |
| 57 pointerEventInit.setButtons(MouseEvent::platformModifiersToButtons(mouseEven t.modifiers())); | |
| 58 | |
| 59 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, mouseEvent.m odifiers()); | |
| 60 | |
| 61 pointerEventInit.setBubbles(type != EventTypeNames::pointerenter | |
| 62 && type != EventTypeNames::pointerleave); | |
| 63 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter | |
| 64 && type != EventTypeNames::pointerleave && type != EventTypeNames::point ercancel); | |
| 65 | |
| 66 pointerEventInit.setView(view); | |
| 67 if (relatedTarget) | |
| 68 pointerEventInit.setRelatedTarget(relatedTarget); | |
| 69 | |
| 70 return PointerEvent::create(type, pointerEventInit); | |
| 71 } | |
| 72 | |
| 73 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr ing& type, | |
| 74 const PlatformTouchPoint& touchPoint, PlatformEvent::Modifiers modifiers, | |
| 75 const double width, const double height, | |
| 76 const double clientX, const double clientY) | |
| 77 { | |
| 78 const PlatformTouchPoint::State pointState = touchPoint.state(); | |
| 79 | |
| 80 bool pointerReleasedOrCancelled = pointState == PlatformTouchPoint::TouchRel eased | |
| 81 || pointState == PlatformTouchPoint::TouchCancelled; | |
| 82 const WebPointerProperties::PointerType pointerType = touchPoint.pointerProp erties().pointerType; | |
| 83 MappedId pointerId = add(PointerEventFactory::IncomingId(toInt(pointerType), touchPoint.id())); | |
| 84 | |
| 85 bool isEnterOrLeave = false; | |
| 86 | |
| 87 PointerEventInit pointerEventInit; | |
| 88 pointerEventInit.setPointerId(pointerId); | |
| 89 pointerEventInit.setWidth(width); | |
| 90 pointerEventInit.setHeight(height); | |
| 91 pointerEventInit.setPressure(touchPoint.force()); | |
| 92 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX); | |
| 93 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY); | |
| 94 pointerEventInit.setPointerType(pointerTypeNameForWebPointPointerType(pointe rType)); | |
| 95 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | |
| 96 pointerEventInit.setScreenX(touchPoint.screenPos().x()); | |
| 97 pointerEventInit.setScreenY(touchPoint.screenPos().y()); | |
| 98 pointerEventInit.setClientX(clientX); | |
| 99 pointerEventInit.setClientY(clientY); | |
| 100 pointerEventInit.setButton(0); | |
| 101 pointerEventInit.setButtons(pointerReleasedOrCancelled ? 0 : 1); | |
| 102 | |
| 103 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); | |
| 104 | |
| 105 pointerEventInit.setBubbles(!isEnterOrLeave); | |
| 106 pointerEventInit.setCancelable(!isEnterOrLeave && pointState != PlatformTouc hPoint::TouchCancelled); | |
| 107 | |
| 108 return PointerEvent::create(type, pointerEventInit); | |
| 109 } | |
| 110 | |
| 111 | |
| 112 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::createPointerCancel(co nst PlatformTouchPoint& touchPoint) | |
| 113 { | |
| 114 const WebPointerProperties::PointerType pointerType = touchPoint.pointerProp erties().pointerType; | |
| 115 const String& pointerTypeStr = pointerTypeNameForWebPointPointerType(pointer Type); | |
| 116 | |
| 117 MappedId pointerId = add(PointerEventFactory::IncomingId(toInt(pointerType), touchPoint.id())); | |
| 118 PointerEventInit pointerEventInit; | |
| 119 pointerEventInit.setPointerId(pointerId); | |
| 120 pointerEventInit.setPointerType(pointerTypeStr); | |
| 121 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | |
| 122 pointerEventInit.setBubbles(true); | |
| 123 pointerEventInit.setCancelable(false); | |
| 124 | |
| 125 return PointerEvent::create(EventTypeNames::pointercancel, pointerEventInit) ; | |
| 126 } | |
| 127 | |
| 128 PointerEventFactory::PointerEventFactory() | |
| 129 : m_currentId(PointerEventFactory::s_mouseId+1) | |
|
mustaq
2015/12/02 15:53:46
Perhaps we should set m_currentId in clear().
Navid Zolghadr
2015/12/02 16:49:08
Done.
| |
| 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 | |
| 154 PointerEventFactory::MappedId PointerEventFactory::add(const IncomingId p) | |
| 155 { | |
| 156 // Do not add extra mouse pointer as it was added in initialization | |
| 157 if (p.first == toInt(WebPointerProperties::PointerType::Mouse)) | |
| 158 return s_mouseId; | |
| 159 | |
| 160 int type = p.first; | |
| 161 if (m_idMapping.contains(p)) | |
| 162 return m_idMapping.get(p); | |
| 163 // We do not handle the overflow of m_currentId as it should be very rare | |
| 164 MappedId mappedId = m_currentId++; | |
| 165 if (!m_idCount[type]) | |
| 166 m_primaryId[type] = mappedId; | |
| 167 m_idCount[type]++; | |
| 168 m_idMapping.add(p, mappedId); | |
| 169 m_idReverseMapping.add(mappedId, p); | |
| 170 return static_cast<PointerEventFactory::MappedId>(mappedId); | |
| 171 } | |
| 172 | |
| 173 void PointerEventFactory::remove(const PassRefPtrWillBeRawPtr<PointerEvent> poin terEvent) | |
| 174 { | |
| 175 MappedId mappedId = pointerEvent->pointerId(); | |
| 176 // Do not remove mouse pointer id as it should always be there | |
| 177 if (mappedId == s_mouseId || !m_idReverseMapping.contains(mappedId)) | |
| 178 return; | |
| 179 | |
| 180 IncomingId p = m_idReverseMapping.get(mappedId); | |
| 181 int type = p.first; | |
| 182 m_idReverseMapping.remove(mappedId); | |
| 183 m_idMapping.remove(p); | |
| 184 if (m_primaryId[type] == mappedId) | |
| 185 m_primaryId[type] = PointerEventFactory::s_invalidId; | |
| 186 m_idCount[type]--; | |
| 187 } | |
| 188 | |
| 189 bool PointerEventFactory::isPrimary(PointerEventFactory::MappedId mappedId) cons t | |
| 190 { | |
| 191 if (!m_idReverseMapping.contains(mappedId)) | |
| 192 return false; | |
| 193 | |
| 194 IncomingId p = m_idReverseMapping.get(mappedId); | |
| 195 int type = p.first; | |
| 196 return m_primaryId[type] == mappedId; | |
| 197 } | |
| 198 | |
| 199 | |
| 200 } // namespace blink | |
| OLD | NEW |