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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // @flow
  2. import { connect } from 'react-redux';
  3. import {
  4. ACTION_SHORTCUT_TRIGGERED,
  5. AUDIO_MUTE,
  6. createShortcutEvent,
  7. createToolbarEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { translate } from '../../base/i18n';
  11. import { MEDIA_TYPE, setAudioMuted } from '../../base/media';
  12. import { AbstractAudioMuteButton } from '../../base/toolbox';
  13. import type { AbstractButtonProps } from '../../base/toolbox';
  14. import { isLocalTrackMuted } from '../../base/tracks';
  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. * The redux {@code dispatch} function.
  26. */
  27. dispatch: Function
  28. }
  29. /**
  30. * Component that renders a toolbar button for toggling audio mute.
  31. *
  32. * @extends AbstractAudioMuteButton
  33. */
  34. class AudioMuteButton extends AbstractAudioMuteButton<Props, *> {
  35. label = 'toolbar.mute';
  36. tooltip = 'toolbar.mute';
  37. /**
  38. * Initializes a new {@code AudioMuteButton} instance.
  39. *
  40. * @param {Props} props - The read-only React {@code Component} props with
  41. * which the new instance is to be initialized.
  42. */
  43. constructor(props: Props) {
  44. super(props);
  45. // Bind event handlers so they are only bound once per instance.
  46. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  47. }
  48. /**
  49. * Registers the keyboard shortcut that toggles the audio muting.
  50. *
  51. * @inheritdoc
  52. * @returns {void}
  53. */
  54. componentDidMount() {
  55. typeof APP === 'undefined'
  56. || APP.keyboardshortcut.registerShortcut(
  57. 'M',
  58. null,
  59. this._onKeyboardShortcut,
  60. 'keyboardShortcuts.mute');
  61. }
  62. /**
  63. * Unregisters the keyboard shortcut that toggles the audio muting.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentWillUnmount() {
  69. typeof APP === 'undefined'
  70. || APP.keyboardshortcut.unregisterShortcut('M');
  71. }
  72. /**
  73. * Indicates if audio is currently muted ot nor.
  74. *
  75. * @override
  76. * @protected
  77. * @returns {boolean}
  78. */
  79. _isAudioMuted() {
  80. return this.props._audioMuted;
  81. }
  82. _onKeyboardShortcut: () => void;
  83. /**
  84. * Creates an analytics keyboard shortcut event and dispatches an action to
  85. * toggle the audio muting.
  86. *
  87. * @private
  88. * @returns {void}
  89. */
  90. _onKeyboardShortcut() {
  91. sendAnalytics(
  92. createShortcutEvent(
  93. AUDIO_MUTE,
  94. ACTION_SHORTCUT_TRIGGERED,
  95. { enable: !this._isAudioMuted() }));
  96. super._handleClick();
  97. }
  98. /**
  99. * Changes the muted state.
  100. *
  101. * @param {boolean} audioMuted - Whether audio should be muted or not.
  102. * @protected
  103. * @returns {void}
  104. */
  105. _setAudioMuted(audioMuted: boolean) {
  106. sendAnalytics(createToolbarEvent(AUDIO_MUTE, { enable: audioMuted }));
  107. this.props.dispatch(setAudioMuted(audioMuted));
  108. }
  109. }
  110. /**
  111. * Maps (parts of) the redux state to the associated props for the
  112. * {@code AudioMuteButton} component.
  113. *
  114. * @param {Object} state - The Redux state.
  115. * @private
  116. * @returns {{
  117. * _audioMuted: boolean
  118. * }}
  119. */
  120. function _mapStateToProps(state): Object {
  121. const tracks = state['features/base/tracks'];
  122. return {
  123. _audioMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO)
  124. };
  125. }
  126. export default translate(connect(_mapStateToProps)(AudioMuteButton));