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.

AudioMuteButton.web.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React from 'react';
  4. import { connect } from 'react-redux';
  5. import UIEvents from '../../../../../service/UI/UIEvents';
  6. import {
  7. ACTION_SHORTCUT_TRIGGERED,
  8. AUDIO_MUTE,
  9. createShortcutEvent,
  10. sendAnalytics
  11. } from '../../../analytics';
  12. import { translate } from '../../../base/i18n';
  13. import { MEDIA_TYPE } from '../../../base/media';
  14. import { isLocalTrackMuted } from '../../../base/tracks';
  15. import AbstractAudioMuteButton from './AbstractAudioMuteButton';
  16. import ToolbarButton from '../ToolbarButton';
  17. declare var APP: Object;
  18. /**
  19. * Component that renders a toolbar button for toggling audio mute.
  20. *
  21. * @extends Component
  22. */
  23. export class AudioMuteButton extends AbstractAudioMuteButton {
  24. /**
  25. * Default values for {@code AudioMuteButton} component's properties.
  26. *
  27. * @static
  28. */
  29. static defaultProps = {
  30. tooltipPosition: 'top'
  31. };
  32. /**
  33. * {@code AudioMuteButton} component's property types.
  34. *
  35. * @static
  36. */
  37. static propTypes = {
  38. ...AbstractAudioMuteButton.propTypes,
  39. /**
  40. * The {@code JitsiConference} for the current conference.
  41. */
  42. _conference: PropTypes.object,
  43. /**
  44. * Invoked to update the audio mute status.
  45. */
  46. dispatch: PropTypes.func,
  47. /**
  48. * Invoked to obtain translated strings.
  49. */
  50. t: PropTypes.func,
  51. /**
  52. * Where the tooltip should display, relative to the button.
  53. */
  54. tooltipPosition: PropTypes.string
  55. };
  56. /**
  57. * Initializes a new {@code AudioMuteButton} instance.
  58. *
  59. * @param {Props} props - The read-only React {@code Component} props with
  60. * which the new instance is to be initialized.
  61. */
  62. constructor(props: Object) {
  63. super(props);
  64. // Bind event handlers so it is only bound once per instance.
  65. this._onShortcutToggleAudio = this._onShortcutToggleAudio.bind(this);
  66. }
  67. /**
  68. * Sets a keyboard shortcuts for toggling audio mute.
  69. *
  70. * @inheritdoc
  71. * @returns {void}
  72. */
  73. componentDidMount() {
  74. APP.keyboardshortcut.registerShortcut(
  75. 'M',
  76. null,
  77. this._onShortcutToggleAudio,
  78. 'keyboardShortcuts.mute');
  79. }
  80. /**
  81. * Removes the registered keyboard shortcut handler.
  82. *
  83. * @inheritdoc
  84. * @returns {void}
  85. */
  86. componentWillUnmount() {
  87. APP.keyboardshortcut.unregisterShortcut('M');
  88. }
  89. /**
  90. * Implements React's {@link Component#render()}.
  91. *
  92. * @inheritdoc
  93. * @returns {ReactElement}
  94. */
  95. render() {
  96. const { _audioMuted, _conference, t, tooltipPosition } = this.props;
  97. return (
  98. <ToolbarButton
  99. accessibilityLabel = 'Audio mute'
  100. iconName = { _audioMuted && _conference
  101. ? 'icon-mic-disabled toggled'
  102. : 'icon-microphone' }
  103. onClick = { this._onToolbarToggleAudio }
  104. tooltip = { t('toolbar.mute') }
  105. tooltipPosition = { tooltipPosition } />
  106. );
  107. }
  108. _doToggleAudio: () => void;
  109. /**
  110. * Emits an event to signal audio mute should be toggled.
  111. *
  112. * @private
  113. * @returns {void}
  114. */
  115. _doToggleAudio() {
  116. // The old conference logic must be used for now as the redux flows do
  117. // not handle all cases, such as unmuting when the config
  118. // startWithAudioMuted is true.
  119. APP.UI.emitEvent(UIEvents.AUDIO_MUTED, !this.props._audioMuted, true);
  120. }
  121. _onShortcutToggleAudio: () => void;
  122. /**
  123. * Creates an analytics keyboard shortcut event and dispatches an action for
  124. * toggling audio mute.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _onShortcutToggleAudio() {
  130. sendAnalytics(createShortcutEvent(
  131. AUDIO_MUTE,
  132. ACTION_SHORTCUT_TRIGGERED,
  133. { enable: !this.props._audioMuted }));
  134. this._doToggleAudio();
  135. }
  136. _onToolbarToggleAudio: () => void;
  137. }
  138. /**
  139. * Maps (parts of) the Redux state to the associated props for the
  140. * {@code AudioMuteButton} component.
  141. *
  142. * @param {Object} state - The Redux state.
  143. * @private
  144. * @returns {{
  145. * _audioMuted: boolean,
  146. * _conference: Object,
  147. * }}
  148. */
  149. function _mapStateToProps(state) {
  150. const tracks = state['features/base/tracks'];
  151. return {
  152. _audioMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO),
  153. _conference: state['features/base/conference'].conference
  154. };
  155. }
  156. export default translate(connect(_mapStateToProps)(AudioMuteButton));