Files
react-native/ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp
T
Samuel Susla 2016460528 Pass event priority to React
Summary:
Changelog: [internal]

This is a mechanism that will guess event's React priority based on other events ongoing on the platform.

If an event happens within span of ContinuousStart -> ContinuousEnd and its category is unspecified, we deduce it's React priority to be default. All other events are discrete.

Special case: `onScroll`, which is always treated as "Default".

Reviewed By: JoshuaGross

Differential Revision: D28485060

fbshipit-source-id: d2eae63dbcf03271dfed97128a1590dd165a3ce2
2021-05-25 01:16:04 -07:00

108 lines
3.1 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TouchEventEmitter.h"
namespace facebook {
namespace 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) {
auto array = jsi::Array(runtime, touches.size());
int i = 0;
for (auto const &touch : touches) {
auto object = jsi::Object(runtime);
setTouchPayloadOnObject(object, runtime, touch);
array.setValueAtIndex(runtime, i++, object);
}
return array;
}
static jsi::Value touchEventPayload(
jsi::Runtime &runtime,
TouchEvent const &event) {
auto object = jsi::Object(runtime);
object.setProperty(
runtime, "touches", touchesPayload(runtime, event.touches));
object.setProperty(
runtime, "changedTouches", touchesPayload(runtime, event.changedTouches));
object.setProperty(
runtime, "targetTouches", touchesPayload(runtime, event.targetTouches));
if (!event.changedTouches.empty()) {
auto const &firstChangedTouch = *event.changedTouches.begin();
setTouchPayloadOnObject(object, runtime, firstChangedTouch);
}
return object;
}
void TouchEventEmitter::dispatchTouchEvent(
std::string const &type,
TouchEvent const &event,
EventPriority priority,
RawEvent::Category category) const {
dispatchEvent(
type,
[event](jsi::Runtime &runtime) {
return touchEventPayload(runtime, event);
},
priority,
category);
}
void TouchEventEmitter::onTouchStart(TouchEvent const &event) const {
dispatchTouchEvent(
"touchStart",
event,
EventPriority::AsynchronousBatched,
RawEvent::Category::ContinuousStart);
}
void TouchEventEmitter::onTouchMove(TouchEvent const &event) const {
dispatchUniqueEvent("touchMove", [event](jsi::Runtime &runtime) {
return touchEventPayload(runtime, event);
});
}
void TouchEventEmitter::onTouchEnd(TouchEvent const &event) const {
dispatchTouchEvent(
"touchEnd",
event,
EventPriority::AsynchronousBatched,
RawEvent::Category::ContinuousEnd);
}
void TouchEventEmitter::onTouchCancel(TouchEvent const &event) const {
dispatchTouchEvent(
"touchCancel",
event,
EventPriority::AsynchronousBatched,
RawEvent::Category::ContinuousEnd);
}
} // namespace react
} // namespace facebook