jiti-meet/react/features/video-menu/components/native/VolumeSlider.js

115 lines
2.9 KiB
JavaScript
Raw Normal View History

// @flow
import Slider from '@react-native-community/slider';
import _ from 'lodash';
import React, { Component } from 'react';
import { View } from 'react-native';
import { withTheme } from 'react-native-paper';
import { Icon, IconVolumeEmpty } from '../../../base/icons';
import { VOLUME_SLIDER_SCALE } from '../../constants';
/**
* The type of the React {@code Component} props of {@link VolumeSlider}.
*/
type Props = {
/**
* The value of the audio slider should display at when the component first
* mounts. Changes will be stored in state. The value should be a number
* between 0 and 1.
*/
initialValue: number,
/**
* Theme used for styles.
*/
theme: Object
};
/**
* The type of the React {@code Component} state of {@link VolumeSlider}.
*/
type State = {
/**
* The volume of the participant's audio element. The value will
* be represented by a slider.
*/
volumeLevel: number
};
/**
* Component that renders the volume slider.
*
* @returns {React$Element<any>}
*/
class VolumeSlider extends Component<Props, State> {
_onVolumeChange: Function;
_originalVolumeChange: Function;
/**
* Initializes a new {@code VolumeSlider} instance.
*
* @param {Object} props - The read-only properties with which the new
* instance is to be initialized.
*/
constructor(props: Props) {
super(props);
this.state = {
volumeLevel: (props.initialValue || 0) * VOLUME_SLIDER_SCALE
};
this._originalVolumeChange = this._onVolumeChange;
this._onVolumeChange = _.throttle(
volumeLevel => this._originalVolumeChange(volumeLevel), 500
);
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
const { theme } = this.props;
const { volumeLevel } = this.state;
const { palette } = theme;
return (
<View>
<Icon
size = { 24 }
src = { IconVolumeEmpty } />
<Slider
maximumTrackTintColor = { palette.field02 }
maximumValue = { VOLUME_SLIDER_SCALE }
minimumTrackTintColor = { palette.action01 }
minimumValue = { 0 }
onValueChange = { this._onVolumeChange }
thumbTintColor = { palette.field02 }
value = { volumeLevel } />
</View>
);
}
/**
* Sets the internal state of the volume level for the volume slider.
* Invokes the prop onVolumeChange to notify of volume changes.
*
* @param {number} volumeLevel - Selected volume on slider.
* @private
* @returns {void}
*/
_onVolumeChange(volumeLevel) {
this.setState({ volumeLevel });
}
}
export default withTheme(VolumeSlider);