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.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import {
  3. ACTION_SHORTCUT_TRIGGERED,
  4. AUDIO_MUTE,
  5. createShortcutEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { translate } from '../../base/i18n';
  9. import { MEDIA_TYPE } from '../../base/media';
  10. import { connect } from '../../base/redux';
  11. import { AbstractAudioMuteButton } from '../../base/toolbox';
  12. import type { AbstractButtonProps } from '../../base/toolbox';
  13. import { isLocalTrackMuted } from '../../base/tracks';
  14. import {
  15. isPrejoinAudioMuted,
  16. isAudioDisabled,
  17. isPrejoinPageVisible
  18. } from '../../prejoin';
  19. import { muteLocal } from '../../remote-video-menu/actions';
  20. declare var APP: Object;
  21. /**
  22. * The type of the React {@code Component} props of {@link AudioMuteButton}.
  23. */
  24. type Props = AbstractButtonProps & {
  25. /**
  26. * Whether audio is currently muted or not.
  27. */
  28. _audioMuted: boolean,
  29. /**
  30. * Whether the button is disabled.
  31. */
  32. _disabled: boolean,
  33. /**
  34. * The redux {@code dispatch} function.
  35. */
  36. dispatch: Function
  37. }
  38. /**
  39. * Component that renders a toolbar button for toggling audio mute.
  40. *
  41. * @extends AbstractAudioMuteButton
  42. */
  43. class AudioMuteButton extends AbstractAudioMuteButton<Props, *> {
  44. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  45. label = 'toolbar.mute';
  46. tooltip = 'toolbar.mute';
  47. /**
  48. * Initializes a new {@code AudioMuteButton} instance.
  49. *
  50. * @param {Props} props - The read-only React {@code Component} props with
  51. * which the new instance is to be initialized.
  52. */
  53. constructor(props: Props) {
  54. super(props);
  55. // Bind event handlers so they are only bound once per instance.
  56. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  57. }
  58. /**
  59. * Registers the keyboard shortcut that toggles the audio muting.
  60. *
  61. * @inheritdoc
  62. * @returns {void}
  63. */
  64. componentDidMount() {
  65. typeof APP === 'undefined'
  66. || APP.keyboardshortcut.registerShortcut(
  67. 'M',
  68. null,
  69. this._onKeyboardShortcut,
  70. 'keyboardShortcuts.mute');
  71. }
  72. /**
  73. * Unregisters the keyboard shortcut that toggles the audio muting.
  74. *
  75. * @inheritdoc
  76. * @returns {void}
  77. */
  78. componentWillUnmount() {
  79. typeof APP === 'undefined'
  80. || APP.keyboardshortcut.unregisterShortcut('M');
  81. }
  82. /**
  83. * Indicates if audio is currently muted ot nor.
  84. *
  85. * @override
  86. * @protected
  87. * @returns {boolean}
  88. */
  89. _isAudioMuted() {
  90. return this.props._audioMuted;
  91. }
  92. _onKeyboardShortcut: () => void;
  93. /**
  94. * Creates an analytics keyboard shortcut event and dispatches an action to
  95. * toggle the audio muting.
  96. *
  97. * @private
  98. * @returns {void}
  99. */
  100. _onKeyboardShortcut() {
  101. sendAnalytics(
  102. createShortcutEvent(
  103. AUDIO_MUTE,
  104. ACTION_SHORTCUT_TRIGGERED,
  105. { enable: !this._isAudioMuted() }));
  106. super._handleClick();
  107. }
  108. /**
  109. * Changes the muted state.
  110. *
  111. * @param {boolean} audioMuted - Whether audio should be muted or not.
  112. * @protected
  113. * @returns {void}
  114. */
  115. _setAudioMuted(audioMuted: boolean) {
  116. this.props.dispatch(muteLocal(audioMuted));
  117. }
  118. /**
  119. * Return a boolean value indicating if this button is disabled or not.
  120. *
  121. * @returns {boolean}
  122. */
  123. _isDisabled() {
  124. return this.props._disabled;
  125. }
  126. }
  127. /**
  128. * Maps (parts of) the redux state to the associated props for the
  129. * {@code AudioMuteButton} component.
  130. *
  131. * @param {Object} state - The Redux state.
  132. * @private
  133. * @returns {{
  134. * _audioMuted: boolean,
  135. * _disabled: boolean
  136. * }}
  137. */
  138. function _mapStateToProps(state): Object {
  139. let _audioMuted;
  140. let _disabled;
  141. if (isPrejoinPageVisible(state)) {
  142. _audioMuted = isPrejoinAudioMuted(state);
  143. _disabled = state['features/base/config'].startSilent;
  144. } else {
  145. const tracks = state['features/base/tracks'];
  146. _audioMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  147. _disabled = state['features/base/config'].startSilent || isAudioDisabled(state);
  148. }
  149. return {
  150. _audioMuted,
  151. _disabled
  152. };
  153. }
  154. export default translate(connect(_mapStateToProps)(AudioMuteButton));