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 3.9KB

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