Skip to content

Commit 83f2398

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Touch and TouchEvent got own files and support for debug printing
Summary: That's essential for debugging touch events. Reviewed By: mdvacca Differential Revision: D15498192 fbshipit-source-id: 4a8e0a2b84a1935722518fdce03c10ba277f5702
1 parent 6dbd809 commit 83f2398

File tree

7 files changed

+234
-105
lines changed

7 files changed

+234
-105
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
#include "Touch.h"
9+
10+
namespace facebook {
11+
namespace react {
12+
13+
#if RN_DEBUG_STRING_CONVERTIBLE
14+
15+
std::string getDebugName(Touch const &touch) {
16+
return "Touch";
17+
}
18+
19+
std::vector<DebugStringConvertibleObject> getDebugProps(
20+
Touch const &touch,
21+
DebugStringConvertibleOptions options) {
22+
return {
23+
{"pagePoint", getDebugDescription(touch.pagePoint, options)},
24+
{"offsetPoint", getDebugDescription(touch.offsetPoint, options)},
25+
{"screenPoint", getDebugDescription(touch.screenPoint, options)},
26+
{"identifier", getDebugDescription(touch.identifier, options)},
27+
{"target", getDebugDescription(touch.target, options)},
28+
{"force", getDebugDescription(touch.force, options)},
29+
{"timestamp", getDebugDescription(touch.timestamp, options)},
30+
};
31+
}
32+
33+
#endif
34+
35+
} // namespace react
36+
} // namespace facebook
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
#pragma once
8+
9+
#include <react/graphics/Geometry.h>
10+
#include <react/core/ReactPrimitives.h>
11+
#include <react/debug/DebugStringConvertible.h>
12+
13+
namespace facebook {
14+
namespace 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+
76+
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 &object,
83+
DebugStringConvertibleOptions options = {});
84+
85+
#endif
86+
87+
} // namespace react
88+
} // namespace facebook
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
#include "TouchEvent.h"
9+
10+
namespace facebook {
11+
namespace react {
12+
13+
#if RN_DEBUG_STRING_CONVERTIBLE
14+
15+
std::string getDebugName(TouchEvent const &touchEvent) {
16+
return "TouchEvent";
17+
}
18+
19+
std::vector<DebugStringConvertibleObject> getDebugProps(
20+
TouchEvent const &touchEvent,
21+
DebugStringConvertibleOptions options) {
22+
23+
return {
24+
{"touches", getDebugDescription(touchEvent.touches, options)},
25+
{"changedTouches", getDebugDescription(touchEvent.changedTouches, options)},
26+
{"targetTouches", getDebugDescription(touchEvent.targetTouches, options)},
27+
};
28+
}
29+
30+
#endif
31+
32+
} // namespace react
33+
} // namespace facebook
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
#pragma once
8+
9+
#include <react/debug/DebugStringConvertible.h>
10+
11+
#include <unordered_set>
12+
13+
#include <react/components/view/Touch.h>
14+
15+
namespace facebook {
16+
namespace react {
17+
18+
using Touches = std::unordered_set<Touch, Touch::Hasher, Touch::Comparator>;
19+
20+
/*
21+
* Defines the `touchstart`, `touchend`, `touchmove`, and `touchcancel` event
22+
* types.
23+
*/
24+
struct TouchEvent {
25+
/*
26+
* A list of Touches for every point of contact currently touching the
27+
* surface.
28+
*/
29+
Touches touches;
30+
31+
/*
32+
* A list of Touches for every point of contact which contributed to the
33+
* event.
34+
*/
35+
Touches changedTouches;
36+
37+
/*
38+
* A list of Touches for every point of contact that is touching the surface
39+
* and started on the element that is the target of the current event.
40+
*/
41+
Touches targetTouches;
42+
};
43+
44+
#if RN_DEBUG_STRING_CONVERTIBLE
45+
46+
std::string getDebugName(TouchEvent const &touchEvent);
47+
std::vector<DebugStringConvertibleObject> getDebugProps(
48+
TouchEvent const &touchEvent,
49+
DebugStringConvertibleOptions options = {});
50+
51+
#endif
52+
53+
} // namespace react
54+
} // namespace facebook

ReactCommon/fabric/components/view/TouchEventEmitter.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace react {
1212

1313
#pragma mark - Touches
1414

15-
static jsi::Value touchPayload(jsi::Runtime &runtime, const Touch &touch) {
15+
static jsi::Value touchPayload(jsi::Runtime &runtime, Touch const &touch) {
1616
auto object = jsi::Object(runtime);
1717
object.setProperty(runtime, "locationX", touch.offsetPoint.x);
1818
object.setProperty(runtime, "locationY", touch.offsetPoint.y);
@@ -29,18 +29,18 @@ static jsi::Value touchPayload(jsi::Runtime &runtime, const Touch &touch) {
2929

3030
static jsi::Value touchesPayload(
3131
jsi::Runtime &runtime,
32-
const Touches &touches) {
32+
Touches const &touches) {
3333
auto array = jsi::Array(runtime, touches.size());
3434
int i = 0;
35-
for (const auto &touch : touches) {
35+
for (auto const &touch : touches) {
3636
array.setValueAtIndex(runtime, i++, touchPayload(runtime, touch));
3737
}
3838
return array;
3939
}
4040

4141
static jsi::Value touchEventPayload(
4242
jsi::Runtime &runtime,
43-
const TouchEvent &event) {
43+
TouchEvent const &event) {
4444
auto object = jsi::Object(runtime);
4545
object.setProperty(
4646
runtime, "touches", touchesPayload(runtime, event.touches));
@@ -52,9 +52,9 @@ static jsi::Value touchEventPayload(
5252
}
5353

5454
void TouchEventEmitter::dispatchTouchEvent(
55-
const std::string &type,
56-
const TouchEvent &event,
57-
const EventPriority &priority) const {
55+
std::string const &type,
56+
TouchEvent const &event,
57+
EventPriority const &priority) const {
5858
dispatchEvent(
5959
type,
6060
[event](jsi::Runtime &runtime) {
@@ -63,19 +63,19 @@ void TouchEventEmitter::dispatchTouchEvent(
6363
priority);
6464
}
6565

66-
void TouchEventEmitter::onTouchStart(const TouchEvent &event) const {
66+
void TouchEventEmitter::onTouchStart(TouchEvent const &event) const {
6767
dispatchTouchEvent("touchStart", event, EventPriority::SynchronousUnbatched);
6868
}
6969

70-
void TouchEventEmitter::onTouchMove(const TouchEvent &event) const {
70+
void TouchEventEmitter::onTouchMove(TouchEvent const &event) const {
7171
dispatchTouchEvent("touchMove", event, EventPriority::SynchronousBatched);
7272
}
7373

74-
void TouchEventEmitter::onTouchEnd(const TouchEvent &event) const {
74+
void TouchEventEmitter::onTouchEnd(TouchEvent const &event) const {
7575
dispatchTouchEvent("touchEnd", event, EventPriority::SynchronousBatched);
7676
}
7777

78-
void TouchEventEmitter::onTouchCancel(const TouchEvent &event) const {
78+
void TouchEventEmitter::onTouchCancel(TouchEvent const &event) const {
7979
dispatchTouchEvent("touchCancel", event, EventPriority::SynchronousBatched);
8080
}
8181

0 commit comments

Comments
 (0)