Skip to content

Commit

Permalink
Allow out-of-tree platforms to extend Touch fields (#38681)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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: 1e40f1f633387d368c5f642290b192174d53551a
  • Loading branch information
rozele authored and facebook-github-bot committed Jul 31, 2023
1 parent e590464 commit d469a0f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 comments on commit d469a0f

Please sign in to comment.