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

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