2018-02-02 12:21:14 +00:00
|
|
|
// @flow
|
|
|
|
|
2018-05-22 14:23:03 +00:00
|
|
|
import React, { Component, type Node } from 'react';
|
|
|
|
import { type Dispatch } from 'redux';
|
2017-10-13 16:13:46 +00:00
|
|
|
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../redux';
|
|
|
|
|
2017-11-07 14:28:08 +00:00
|
|
|
import { setAspectRatio } from '../actions';
|
2018-02-06 18:14:05 +00:00
|
|
|
import DimensionsDetector from './DimensionsDetector';
|
2017-10-13 16:13:46 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-02 12:21:14 +00:00
|
|
|
* AspectRatioDetector component's property types.
|
2017-10-13 16:13:46 +00:00
|
|
|
*/
|
2018-02-02 12:21:14 +00:00
|
|
|
type Props = {
|
|
|
|
|
2017-10-13 16:13:46 +00:00
|
|
|
/**
|
2018-02-02 12:21:14 +00:00
|
|
|
* The "onDimensionsHandler" handler.
|
2017-10-13 16:13:46 +00:00
|
|
|
*/
|
2018-02-02 12:21:14 +00:00
|
|
|
_onDimensionsChanged: Function,
|
2017-10-13 16:13:46 +00:00
|
|
|
|
2018-02-02 12:21:14 +00:00
|
|
|
/**
|
|
|
|
* Any nested components.
|
|
|
|
*/
|
2018-05-22 14:23:03 +00:00
|
|
|
children: Node
|
2018-02-02 12:21:14 +00:00
|
|
|
};
|
2017-10-13 16:13:46 +00:00
|
|
|
|
2018-02-02 12:21:14 +00:00
|
|
|
/**
|
|
|
|
* A root {@link View} which captures the 'onLayout' event and figures out
|
|
|
|
* the aspect ratio of the app.
|
|
|
|
*/
|
|
|
|
class AspectRatioDetector extends Component<Props> {
|
2017-10-13 16:13:46 +00:00
|
|
|
/**
|
|
|
|
* Renders the root view and it's children.
|
|
|
|
*
|
|
|
|
* @returns {Component}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
return (
|
2018-02-02 12:21:14 +00:00
|
|
|
<DimensionsDetector
|
|
|
|
onDimensionsChanged = { this.props._onDimensionsChanged } >
|
2017-11-07 14:28:08 +00:00
|
|
|
{ this.props.children }
|
2018-02-02 12:21:14 +00:00
|
|
|
</DimensionsDetector>
|
2017-11-07 14:28:08 +00:00
|
|
|
);
|
2017-10-13 16:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps dispatching of the aspect ratio actions to React component props.
|
|
|
|
*
|
|
|
|
* @param {Function} dispatch - Redux action dispatcher.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
2018-02-02 12:21:14 +00:00
|
|
|
* _onDimensionsChanged: Function
|
2017-10-13 16:13:46 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2019-03-19 15:42:25 +00:00
|
|
|
function _mapDispatchToProps(dispatch: Dispatch<any>) {
|
2017-10-13 16:13:46 +00:00
|
|
|
return {
|
|
|
|
/**
|
2018-02-02 12:21:14 +00:00
|
|
|
* Handles the "on dimensions changed" event and dispatches aspect ratio
|
2017-10-13 16:13:46 +00:00
|
|
|
* changed action.
|
|
|
|
*
|
2018-02-02 12:21:14 +00:00
|
|
|
* @param {number} width - The new width for the component.
|
|
|
|
* @param {number} height - The new height for the component.
|
2017-10-13 16:13:46 +00:00
|
|
|
* @private
|
2017-11-07 14:28:08 +00:00
|
|
|
* @returns {void}
|
2017-10-13 16:13:46 +00:00
|
|
|
*/
|
2018-02-02 12:21:14 +00:00
|
|
|
_onDimensionsChanged(width: number, height: number) {
|
2017-11-07 14:28:08 +00:00
|
|
|
dispatch(setAspectRatio(width, height));
|
2017-10-13 16:13:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(undefined, _mapDispatchToProps)(AspectRatioDetector);
|