You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Toolbox.native.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { toggleAudioOnly } from '../../base/conference';
  6. import {
  7. MEDIA_TYPE,
  8. setAudioMuted,
  9. setVideoMuted,
  10. toggleCameraFacingMode,
  11. VIDEO_MUTISM_AUTHORITY
  12. } from '../../base/media';
  13. import { Container } from '../../base/react';
  14. import { ColorPalette } from '../../base/styles';
  15. import { beginRoomLockRequest } from '../../room-lock';
  16. import { beginShareRoom } from '../../share-room';
  17. import {
  18. abstractMapDispatchToProps,
  19. abstractMapStateToProps
  20. } from '../functions';
  21. import styles from './styles';
  22. import ToolbarButton from './ToolbarButton';
  23. /**
  24. * The indicator which determines (at bundle time) whether there should be a
  25. * {@code ToolbarButton} in {@code Toolbox} to expose the functionality of the
  26. * feature share-room in the user interface of the app.
  27. *
  28. * @private
  29. * @type {boolean}
  30. */
  31. const _SHARE_ROOM_TOOLBAR_BUTTON = true;
  32. /**
  33. * Implements the conference toolbox on React Native.
  34. */
  35. class Toolbox extends Component {
  36. /**
  37. * Toolbox component's property types.
  38. *
  39. * @static
  40. */
  41. static propTypes = {
  42. /**
  43. * Flag showing that audio is muted.
  44. */
  45. _audioMuted: PropTypes.bool,
  46. /**
  47. * Flag showing whether the audio-only mode is in use.
  48. */
  49. _audioOnly: PropTypes.bool,
  50. /**
  51. * Flag showing whether room is locked.
  52. */
  53. _locked: PropTypes.bool,
  54. /**
  55. * Handler for hangup.
  56. */
  57. _onHangup: PropTypes.func,
  58. /**
  59. * Sets the lock i.e. password protection of the conference/room.
  60. */
  61. _onRoomLock: PropTypes.func,
  62. /**
  63. * Begins the UI procedure to share the conference/room URL.
  64. */
  65. _onShareRoom: PropTypes.func,
  66. /**
  67. * Toggles the audio-only flag of the conference.
  68. */
  69. _onToggleAudioOnly: PropTypes.func,
  70. /**
  71. * Switches between the front/user-facing and back/environment-facing
  72. * cameras.
  73. */
  74. _onToggleCameraFacingMode: PropTypes.func,
  75. /**
  76. * Flag showing whether video is muted.
  77. */
  78. _videoMuted: PropTypes.bool,
  79. /**
  80. * Flag showing whether toolbar is visible.
  81. */
  82. _visible: PropTypes.bool,
  83. dispatch: PropTypes.func
  84. };
  85. /**
  86. * Initializes a new {@code Toolbox} instance.
  87. *
  88. * @param {Object} props - The read-only React {@code Component} props with
  89. * which the new instance is to be initialized.
  90. */
  91. constructor(props) {
  92. super(props);
  93. // Bind event handlers so they are only bound once per instance.
  94. this._onToggleAudio = this._onToggleAudio.bind(this);
  95. this._onToggleVideo = this._onToggleVideo.bind(this);
  96. }
  97. /**
  98. * Implements React's {@link Component#render()}.
  99. *
  100. * @inheritdoc
  101. * @returns {ReactElement}
  102. */
  103. render() {
  104. return (
  105. <Container
  106. style = { styles.toolbarContainer }
  107. visible = { this.props._visible }>
  108. {
  109. this._renderPrimaryToolbar()
  110. }
  111. {
  112. this._renderSecondaryToolbar()
  113. }
  114. </Container>
  115. );
  116. }
  117. /**
  118. * Gets the styles for a button that toggles the mute state of a specific
  119. * media type.
  120. *
  121. * @param {string} mediaType - The {@link MEDIA_TYPE} associated with the
  122. * button to get styles for.
  123. * @protected
  124. * @returns {{
  125. * iconName: string,
  126. * iconStyle: Object,
  127. * style: Object
  128. * }}
  129. */
  130. _getMuteButtonStyles(mediaType) {
  131. let iconName;
  132. let iconStyle;
  133. let style;
  134. if (this.props[`_${mediaType}Muted`]) {
  135. iconName = this[`${mediaType}MutedIcon`];
  136. iconStyle = styles.whitePrimaryToolbarButtonIcon;
  137. style = styles.whitePrimaryToolbarButton;
  138. } else {
  139. iconName = this[`${mediaType}Icon`];
  140. iconStyle = styles.primaryToolbarButtonIcon;
  141. style = styles.primaryToolbarButton;
  142. }
  143. return {
  144. iconName,
  145. iconStyle,
  146. style
  147. };
  148. }
  149. /**
  150. * Dispatches an action to toggle the mute state of the audio/microphone.
  151. *
  152. * @private
  153. * @returns {void}
  154. */
  155. _onToggleAudio() {
  156. // The user sees the reality i.e. the state of base/tracks and intends
  157. // to change reality by tapping on the respective button i.e. the user
  158. // sets the state of base/media. Whether the user's intention will turn
  159. // into reality is a whole different story which is of no concern to the
  160. // tapping.
  161. this.props.dispatch(
  162. setAudioMuted(
  163. !this.props._audioMuted,
  164. VIDEO_MUTISM_AUTHORITY.USER,
  165. /* ensureTrack */ true));
  166. }
  167. /**
  168. * Dispatches an action to toggle the mute state of the video/camera.
  169. *
  170. * @private
  171. * @returns {void}
  172. */
  173. _onToggleVideo() {
  174. // The user sees the reality i.e. the state of base/tracks and intends
  175. // to change reality by tapping on the respective button i.e. the user
  176. // sets the state of base/media. Whether the user's intention will turn
  177. // into reality is a whole different story which is of no concern to the
  178. // tapping.
  179. this.props.dispatch(
  180. setVideoMuted(
  181. !this.props._videoMuted,
  182. VIDEO_MUTISM_AUTHORITY.USER,
  183. /* ensureTrack */ true));
  184. }
  185. /**
  186. * Renders the toolbar which contains the primary buttons such as hangup,
  187. * audio and video mute.
  188. *
  189. * @private
  190. * @returns {ReactElement}
  191. */
  192. _renderPrimaryToolbar() {
  193. const audioButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.AUDIO);
  194. const videoButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.VIDEO);
  195. /* eslint-disable react/jsx-handler-names */
  196. return (
  197. <View style = { styles.primaryToolbar }>
  198. <ToolbarButton
  199. iconName = { audioButtonStyles.iconName }
  200. iconStyle = { audioButtonStyles.iconStyle }
  201. onClick = { this._onToggleAudio }
  202. style = { audioButtonStyles.style } />
  203. <ToolbarButton
  204. iconName = 'hangup'
  205. iconStyle = { styles.whitePrimaryToolbarButtonIcon }
  206. onClick = { this.props._onHangup }
  207. style = { styles.hangup }
  208. underlayColor = { ColorPalette.buttonUnderlay } />
  209. <ToolbarButton
  210. disabled = { this.props._audioOnly }
  211. iconName = { videoButtonStyles.iconName }
  212. iconStyle = { videoButtonStyles.iconStyle }
  213. onClick = { this._onToggleVideo }
  214. style = { videoButtonStyles.style } />
  215. </View>
  216. );
  217. /* eslint-enable react/jsx-handler-names */
  218. }
  219. /**
  220. * Renders the toolbar which contains the secondary buttons such as toggle
  221. * camera facing mode.
  222. *
  223. * @private
  224. * @returns {ReactElement}
  225. */
  226. _renderSecondaryToolbar() {
  227. const iconStyle = styles.secondaryToolbarButtonIcon;
  228. const style = styles.secondaryToolbarButton;
  229. const underlayColor = 'transparent';
  230. const {
  231. _audioOnly: audioOnly,
  232. _videoMuted: videoMuted
  233. } = this.props;
  234. /* eslint-disable react/jsx-curly-spacing,react/jsx-handler-names */
  235. return (
  236. <View style = { styles.secondaryToolbar }>
  237. <ToolbarButton
  238. disabled = { audioOnly || videoMuted }
  239. iconName = 'switch-camera'
  240. iconStyle = { iconStyle }
  241. onClick = { this.props._onToggleCameraFacingMode }
  242. style = { style }
  243. underlayColor = { underlayColor } />
  244. <ToolbarButton
  245. iconName = {
  246. this.props._locked ? 'security-locked' : 'security'
  247. }
  248. iconStyle = { iconStyle }
  249. onClick = { this.props._onRoomLock }
  250. style = { style }
  251. underlayColor = { underlayColor } />
  252. <ToolbarButton
  253. iconName = { audioOnly ? 'visibility-off' : 'visibility' }
  254. iconStyle = { iconStyle }
  255. onClick = { this.props._onToggleAudioOnly }
  256. style = { style }
  257. underlayColor = { underlayColor } />
  258. {
  259. _SHARE_ROOM_TOOLBAR_BUTTON
  260. && <ToolbarButton
  261. iconName = 'link'
  262. iconStyle = { iconStyle }
  263. onClick = { this.props._onShareRoom }
  264. style = { style }
  265. underlayColor = { underlayColor } />
  266. }
  267. </View>
  268. );
  269. /* eslint-enable react/jsx-curly-spacing,react/jsx-handler-names */
  270. }
  271. }
  272. /**
  273. * Additional properties for various icons, which are now platform-dependent.
  274. * This is done to have common logic of generating styles for web and native.
  275. * TODO As soon as we have common font sets for web and native, this will no
  276. * longer be required.
  277. */
  278. Object.assign(Toolbox.prototype, {
  279. audioIcon: 'microphone',
  280. audioMutedIcon: 'mic-disabled',
  281. videoIcon: 'camera',
  282. videoMutedIcon: 'camera-disabled'
  283. });
  284. /**
  285. * Maps actions to React component props.
  286. *
  287. * @param {Function} dispatch - Redux action dispatcher.
  288. * @returns {{
  289. * _onRoomLock: Function,
  290. * _onToggleAudioOnly: Function,
  291. * _onToggleCameraFacingMode: Function,
  292. * }}
  293. * @private
  294. */
  295. function _mapDispatchToProps(dispatch) {
  296. return {
  297. ...abstractMapDispatchToProps(dispatch),
  298. /**
  299. * Sets the lock i.e. password protection of the conference/room.
  300. *
  301. * @private
  302. * @returns {void}
  303. * @type {Function}
  304. */
  305. _onRoomLock() {
  306. dispatch(beginRoomLockRequest());
  307. },
  308. /**
  309. * Begins the UI procedure to share the conference/room URL.
  310. *
  311. * @private
  312. * @returns {void}
  313. * @type {Function}
  314. */
  315. _onShareRoom() {
  316. dispatch(beginShareRoom());
  317. },
  318. /**
  319. * Toggles the audio-only flag of the conference.
  320. *
  321. * @private
  322. * @returns {void}
  323. * @type {Function}
  324. */
  325. _onToggleAudioOnly() {
  326. dispatch(toggleAudioOnly());
  327. },
  328. /**
  329. * Switches between the front/user-facing and back/environment-facing
  330. * cameras.
  331. *
  332. * @private
  333. * @returns {void}
  334. * @type {Function}
  335. */
  336. _onToggleCameraFacingMode() {
  337. dispatch(toggleCameraFacingMode());
  338. }
  339. };
  340. }
  341. /**
  342. * Maps part of Redux store to React component props.
  343. *
  344. * @param {Object} state - Redux store.
  345. * @returns {{
  346. * _audioOnly: boolean,
  347. * _locked: boolean
  348. * }}
  349. * @private
  350. */
  351. function _mapStateToProps(state) {
  352. const conference = state['features/base/conference'];
  353. return {
  354. ...abstractMapStateToProps(state),
  355. /**
  356. * The indicator which determines whether the conference is in
  357. * audio-only mode.
  358. *
  359. * @protected
  360. * @type {boolean}
  361. */
  362. _audioOnly: Boolean(conference.audioOnly),
  363. /**
  364. * The indicator which determines whether the conference is
  365. * locked/password-protected.
  366. *
  367. * @protected
  368. * @type {boolean}
  369. */
  370. _locked: Boolean(conference.locked)
  371. };
  372. }
  373. export default connect(_mapStateToProps, _mapDispatchToProps)(Toolbox);