From 7c0b7077544de1d2b988d72e5ab653ed415ae9bc Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Mon, 10 Sep 2018 11:19:19 -0700 Subject: [PATCH] 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 --- .../components/view/TouchEventEmitter.cpp | 63 ++++++++++ .../components/view/TouchEventEmitter.h | 112 ++++++++++++++++++ .../components/view/ViewEventEmitter.cpp | 49 -------- .../fabric/components/view/ViewEventEmitter.h | 97 +-------------- 4 files changed, 178 insertions(+), 143 deletions(-) create mode 100644 ReactCommon/fabric/components/view/TouchEventEmitter.cpp create mode 100644 ReactCommon/fabric/components/view/TouchEventEmitter.h diff --git a/ReactCommon/fabric/components/view/TouchEventEmitter.cpp b/ReactCommon/fabric/components/view/TouchEventEmitter.cpp new file mode 100644 index 000000000..6791de76f --- /dev/null +++ b/ReactCommon/fabric/components/view/TouchEventEmitter.cpp @@ -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 diff --git a/ReactCommon/fabric/components/view/TouchEventEmitter.h b/ReactCommon/fabric/components/view/TouchEventEmitter.h new file mode 100644 index 000000000..ec474291f --- /dev/null +++ b/ReactCommon/fabric/components/view/TouchEventEmitter.h @@ -0,0 +1,112 @@ +/** + * 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. + */ +#pragma once + +#include +#include +#include + +namespace facebook { +namespace 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()(const Touch &touch) const { + return std::hash()(touch.identifier); + } + }; + + struct Comparator { + bool operator()(const Touch &lhs, const Touch &rhs) const { + return lhs.identifier == rhs.identifier; + } + }; +}; + +using Touches = std::unordered_set; + +/* + * Defines the `touchstart`, `touchend`, `touchmove`, and `touchcancel` event types. + */ +struct TouchEvent { + /* + * A list of Touches for every point of contact currently touching the surface. + */ + Touches touches; + + /* + * A list of Touches for every point of contact which contributed to the event. + */ + Touches changedTouches; + + /* + * A list of Touches for every point of contact that is touching the surface + * and started on the element that is the target of the current event. + */ + Touches targetTouches; +}; + +class TouchEventEmitter; +using SharedTouchEventEmitter = std::shared_ptr; + +class TouchEventEmitter: public EventEmitter { +public: + using EventEmitter::EventEmitter; + + void onTouchStart(const TouchEvent &event) const; + void onTouchMove(const TouchEvent &event) const; + void onTouchEnd(const TouchEvent &event) const; + void onTouchCancel(const TouchEvent &event) const; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/components/view/ViewEventEmitter.cpp b/ReactCommon/fabric/components/view/ViewEventEmitter.cpp index 2bc665412..6ac00bb7c 100644 --- a/ReactCommon/fabric/components/view/ViewEventEmitter.cpp +++ b/ReactCommon/fabric/components/view/ViewEventEmitter.cpp @@ -38,54 +38,5 @@ void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const { dispatchEvent("layout", payload); } -#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 ViewEventEmitter::onTouchStart(const TouchEvent &event) const { - dispatchEvent("touchStart", touchEventPayload(event), EventPriority::SynchronousUnbatched); -} - -void ViewEventEmitter::onTouchMove(const TouchEvent &event) const { - dispatchEvent("touchMove", touchEventPayload(event), EventPriority::SynchronousBatched); -} - -void ViewEventEmitter::onTouchEnd(const TouchEvent &event) const { - dispatchEvent("touchEnd", touchEventPayload(event), EventPriority::SynchronousBatched); -} - -void ViewEventEmitter::onTouchCancel(const TouchEvent &event) const { - dispatchEvent("touchCancel", touchEventPayload(event), EventPriority::SynchronousBatched); -} - } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/components/view/ViewEventEmitter.h b/ReactCommon/fabric/components/view/ViewEventEmitter.h index 977040143..9af783a5f 100644 --- a/ReactCommon/fabric/components/view/ViewEventEmitter.h +++ b/ReactCommon/fabric/components/view/ViewEventEmitter.h @@ -10,103 +10,19 @@ #include #include -#include +#include "TouchEventEmitter.h" namespace facebook { namespace 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()(const Touch &touch) const { - return std::hash()(touch.identifier); - } - }; - - struct Comparator { - bool operator()(const Touch &lhs, const Touch &rhs) const { - return lhs.identifier == rhs.identifier; - } - }; -}; - -using Touches = std::unordered_set; - -/* - * Defines the `touchstart`, `touchend`, `touchmove`, and `touchcancel` event types. - */ -struct TouchEvent { - /* - * A list of Touches for every point of contact currently touching the surface. - */ - Touches touches; - - /* - * A list of Touches for every point of contact which contributed to the event. - */ - Touches changedTouches; - - /* - * A list of Touches for every point of contact that is touching the surface - * and started on the element that is the target of the current event. - */ - Touches targetTouches; -}; - class ViewEventEmitter; using SharedViewEventEmitter = std::shared_ptr; class ViewEventEmitter: - public EventEmitter { - + public TouchEventEmitter { public: - - using EventEmitter::EventEmitter; + using TouchEventEmitter::TouchEventEmitter; #pragma mark - Accessibility @@ -117,13 +33,6 @@ public: #pragma mark - Layout void onLayout(const LayoutMetrics &layoutMetrics) const; - -#pragma mark - Touches - - void onTouchStart(const TouchEvent &event) const; - void onTouchMove(const TouchEvent &event) const; - void onTouchEnd(const TouchEvent &event) const; - void onTouchCancel(const TouchEvent &event) const; }; } // namespace react