Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Toolbox.native.js 13KB

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