您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Toolbox.native.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. TOOLBAR_AUDIO_MUTED,
  7. TOOLBAR_AUDIO_UNMUTED,
  8. TOOLBAR_VIDEO_,
  9. sendAnalyticsEvent
  10. } from '../../analytics';
  11. import {
  12. isNarrowAspectRatio,
  13. makeAspectRatioAware
  14. } from '../../base/aspect-ratio';
  15. import { toggleAudioOnly } from '../../base/conference';
  16. import {
  17. MEDIA_TYPE,
  18. setAudioMuted,
  19. setVideoMuted,
  20. toggleCameraFacingMode,
  21. VIDEO_MUTISM_AUTHORITY
  22. } from '../../base/media';
  23. import { Container } from '../../base/react';
  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. sendAnalyticsEvent(mute ? TOOLBAR_AUDIO_MUTED : TOOLBAR_AUDIO_UNMUTED);
  168. // The user sees the reality i.e. the state of base/tracks and intends
  169. // to change reality by tapping on the respective button i.e. the user
  170. // sets the state of base/media. Whether the user's intention will turn
  171. // into reality is a whole different story which is of no concern to the
  172. // tapping.
  173. this.props.dispatch(
  174. setAudioMuted(
  175. mute,
  176. VIDEO_MUTISM_AUTHORITY.USER,
  177. /* ensureTrack */ true));
  178. }
  179. /**
  180. * Dispatches an action to toggle the mute state of the video/camera.
  181. *
  182. * @private
  183. * @returns {void}
  184. */
  185. _onToggleVideo() {
  186. const mute = !this.props._videoMuted;
  187. sendAnalyticsEvent(`${TOOLBAR_VIDEO_}.${mute ? 'muted' : 'unmuted'}`);
  188. // The user sees the reality i.e. the state of base/tracks and intends
  189. // to change reality by tapping on the respective button i.e. the user
  190. // sets the state of base/media. Whether the user's intention will turn
  191. // into reality is a whole different story which is of no concern to the
  192. // tapping.
  193. this.props.dispatch(
  194. setVideoMuted(
  195. !this.props._videoMuted,
  196. VIDEO_MUTISM_AUTHORITY.USER,
  197. /* ensureTrack */ true));
  198. }
  199. /**
  200. * Renders the toolbar which contains the primary buttons such as hangup,
  201. * audio and video mute.
  202. *
  203. * @private
  204. * @returns {ReactElement}
  205. */
  206. _renderPrimaryToolbar() {
  207. const audioButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.AUDIO);
  208. const videoButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.VIDEO);
  209. /* eslint-disable react/jsx-handler-names */
  210. return (
  211. <View
  212. key = 'primaryToolbar'
  213. style = { styles.primaryToolbar }>
  214. <ToolbarButton
  215. iconName = { audioButtonStyles.iconName }
  216. iconStyle = { audioButtonStyles.iconStyle }
  217. onClick = { this._onToggleAudio }
  218. style = { audioButtonStyles.style } />
  219. <ToolbarButton
  220. accessibilityLabel = 'Hangup'
  221. iconName = 'hangup'
  222. iconStyle = { styles.whitePrimaryToolbarButtonIcon }
  223. onClick = { this.props._onHangup }
  224. style = { styles.hangup }
  225. underlayColor = { ColorPalette.buttonUnderlay } />
  226. <ToolbarButton
  227. disabled = { this.props._audioOnly }
  228. iconName = { videoButtonStyles.iconName }
  229. iconStyle = { videoButtonStyles.iconStyle }
  230. onClick = { this._onToggleVideo }
  231. style = { videoButtonStyles.style } />
  232. </View>
  233. );
  234. /* eslint-enable react/jsx-handler-names */
  235. }
  236. /**
  237. * Renders the toolbar which contains the secondary buttons such as toggle
  238. * camera facing mode.
  239. *
  240. * @private
  241. * @returns {ReactElement}
  242. */
  243. _renderSecondaryToolbar() {
  244. const iconStyle = styles.secondaryToolbarButtonIcon;
  245. const style = styles.secondaryToolbarButton;
  246. const underlayColor = 'transparent';
  247. const {
  248. _audioOnly: audioOnly,
  249. _videoMuted: videoMuted
  250. } = this.props;
  251. /* eslint-disable react/jsx-curly-spacing,react/jsx-handler-names */
  252. return (
  253. <View
  254. key = 'secondaryToolbar'
  255. style = { styles.secondaryToolbar }>
  256. {
  257. AudioRouteButton
  258. && <AudioRouteButton
  259. iconName = { 'volume' }
  260. iconStyle = { iconStyle }
  261. style = { style }
  262. underlayColor = { underlayColor } />
  263. }
  264. <ToolbarButton
  265. disabled = { audioOnly || videoMuted }
  266. iconName = 'switch-camera'
  267. iconStyle = { iconStyle }
  268. onClick = { this.props._onToggleCameraFacingMode }
  269. style = { style }
  270. underlayColor = { underlayColor } />
  271. <ToolbarButton
  272. iconName = { audioOnly ? 'visibility-off' : 'visibility' }
  273. iconStyle = { iconStyle }
  274. onClick = { this.props._onToggleAudioOnly }
  275. style = { style }
  276. underlayColor = { underlayColor } />
  277. <ToolbarButton
  278. iconName = {
  279. this.props._locked ? 'security-locked' : 'security'
  280. }
  281. iconStyle = { iconStyle }
  282. onClick = { this.props._onRoomLock }
  283. style = { style }
  284. underlayColor = { underlayColor } />
  285. {
  286. _SHARE_ROOM_TOOLBAR_BUTTON
  287. && <ToolbarButton
  288. iconName = 'link'
  289. iconStyle = { iconStyle }
  290. onClick = { this.props._onShareRoom }
  291. style = { style }
  292. underlayColor = { underlayColor } />
  293. }
  294. </View>
  295. );
  296. /* eslint-enable react/jsx-curly-spacing,react/jsx-handler-names */
  297. }
  298. /**
  299. * Renders the primary and the secondary toolbars.
  300. *
  301. * @private
  302. * @returns {[ReactElement, ReactElement]}
  303. */
  304. _renderToolbars() {
  305. return [
  306. this._renderSecondaryToolbar(),
  307. this._renderPrimaryToolbar()
  308. ];
  309. }
  310. }
  311. /**
  312. * Additional properties for various icons, which are now platform-dependent.
  313. * This is done to have common logic of generating styles for web and native.
  314. * TODO As soon as we have common font sets for web and native, this will no
  315. * longer be required.
  316. */
  317. Object.assign(Toolbox.prototype, {
  318. audioIcon: 'microphone',
  319. audioMutedIcon: 'mic-disabled',
  320. videoIcon: 'camera',
  321. videoMutedIcon: 'camera-disabled'
  322. });
  323. /**
  324. * Maps actions to React component props.
  325. *
  326. * @param {Function} dispatch - Redux action dispatcher.
  327. * @returns {{
  328. * _onRoomLock: Function,
  329. * _onToggleAudioOnly: Function,
  330. * _onToggleCameraFacingMode: Function,
  331. * }}
  332. * @private
  333. */
  334. function _mapDispatchToProps(dispatch) {
  335. return {
  336. ...abstractMapDispatchToProps(dispatch),
  337. /**
  338. * Sets the lock i.e. password protection of the conference/room.
  339. *
  340. * @private
  341. * @returns {void}
  342. * @type {Function}
  343. */
  344. _onRoomLock() {
  345. dispatch(beginRoomLockRequest());
  346. },
  347. /**
  348. * Begins the UI procedure to share the conference/room URL.
  349. *
  350. * @private
  351. * @returns {void}
  352. * @type {Function}
  353. */
  354. _onShareRoom() {
  355. dispatch(beginShareRoom());
  356. },
  357. /**
  358. * Toggles the audio-only flag of the conference.
  359. *
  360. * @private
  361. * @returns {void}
  362. * @type {Function}
  363. */
  364. _onToggleAudioOnly() {
  365. dispatch(toggleAudioOnly());
  366. },
  367. /**
  368. * Switches between the front/user-facing and back/environment-facing
  369. * cameras.
  370. *
  371. * @private
  372. * @returns {void}
  373. * @type {Function}
  374. */
  375. _onToggleCameraFacingMode() {
  376. dispatch(toggleCameraFacingMode());
  377. }
  378. };
  379. }
  380. /**
  381. * Maps part of Redux store to React component props.
  382. *
  383. * @param {Object} state - Redux store.
  384. * @returns {{
  385. * _audioOnly: boolean,
  386. * _locked: boolean
  387. * }}
  388. * @private
  389. */
  390. function _mapStateToProps(state) {
  391. const conference = state['features/base/conference'];
  392. return {
  393. ...abstractMapStateToProps(state),
  394. /**
  395. * The indicator which determines whether the conference is in
  396. * audio-only mode.
  397. *
  398. * @protected
  399. * @type {boolean}
  400. */
  401. _audioOnly: Boolean(conference.audioOnly),
  402. /**
  403. * The indicator which determines whether the conference is
  404. * locked/password-protected.
  405. *
  406. * @protected
  407. * @type {boolean}
  408. */
  409. _locked: Boolean(conference.locked)
  410. };
  411. }
  412. export default connect(_mapStateToProps, _mapDispatchToProps)(
  413. makeAspectRatioAware(Toolbox));