Skip to content

Commit 4884322

Browse files
rozelefacebook-github-bot
authored andcommitted
Allow out-of-tree platforms to extend Touch fields (#38681)
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: 02e3fce854302412381b0bd9254474c6bb5c63ac
1 parent 2b688f6 commit 4884322

File tree

6 files changed

+139
-92
lines changed

6 files changed

+139
-92
lines changed

packages/react-native/ReactCommon/react/renderer/components/view/Touch.cpp renamed to packages/react-native/ReactCommon/react/renderer/components/view/BaseTouch.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@
99

1010
namespace facebook::react {
1111

12+
void setTouchPayloadOnObject(
13+
jsi::Object &object,
14+
jsi::Runtime &runtime,
15+
BaseTouch const &touch) {
16+
object.setProperty(runtime, "locationX", touch.offsetPoint.x);
17+
object.setProperty(runtime, "locationY", touch.offsetPoint.y);
18+
object.setProperty(runtime, "pageX", touch.pagePoint.x);
19+
object.setProperty(runtime, "pageY", touch.pagePoint.y);
20+
object.setProperty(runtime, "screenX", touch.screenPoint.x);
21+
object.setProperty(runtime, "screenY", touch.screenPoint.y);
22+
object.setProperty(runtime, "identifier", touch.identifier);
23+
object.setProperty(runtime, "target", touch.target);
24+
object.setProperty(runtime, "timestamp", touch.timestamp * 1000);
25+
object.setProperty(runtime, "force", touch.force);
26+
}
27+
1228
#if RN_DEBUG_STRING_CONVERTIBLE
1329

14-
std::string getDebugName(Touch const & /*touch*/) {
30+
std::string getDebugName(BaseTouch const & /*touch*/) {
1531
return "Touch";
1632
}
1733

1834
std::vector<DebugStringConvertibleObject> getDebugProps(
19-
Touch const &touch,
35+
BaseTouch const &touch,
2036
DebugStringConvertibleOptions options) {
2137
return {
2238
{"pagePoint", getDebugDescription(touch.pagePoint, options)},
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <jsi/jsi.h>
11+
#include <react/renderer/core/ReactPrimitives.h>
12+
#include <react/renderer/debug/DebugStringConvertible.h>
13+
#include <react/renderer/graphics/Point.h>
14+
15+
namespace facebook::react {
16+
17+
/*
18+
* Describes an individual touch point for a touch event.
19+
* See https://www.w3.org/TR/touch-events/ for more details.
20+
*/
21+
struct BaseTouch {
22+
/*
23+
* The coordinate of point relative to the root component in points.
24+
*/
25+
Point pagePoint;
26+
27+
/*
28+
* The coordinate of point relative to the target component in points.
29+
*/
30+
Point offsetPoint;
31+
32+
/*
33+
* The coordinate of point relative to the screen component in points.
34+
*/
35+
Point screenPoint;
36+
37+
/*
38+
* An identification number for each touch point.
39+
*/
40+
int identifier;
41+
42+
/*
43+
* The tag of a component on which the touch point started when it was first
44+
* placed on the surface, even if the touch point has since moved outside the
45+
* interactive area of that element.
46+
*/
47+
Tag target;
48+
49+
/*
50+
* The force of the touch.
51+
*/
52+
Float force;
53+
54+
/*
55+
* The time in seconds when the touch occurred or when it was last mutated.
56+
*/
57+
Float timestamp;
58+
59+
/*
60+
* The particular implementation of `Hasher` and (especially) `Comparator`
61+
* make sense only when `Touch` object is used as a *key* in indexed
62+
* collections. Because of that they are expressed as separate classes.
63+
*/
64+
struct Hasher {
65+
size_t operator()(BaseTouch const &touch) const {
66+
return std::hash<decltype(touch.identifier)>()(touch.identifier);
67+
}
68+
};
69+
70+
struct Comparator {
71+
bool operator()(BaseTouch const &lhs, BaseTouch const &rhs) const {
72+
return lhs.identifier == rhs.identifier;
73+
}
74+
};
75+
};
76+
77+
void setTouchPayloadOnObject(
78+
jsi::Object &object,
79+
jsi::Runtime &runtime,
80+
BaseTouch const &touch);
81+
82+
#if RN_DEBUG_STRING_CONVERTIBLE
83+
84+
std::string getDebugName(BaseTouch const &touch);
85+
std::vector<DebugStringConvertibleObject> getDebugProps(
86+
BaseTouch const &touch,
87+
DebugStringConvertibleOptions options);
88+
89+
#endif
90+
91+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/view/Touch.h

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,9 @@
77

88
#pragma once
99

10-
#include <react/renderer/core/ReactPrimitives.h>
11-
#include <react/renderer/debug/DebugStringConvertible.h>
12-
#include <react/renderer/graphics/Point.h>
10+
#include <react/renderer/components/view/HostPlatformTouch.h>
1311

1412
namespace facebook::react {
15-
16-
/*
17-
* Describes an individual touch point for a touch event.
18-
* See https://www.w3.org/TR/touch-events/ for more details.
19-
*/
20-
struct Touch {
21-
/*
22-
* The coordinate of point relative to the root component in points.
23-
*/
24-
Point pagePoint;
25-
26-
/*
27-
* The coordinate of point relative to the target component in points.
28-
*/
29-
Point offsetPoint;
30-
31-
/*
32-
* The coordinate of point relative to the screen component in points.
33-
*/
34-
Point screenPoint;
35-
36-
/*
37-
* An identification number for each touch point.
38-
*/
39-
int identifier;
40-
41-
/*
42-
* The tag of a component on which the touch point started when it was first
43-
* placed on the surface, even if the touch point has since moved outside the
44-
* interactive area of that element.
45-
*/
46-
Tag target;
47-
48-
/*
49-
* The force of the touch.
50-
*/
51-
Float force;
52-
53-
/*
54-
* The time in seconds when the touch occurred or when it was last mutated.
55-
*/
56-
Float timestamp;
57-
58-
/*
59-
* The particular implementation of `Hasher` and (especially) `Comparator`
60-
* make sense only when `Touch` object is used as a *key* in indexed
61-
* collections. Because of that they are expressed as separate classes.
62-
*/
63-
struct Hasher {
64-
size_t operator()(Touch const &touch) const {
65-
return std::hash<decltype(touch.identifier)>()(touch.identifier);
66-
}
67-
};
68-
69-
struct Comparator {
70-
bool operator()(Touch const &lhs, Touch const &rhs) const {
71-
return lhs.identifier == rhs.identifier;
72-
}
73-
};
74-
};
75-
13+
using Touch = HostPlatformTouch;
7614
using Touches = std::unordered_set<Touch, Touch::Hasher, Touch::Comparator>;
77-
78-
#if RN_DEBUG_STRING_CONVERTIBLE
79-
80-
std::string getDebugName(Touch const &touch);
81-
std::vector<DebugStringConvertibleObject> getDebugProps(
82-
Touch const &touch,
83-
DebugStringConvertibleOptions options);
84-
85-
#endif
86-
8715
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ namespace facebook::react {
1111

1212
#pragma mark - Touches
1313

14-
static void setTouchPayloadOnObject(
15-
jsi::Object &object,
16-
jsi::Runtime &runtime,
17-
Touch const &touch) {
18-
object.setProperty(runtime, "locationX", touch.offsetPoint.x);
19-
object.setProperty(runtime, "locationY", touch.offsetPoint.y);
20-
object.setProperty(runtime, "pageX", touch.pagePoint.x);
21-
object.setProperty(runtime, "pageY", touch.pagePoint.y);
22-
object.setProperty(runtime, "screenX", touch.screenPoint.x);
23-
object.setProperty(runtime, "screenY", touch.screenPoint.y);
24-
object.setProperty(runtime, "identifier", touch.identifier);
25-
object.setProperty(runtime, "target", touch.target);
26-
object.setProperty(runtime, "timestamp", touch.timestamp * 1000);
27-
object.setProperty(runtime, "force", touch.force);
28-
}
29-
3014
static jsi::Value touchesPayload(
3115
jsi::Runtime &runtime,
3216
Touches const &touches) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/components/view/BaseTouch.h>
11+
12+
namespace facebook::react {
13+
using HostPlatformTouch = BaseTouch;
14+
} // namespace facebook::react
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/components/view/BaseTouch.h>
11+
12+
namespace facebook::react {
13+
using HostPlatformTouch = BaseTouch;
14+
} // namespace facebook::react

0 commit comments

Comments
 (0)