2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2016-04-14 21:27:35 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-04-14 21:27:35 +00:00
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
* @providesModule Touchable
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-14 21:27:35 +00:00
|
|
|
const BoundingDimensions = require('BoundingDimensions');
|
2016-12-19 14:26:07 +00:00
|
|
|
const Platform = require('Platform');
|
2016-04-14 21:27:35 +00:00
|
|
|
const Position = require('Position');
|
2016-12-19 14:26:07 +00:00
|
|
|
const React = require('React');
|
2017-02-09 03:14:03 +00:00
|
|
|
const ReactNative = require('ReactNative');
|
2016-12-19 14:26:07 +00:00
|
|
|
const TVEventHandler = require('TVEventHandler');
|
2016-04-14 21:27:35 +00:00
|
|
|
const TouchEventUtils = require('fbjs/lib/TouchEventUtils');
|
2016-10-11 18:41:30 +00:00
|
|
|
const UIManager = require('UIManager');
|
2016-04-14 21:27:35 +00:00
|
|
|
const View = require('View');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-04-14 21:27:35 +00:00
|
|
|
const keyMirror = require('fbjs/lib/keyMirror');
|
|
|
|
const normalizeColor = require('normalizeColor');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `Touchable`: Taps done right.
|
|
|
|
*
|
|
|
|
* You hook your `ResponderEventPlugin` events into `Touchable`. `Touchable`
|
|
|
|
* will measure time/geometry and tells you when to give feedback to the user.
|
|
|
|
*
|
|
|
|
* ====================== Touchable Tutorial ===============================
|
|
|
|
* The `Touchable` mixin helps you handle the "press" interaction. It analyzes
|
|
|
|
* the geometry of elements, and observes when another responder (scroll view
|
|
|
|
* etc) has stolen the touch lock. It notifies your component when it should
|
|
|
|
* give feedback to the user. (bouncing/highlighting/unhighlighting).
|
|
|
|
*
|
|
|
|
* - When a touch was activated (typically you highlight)
|
|
|
|
* - When a touch was deactivated (typically you unhighlight)
|
|
|
|
* - When a touch was "pressed" - a touch ended while still within the geometry
|
|
|
|
* of the element, and no other element (like scroller) has "stolen" touch
|
|
|
|
* lock ("responder") (Typically you bounce the element).
|
|
|
|
*
|
|
|
|
* A good tap interaction isn't as simple as you might think. There should be a
|
|
|
|
* slight delay before showing a highlight when starting a touch. If a
|
2015-12-15 17:08:39 +00:00
|
|
|
* subsequent touch move exceeds the boundary of the element, it should
|
2015-02-20 04:10:52 +00:00
|
|
|
* unhighlight, but if that same touch is brought back within the boundary, it
|
|
|
|
* should rehighlight again. A touch can move in and out of that boundary
|
|
|
|
* several times, each time toggling highlighting, but a "press" is only
|
|
|
|
* triggered if that touch ends while within the element's boundary and no
|
|
|
|
* scroller (or anything else) has stolen the lock on touches.
|
|
|
|
*
|
|
|
|
* To create a new type of component that handles interaction using the
|
|
|
|
* `Touchable` mixin, do the following:
|
|
|
|
*
|
|
|
|
* - Initialize the `Touchable` state.
|
|
|
|
*
|
|
|
|
* getInitialState: function() {
|
|
|
|
* return merge(this.touchableGetInitialState(), yourComponentState);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* - Choose the rendered component who's touches should start the interactive
|
|
|
|
* sequence. On that rendered node, forward all `Touchable` responder
|
2016-01-27 17:04:14 +00:00
|
|
|
* handlers. You can choose any rendered node you like. Choose a node whose
|
2015-02-20 04:10:52 +00:00
|
|
|
* hit target you'd like to instigate the interaction sequence:
|
|
|
|
*
|
|
|
|
* // In render function:
|
|
|
|
* return (
|
2016-01-27 17:04:14 +00:00
|
|
|
* <View
|
2015-02-20 04:10:52 +00:00
|
|
|
* onStartShouldSetResponder={this.touchableHandleStartShouldSetResponder}
|
|
|
|
* onResponderTerminationRequest={this.touchableHandleResponderTerminationRequest}
|
|
|
|
* onResponderGrant={this.touchableHandleResponderGrant}
|
|
|
|
* onResponderMove={this.touchableHandleResponderMove}
|
|
|
|
* onResponderRelease={this.touchableHandleResponderRelease}
|
|
|
|
* onResponderTerminate={this.touchableHandleResponderTerminate}>
|
2016-01-27 17:04:14 +00:00
|
|
|
* <View>
|
2015-02-20 04:10:52 +00:00
|
|
|
* Even though the hit detection/interactions are triggered by the
|
|
|
|
* wrapping (typically larger) node, we usually end up implementing
|
|
|
|
* custom logic that highlights this inner one.
|
2016-01-27 17:04:14 +00:00
|
|
|
* </View>
|
|
|
|
* </View>
|
2015-02-20 04:10:52 +00:00
|
|
|
* );
|
|
|
|
*
|
|
|
|
* - You may set up your own handlers for each of these events, so long as you
|
|
|
|
* also invoke the `touchable*` handlers inside of your custom handler.
|
|
|
|
*
|
|
|
|
* - Implement the handlers on your component class in order to provide
|
|
|
|
* feedback to the user. See documentation for each of these class methods
|
|
|
|
* that you should implement.
|
|
|
|
*
|
|
|
|
* touchableHandlePress: function() {
|
|
|
|
* this.performBounceAnimation(); // or whatever you want to do.
|
|
|
|
* },
|
|
|
|
* touchableHandleActivePressIn: function() {
|
|
|
|
* this.beginHighlighting(...); // Whatever you like to convey activation
|
|
|
|
* },
|
|
|
|
* touchableHandleActivePressOut: function() {
|
|
|
|
* this.endHighlighting(...); // Whatever you like to convey deactivation
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* - There are more advanced methods you can implement (see documentation below):
|
|
|
|
* touchableGetHighlightDelayMS: function() {
|
|
|
|
* return 20;
|
|
|
|
* }
|
|
|
|
* // In practice, *always* use a predeclared constant (conserve memory).
|
|
|
|
* touchableGetPressRectOffset: function() {
|
|
|
|
* return {top: 20, left: 20, right: 20, bottom: 100};
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Touchable states.
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const States = keyMirror({
|
2015-02-20 04:10:52 +00:00
|
|
|
NOT_RESPONDER: null, // Not the responder
|
|
|
|
RESPONDER_INACTIVE_PRESS_IN: null, // Responder, inactive, in the `PressRect`
|
|
|
|
RESPONDER_INACTIVE_PRESS_OUT: null, // Responder, inactive, out of `PressRect`
|
|
|
|
RESPONDER_ACTIVE_PRESS_IN: null, // Responder, active, in the `PressRect`
|
|
|
|
RESPONDER_ACTIVE_PRESS_OUT: null, // Responder, active, out of `PressRect`
|
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_IN: null, // Responder, active, in the `PressRect`, after long press threshold
|
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_OUT: null, // Responder, active, out of `PressRect`, after long press threshold
|
|
|
|
ERROR: null
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quick lookup map for states that are considered to be "active"
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const IsActive = {
|
2015-02-20 04:10:52 +00:00
|
|
|
RESPONDER_ACTIVE_PRESS_OUT: true,
|
|
|
|
RESPONDER_ACTIVE_PRESS_IN: true
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quick lookup for states that are considered to be "pressing" and are
|
|
|
|
* therefore eligible to result in a "selection" if the press stops.
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const IsPressingIn = {
|
2015-02-20 04:10:52 +00:00
|
|
|
RESPONDER_INACTIVE_PRESS_IN: true,
|
|
|
|
RESPONDER_ACTIVE_PRESS_IN: true,
|
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_IN: true,
|
|
|
|
};
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const IsLongPressingIn = {
|
2015-02-20 04:10:52 +00:00
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_IN: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inputs to the state machine.
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const Signals = keyMirror({
|
2015-02-20 04:10:52 +00:00
|
|
|
DELAY: null,
|
|
|
|
RESPONDER_GRANT: null,
|
|
|
|
RESPONDER_RELEASE: null,
|
|
|
|
RESPONDER_TERMINATED: null,
|
|
|
|
ENTER_PRESS_RECT: null,
|
|
|
|
LEAVE_PRESS_RECT: null,
|
|
|
|
LONG_PRESS_DETECTED: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping from States x Signals => States
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const Transitions = {
|
2015-02-20 04:10:52 +00:00
|
|
|
NOT_RESPONDER: {
|
|
|
|
DELAY: States.ERROR,
|
|
|
|
RESPONDER_GRANT: States.RESPONDER_INACTIVE_PRESS_IN,
|
|
|
|
RESPONDER_RELEASE: States.ERROR,
|
|
|
|
RESPONDER_TERMINATED: States.ERROR,
|
|
|
|
ENTER_PRESS_RECT: States.ERROR,
|
|
|
|
LEAVE_PRESS_RECT: States.ERROR,
|
|
|
|
LONG_PRESS_DETECTED: States.ERROR,
|
|
|
|
},
|
|
|
|
RESPONDER_INACTIVE_PRESS_IN: {
|
|
|
|
DELAY: States.RESPONDER_ACTIVE_PRESS_IN,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_INACTIVE_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_INACTIVE_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.ERROR,
|
|
|
|
},
|
|
|
|
RESPONDER_INACTIVE_PRESS_OUT: {
|
|
|
|
DELAY: States.RESPONDER_ACTIVE_PRESS_OUT,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_INACTIVE_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_INACTIVE_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.ERROR,
|
|
|
|
},
|
|
|
|
RESPONDER_ACTIVE_PRESS_IN: {
|
|
|
|
DELAY: States.ERROR,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_ACTIVE_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_ACTIVE_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.RESPONDER_ACTIVE_LONG_PRESS_IN,
|
|
|
|
},
|
|
|
|
RESPONDER_ACTIVE_PRESS_OUT: {
|
|
|
|
DELAY: States.ERROR,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_ACTIVE_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_ACTIVE_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.ERROR,
|
|
|
|
},
|
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_IN: {
|
|
|
|
DELAY: States.ERROR,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_ACTIVE_LONG_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_ACTIVE_LONG_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.RESPONDER_ACTIVE_LONG_PRESS_IN,
|
|
|
|
},
|
|
|
|
RESPONDER_ACTIVE_LONG_PRESS_OUT: {
|
|
|
|
DELAY: States.ERROR,
|
|
|
|
RESPONDER_GRANT: States.ERROR,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.RESPONDER_ACTIVE_LONG_PRESS_IN,
|
|
|
|
LEAVE_PRESS_RECT: States.RESPONDER_ACTIVE_LONG_PRESS_OUT,
|
|
|
|
LONG_PRESS_DETECTED: States.ERROR,
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
DELAY: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_GRANT: States.RESPONDER_INACTIVE_PRESS_IN,
|
|
|
|
RESPONDER_RELEASE: States.NOT_RESPONDER,
|
|
|
|
RESPONDER_TERMINATED: States.NOT_RESPONDER,
|
|
|
|
ENTER_PRESS_RECT: States.NOT_RESPONDER,
|
|
|
|
LEAVE_PRESS_RECT: States.NOT_RESPONDER,
|
|
|
|
LONG_PRESS_DETECTED: States.NOT_RESPONDER,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== Typical Constants for integrating into UI components ====
|
|
|
|
// var HIT_EXPAND_PX = 20;
|
|
|
|
// var HIT_VERT_OFFSET_PX = 10;
|
2018-03-03 23:04:46 +00:00
|
|
|
const HIGHLIGHT_DELAY_MS = 130;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const PRESS_EXPAND_PX = 20;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const LONG_PRESS_THRESHOLD = 500;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const LONG_PRESS_DELAY_MS = LONG_PRESS_THRESHOLD - HIGHLIGHT_DELAY_MS;
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const LONG_PRESS_ALLOWED_MOVEMENT = 10;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
// Default amount "active" region protrudes beyond box
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By convention, methods prefixed with underscores are meant to be @private,
|
|
|
|
* and not @protected. Mixers shouldn't access them - not even to provide them
|
|
|
|
* as callback handlers.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ========== Geometry =========
|
|
|
|
* `Touchable` only assumes that there exists a `HitRect` node. The `PressRect`
|
|
|
|
* is an abstract box that is extended beyond the `HitRect`.
|
|
|
|
*
|
|
|
|
* +--------------------------+
|
|
|
|
* | | - "Start" events in `HitRect` cause `HitRect`
|
|
|
|
* | +--------------------+ | to become the responder.
|
|
|
|
* | | +--------------+ | | - `HitRect` is typically expanded around
|
|
|
|
* | | | | | | the `VisualRect`, but shifted downward.
|
|
|
|
* | | | VisualRect | | | - After pressing down, after some delay,
|
|
|
|
* | | | | | | and before letting up, the Visual React
|
|
|
|
* | | +--------------+ | | will become "active". This makes it eligible
|
|
|
|
* | | HitRect | | for being highlighted (so long as the
|
|
|
|
* | +--------------------+ | press remains in the `PressRect`).
|
|
|
|
* | PressRect o |
|
|
|
|
* +----------------------|---+
|
|
|
|
* Out Region |
|
|
|
|
* +-----+ This gap between the `HitRect` and
|
|
|
|
* `PressRect` allows a touch to move far away
|
|
|
|
* from the original hit rect, and remain
|
|
|
|
* highlighted, and eligible for a "Press".
|
|
|
|
* Customize this via
|
|
|
|
* `touchableGetPressRectOffset()`.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ======= State Machine =======
|
|
|
|
*
|
|
|
|
* +-------------+ <---+ RESPONDER_RELEASE
|
|
|
|
* |NOT_RESPONDER|
|
|
|
|
* +-------------+ <---+ RESPONDER_TERMINATED
|
|
|
|
* +
|
|
|
|
* | RESPONDER_GRANT (HitRect)
|
|
|
|
* v
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
* +---------------------------+ DELAY +-------------------------+ T + DELAY +------------------------------+
|
2015-02-20 04:10:52 +00:00
|
|
|
* |RESPONDER_INACTIVE_PRESS_IN|+-------->|RESPONDER_ACTIVE_PRESS_IN| +------------> |RESPONDER_ACTIVE_LONG_PRESS_IN|
|
|
|
|
* +---------------------------+ +-------------------------+ +------------------------------+
|
|
|
|
* + ^ + ^ + ^
|
|
|
|
* |LEAVE_ |ENTER_ |LEAVE_ |ENTER_ |LEAVE_ |ENTER_
|
|
|
|
* |PRESS_RECT |PRESS_RECT |PRESS_RECT |PRESS_RECT |PRESS_RECT |PRESS_RECT
|
|
|
|
* | | | | | |
|
|
|
|
* v + v + v +
|
|
|
|
* +----------------------------+ DELAY +--------------------------+ +-------------------------------+
|
|
|
|
* |RESPONDER_INACTIVE_PRESS_OUT|+------->|RESPONDER_ACTIVE_PRESS_OUT| |RESPONDER_ACTIVE_LONG_PRESS_OUT|
|
|
|
|
* +----------------------------+ +--------------------------+ +-------------------------------+
|
|
|
|
*
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
* T + DELAY => LONG_PRESS_DELAY_MS + DELAY
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* Not drawn are the side effects of each transition. The most important side
|
|
|
|
* effect is the `touchableHandlePress` abstract method invocation that occurs
|
|
|
|
* when a responder is released while in either of the "Press" states.
|
|
|
|
*
|
|
|
|
* The other important side effects are the highlight abstract method
|
|
|
|
* invocations (internal callbacks) to be implemented by the mixer.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @lends Touchable.prototype
|
|
|
|
*/
|
2018-03-03 23:04:46 +00:00
|
|
|
const TouchableMixin = {
|
2016-12-19 14:26:07 +00:00
|
|
|
componentDidMount: function() {
|
Add support for Android TV devices
Summary:
<!--
Thank you for sending the PR! We appreciate you spending the time to work on these changes.
Help us understand your motivation by explaining why you decided to make this change.
You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html
Happy contributing!
-->
* To be on par with Apple TV support, this makes it possible to run React Native apps on Android TV devices (See also: https://react-native.canny.io/feature-requests/p/android-tv-support)
* These changes also make it possible to navigate through the app using D-PAD buttons that are present on some mobile devices
* Since these changes affect, among others, `ReactRootView.java` and `Touchable.js` code and are closely related to Apple TV implementation, it makes sense for them to be included in the core
- React native apps can be launched on Android TV devices and properly render their content
- Navigation is possible using left, right, top, bottom arrows from the remote (or D-PAD)
- Touchable components can handle D-PAD center button press events and correctly fire their `onPress` handlers
- Touchable components will receive `onPressIn` and `onPressOut` events and can react to focus/blur changes appropriately (just like on Apple TV)
- `Platform` constants allow to check if the react-native app is running on TV (`Platform.isTV`)
- `ScrollView`s behave correctly (same as native implementation) when switching to view outside bounds – that is, the container would scroll such that the newly focused element is fully visible
- Native "clicking" sounds are played when moving between focusable elements
- Play/Pause click event is send to `TVEventHandler`
- Rewind and FastForward events are send to `TVEventHandler`
- Back button behaves as a normal Android back button
- Diagonal buttons work correctly on Android TV, e.g. if there is no button directly to the right from the focused one, but there is one to the right but a bit higher/lower it will grab focus
- Dev menu can be accessed by long pressing fast forward button
A demo showing RNTester app running on Android TV device (Amazon Fire TV Stick) can be found here:
[![RNAndroidTVDemo](http://img.youtube.com/vi/EzIQErHhY20/0.jpg)](http://www.youtube.com/watch?v=EzIQErHhY20)
- `TextInput` will not work on Android TV devices. There's an issue with native `ReactEditText` implementation that prevents it from receiving focus. This makes it impossible to navigate to `TextInput`.
This will be fixed next, but will be included in a separate Pull Request
- ~Overlay permissions cannot be granted on Android TV devices running Android version >= 6.0
This is because the overlay permission can only be granted by firing an Intent to open settings page (`ACTION_MANAGE_OVERLAY_PERMISSION`). Since this page does not exist on TV devices the permission cannot be requested. This will make the app crash when trying to open dev menu (⌘+M) or displaying a redbox error.
Note: This does not affect devices running Android version < 6.0 (for example Amazon Fire TV Stick)~
This is now fixed by: https://github.com/facebook/react-native/pull/16596
* Launch the RNTester app on Android TV device.
* Ensure it launches without a crash
* Ensure basic navigation is possible
* Ensure Touchable components can receive select events
* Ensure the changes do not break current Android and iOS mobile devices functionality.
* Ensure the changes do not break current Apple TV functionality.
[RNAndroidTVDemo video](http://img.youtube.com/vi/EzIQErHhY20/0.jpg)
* Added `ReactAndroidTVViewManager` that handles TV `KeyEvent`s and dispatches events to JS - This is the core that enables basic navigation functionality on Android TV devices
* Following the above change we copy `TVEventHandler.ios.js` into `TVEventHandler.android.js` to enable JS to pick up those native navigation events and dispatch them further to subscribed views. (Note: We do not have a native `TVNavigationEventEmitter` implementation on Android, thus this file is slightly modified, e.g. it does pass `null` to `NativeEventEmitter` constructor)
* Added `uiMode` to `AndroidInfoModule`. (**Note**: This required changing `extends BaseJavaModule` to `extends ReactContextBaseJavaModule` to be able to use `getSystemService` which requires `Context` instance!
* Added `isTV` constants to both `Platform.ios.js` (keeping the deprecated `isTVOS` as well) and `Platform.android.js`
* Changed condition check on `Touchable.js` to use the newly added `isTV` flag to properly handle TV navigation events on Android as well
* Added `LEANBACK_LAUNCHER` to `RNTester` `intent-filter` so that it is possible to launch it on Android TV devices.
* See also a PR to `react-native-website` repo with updated docs for Android TV: https://github.com/facebook/react-native-website/pull/59
- [ ] Fix `TextInput` components handling by allowing them to be focused and making a proper navigation between them (and/or other components) possible. One thing to note here that the default behavior to immediately open software keyboard when focused on `TextInput` field will need to be adjusted on Android TV as well)
- [x] Fix overlay permissions issue by changing the way redbox/dev menu are displayed (see: https://github.com/facebook/react-native/pull/16596)
- [ ] Adjust placement of TV-related files (e.g. the `TVEventHandler.js` file is placed inside `AppleTV` directory which is not accurate, since it does handle Android TV events as well)
Previous discussion: https://github.com/SoftwareMansion/react-native/pull/1
<!--
Help reviewers and the release process by writing your own release notes
**INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.**
CATEGORY
[----------] TYPE
[ CLI ] [-------------] LOCATION
[ DOCS ] [ BREAKING ] [-------------]
[ GENERAl ] [ BUGFIX ] [-{Component}-]
[ INTERNAL ] [ ENHANCEMENT ] [ {File} ]
[ IOS ] [ FEATURE ] [ {Directory} ] |-----------|
[ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} |
[----------] [-------------] [-------------] |-----------|
[CATEGORY] [TYPE] [LOCATION] - MESSAGE
EXAMPLES:
[IOS] [BREAKING] [FlatList] - Change a thing that breaks other things
[ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput
[CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with
[DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word
[GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position
[INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see
-->
[ANDROID] [FEATURE] [TV] - Added support for Android TV devices
Closes https://github.com/facebook/react-native/pull/16500
Differential Revision: D6536847
Pulled By: hramos
fbshipit-source-id: 17bbb11e8583b97f195ced5fd9762f8902fb8a3d
2018-03-06 18:41:27 +00:00
|
|
|
if (!Platform.isTV) {
|
2016-12-19 14:26:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tvEventHandler = new TVEventHandler();
|
|
|
|
this._tvEventHandler.enable(this, function(cmp, evt) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const myTag = ReactNative.findNodeHandle(cmp);
|
2016-12-19 14:26:07 +00:00
|
|
|
evt.dispatchConfig = {};
|
|
|
|
if (myTag === evt.tag) {
|
|
|
|
if (evt.eventType === 'focus') {
|
|
|
|
cmp.touchableHandleActivePressIn && cmp.touchableHandleActivePressIn(evt);
|
|
|
|
} else if (evt.eventType === 'blur') {
|
|
|
|
cmp.touchableHandleActivePressOut && cmp.touchableHandleActivePressOut(evt);
|
|
|
|
} else if (evt.eventType === 'select') {
|
Add support for Android TV devices
Summary:
<!--
Thank you for sending the PR! We appreciate you spending the time to work on these changes.
Help us understand your motivation by explaining why you decided to make this change.
You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html
Happy contributing!
-->
* To be on par with Apple TV support, this makes it possible to run React Native apps on Android TV devices (See also: https://react-native.canny.io/feature-requests/p/android-tv-support)
* These changes also make it possible to navigate through the app using D-PAD buttons that are present on some mobile devices
* Since these changes affect, among others, `ReactRootView.java` and `Touchable.js` code and are closely related to Apple TV implementation, it makes sense for them to be included in the core
- React native apps can be launched on Android TV devices and properly render their content
- Navigation is possible using left, right, top, bottom arrows from the remote (or D-PAD)
- Touchable components can handle D-PAD center button press events and correctly fire their `onPress` handlers
- Touchable components will receive `onPressIn` and `onPressOut` events and can react to focus/blur changes appropriately (just like on Apple TV)
- `Platform` constants allow to check if the react-native app is running on TV (`Platform.isTV`)
- `ScrollView`s behave correctly (same as native implementation) when switching to view outside bounds – that is, the container would scroll such that the newly focused element is fully visible
- Native "clicking" sounds are played when moving between focusable elements
- Play/Pause click event is send to `TVEventHandler`
- Rewind and FastForward events are send to `TVEventHandler`
- Back button behaves as a normal Android back button
- Diagonal buttons work correctly on Android TV, e.g. if there is no button directly to the right from the focused one, but there is one to the right but a bit higher/lower it will grab focus
- Dev menu can be accessed by long pressing fast forward button
A demo showing RNTester app running on Android TV device (Amazon Fire TV Stick) can be found here:
[![RNAndroidTVDemo](http://img.youtube.com/vi/EzIQErHhY20/0.jpg)](http://www.youtube.com/watch?v=EzIQErHhY20)
- `TextInput` will not work on Android TV devices. There's an issue with native `ReactEditText` implementation that prevents it from receiving focus. This makes it impossible to navigate to `TextInput`.
This will be fixed next, but will be included in a separate Pull Request
- ~Overlay permissions cannot be granted on Android TV devices running Android version >= 6.0
This is because the overlay permission can only be granted by firing an Intent to open settings page (`ACTION_MANAGE_OVERLAY_PERMISSION`). Since this page does not exist on TV devices the permission cannot be requested. This will make the app crash when trying to open dev menu (⌘+M) or displaying a redbox error.
Note: This does not affect devices running Android version < 6.0 (for example Amazon Fire TV Stick)~
This is now fixed by: https://github.com/facebook/react-native/pull/16596
* Launch the RNTester app on Android TV device.
* Ensure it launches without a crash
* Ensure basic navigation is possible
* Ensure Touchable components can receive select events
* Ensure the changes do not break current Android and iOS mobile devices functionality.
* Ensure the changes do not break current Apple TV functionality.
[RNAndroidTVDemo video](http://img.youtube.com/vi/EzIQErHhY20/0.jpg)
* Added `ReactAndroidTVViewManager` that handles TV `KeyEvent`s and dispatches events to JS - This is the core that enables basic navigation functionality on Android TV devices
* Following the above change we copy `TVEventHandler.ios.js` into `TVEventHandler.android.js` to enable JS to pick up those native navigation events and dispatch them further to subscribed views. (Note: We do not have a native `TVNavigationEventEmitter` implementation on Android, thus this file is slightly modified, e.g. it does pass `null` to `NativeEventEmitter` constructor)
* Added `uiMode` to `AndroidInfoModule`. (**Note**: This required changing `extends BaseJavaModule` to `extends ReactContextBaseJavaModule` to be able to use `getSystemService` which requires `Context` instance!
* Added `isTV` constants to both `Platform.ios.js` (keeping the deprecated `isTVOS` as well) and `Platform.android.js`
* Changed condition check on `Touchable.js` to use the newly added `isTV` flag to properly handle TV navigation events on Android as well
* Added `LEANBACK_LAUNCHER` to `RNTester` `intent-filter` so that it is possible to launch it on Android TV devices.
* See also a PR to `react-native-website` repo with updated docs for Android TV: https://github.com/facebook/react-native-website/pull/59
- [ ] Fix `TextInput` components handling by allowing them to be focused and making a proper navigation between them (and/or other components) possible. One thing to note here that the default behavior to immediately open software keyboard when focused on `TextInput` field will need to be adjusted on Android TV as well)
- [x] Fix overlay permissions issue by changing the way redbox/dev menu are displayed (see: https://github.com/facebook/react-native/pull/16596)
- [ ] Adjust placement of TV-related files (e.g. the `TVEventHandler.js` file is placed inside `AppleTV` directory which is not accurate, since it does handle Android TV events as well)
Previous discussion: https://github.com/SoftwareMansion/react-native/pull/1
<!--
Help reviewers and the release process by writing your own release notes
**INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.**
CATEGORY
[----------] TYPE
[ CLI ] [-------------] LOCATION
[ DOCS ] [ BREAKING ] [-------------]
[ GENERAl ] [ BUGFIX ] [-{Component}-]
[ INTERNAL ] [ ENHANCEMENT ] [ {File} ]
[ IOS ] [ FEATURE ] [ {Directory} ] |-----------|
[ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} |
[----------] [-------------] [-------------] |-----------|
[CATEGORY] [TYPE] [LOCATION] - MESSAGE
EXAMPLES:
[IOS] [BREAKING] [FlatList] - Change a thing that breaks other things
[ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput
[CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with
[DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word
[GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position
[INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see
-->
[ANDROID] [FEATURE] [TV] - Added support for Android TV devices
Closes https://github.com/facebook/react-native/pull/16500
Differential Revision: D6536847
Pulled By: hramos
fbshipit-source-id: 17bbb11e8583b97f195ced5fd9762f8902fb8a3d
2018-03-06 18:41:27 +00:00
|
|
|
cmp.touchableHandlePress && !cmp.props.disabled && cmp.touchableHandlePress(evt);
|
2016-12-19 14:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-03 18:45:28 +00:00
|
|
|
/**
|
|
|
|
* Clear all timeouts on unmount
|
|
|
|
*/
|
|
|
|
componentWillUnmount: function() {
|
2016-12-19 14:26:07 +00:00
|
|
|
if (this._tvEventHandler) {
|
|
|
|
this._tvEventHandler.disable();
|
|
|
|
delete this._tvEventHandler;
|
|
|
|
}
|
2015-10-03 18:45:28 +00:00
|
|
|
this.touchableDelayTimeout && clearTimeout(this.touchableDelayTimeout);
|
|
|
|
this.longPressDelayTimeout && clearTimeout(this.longPressDelayTimeout);
|
|
|
|
this.pressOutDelayTimeout && clearTimeout(this.pressOutDelayTimeout);
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* It's prefer that mixins determine state in this way, having the class
|
|
|
|
* explicitly mix the state in the one and only `getInitialState` method.
|
|
|
|
*
|
|
|
|
* @return {object} State object to be placed inside of
|
|
|
|
* `this.state.touchable`.
|
|
|
|
*/
|
|
|
|
touchableGetInitialState: function() {
|
|
|
|
return {
|
|
|
|
touchable: {touchState: undefined, responderID: null}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// ==== Hooks to Gesture Responder system ====
|
|
|
|
/**
|
|
|
|
* Must return true if embedded in a native platform scroll view.
|
|
|
|
*/
|
|
|
|
touchableHandleResponderTerminationRequest: function() {
|
|
|
|
return !this.props.rejectResponderTermination;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must return true to start the process of `Touchable`.
|
|
|
|
*/
|
|
|
|
touchableHandleStartShouldSetResponder: function() {
|
2016-02-24 03:38:51 +00:00
|
|
|
return !this.props.disabled;
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true to cancel press on long press.
|
|
|
|
*/
|
|
|
|
touchableLongPressCancelsPress: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place as callback for a DOM element's `onResponderGrant` event.
|
|
|
|
* @param {SyntheticEvent} e Synthetic event from event system.
|
|
|
|
*
|
|
|
|
*/
|
2016-04-21 16:27:50 +00:00
|
|
|
touchableHandleResponderGrant: function(e) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const dispatchID = e.currentTarget;
|
2015-02-20 04:10:52 +00:00
|
|
|
// Since e is used in a callback invoked on another event loop
|
|
|
|
// (as in setTimeout etc), we need to call e.persist() on the
|
|
|
|
// event to make sure it doesn't get reused in the event object pool.
|
|
|
|
e.persist();
|
|
|
|
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
this.pressOutDelayTimeout && clearTimeout(this.pressOutDelayTimeout);
|
|
|
|
this.pressOutDelayTimeout = null;
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.touchable.touchState = States.NOT_RESPONDER;
|
|
|
|
this.state.touchable.responderID = dispatchID;
|
|
|
|
this._receiveSignal(Signals.RESPONDER_GRANT, e);
|
2018-03-03 23:04:46 +00:00
|
|
|
let delayMS =
|
2015-02-20 04:10:52 +00:00
|
|
|
this.touchableGetHighlightDelayMS !== undefined ?
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
Math.max(this.touchableGetHighlightDelayMS(), 0) : HIGHLIGHT_DELAY_MS;
|
|
|
|
delayMS = isNaN(delayMS) ? HIGHLIGHT_DELAY_MS : delayMS;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (delayMS !== 0) {
|
|
|
|
this.touchableDelayTimeout = setTimeout(
|
|
|
|
this._handleDelay.bind(this, e),
|
|
|
|
delayMS
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._handleDelay(e);
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
let longDelayMS =
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
this.touchableGetLongPressDelayMS !== undefined ?
|
|
|
|
Math.max(this.touchableGetLongPressDelayMS(), 10) : LONG_PRESS_DELAY_MS;
|
|
|
|
longDelayMS = isNaN(longDelayMS) ? LONG_PRESS_DELAY_MS : longDelayMS;
|
2015-02-20 04:10:52 +00:00
|
|
|
this.longPressDelayTimeout = setTimeout(
|
|
|
|
this._handleLongDelay.bind(this, e),
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
longDelayMS + delayMS
|
2015-02-20 04:10:52 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place as callback for a DOM element's `onResponderRelease` event.
|
|
|
|
*/
|
|
|
|
touchableHandleResponderRelease: function(e) {
|
|
|
|
this._receiveSignal(Signals.RESPONDER_RELEASE, e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place as callback for a DOM element's `onResponderTerminate` event.
|
|
|
|
*/
|
|
|
|
touchableHandleResponderTerminate: function(e) {
|
|
|
|
this._receiveSignal(Signals.RESPONDER_TERMINATED, e);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place as callback for a DOM element's `onResponderMove` event.
|
|
|
|
*/
|
|
|
|
touchableHandleResponderMove: function(e) {
|
|
|
|
// Not enough time elapsed yet, wait for highlight -
|
|
|
|
// this is just a perf optimization.
|
|
|
|
if (this.state.touchable.touchState === States.RESPONDER_INACTIVE_PRESS_IN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measurement may not have returned yet.
|
|
|
|
if (!this.state.touchable.positionOnActivate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const positionOnActivate = this.state.touchable.positionOnActivate;
|
|
|
|
const dimensionsOnActivate = this.state.touchable.dimensionsOnActivate;
|
|
|
|
const pressRectOffset = this.touchableGetPressRectOffset ?
|
2015-10-15 13:35:52 +00:00
|
|
|
this.touchableGetPressRectOffset() : {
|
|
|
|
left: PRESS_EXPAND_PX,
|
|
|
|
right: PRESS_EXPAND_PX,
|
|
|
|
top: PRESS_EXPAND_PX,
|
|
|
|
bottom: PRESS_EXPAND_PX
|
|
|
|
};
|
2015-12-01 01:14:10 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
let pressExpandLeft = pressRectOffset.left;
|
|
|
|
let pressExpandTop = pressRectOffset.top;
|
|
|
|
let pressExpandRight = pressRectOffset.right;
|
|
|
|
let pressExpandBottom = pressRectOffset.bottom;
|
2015-12-01 01:14:10 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const hitSlop = this.touchableGetHitSlop ?
|
2016-02-17 00:50:35 +00:00
|
|
|
this.touchableGetHitSlop() : null;
|
|
|
|
|
|
|
|
if (hitSlop) {
|
|
|
|
pressExpandLeft += hitSlop.left;
|
|
|
|
pressExpandTop += hitSlop.top;
|
|
|
|
pressExpandRight += hitSlop.right;
|
|
|
|
pressExpandBottom += hitSlop.bottom;
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent);
|
|
|
|
const pageX = touch && touch.pageX;
|
|
|
|
const pageY = touch && touch.pageY;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (this.pressInLocation) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const movedDistance = this._getDistanceBetweenPoints(pageX, pageY, this.pressInLocation.pageX, this.pressInLocation.pageY);
|
2015-02-20 04:10:52 +00:00
|
|
|
if (movedDistance > LONG_PRESS_ALLOWED_MOVEMENT) {
|
|
|
|
this._cancelLongPressDelayTimeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const isTouchWithinActive =
|
2015-02-20 04:10:52 +00:00
|
|
|
pageX > positionOnActivate.left - pressExpandLeft &&
|
|
|
|
pageY > positionOnActivate.top - pressExpandTop &&
|
|
|
|
pageX <
|
|
|
|
positionOnActivate.left +
|
|
|
|
dimensionsOnActivate.width +
|
|
|
|
pressExpandRight &&
|
|
|
|
pageY <
|
|
|
|
positionOnActivate.top +
|
|
|
|
dimensionsOnActivate.height +
|
|
|
|
pressExpandBottom;
|
|
|
|
if (isTouchWithinActive) {
|
|
|
|
this._receiveSignal(Signals.ENTER_PRESS_RECT, e);
|
2018-03-03 23:04:46 +00:00
|
|
|
const curState = this.state.touchable.touchState;
|
2015-12-02 17:05:39 +00:00
|
|
|
if (curState === States.RESPONDER_INACTIVE_PRESS_IN) {
|
|
|
|
// fix for t7967420
|
|
|
|
this._cancelLongPressDelayTimeout();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
} else {
|
2015-04-28 03:27:59 +00:00
|
|
|
this._cancelLongPressDelayTimeout();
|
2015-02-20 04:10:52 +00:00
|
|
|
this._receiveSignal(Signals.LEAVE_PRESS_RECT, e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ==== Abstract Application Callbacks ====
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when the item should be highlighted. Mixers should implement this
|
|
|
|
* to visually distinguish the `VisualRect` so that the user knows that
|
|
|
|
* releasing a touch will result in a "selection" (analog to click).
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableHandleActivePressIn: function,
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when the item is "active" (in that it is still eligible to become
|
|
|
|
* a "select") but the touch has left the `PressRect`. Usually the mixer will
|
|
|
|
* want to unhighlight the `VisualRect`. If the user (while pressing) moves
|
|
|
|
* back into the `PressRect` `touchableHandleActivePressIn` will be invoked
|
|
|
|
* again and the mixer should probably highlight the `VisualRect` again. This
|
|
|
|
* event will not fire on an `touchEnd/mouseUp` event, only move events while
|
|
|
|
* the user is depressing the mouse/touch.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableHandleActivePressOut: function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when the item is "selected" - meaning the interaction ended by
|
|
|
|
* letting up while the item was either in the state
|
|
|
|
* `RESPONDER_ACTIVE_PRESS_IN` or `RESPONDER_INACTIVE_PRESS_IN`.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableHandlePress: function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when the item is long pressed - meaning the interaction ended by
|
|
|
|
* letting up while the item was in `RESPONDER_ACTIVE_LONG_PRESS_IN`. If
|
|
|
|
* `touchableHandleLongPress` is *not* provided, `touchableHandlePress` will
|
|
|
|
* be called as it normally is. If `touchableHandleLongPress` is provided, by
|
|
|
|
* default any `touchableHandlePress` callback will not be invoked. To
|
|
|
|
* override this default behavior, override `touchableLongPressCancelsPress`
|
|
|
|
* to return false. As a result, `touchableHandlePress` will be called when
|
|
|
|
* lifting up, even if `touchableHandleLongPress` has also been called.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableHandleLongPress: function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of millis to wait before triggering a highlight.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableGetHighlightDelayMS: function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the amount to extend the `HitRect` into the `PressRect`. Positive
|
|
|
|
* numbers mean the size expands outwards.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* touchableGetPressRectOffset: function
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==== Internal Logic ====
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Measures the `HitRect` node on activation. The Bounding rectangle is with
|
|
|
|
* respect to viewport - not page, so adding the `pageXOffset/pageYOffset`
|
|
|
|
* should result in points that are in the same coordinate system as an
|
|
|
|
* event's `globalX/globalY` data values.
|
|
|
|
*
|
|
|
|
* - Consider caching this for the lifetime of the component, or possibly
|
|
|
|
* being able to share this cache between any `ScrollMap` view.
|
|
|
|
*
|
|
|
|
* @sideeffects
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_remeasureMetricsOnActivation: function() {
|
2016-10-11 18:41:30 +00:00
|
|
|
const tag = this.state.touchable.responderID;
|
|
|
|
if (tag == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:16:27 +00:00
|
|
|
UIManager.measure(tag, this._handleQueryLayout);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_handleQueryLayout: function(l, t, w, h, globalX, globalY) {
|
2017-02-10 04:47:42 +00:00
|
|
|
//don't do anything UIManager failed to measure node
|
|
|
|
if (!l && !t && !w && !h && !globalX && !globalY) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
this.state.touchable.positionOnActivate &&
|
|
|
|
Position.release(this.state.touchable.positionOnActivate);
|
|
|
|
this.state.touchable.dimensionsOnActivate &&
|
|
|
|
BoundingDimensions.release(this.state.touchable.dimensionsOnActivate);
|
|
|
|
this.state.touchable.positionOnActivate = Position.getPooled(globalX, globalY);
|
|
|
|
this.state.touchable.dimensionsOnActivate = BoundingDimensions.getPooled(w, h);
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleDelay: function(e) {
|
|
|
|
this.touchableDelayTimeout = null;
|
|
|
|
this._receiveSignal(Signals.DELAY, e);
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleLongDelay: function(e) {
|
|
|
|
this.longPressDelayTimeout = null;
|
2018-03-03 23:04:46 +00:00
|
|
|
const curState = this.state.touchable.touchState;
|
2015-12-02 17:05:39 +00:00
|
|
|
if (curState !== States.RESPONDER_ACTIVE_PRESS_IN &&
|
|
|
|
curState !== States.RESPONDER_ACTIVE_LONG_PRESS_IN) {
|
2015-12-03 15:43:34 +00:00
|
|
|
console.error('Attempted to transition from state `' + curState + '` to `' +
|
|
|
|
States.RESPONDER_ACTIVE_LONG_PRESS_IN + '`, which is not supported. This is ' +
|
2015-12-15 17:08:39 +00:00
|
|
|
'most likely due to `Touchable.longPressDelayTimeout` not being cancelled.');
|
2015-12-02 17:05:39 +00:00
|
|
|
} else {
|
|
|
|
this._receiveSignal(Signals.LONG_PRESS_DETECTED, e);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a state machine signal, performs side effects of the transition
|
|
|
|
* and stores the new state. Validates the transition as well.
|
|
|
|
*
|
|
|
|
* @param {Signals} signal State machine signal.
|
|
|
|
* @throws Error if invalid state transition or unrecognized signal.
|
|
|
|
* @sideeffects
|
|
|
|
*/
|
|
|
|
_receiveSignal: function(signal, e) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const responderID = this.state.touchable.responderID;
|
|
|
|
const curState = this.state.touchable.touchState;
|
|
|
|
const nextState = Transitions[curState] && Transitions[curState][signal];
|
2016-03-15 19:16:52 +00:00
|
|
|
if (!responderID && signal === Signals.RESPONDER_RELEASE) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 17:05:39 +00:00
|
|
|
if (!nextState) {
|
2015-02-20 04:10:52 +00:00
|
|
|
throw new Error(
|
|
|
|
'Unrecognized signal `' + signal + '` or state `' + curState +
|
2016-03-15 19:16:52 +00:00
|
|
|
'` for Touchable responder `' + responderID + '`'
|
2015-02-20 04:10:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (nextState === States.ERROR) {
|
|
|
|
throw new Error(
|
|
|
|
'Touchable cannot transition from `' + curState + '` to `' + signal +
|
2016-03-15 19:16:52 +00:00
|
|
|
'` for responder `' + responderID + '`'
|
2015-02-20 04:10:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (curState !== nextState) {
|
|
|
|
this._performSideEffectsForTransition(curState, nextState, signal, e);
|
|
|
|
this.state.touchable.touchState = nextState;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_cancelLongPressDelayTimeout: function () {
|
|
|
|
this.longPressDelayTimeout && clearTimeout(this.longPressDelayTimeout);
|
|
|
|
this.longPressDelayTimeout = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_isHighlight: function (state) {
|
|
|
|
return state === States.RESPONDER_ACTIVE_PRESS_IN ||
|
|
|
|
state === States.RESPONDER_ACTIVE_LONG_PRESS_IN;
|
|
|
|
},
|
|
|
|
|
|
|
|
_savePressInLocation: function(e) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent);
|
|
|
|
const pageX = touch && touch.pageX;
|
|
|
|
const pageY = touch && touch.pageY;
|
|
|
|
const locationX = touch && touch.locationX;
|
|
|
|
const locationY = touch && touch.locationY;
|
2016-01-21 04:35:18 +00:00
|
|
|
this.pressInLocation = {pageX, pageY, locationX, locationY};
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_getDistanceBetweenPoints: function (aX, aY, bX, bY) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const deltaX = aX - bX;
|
|
|
|
const deltaY = aY - bY;
|
2015-02-20 04:10:52 +00:00
|
|
|
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will perform a transition between touchable states, and identify any
|
|
|
|
* highlighting or unhighlighting that must be performed for this particular
|
|
|
|
* transition.
|
|
|
|
*
|
|
|
|
* @param {States} curState Current Touchable state.
|
|
|
|
* @param {States} nextState Next Touchable state.
|
|
|
|
* @param {Signal} signal Signal that triggered the transition.
|
|
|
|
* @param {Event} e Native event.
|
|
|
|
* @sideeffects
|
|
|
|
*/
|
|
|
|
_performSideEffectsForTransition: function(curState, nextState, signal, e) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const curIsHighlight = this._isHighlight(curState);
|
|
|
|
const newIsHighlight = this._isHighlight(nextState);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const isFinalSignal =
|
2015-02-20 04:10:52 +00:00
|
|
|
signal === Signals.RESPONDER_TERMINATED ||
|
|
|
|
signal === Signals.RESPONDER_RELEASE;
|
|
|
|
|
|
|
|
if (isFinalSignal) {
|
|
|
|
this._cancelLongPressDelayTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsActive[curState] && IsActive[nextState]) {
|
|
|
|
this._remeasureMetricsOnActivation();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPressingIn[curState] && signal === Signals.LONG_PRESS_DETECTED) {
|
2015-08-21 08:52:13 +00:00
|
|
|
this.touchableHandleLongPress && this.touchableHandleLongPress(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newIsHighlight && !curIsHighlight) {
|
2016-10-03 11:27:15 +00:00
|
|
|
this._startHighlight(e);
|
|
|
|
} else if (!newIsHighlight && curIsHighlight) {
|
|
|
|
this._endHighlight(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPressingIn[curState] && signal === Signals.RESPONDER_RELEASE) {
|
2018-03-03 23:04:46 +00:00
|
|
|
const hasLongPressHandler = !!this.props.onLongPress;
|
|
|
|
const pressIsLongButStillCallOnPress =
|
2015-02-20 04:10:52 +00:00
|
|
|
IsLongPressingIn[curState] && ( // We *are* long pressing..
|
2018-03-03 23:04:46 +00:00
|
|
|
(// But either has no long handler
|
|
|
|
!hasLongPressHandler || !this.touchableLongPressCancelsPress()) // or we're told to ignore it.
|
2015-02-20 04:10:52 +00:00
|
|
|
);
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const shouldInvokePress = !IsLongPressingIn[curState] || pressIsLongButStillCallOnPress;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (shouldInvokePress && this.touchableHandlePress) {
|
2016-10-03 11:27:15 +00:00
|
|
|
if (!newIsHighlight && !curIsHighlight) {
|
|
|
|
// we never highlighted because of delay, but we should highlight now
|
|
|
|
this._startHighlight(e);
|
|
|
|
this._endHighlight(e);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
this.touchableHandlePress(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.touchableDelayTimeout && clearTimeout(this.touchableDelayTimeout);
|
|
|
|
this.touchableDelayTimeout = null;
|
2016-10-03 11:27:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_startHighlight: function(e) {
|
|
|
|
this._savePressInLocation(e);
|
|
|
|
this.touchableHandleActivePressIn && this.touchableHandleActivePressIn(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
_endHighlight: function(e) {
|
|
|
|
if (this.touchableHandleActivePressOut) {
|
|
|
|
if (this.touchableGetPressOutDelayMS && this.touchableGetPressOutDelayMS()) {
|
|
|
|
this.pressOutDelayTimeout = setTimeout(() => {
|
|
|
|
this.touchableHandleActivePressOut(e);
|
|
|
|
}, this.touchableGetPressOutDelayMS());
|
|
|
|
} else {
|
|
|
|
this.touchableHandleActivePressOut(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const Touchable = {
|
2016-04-14 21:27:35 +00:00
|
|
|
Mixin: TouchableMixin,
|
2016-04-16 18:56:07 +00:00
|
|
|
TOUCH_TARGET_DEBUG: false, // Highlights all touchable targets. Toggle with Inspector.
|
2016-04-14 21:27:35 +00:00
|
|
|
/**
|
|
|
|
* Renders a debugging overlay to visualize touch target with hitSlop (might not work on Android).
|
|
|
|
*/
|
|
|
|
renderDebugView: ({color, hitSlop}) => {
|
|
|
|
if (!Touchable.TOUCH_TARGET_DEBUG) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!__DEV__) {
|
|
|
|
throw Error('Touchable.TOUCH_TARGET_DEBUG should not be enabled in prod!');
|
|
|
|
}
|
|
|
|
const debugHitSlopStyle = {};
|
|
|
|
hitSlop = hitSlop || {top: 0, bottom: 0, left: 0, right: 0};
|
|
|
|
for (const key in hitSlop) {
|
|
|
|
debugHitSlopStyle[key] = -hitSlop[key];
|
|
|
|
}
|
|
|
|
const hexColor = '#' + ('00000000' + normalizeColor(color).toString(16)).substr(-8);
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
pointerEvents="none"
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
borderColor: hexColor.slice(0, -2) + '55', // More opaque
|
|
|
|
borderWidth: 1,
|
|
|
|
borderStyle: 'dashed',
|
|
|
|
backgroundColor: hexColor.slice(0, -2) + '0F', // Less opaque
|
|
|
|
...debugHitSlopStyle
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Touchable;
|