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 13KB

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