Extract TouchEventEmitter from ViewEventEmitter

Summary: In the future, we may want some components (like Virtual Text) to handle only touch events. Therefore, I've extracted `TouchEventEmitter` from `ViewEventEmitter`.

Reviewed By: shergin

Differential Revision: D9696903

fbshipit-source-id: f6acc90d8ff53b5e6badaa472a5e099fb7cf03ff
This commit is contained in:
Ramanpreet Nara
2018-09-10 11:19:19 -07:00
committed by Facebook Github Bot
parent 4e841f2114
commit 7c0b707754
4 changed files with 178 additions and 143 deletions

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* 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 folly::dynamic touchPayload(const Touch &touch) {
folly::dynamic object = folly::dynamic::object();
object["locationX"] = touch.offsetPoint.x;
object["locationY"] = touch.offsetPoint.x;
object["pageX"] = touch.pagePoint.x;
object["pageY"] = touch.pagePoint.x;
object["screenX"] = touch.screenPoint.x;
object["screenY"] = touch.screenPoint.x;
object["identifier"] = touch.identifier;
object["target"] = touch.target;
object["timestamp"] = touch.timestamp * 1000;
object["force"] = touch.force;
return object;
}
static folly::dynamic touchesPayload(const Touches &touches) {
folly::dynamic array = folly::dynamic::array();
for (const auto &touch : touches) {
array.push_back(touchPayload(touch));
}
return array;
}
static folly::dynamic touchEventPayload(const TouchEvent &event) {
folly::dynamic object = folly::dynamic::object();
object["touches"] = touchesPayload(event.touches);
object["changedTouches"] = touchesPayload(event.changedTouches);
object["targetTouches"] = touchesPayload(event.targetTouches);
return object;
}
void TouchEventEmitter::onTouchStart(const TouchEvent &event) const {
dispatchEvent("touchStart", touchEventPayload(event), EventPriority::SynchronousUnbatched);
}
void TouchEventEmitter::onTouchMove(const TouchEvent &event) const {
dispatchEvent("touchMove", touchEventPayload(event), EventPriority::SynchronousBatched);
}
void TouchEventEmitter::onTouchEnd(const TouchEvent &event) const {
dispatchEvent("touchEnd", touchEventPayload(event), EventPriority::SynchronousBatched);
}
void TouchEventEmitter::onTouchCancel(const TouchEvent &event) const {
dispatchEvent("touchCancel", touchEventPayload(event), EventPriority::SynchronousBatched);
}
} // namespace react
} // namespace facebook