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 // Mouse id is 1 to behave the same as MS Edge for compatibility reasons. | |
|
mustaq
2015/11/25 21:43:39
Please add an empty line above the comment to clar
Navid Zolghadr
2015/11/25 22:22:59
Done.
| |
| 34 const PointerEventFactory::MappedId PointerEventFactory::s_mouseId = 1; | |
| 35 | |
| 36 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr ing& type, | |
| 37 const PlatformMouseEvent& mouseEvent, | |
| 38 PassRefPtrWillBeRawPtr<Node> relatedTarget, | |
| 39 PassRefPtrWillBeRawPtr<AbstractView> view) | |
| 40 { | |
| 41 PointerEventInit pointerEventInit; | |
| 42 | |
| 43 pointerEventInit.setPointerId(s_mouseId); | |
| 44 pointerEventInit.setPointerType(pointerTypeNameForWebPointPointerType(WebPoi nterProperties::PointerType::Mouse)); | |
|
mustaq
2015/11/25 21:43:39
Please use mouseEvent.pointerProperties().pointerT
Navid Zolghadr
2015/11/25 22:22:59
I guess we need to further look into the stylus as
| |
| 45 pointerEventInit.setIsPrimary(true); | |
| 46 | |
| 47 pointerEventInit.setScreenX(mouseEvent.globalPosition().x()); | |
| 48 pointerEventInit.setScreenY(mouseEvent.globalPosition().y()); | |
| 49 pointerEventInit.setClientX(mouseEvent.position().x()); | |
| 50 pointerEventInit.setClientY(mouseEvent.position().y()); | |
| 51 | |
| 52 pointerEventInit.setButton(mouseEvent.button()); | |
| 53 pointerEventInit.setButtons(MouseEvent::platformModifiersToButtons(mouseEven t.modifiers())); | |
| 54 | |
| 55 | |
| 56 | |
|
mustaq
2015/11/25 21:43:39
Too many empty lines?
Navid Zolghadr
2015/11/25 22:22:59
Done.
| |
| 57 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, mouseEvent.m odifiers()); | |
| 58 | |
| 59 pointerEventInit.setBubbles(type != EventTypeNames::pointerenter | |
| 60 && type != EventTypeNames::pointerleave); | |
| 61 pointerEventInit.setCancelable(type != EventTypeNames::pointerenter | |
| 62 && type != EventTypeNames::pointerleave && type != EventTypeNames::point ercancel); | |
| 63 | |
| 64 pointerEventInit.setView(view); | |
| 65 if (relatedTarget) | |
| 66 pointerEventInit.setRelatedTarget(relatedTarget); | |
| 67 | |
| 68 return PointerEvent::create(type, pointerEventInit); | |
| 69 } | |
| 70 | |
| 71 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::create(const AtomicStr ing& type, | |
| 72 const PlatformTouchPoint& touchPoint, PlatformEvent::Modifiers modifiers, | |
| 73 const double width, const double height, | |
| 74 const double clientX, const double clientY) | |
| 75 { | |
| 76 const PlatformTouchPoint::State pointState = touchPoint.state(); | |
| 77 | |
| 78 bool pointerReleasedOrCancelled = pointState == PlatformTouchPoint::TouchRel eased | |
| 79 || pointState == PlatformTouchPoint::TouchCancelled; | |
| 80 const WebPointerProperties::PointerType pointerType = touchPoint.pointerProp erties().pointerType; | |
| 81 const String& pointerTypeStr = pointerTypeNameForWebPointPointerType(pointer Type); | |
| 82 | |
| 83 bool isEnterOrLeave = false; | |
| 84 | |
| 85 MappedId pointerId = add(PointerEventFactory::GeneratedPointer(pointerType, touchPoint.id())); | |
| 86 PointerEventInit pointerEventInit; | |
| 87 pointerEventInit.setPointerId(pointerId); | |
| 88 pointerEventInit.setWidth(width); | |
| 89 pointerEventInit.setHeight(height); | |
| 90 pointerEventInit.setPressure(touchPoint.force()); | |
| 91 pointerEventInit.setTiltX(touchPoint.pointerProperties().tiltX); | |
| 92 pointerEventInit.setTiltY(touchPoint.pointerProperties().tiltY); | |
| 93 pointerEventInit.setPointerType(pointerTypeStr); | |
| 94 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | |
| 95 pointerEventInit.setScreenX(touchPoint.screenPos().x()); | |
| 96 pointerEventInit.setScreenY(touchPoint.screenPos().y()); | |
| 97 pointerEventInit.setClientX(clientX); | |
| 98 pointerEventInit.setClientY(clientY); | |
| 99 pointerEventInit.setButton(0); | |
| 100 pointerEventInit.setButtons(pointerReleasedOrCancelled ? 0 : 1); | |
| 101 | |
| 102 UIEventWithKeyState::setFromPlatformModifiers(pointerEventInit, modifiers); | |
| 103 | |
| 104 pointerEventInit.setBubbles(!isEnterOrLeave); | |
| 105 pointerEventInit.setCancelable(!isEnterOrLeave && pointState != PlatformTouc hPoint::TouchCancelled); | |
| 106 | |
| 107 return PointerEvent::create(type, pointerEventInit); | |
| 108 } | |
| 109 | |
| 110 | |
| 111 PassRefPtrWillBeRawPtr<PointerEvent> PointerEventFactory::createPointerCancel(co nst PlatformTouchPoint& touchPoint) | |
| 112 { | |
| 113 const WebPointerProperties::PointerType pointerType = touchPoint.pointerProp erties().pointerType; | |
| 114 const String& pointerTypeStr = pointerTypeNameForWebPointPointerType(pointer Type); | |
| 115 | |
| 116 MappedId pointerId = add(PointerEventFactory::GeneratedPointer(pointerType, touchPoint.id())); | |
| 117 PointerEventInit pointerEventInit; | |
| 118 pointerEventInit.setPointerId(pointerId); | |
| 119 pointerEventInit.setPointerType(pointerTypeStr); | |
| 120 pointerEventInit.setIsPrimary(isPrimary(pointerId)); | |
| 121 pointerEventInit.setBubbles(true); | |
| 122 pointerEventInit.setCancelable(false); | |
| 123 | |
| 124 return PointerEvent::create(EventTypeNames::pointercancel, pointerEventInit) ; | |
| 125 } | |
| 126 | |
| 127 PointerEventFactory::PointerEventFactory() | |
| 128 : m_currentId(PointerEventFactory::s_mouseId+1) | |
| 129 { | |
| 130 clear(); | |
| 131 } | |
| 132 | |
| 133 PointerEventFactory::~PointerEventFactory() | |
| 134 { | |
| 135 clear(); | |
| 136 } | |
| 137 | |
| 138 void PointerEventFactory::clear() | |
| 139 { | |
| 140 for (int type = 0; type <= toInt(WebPointerProperties::PointerType::LastEntr y); type++) { | |
| 141 m_ids[type].clear(); | |
| 142 m_primaryId[type] = PointerEventFactory::s_invalidId; | |
| 143 } | |
| 144 m_reverseMapping.clear(); | |
| 145 | |
| 146 // Always add mouse pointer in initialization and never remove it. | |
| 147 // No need to add it to m_ids as it is not going to be used with the existin g APIs | |
| 148 m_primaryId[toInt(WebPointerProperties::PointerType::Mouse)] = s_mouseId; | |
| 149 m_reverseMapping.add(s_mouseId, GeneratedPointer(WebPointerProperties::Point erType::Mouse, 0)); | |
| 150 } | |
| 151 | |
| 152 PointerEventFactory::MappedId PointerEventFactory::add(const GeneratedPointer p) | |
| 153 { | |
| 154 // Do not add extra mouse pointer as it was added in initialization | |
| 155 if (p.first == WebPointerProperties::PointerType::Mouse) | |
| 156 return s_mouseId; | |
| 157 | |
| 158 int type = toInt(p.first); | |
| 159 unsigned id = p.second; | |
| 160 if (m_ids[type].contains(id)) | |
| 161 return m_ids[type].get(id); | |
| 162 // We do not handle the overflow of m_currentId as it should be very rare | |
| 163 MappedId mappedId = m_currentId++; | |
| 164 if (m_ids[type].isEmpty()) | |
| 165 m_primaryId[type] = mappedId; | |
| 166 m_ids[type].add(id, mappedId); | |
| 167 m_reverseMapping.add(mappedId, p); | |
| 168 return static_cast<PointerEventFactory::MappedId>(mappedId); | |
| 169 } | |
| 170 | |
| 171 void PointerEventFactory::remove(const PassRefPtrWillBeRawPtr<PointerEvent> poin terEvent) | |
| 172 { | |
| 173 MappedId mappedId = pointerEvent->pointerId(); | |
| 174 // Do not remove mouse pointer id as it should always be there | |
| 175 if (mappedId == s_mouseId || !m_reverseMapping.contains(mappedId)) | |
| 176 return; | |
| 177 | |
| 178 GeneratedPointer p = m_reverseMapping.get(mappedId); | |
| 179 int type = toInt(p.first); | |
| 180 unsigned id = p.second; | |
| 181 m_reverseMapping.remove(mappedId); | |
| 182 m_ids[type].remove(id); | |
| 183 if (m_primaryId[type] == mappedId) | |
| 184 m_primaryId[type] = PointerEventFactory::s_invalidId; | |
| 185 } | |
| 186 | |
| 187 bool PointerEventFactory::isPrimary(PointerEventFactory::MappedId mappedId) cons t | |
|
mustaq
2015/11/25 21:43:39
Let's nuke isPrimary(), getPrimaryId() and getType
Navid Zolghadr
2015/11/25 22:22:59
Reverse mapping will be use particurarly for the r
| |
| 188 { | |
| 189 if (!m_reverseMapping.contains(mappedId)) | |
| 190 return false; | |
| 191 | |
| 192 GeneratedPointer p = m_reverseMapping.get(mappedId); | |
| 193 int type = toInt(p.first); | |
| 194 return m_primaryId[type] == mappedId; | |
| 195 } | |
| 196 | |
| 197 PointerEventFactory::MappedId PointerEventFactory::getPrimaryId(const WebPointer Properties::PointerType type) const | |
| 198 { | |
| 199 return m_primaryId[toInt(type)]; | |
| 200 } | |
| 201 | |
| 202 WebPointerProperties::PointerType PointerEventFactory::getType(const PointerEven tFactory::MappedId mappedId) const | |
| 203 { | |
| 204 if (!m_reverseMapping.contains(mappedId)) | |
| 205 return WebPointerProperties::PointerType::Unknown; | |
| 206 | |
| 207 return m_reverseMapping.get(mappedId).first; | |
| 208 } | |
| 209 | |
| 210 | |
| 211 } // namespace blink | |
| OLD | NEW |