Allow out-of-tree platforms to extend Touch fields (#38681)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38681

Desktop platforms send additional information with onTouch(Start|Move|End|Cancel) events, including modifier keys and mouse button information.

This information has not historically been available for mobile platforms, so rather than blindly adding the fields, this change adds a hook for out-of-tree platforms to extend the data in nativeEvent payload for Fabric Touch events.

## Changelog:
[General] [Added] - Support customization of underlying Touch event representation in out-of-tree platforms

Reviewed By: christophpurrer

Differential Revision: D47896016

fbshipit-source-id: 02e3fce854302412381b0bd9254474c6bb5c63ac
This commit is contained in:
Eric Rozell
2023-07-31 11:38:36 -07:00
committed by Facebook GitHub Bot
parent 2b688f6031
commit 4884322781
6 changed files with 139 additions and 92 deletions
@@ -9,14 +9,30 @@
namespace facebook::react {
void setTouchPayloadOnObject(
jsi::Object &object,
jsi::Runtime &runtime,
BaseTouch const &touch) {
object.setProperty(runtime, "locationX", touch.offsetPoint.x);
object.setProperty(runtime, "locationY", touch.offsetPoint.y);
object.setProperty(runtime, "pageX", touch.pagePoint.x);
object.setProperty(runtime, "pageY", touch.pagePoint.y);
object.setProperty(runtime, "screenX", touch.screenPoint.x);
object.setProperty(runtime, "screenY", touch.screenPoint.y);
object.setProperty(runtime, "identifier", touch.identifier);
object.setProperty(runtime, "target", touch.target);
object.setProperty(runtime, "timestamp", touch.timestamp * 1000);
object.setProperty(runtime, "force", touch.force);
}
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(Touch const & /*touch*/) {
std::string getDebugName(BaseTouch const & /*touch*/) {
return "Touch";
}
std::vector<DebugStringConvertibleObject> getDebugProps(
Touch const &touch,
BaseTouch const &touch,
DebugStringConvertibleOptions options) {
return {
{"pagePoint", getDebugDescription(touch.pagePoint, options)},
@@ -0,0 +1,91 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <jsi/jsi.h>
#include <react/renderer/core/ReactPrimitives.h>
#include <react/renderer/debug/DebugStringConvertible.h>
#include <react/renderer/graphics/Point.h>
namespace facebook::react {
/*
* Describes an individual touch point for a touch event.
* See https://www.w3.org/TR/touch-events/ for more details.
*/
struct BaseTouch {
/*
* The coordinate of point relative to the root component in points.
*/
Point pagePoint;
/*
* The coordinate of point relative to the target component in points.
*/
Point offsetPoint;
/*
* The coordinate of point relative to the screen component in points.
*/
Point screenPoint;
/*
* An identification number for each touch point.
*/
int identifier;
/*
* The tag of a component on which the touch point started when it was first
* placed on the surface, even if the touch point has since moved outside the
* interactive area of that element.
*/
Tag target;
/*
* The force of the touch.
*/
Float force;
/*
* The time in seconds when the touch occurred or when it was last mutated.
*/
Float timestamp;
/*
* The particular implementation of `Hasher` and (especially) `Comparator`
* make sense only when `Touch` object is used as a *key* in indexed
* collections. Because of that they are expressed as separate classes.
*/
struct Hasher {
size_t operator()(BaseTouch const &touch) const {
return std::hash<decltype(touch.identifier)>()(touch.identifier);
}
};
struct Comparator {
bool operator()(BaseTouch const &lhs, BaseTouch const &rhs) const {
return lhs.identifier == rhs.identifier;
}
};
};
void setTouchPayloadOnObject(
jsi::Object &object,
jsi::Runtime &runtime,
BaseTouch const &touch);
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(BaseTouch const &touch);
std::vector<DebugStringConvertibleObject> getDebugProps(
BaseTouch const &touch,
DebugStringConvertibleOptions options);
#endif
} // namespace facebook::react
@@ -7,81 +7,9 @@
#pragma once
#include <react/renderer/core/ReactPrimitives.h>
#include <react/renderer/debug/DebugStringConvertible.h>
#include <react/renderer/graphics/Point.h>
#include <react/renderer/components/view/HostPlatformTouch.h>
namespace facebook::react {
/*
* Describes an individual touch point for a touch event.
* See https://www.w3.org/TR/touch-events/ for more details.
*/
struct Touch {
/*
* The coordinate of point relative to the root component in points.
*/
Point pagePoint;
/*
* The coordinate of point relative to the target component in points.
*/
Point offsetPoint;
/*
* The coordinate of point relative to the screen component in points.
*/
Point screenPoint;
/*
* An identification number for each touch point.
*/
int identifier;
/*
* The tag of a component on which the touch point started when it was first
* placed on the surface, even if the touch point has since moved outside the
* interactive area of that element.
*/
Tag target;
/*
* The force of the touch.
*/
Float force;
/*
* The time in seconds when the touch occurred or when it was last mutated.
*/
Float timestamp;
/*
* The particular implementation of `Hasher` and (especially) `Comparator`
* make sense only when `Touch` object is used as a *key* in indexed
* collections. Because of that they are expressed as separate classes.
*/
struct Hasher {
size_t operator()(Touch const &touch) const {
return std::hash<decltype(touch.identifier)>()(touch.identifier);
}
};
struct Comparator {
bool operator()(Touch const &lhs, Touch const &rhs) const {
return lhs.identifier == rhs.identifier;
}
};
};
using Touch = HostPlatformTouch;
using Touches = std::unordered_set<Touch, Touch::Hasher, Touch::Comparator>;
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(Touch const &touch);
std::vector<DebugStringConvertibleObject> getDebugProps(
Touch const &touch,
DebugStringConvertibleOptions options);
#endif
} // namespace facebook::react
@@ -11,22 +11,6 @@ namespace facebook::react {
#pragma mark - Touches
static void setTouchPayloadOnObject(
jsi::Object &object,
jsi::Runtime &runtime,
Touch const &touch) {
object.setProperty(runtime, "locationX", touch.offsetPoint.x);
object.setProperty(runtime, "locationY", touch.offsetPoint.y);
object.setProperty(runtime, "pageX", touch.pagePoint.x);
object.setProperty(runtime, "pageY", touch.pagePoint.y);
object.setProperty(runtime, "screenX", touch.screenPoint.x);
object.setProperty(runtime, "screenY", touch.screenPoint.y);
object.setProperty(runtime, "identifier", touch.identifier);
object.setProperty(runtime, "target", touch.target);
object.setProperty(runtime, "timestamp", touch.timestamp * 1000);
object.setProperty(runtime, "force", touch.force);
}
static jsi::Value touchesPayload(
jsi::Runtime &runtime,
Touches const &touches) {
@@ -0,0 +1,14 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/renderer/components/view/BaseTouch.h>
namespace facebook::react {
using HostPlatformTouch = BaseTouch;
} // namespace facebook::react
@@ -0,0 +1,14 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/renderer/components/view/BaseTouch.h>
namespace facebook::react {
using HostPlatformTouch = BaseTouch;
} // namespace facebook::react