Skip to content

Commit 5665a77

Browse files
vincentriemerfacebook-github-bot
authored andcommitted
Implement screenX/screenY properties on the PointerEvent interface
Summary: Changelog: [iOS][Internal] - Implement screenX/screenY properties on the PointerEvent interface This diff implements the screenX/screenY properties which report the position of a pointer in the device's physical screen coordinates. Reviewed By: lunaleaps Differential Revision: D37794415 fbshipit-source-id: 6c39c3651812f99e66b93647579a2935598ef6f2
1 parent ef2355f commit 5665a77

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

React/Fabric/RCTSurfaceTouchHandler.mm

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ static void UpdateActiveTouchWithUITouch(
156156
CGPoint rootViewOriginOffset)
157157
{
158158
CGPoint offsetPoint = [uiTouch locationInView:activeTouch.componentView];
159-
CGPoint screenPoint = [uiTouch locationInView:uiTouch.window];
160159
CGPoint pagePoint = [uiTouch locationInView:rootComponentView];
160+
CGPoint screenPoint = [rootComponentView convertPoint:pagePoint
161+
toCoordinateSpace:rootComponentView.window.screen.coordinateSpace];
162+
161163
pagePoint = CGPointMake(pagePoint.x + rootViewOriginOffset.x, pagePoint.y + rootViewOriginOffset.y);
162164

163165
activeTouch.touch.offsetPoint = RCTPointFromCGPoint(offsetPoint);
@@ -255,6 +257,7 @@ static PointerEvent CreatePointerEventFromActiveTouch(ActiveTouch activeTouch, R
255257
event.pressure = touch.force;
256258
event.pointerType = PointerTypeCStringFromUITouchType(activeTouch.touchType);
257259
event.clientPoint = touch.pagePoint;
260+
event.screenPoint = touch.screenPoint;
258261

259262
CGFloat pointerSize = activeTouch.majorRadius * 2.0;
260263
if (@available(iOS 13.4, *)) {
@@ -285,8 +288,11 @@ static PointerEvent CreatePointerEventFromActiveTouch(ActiveTouch activeTouch, R
285288
return event;
286289
}
287290

288-
static PointerEvent
289-
CreatePointerEventFromIncompleteHoverData(UIView *view, CGPoint clientLocation, NSTimeInterval timestamp)
291+
static PointerEvent CreatePointerEventFromIncompleteHoverData(
292+
UIView *view,
293+
CGPoint clientLocation,
294+
CGPoint screenLocation,
295+
NSTimeInterval timestamp)
290296
{
291297
PointerEvent event = {};
292298
// "touch" events produced from a mouse cursor on iOS always have the ID 0 so
@@ -296,6 +302,7 @@ static PointerEvent CreatePointerEventFromActiveTouch(ActiveTouch activeTouch, R
296302
event.pressure = 0.0;
297303
event.pointerType = "mouse";
298304
event.clientPoint = RCTPointFromCGPoint(clientLocation);
305+
event.screenPoint = RCTPointFromCGPoint(screenLocation);
299306
event.width = 1.0;
300307
event.height = 1.0;
301308
event.tiltX = 0;
@@ -676,6 +683,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
676683
{
677684
UIView *listenerView = recognizer.view;
678685
CGPoint clientLocation = [recognizer locationInView:listenerView];
686+
CGPoint screenLocation = [listenerView convertPoint:clientLocation
687+
toCoordinateSpace:listenerView.window.screen.coordinateSpace];
679688

680689
UIView *targetView = [listenerView hitTest:clientLocation withEvent:nil];
681690
targetView = FindClosestFabricManagedTouchableView(targetView);
@@ -692,7 +701,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
692701
BOOL shouldEmitOverEvent = IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerOver);
693702
SharedTouchEventEmitter eventEmitter = GetTouchEmitterFromView(targetView, [recognizer locationInView:targetView]);
694703
if (shouldEmitOverEvent && eventEmitter != nil) {
695-
PointerEvent event = CreatePointerEventFromIncompleteHoverData(targetView, clientLocation, timestamp);
704+
PointerEvent event =
705+
CreatePointerEventFromIncompleteHoverData(targetView, clientLocation, screenLocation, timestamp);
696706
eventEmitter->onPointerOver(event);
697707
}
698708
}
@@ -714,7 +724,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
714724
SharedTouchEventEmitter eventEmitter =
715725
GetTouchEmitterFromView(componentView, [recognizer locationInView:componentView]);
716726
if (eventEmitter != nil) {
717-
PointerEvent event = CreatePointerEventFromIncompleteHoverData(componentView, clientLocation, timestamp);
727+
PointerEvent event =
728+
CreatePointerEventFromIncompleteHoverData(componentView, clientLocation, screenLocation, timestamp);
718729
eventEmitter->onPointerEnter(event);
719730
}
720731
}
@@ -732,7 +743,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
732743
if (hasMoveListenerInEventPath) {
733744
SharedTouchEventEmitter eventEmitter = GetTouchEmitterFromView(targetView, [recognizer locationInView:targetView]);
734745
if (eventEmitter != nil) {
735-
PointerEvent event = CreatePointerEventFromIncompleteHoverData(targetView, clientLocation, timestamp);
746+
PointerEvent event =
747+
CreatePointerEventFromIncompleteHoverData(targetView, clientLocation, screenLocation, timestamp);
736748
eventEmitter->onPointerMove(event);
737749
}
738750
}
@@ -743,7 +755,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
743755
SharedTouchEventEmitter eventEmitter =
744756
GetTouchEmitterFromView(prevTargetView, [recognizer locationInView:prevTargetView]);
745757
if (shouldEmitOutEvent && eventEmitter != nil) {
746-
PointerEvent event = CreatePointerEventFromIncompleteHoverData(prevTargetView, clientLocation, timestamp);
758+
PointerEvent event =
759+
CreatePointerEventFromIncompleteHoverData(prevTargetView, clientLocation, screenLocation, timestamp);
747760
eventEmitter->onPointerOut(event);
748761
}
749762
}
@@ -774,7 +787,8 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
774787
SharedTouchEventEmitter eventEmitter =
775788
GetTouchEmitterFromView(componentView, [recognizer locationInView:componentView]);
776789
if (eventEmitter != nil) {
777-
PointerEvent event = CreatePointerEventFromIncompleteHoverData(componentView, clientLocation, timestamp);
790+
PointerEvent event =
791+
CreatePointerEventFromIncompleteHoverData(componentView, clientLocation, screenLocation, timestamp);
778792
eventEmitter->onPointerLeave(event);
779793
}
780794
}

ReactCommon/react/renderer/components/view/PointerEvent.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ std::vector<DebugStringConvertibleObject> getDebugProps(
2424
{"pressure", getDebugDescription(pointerEvent.pressure, options)},
2525
{"pointerType", getDebugDescription(pointerEvent.pointerType, options)},
2626
{"clientPoint", getDebugDescription(pointerEvent.clientPoint, options)},
27+
{"screenPoint", getDebugDescription(pointerEvent.screenPoint, options)},
2728
{"width", getDebugDescription(pointerEvent.width, options)},
2829
{"height", getDebugDescription(pointerEvent.height, options)},
2930
{"tiltX", getDebugDescription(pointerEvent.tiltX, options)},

ReactCommon/react/renderer/components/view/PointerEvent.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ struct PointerEvent {
3434
* opposed to the coordinate within the page).
3535
*/
3636
Point clientPoint;
37+
/*
38+
* The X/Y coordinate of the pointer in global (screen) coordinates.
39+
*/
40+
Point screenPoint;
3741
/*
3842
* The width (magnitude on the X axis), in CSS pixels, of the contact geometry
3943
* of the pointer

ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ static jsi::Value pointerEventPayload(
7575
// clientX/Y
7676
object.setProperty(runtime, "pageX", event.clientPoint.x);
7777
object.setProperty(runtime, "pageY", event.clientPoint.y);
78+
object.setProperty(runtime, "screenX", event.screenPoint.x);
79+
object.setProperty(runtime, "screenY", event.screenPoint.y);
7880
object.setProperty(runtime, "width", event.width);
7981
object.setProperty(runtime, "height", event.height);
8082
object.setProperty(runtime, "tiltX", event.tiltX);

0 commit comments

Comments
 (0)