Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Toolbox.native.js 12KB

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