2018-04-07 07:52:38 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-04-24 13:48:18 +00:00
|
|
|
import { PanResponder, PixelRatio, View } from 'react-native';
|
2023-01-10 13:31:57 +00:00
|
|
|
import { SafeAreaView } from 'react-native-safe-area-context';
|
2018-05-22 14:23:03 +00:00
|
|
|
import { type Dispatch } from 'redux';
|
2018-04-07 07:52:38 +00:00
|
|
|
|
2023-01-10 13:31:57 +00:00
|
|
|
import { IReduxState } from '../../../../app/types';
|
|
|
|
import { connect } from '../../../redux/functions';
|
|
|
|
import { ASPECT_RATIO_WIDE } from '../../../responsive-ui/constants';
|
2018-04-07 07:52:38 +00:00
|
|
|
import { storeVideoTransform } from '../../actions';
|
2020-05-20 10:57:03 +00:00
|
|
|
|
2023-01-10 13:31:57 +00:00
|
|
|
// @ts-ignore
|
2018-04-07 07:52:38 +00:00
|
|
|
import styles from './styles';
|
|
|
|
|
2023-01-10 13:31:57 +00:00
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
/**
|
|
|
|
* The default/initial transform (= no transform).
|
|
|
|
*/
|
|
|
|
const DEFAULT_TRANSFORM = {
|
|
|
|
scale: 1,
|
|
|
|
translateX: 0,
|
|
|
|
translateY: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum scale (magnification) multiplier. 1 is equal to objectFit
|
|
|
|
* = 'contain'.
|
|
|
|
*/
|
|
|
|
const MIN_SCALE = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The max distance from the edge of the screen where we let the user move the
|
|
|
|
* view to. This is large enough now to let the user drag the view to a position
|
|
|
|
* where no other displayed components cover it (such as filmstrip). If a
|
|
|
|
* ViewPort (hint) support is added to the LargeVideo component then this
|
2021-03-16 15:59:33 +00:00
|
|
|
* constant will not be necessary anymore.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
|
|
|
const MAX_OFFSET = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The max allowed scale (magnification) multiplier.
|
|
|
|
*/
|
|
|
|
const MAX_SCALE = 5;
|
|
|
|
|
|
|
|
/**
|
2018-04-24 13:48:18 +00:00
|
|
|
* The threshold to allow the fingers move before we consider a gesture a
|
|
|
|
* move instead of a touch.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2018-04-24 13:48:18 +00:00
|
|
|
const MOVE_THRESHOLD_DISMISSES_TOUCH = 5;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A tap timeout after which we consider a gesture a long tap and will not
|
|
|
|
* trigger onPress (unless long tap gesture support is added in the future).
|
|
|
|
*/
|
|
|
|
const TAP_TIMEOUT_MS = 400;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type of a transform object this component is capable of handling.
|
|
|
|
*/
|
|
|
|
type Transform = {
|
2023-01-10 13:31:57 +00:00
|
|
|
scale: number;
|
|
|
|
translateX: number;
|
|
|
|
translateY: number;
|
2018-04-07 07:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
2022-08-01 14:35:14 +00:00
|
|
|
/**
|
|
|
|
* The current aspect ratio of the screen.
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_aspectRatio: Symbol;
|
2022-08-01 14:35:14 +00:00
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* Action to dispatch when the component is unmounted.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onUnmount: Function;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* The stored transforms retrieved from Redux to be initially applied
|
|
|
|
* to different streams.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_transforms: Object;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* The children components of this view.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
children: Object;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* Transformation is only enabled when this flag is true.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
enabled: boolean;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* Function to invoke when a press event is detected.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
onPress?: Function;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* The id of the current stream that is displayed.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
streamId: string;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-01-10 13:31:57 +00:00
|
|
|
* Style of the top level transformable view.
|
2018-04-07 07:52:38 +00:00
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
style: Object;
|
2018-04-07 07:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current (non-transformed) layout of the View.
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
layout: any;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current transform that is applied.
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
transform: Transform;
|
2018-04-07 07:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An container that captures gestures such as pinch&zoom, touch or move.
|
|
|
|
*/
|
|
|
|
class VideoTransform extends Component<Props, State> {
|
|
|
|
/**
|
|
|
|
* The gesture handler object.
|
|
|
|
*/
|
2022-01-07 10:54:42 +00:00
|
|
|
gestureHandlers: Object;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial distance of the fingers on pinch start.
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
initialDistance: number;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial position of the finger on touch start.
|
|
|
|
*/
|
|
|
|
initialPosition: {
|
2023-01-10 13:31:57 +00:00
|
|
|
x: number;
|
|
|
|
y: number;
|
2018-04-07 07:52:38 +00:00
|
|
|
};
|
|
|
|
|
2018-04-24 13:48:18 +00:00
|
|
|
/**
|
|
|
|
* The actual move threshold that is calculated for this device/screen.
|
|
|
|
*/
|
|
|
|
moveThreshold: number;
|
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
/**
|
|
|
|
* Time of the last tap.
|
|
|
|
*/
|
|
|
|
lastTap: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of the component.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
layout: null,
|
2018-11-30 03:11:34 +00:00
|
|
|
transform:
|
|
|
|
this._getSavedTransform(props.streamId) || DEFAULT_TRANSFORM
|
2018-04-07 07:52:38 +00:00
|
|
|
};
|
|
|
|
|
2018-04-24 13:48:18 +00:00
|
|
|
this._didMove = this._didMove.bind(this);
|
2018-04-07 07:52:38 +00:00
|
|
|
this._getTransformStyle = this._getTransformStyle.bind(this);
|
|
|
|
this._onGesture = this._onGesture.bind(this);
|
|
|
|
this._onLayout = this._onLayout.bind(this);
|
|
|
|
this._onMoveShouldSetPanResponder
|
|
|
|
= this._onMoveShouldSetPanResponder.bind(this);
|
|
|
|
this._onPanResponderGrant = this._onPanResponderGrant.bind(this);
|
|
|
|
this._onPanResponderMove = this._onPanResponderMove.bind(this);
|
|
|
|
this._onPanResponderRelease = this._onPanResponderRelease.bind(this);
|
|
|
|
this._onStartShouldSetPanResponder
|
|
|
|
= this._onStartShouldSetPanResponder.bind(this);
|
|
|
|
|
2018-04-24 13:48:18 +00:00
|
|
|
// The move threshold should be adaptive to the pixel ratio of the
|
|
|
|
// screen to avoid making it too sensitive or difficult to handle on
|
|
|
|
// different pixel ratio screens.
|
|
|
|
this.moveThreshold
|
|
|
|
= PixelRatio.get() * MOVE_THRESHOLD_DISMISSES_TOUCH;
|
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
this.gestureHandlers = PanResponder.create({
|
|
|
|
onPanResponderGrant: this._onPanResponderGrant,
|
|
|
|
onPanResponderMove: this._onPanResponderMove,
|
|
|
|
onPanResponderRelease: this._onPanResponderRelease,
|
|
|
|
onPanResponderTerminationRequest: () => true,
|
|
|
|
onMoveShouldSetPanResponder: this._onMoveShouldSetPanResponder,
|
|
|
|
onShouldBlockNativeResponder: () => false,
|
|
|
|
onStartShouldSetPanResponder: this._onStartShouldSetPanResponder
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-30 03:11:34 +00:00
|
|
|
* Implements React Component's componentDidUpdate.
|
2018-04-07 07:52:38 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2019-03-19 15:42:25 +00:00
|
|
|
componentDidUpdate(prevProps: Props, prevState: State) {
|
2018-11-30 03:11:34 +00:00
|
|
|
if (prevProps.streamId !== this.props.streamId) {
|
|
|
|
this._storeTransform(prevProps.streamId, prevState.transform);
|
|
|
|
this._restoreTransform(this.props.streamId);
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React Component's componentWillUnmount.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
2018-11-30 03:11:34 +00:00
|
|
|
this._storeTransform(this.props.streamId, this.state.transform);
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the empty component that captures the gestures.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
2022-08-01 14:35:14 +00:00
|
|
|
const { _aspectRatio, children, style } = this.props;
|
|
|
|
const isAspectRatioWide = _aspectRatio === ASPECT_RATIO_WIDE;
|
|
|
|
|
|
|
|
const videoTransformedViewContainerStyles
|
|
|
|
= isAspectRatioWide ? styles.videoTransformedViewContainerWide : styles.videoTransformedViewContainer;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
onLayout = { this._onLayout }
|
|
|
|
pointerEvents = 'box-only'
|
|
|
|
style = { [
|
2022-08-01 14:35:14 +00:00
|
|
|
videoTransformedViewContainerStyles,
|
2018-04-07 07:52:38 +00:00
|
|
|
style
|
|
|
|
] }
|
2023-01-10 13:31:57 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2018-04-07 07:52:38 +00:00
|
|
|
{ ...this.gestureHandlers.panHandlers }>
|
2023-01-10 13:31:57 +00:00
|
|
|
<SafeAreaView
|
|
|
|
edges = { [ 'bottom', 'left' ] }
|
2018-04-07 07:52:38 +00:00
|
|
|
style = { [
|
|
|
|
styles.videoTranformedView,
|
|
|
|
this._getTransformStyle()
|
|
|
|
] }>
|
|
|
|
{ children }
|
2023-01-10 13:31:57 +00:00
|
|
|
</SafeAreaView>
|
2018-04-07 07:52:38 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the new transformation to be applied by merging the current
|
|
|
|
* transform values with the newly received incremental values.
|
|
|
|
*
|
|
|
|
* @param {Transform} transform - The new transform object.
|
|
|
|
* @private
|
|
|
|
* @returns {Transform}
|
|
|
|
*/
|
|
|
|
_calculateTransformIncrement(transform: Transform) {
|
|
|
|
let {
|
|
|
|
scale,
|
|
|
|
translateX,
|
|
|
|
translateY
|
|
|
|
} = this.state.transform;
|
|
|
|
const {
|
|
|
|
scale: newScale,
|
|
|
|
translateX: newTranslateX,
|
|
|
|
translateY: newTranslateY
|
|
|
|
} = transform;
|
|
|
|
|
|
|
|
// Note: We don't limit MIN_SCALE here yet, as we need to detect a scale
|
|
|
|
// down gesture even if the scale is already at MIN_SCALE to let the
|
|
|
|
// user return the screen to center with that gesture. Scale is limited
|
|
|
|
// to MIN_SCALE right before it gets applied.
|
|
|
|
scale = Math.min(scale * (newScale || 1), MAX_SCALE);
|
|
|
|
|
|
|
|
translateX = translateX + ((newTranslateX || 0) / scale);
|
|
|
|
translateY = translateY + ((newTranslateY || 0) / scale);
|
|
|
|
|
|
|
|
return {
|
|
|
|
scale,
|
|
|
|
translateX,
|
|
|
|
translateY
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if there was large enough movement to be handled.
|
|
|
|
*
|
|
|
|
* @param {Object} gestureState - The gesture state.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_didMove({ dx, dy }: any) {
|
2018-04-24 13:48:18 +00:00
|
|
|
return Math.abs(dx) > this.moveThreshold
|
|
|
|
|| Math.abs(dy) > this.moveThreshold;
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:11:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the stored transform a stream should display with initially.
|
|
|
|
*
|
|
|
|
* @param {string} streamId - The id of the stream to match with a stored
|
|
|
|
* transform.
|
|
|
|
* @private
|
|
|
|
* @returns {Object | null}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_getSavedTransform(streamId: string) {
|
2018-11-30 03:11:34 +00:00
|
|
|
const { enabled, _transforms } = this.props;
|
|
|
|
|
2023-01-10 13:31:57 +00:00
|
|
|
// @ts-ignore
|
2018-11-30 03:11:34 +00:00
|
|
|
return (enabled && _transforms[streamId]) || null;
|
|
|
|
}
|
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
/**
|
|
|
|
* Calculates the touch distance on a pinch event.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - The touch event.
|
|
|
|
* @private
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_getTouchDistance({ nativeEvent: { touches } }: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
const dx = Math.abs(touches[0].pageX - touches[1].pageX);
|
|
|
|
const dy = Math.abs(touches[0].pageY - touches[1].pageY);
|
|
|
|
|
|
|
|
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the position of the touch event.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - The touch event.
|
|
|
|
* @private
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_getTouchPosition({ nativeEvent: { touches } }: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
return {
|
|
|
|
x: touches[0].pageX,
|
|
|
|
y: touches[0].pageY
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a transform style object to be used on the component.
|
|
|
|
*
|
|
|
|
* @returns {{string: Array<{string: number}>}}
|
|
|
|
*/
|
|
|
|
_getTransformStyle() {
|
|
|
|
const { enabled } = this.props;
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
scale,
|
|
|
|
translateX,
|
|
|
|
translateY
|
|
|
|
} = this.state.transform;
|
|
|
|
|
|
|
|
return {
|
|
|
|
transform: [
|
|
|
|
{ scale },
|
|
|
|
{ translateX },
|
|
|
|
{ translateY }
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limits the move matrix and then applies the transformation to the
|
|
|
|
* component (updates state).
|
|
|
|
*
|
|
|
|
* Note: Points A (top-left) and D (bottom-right) are opposite points of
|
|
|
|
* the View rectangle.
|
|
|
|
*
|
|
|
|
* @param {Transform} transform - The transformation object.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_limitAndApplyTransformation(transform: Transform) {
|
2022-08-01 14:35:14 +00:00
|
|
|
const { _aspectRatio } = this.props;
|
2018-04-07 07:52:38 +00:00
|
|
|
const { layout } = this.state;
|
|
|
|
|
2022-08-01 14:35:14 +00:00
|
|
|
const isAspectRatioWide = _aspectRatio === ASPECT_RATIO_WIDE;
|
|
|
|
|
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
if (layout) {
|
|
|
|
const { scale } = this.state.transform;
|
|
|
|
const { scale: newScaleUnlimited } = transform;
|
|
|
|
let {
|
|
|
|
translateX: newTranslateX,
|
|
|
|
translateY: newTranslateY
|
|
|
|
} = transform;
|
|
|
|
|
|
|
|
// Scale is only limited to MIN_SCALE here to detect downscale
|
|
|
|
// gesture later.
|
|
|
|
const newScale = Math.max(newScaleUnlimited, MIN_SCALE);
|
|
|
|
|
|
|
|
// The A and D points of the original View (before transform).
|
|
|
|
const originalLayout = {
|
|
|
|
a: {
|
|
|
|
x: layout.x,
|
|
|
|
y: layout.y
|
|
|
|
},
|
|
|
|
d: {
|
|
|
|
x: layout.x + layout.width,
|
|
|
|
y: layout.y + layout.height
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The center point (midpoint) of the transformed View.
|
|
|
|
const transformedCenterPoint = {
|
|
|
|
x: ((layout.x + layout.width) / 2) + (newTranslateX * newScale),
|
|
|
|
y: ((layout.y + layout.height) / 2) + (newTranslateY * newScale)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The size of the transformed View.
|
|
|
|
const transformedSize = {
|
|
|
|
height: layout.height * newScale,
|
|
|
|
width: layout.width * newScale
|
|
|
|
};
|
|
|
|
|
|
|
|
// The A and D points of the transformed View.
|
|
|
|
const transformedLayout = {
|
|
|
|
a: {
|
|
|
|
x: transformedCenterPoint.x - (transformedSize.width / 2),
|
|
|
|
y: transformedCenterPoint.y - (transformedSize.height / 2)
|
|
|
|
},
|
|
|
|
d: {
|
|
|
|
x: transformedCenterPoint.x + (transformedSize.width / 2),
|
|
|
|
y: transformedCenterPoint.y + (transformedSize.height / 2)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-01 14:35:14 +00:00
|
|
|
let _MAX_OFFSET = isAspectRatioWide ? 0 : MAX_OFFSET;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
if (newScaleUnlimited < scale) {
|
2022-08-01 14:35:14 +00:00
|
|
|
// This is a negative scale event so we dynamically reduce the
|
2018-04-07 07:52:38 +00:00
|
|
|
// MAX_OFFSET to get the screen back to the center on
|
|
|
|
// downscaling.
|
|
|
|
_MAX_OFFSET = Math.min(MAX_OFFSET, MAX_OFFSET * (newScale - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct move matrix if it goes out of the view
|
|
|
|
// too much (_MAX_OFFSET).
|
|
|
|
newTranslateX
|
|
|
|
-= Math.max(
|
|
|
|
transformedLayout.a.x - originalLayout.a.x - _MAX_OFFSET,
|
|
|
|
0);
|
|
|
|
newTranslateX
|
|
|
|
+= Math.max(
|
|
|
|
originalLayout.d.x - transformedLayout.d.x - _MAX_OFFSET,
|
|
|
|
0);
|
|
|
|
newTranslateY
|
|
|
|
-= Math.max(
|
|
|
|
transformedLayout.a.y - originalLayout.a.y - _MAX_OFFSET,
|
|
|
|
0);
|
|
|
|
newTranslateY
|
|
|
|
+= Math.max(
|
|
|
|
originalLayout.d.y - transformedLayout.d.y - _MAX_OFFSET,
|
|
|
|
0);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
transform: {
|
|
|
|
scale: newScale,
|
|
|
|
translateX: Math.round(newTranslateX),
|
|
|
|
translateY: Math.round(newTranslateY)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles gestures and converts them to transforms.
|
|
|
|
*
|
|
|
|
* Currently supported gestures:
|
|
|
|
* - scale (punch&zoom-type scale).
|
|
|
|
* - move
|
|
|
|
* - press.
|
|
|
|
*
|
|
|
|
* Note: This component supports onPress solely to overcome the problem of
|
|
|
|
* not being able to register gestures via the PanResponder due to the fact
|
|
|
|
* that the entire Conference component was a single touch responder
|
|
|
|
* component in the past (see base/react/.../Container with an onPress
|
|
|
|
* event) - and stock touch responder components seem to have exclusive
|
|
|
|
* priority in handling touches in React.
|
|
|
|
*
|
|
|
|
* @param {string} type - The type of the gesture.
|
|
|
|
* @param {?Object | number} value - The value of the gesture, if any.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onGesture(type: string, value: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
let transform;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'move':
|
|
|
|
transform = {
|
|
|
|
...DEFAULT_TRANSFORM,
|
|
|
|
translateX: value.x,
|
|
|
|
translateY: value.y
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'scale':
|
|
|
|
transform = {
|
|
|
|
...DEFAULT_TRANSFORM,
|
|
|
|
scale: value
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'press': {
|
|
|
|
const { onPress } = this.props;
|
|
|
|
|
|
|
|
typeof onPress === 'function' && onPress();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transform) {
|
|
|
|
this._limitAndApplyTransformation(
|
|
|
|
this._calculateTransformIncrement(transform));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastTap = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the onLayout of the component.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The native props of the onLayout event.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onLayout({ nativeEvent: { layout: { x, y, width, height } } }: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
this.setState({
|
|
|
|
layout: {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to decide whether the responder should respond to a move event.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - The event.
|
|
|
|
* @param {Object} gestureState - Gesture state.
|
|
|
|
* @private
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onMoveShouldSetPanResponder(evt: Object, gestureState: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
return this.props.enabled
|
|
|
|
&& (this._didMove(gestureState)
|
|
|
|
|| gestureState.numberActiveTouches === 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the initial touch distance.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - Touch event.
|
|
|
|
* @param {Object} gestureState - Gesture state.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onPanResponderGrant(evt: Object, { numberActiveTouches }: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
if (numberActiveTouches === 1) {
|
|
|
|
this.initialPosition = this._getTouchPosition(evt);
|
|
|
|
this.lastTap = Date.now();
|
|
|
|
|
|
|
|
} else if (numberActiveTouches === 2) {
|
|
|
|
this.initialDistance = this._getTouchDistance(evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the PanResponder move (touch move) event.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - Touch event.
|
|
|
|
* @param {Object} gestureState - Gesture state.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onPanResponderMove(evt: Object, gestureState: any) {
|
2018-04-07 07:52:38 +00:00
|
|
|
if (gestureState.numberActiveTouches === 2) {
|
|
|
|
// this is a zoom event
|
|
|
|
if (
|
|
|
|
this.initialDistance === undefined
|
|
|
|
|| isNaN(this.initialDistance)
|
|
|
|
) {
|
|
|
|
// there is no initial distance because the user started
|
|
|
|
// with only one finger. We calculate it now.
|
|
|
|
this.initialDistance = this._getTouchDistance(evt);
|
|
|
|
} else {
|
|
|
|
const distance = this._getTouchDistance(evt);
|
|
|
|
const scale = distance / (this.initialDistance || 1);
|
|
|
|
|
|
|
|
this.initialDistance = distance;
|
|
|
|
|
|
|
|
this._onGesture('scale', scale);
|
|
|
|
}
|
|
|
|
} else if (gestureState.numberActiveTouches === 1
|
|
|
|
&& isNaN(this.initialDistance)
|
|
|
|
&& this._didMove(gestureState)) {
|
|
|
|
// this is a move event
|
|
|
|
const position = this._getTouchPosition(evt);
|
|
|
|
const move = {
|
|
|
|
x: position.x - this.initialPosition.x,
|
|
|
|
y: position.y - this.initialPosition.y
|
|
|
|
};
|
|
|
|
|
|
|
|
this.initialPosition = position;
|
|
|
|
|
|
|
|
this._onGesture('move', move);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the PanResponder gesture end event.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onPanResponderRelease() {
|
|
|
|
if (this.lastTap && Date.now() - this.lastTap < TAP_TIMEOUT_MS) {
|
2023-01-10 13:31:57 +00:00
|
|
|
// @ts-ignore
|
2018-04-07 07:52:38 +00:00
|
|
|
this._onGesture('press');
|
|
|
|
}
|
2023-01-10 13:31:57 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2018-04-07 07:52:38 +00:00
|
|
|
delete this.initialDistance;
|
2022-01-07 10:54:42 +00:00
|
|
|
this.initialPosition = {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to decide whether the responder should respond to a start
|
2022-07-14 07:10:08 +00:00
|
|
|
* (touch) event.
|
2018-04-07 07:52:38 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_onStartShouldSetPanResponder() {
|
|
|
|
return typeof this.props.onPress === 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores the last applied transform when the component is mounted, or
|
|
|
|
* a new stream is about to be rendered.
|
|
|
|
*
|
|
|
|
* @param {string} streamId - The stream id to restore transform for.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_restoreTransform(streamId: string) {
|
2018-11-30 03:11:34 +00:00
|
|
|
const savedTransform = this._getSavedTransform(streamId);
|
2018-04-07 07:52:38 +00:00
|
|
|
|
2018-11-30 03:11:34 +00:00
|
|
|
if (savedTransform) {
|
|
|
|
this.setState({
|
|
|
|
transform: savedTransform
|
|
|
|
});
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-30 03:11:34 +00:00
|
|
|
* Stores/saves the a transform when the component is destroyed, or a
|
2018-04-07 07:52:38 +00:00
|
|
|
* new stream is about to be rendered.
|
|
|
|
*
|
2018-11-30 03:11:34 +00:00
|
|
|
* @param {string} streamId - The stream id associated with the transform.
|
|
|
|
* @param {Object} transform - The {@Transform} to save.
|
2018-04-07 07:52:38 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_storeTransform(streamId: string, transform: Transform) {
|
2018-11-30 03:11:34 +00:00
|
|
|
const { _onUnmount, enabled } = this.props;
|
2018-04-07 07:52:38 +00:00
|
|
|
|
|
|
|
if (enabled) {
|
2018-11-30 03:11:34 +00:00
|
|
|
_onUnmount(streamId, transform);
|
2018-04-07 07:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps dispatching of some action to React component props.
|
|
|
|
*
|
|
|
|
* @param {Function} dispatch - Redux action dispatcher.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* _onUnmount: Function
|
|
|
|
* }}
|
|
|
|
*/
|
2019-03-19 15:42:25 +00:00
|
|
|
function _mapDispatchToProps(dispatch: Dispatch<any>) {
|
2018-04-07 07:52:38 +00:00
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Dispatches actions to store the last applied transform to a video.
|
|
|
|
*
|
|
|
|
* @param {string} streamId - The ID of the stream.
|
|
|
|
* @param {Transform} transform - The last applied transform.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
_onUnmount(streamId: string, transform: Transform) {
|
2018-04-07 07:52:38 +00:00
|
|
|
dispatch(storeVideoTransform(streamId, transform));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps (parts of) the redux state to the component's props.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* _transforms: Object
|
|
|
|
* }}
|
|
|
|
*/
|
2023-01-10 13:31:57 +00:00
|
|
|
function _mapStateToProps(state: IReduxState) {
|
2018-04-07 07:52:38 +00:00
|
|
|
return {
|
2022-08-01 14:35:14 +00:00
|
|
|
_aspectRatio: state['features/base/responsive-ui'].aspectRatio,
|
|
|
|
|
2018-04-07 07:52:38 +00:00
|
|
|
/**
|
2018-05-22 14:23:03 +00:00
|
|
|
* The stored transforms retrieved from Redux to be initially applied to
|
2018-04-07 07:52:38 +00:00
|
|
|
* different streams.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
_transforms: state['features/base/media'].video.transforms
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(_mapStateToProps, _mapDispatchToProps)(VideoTransform);
|