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

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