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

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. return (
  110. <Container
  111. style = {
  112. isNarrowAspectRatio(this)
  113. ? styles.toolbarContainerNarrow
  114. : styles.toolbarContainerWide }
  115. visible = { this.props._visible } >
  116. { this._renderToolbars() }
  117. </Container>
  118. );
  119. }
  120. /**
  121. * Gets the styles for a button that toggles the mute state of a specific
  122. * media type.
  123. *
  124. * @param {string} mediaType - The {@link MEDIA_TYPE} associated with the
  125. * button to get styles for.
  126. * @protected
  127. * @returns {{
  128. * iconName: string,
  129. * iconStyle: Object,
  130. * style: Object
  131. * }}
  132. */
  133. _getMuteButtonStyles(mediaType) {
  134. let iconName;
  135. let iconStyle;
  136. let style;
  137. if (this.props[`_${mediaType}Muted`]) {
  138. iconName = this[`${mediaType}MutedIcon`];
  139. iconStyle = styles.whitePrimaryToolbarButtonIcon;
  140. style = styles.whitePrimaryToolbarButton;
  141. } else {
  142. iconName = this[`${mediaType}Icon`];
  143. iconStyle = styles.primaryToolbarButtonIcon;
  144. style = styles.primaryToolbarButton;
  145. }
  146. return {
  147. iconName,
  148. iconStyle,
  149. style
  150. };
  151. }
  152. /**
  153. * Dispatches an action to toggle the mute state of the audio/microphone.
  154. *
  155. * @private
  156. * @returns {void}
  157. */
  158. _onToggleAudio() {
  159. const mute = !this.props._audioMuted;
  160. sendAnalyticsEvent(`toolbar.audio.${mute ? 'muted' : 'unmuted'}`);
  161. // The user sees the reality i.e. the state of base/tracks and intends
  162. // to change reality by tapping on the respective button i.e. the user
  163. // sets the state of base/media. Whether the user's intention will turn
  164. // into reality is a whole different story which is of no concern to the
  165. // tapping.
  166. this.props.dispatch(
  167. setAudioMuted(
  168. mute,
  169. VIDEO_MUTISM_AUTHORITY.USER,
  170. /* ensureTrack */ true));
  171. }
  172. /**
  173. * Dispatches an action to toggle the mute state of the video/camera.
  174. *
  175. * @private
  176. * @returns {void}
  177. */
  178. _onToggleVideo() {
  179. const mute = !this.props._videoMuted;
  180. sendAnalyticsEvent(`toolbar.video.${mute ? 'muted' : 'unmuted'}`);
  181. // The user sees the reality i.e. the state of base/tracks and intends
  182. // to change reality by tapping on the respective button i.e. the user
  183. // sets the state of base/media. Whether the user's intention will turn
  184. // into reality is a whole different story which is of no concern to the
  185. // tapping.
  186. this.props.dispatch(
  187. setVideoMuted(
  188. !this.props._videoMuted,
  189. VIDEO_MUTISM_AUTHORITY.USER,
  190. /* ensureTrack */ true));
  191. }
  192. /**
  193. * Renders the toolbar which contains the primary buttons such as hangup,
  194. * audio and video mute.
  195. *
  196. * @private
  197. * @returns {ReactElement}
  198. */
  199. _renderPrimaryToolbar() {
  200. const audioButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.AUDIO);
  201. const videoButtonStyles = this._getMuteButtonStyles(MEDIA_TYPE.VIDEO);
  202. /* eslint-disable react/jsx-handler-names */
  203. return (
  204. <View style = { styles.primaryToolbar }>
  205. <ToolbarButton
  206. iconName = { audioButtonStyles.iconName }
  207. iconStyle = { audioButtonStyles.iconStyle }
  208. onClick = { this._onToggleAudio }
  209. style = { audioButtonStyles.style } />
  210. <ToolbarButton
  211. iconName = 'hangup'
  212. iconStyle = { styles.whitePrimaryToolbarButtonIcon }
  213. onClick = { this.props._onHangup }
  214. style = { styles.hangup }
  215. underlayColor = { ColorPalette.buttonUnderlay } />
  216. <ToolbarButton
  217. disabled = { this.props._audioOnly }
  218. iconName = { videoButtonStyles.iconName }
  219. iconStyle = { videoButtonStyles.iconStyle }
  220. onClick = { this._onToggleVideo }
  221. style = { videoButtonStyles.style } />
  222. </View>
  223. );
  224. /* eslint-enable react/jsx-handler-names */
  225. }
  226. /**
  227. * Renders the toolbar which contains the secondary buttons such as toggle
  228. * camera facing mode.
  229. *
  230. * @private
  231. * @returns {ReactElement}
  232. */
  233. _renderSecondaryToolbar() {
  234. const iconStyle = styles.secondaryToolbarButtonIcon;
  235. const style = styles.secondaryToolbarButton;
  236. const underlayColor = 'transparent';
  237. const {
  238. _audioOnly: audioOnly,
  239. _videoMuted: videoMuted
  240. } = this.props;
  241. /* eslint-disable react/jsx-curly-spacing,react/jsx-handler-names */
  242. return (
  243. <View style = { styles.secondaryToolbar }>
  244. <ToolbarButton
  245. disabled = { audioOnly || videoMuted }
  246. iconName = 'switch-camera'
  247. iconStyle = { iconStyle }
  248. onClick = { this.props._onToggleCameraFacingMode }
  249. style = { style }
  250. underlayColor = { underlayColor } />
  251. <ToolbarButton
  252. iconName = {
  253. this.props._locked ? 'security-locked' : 'security'
  254. }
  255. iconStyle = { iconStyle }
  256. onClick = { this.props._onRoomLock }
  257. style = { style }
  258. underlayColor = { underlayColor } />
  259. <ToolbarButton
  260. iconName = { audioOnly ? 'visibility-off' : 'visibility' }
  261. iconStyle = { iconStyle }
  262. onClick = { this.props._onToggleAudioOnly }
  263. style = { style }
  264. underlayColor = { underlayColor } />
  265. {
  266. _SHARE_ROOM_TOOLBAR_BUTTON
  267. && <ToolbarButton
  268. iconName = 'link'
  269. iconStyle = { iconStyle }
  270. onClick = { this.props._onShareRoom }
  271. style = { style }
  272. underlayColor = { underlayColor } />
  273. }
  274. </View>
  275. );
  276. /* eslint-enable react/jsx-curly-spacing,react/jsx-handler-names */
  277. }
  278. /**
  279. * Renders the primary and the secondary toolbars in the order depending on
  280. * the current aspect ratio.
  281. *
  282. * @returns {[ReactElement, ReactElement]}
  283. * @private
  284. */
  285. _renderToolbars() {
  286. if (isNarrowAspectRatio(this)) {
  287. return [
  288. this._renderSecondaryToolbar(),
  289. this._renderPrimaryToolbar()
  290. ];
  291. }
  292. return [ this._renderPrimaryToolbar(), this._renderSecondaryToolbar() ];
  293. }
  294. }
  295. /**
  296. * Additional properties for various icons, which are now platform-dependent.
  297. * This is done to have common logic of generating styles for web and native.
  298. * TODO As soon as we have common font sets for web and native, this will no
  299. * longer be required.
  300. */
  301. Object.assign(Toolbox.prototype, {
  302. audioIcon: 'microphone',
  303. audioMutedIcon: 'mic-disabled',
  304. videoIcon: 'camera',
  305. videoMutedIcon: 'camera-disabled'
  306. });
  307. /**
  308. * Maps actions to React component props.
  309. *
  310. * @param {Function} dispatch - Redux action dispatcher.
  311. * @returns {{
  312. * _onRoomLock: Function,
  313. * _onToggleAudioOnly: Function,
  314. * _onToggleCameraFacingMode: Function,
  315. * }}
  316. * @private
  317. */
  318. function _mapDispatchToProps(dispatch) {
  319. return {
  320. ...abstractMapDispatchToProps(dispatch),
  321. /**
  322. * Sets the lock i.e. password protection of the conference/room.
  323. *
  324. * @private
  325. * @returns {void}
  326. * @type {Function}
  327. */
  328. _onRoomLock() {
  329. dispatch(beginRoomLockRequest());
  330. },
  331. /**
  332. * Begins the UI procedure to share the conference/room URL.
  333. *
  334. * @private
  335. * @returns {void}
  336. * @type {Function}
  337. */
  338. _onShareRoom() {
  339. dispatch(beginShareRoom());
  340. },
  341. /**
  342. * Toggles the audio-only flag of the conference.
  343. *
  344. * @private
  345. * @returns {void}
  346. * @type {Function}
  347. */
  348. _onToggleAudioOnly() {
  349. dispatch(toggleAudioOnly());
  350. },
  351. /**
  352. * Switches between the front/user-facing and back/environment-facing
  353. * cameras.
  354. *
  355. * @private
  356. * @returns {void}
  357. * @type {Function}
  358. */
  359. _onToggleCameraFacingMode() {
  360. dispatch(toggleCameraFacingMode());
  361. }
  362. };
  363. }
  364. /**
  365. * Maps part of Redux store to React component props.
  366. *
  367. * @param {Object} state - Redux store.
  368. * @returns {{
  369. * _audioOnly: boolean,
  370. * _locked: boolean
  371. * }}
  372. * @private
  373. */
  374. function _mapStateToProps(state) {
  375. const conference = state['features/base/conference'];
  376. return {
  377. ...abstractMapStateToProps(state),
  378. /**
  379. * The indicator which determines whether the conference is in
  380. * audio-only mode.
  381. *
  382. * @protected
  383. * @type {boolean}
  384. */
  385. _audioOnly: Boolean(conference.audioOnly),
  386. /**
  387. * The indicator which determines whether the conference is
  388. * locked/password-protected.
  389. *
  390. * @protected
  391. * @type {boolean}
  392. */
  393. _locked: Boolean(conference.locked)
  394. };
  395. }
  396. export default connect(_mapStateToProps, _mapDispatchToProps)(
  397. makeAspectRatioAware(Toolbox));