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