Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AudioMuteButton.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { connect } from 'react-redux';
  2. import { ACTION_SHORTCUT_TRIGGERED, AUDIO_MUTE, createShortcutEvent } from '../../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../../analytics/functions';
  4. import { IReduxState } from '../../app/types';
  5. import { AUDIO_MUTE_BUTTON_ENABLED } from '../../base/flags/constants';
  6. import { getFeatureFlag } from '../../base/flags/functions';
  7. import { translate } from '../../base/i18n/functions';
  8. import { MEDIA_TYPE } from '../../base/media/constants';
  9. import AbstractAudioMuteButton from '../../base/toolbox/components/AbstractAudioMuteButton';
  10. import AbstractButton, { IProps as AbstractButtonProps } from '../../base/toolbox/components/AbstractButton';
  11. import { isLocalTrackMuted } from '../../base/tracks/functions';
  12. import { muteLocal } from '../../video-menu/actions';
  13. import { isAudioMuteButtonDisabled } from '../functions';
  14. /**
  15. * The type of the React {@code Component} props of {@link AudioMuteButton}.
  16. */
  17. interface IProps extends AbstractButtonProps {
  18. /**
  19. * Whether audio is currently muted or not.
  20. */
  21. _audioMuted: boolean;
  22. /**
  23. * Whether the button is disabled.
  24. */
  25. _disabled: boolean;
  26. }
  27. /**
  28. * Component that renders a toolbar button for toggling audio mute.
  29. *
  30. * @augments AbstractAudioMuteButton
  31. */
  32. class AudioMuteButton extends AbstractAudioMuteButton<IProps> {
  33. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  34. toggledAccessibilityLabel = 'toolbar.accessibilityLabel.unmute';
  35. label = 'toolbar.mute';
  36. tooltip = 'toolbar.mute';
  37. toggledTooltip = 'toolbar.unmute';
  38. /**
  39. * Initializes a new {@code AudioMuteButton} instance.
  40. *
  41. * @param {IProps} props - The read-only React {@code Component} props with
  42. * which the new instance is to be initialized.
  43. */
  44. constructor(props: IProps) {
  45. super(props);
  46. // Bind event handlers so they are only bound once per instance.
  47. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  48. }
  49. /**
  50. * Registers the keyboard shortcut that toggles the audio muting.
  51. *
  52. * @inheritdoc
  53. * @returns {void}
  54. */
  55. componentDidMount() {
  56. typeof APP === 'undefined'
  57. || APP.keyboardshortcut.registerShortcut(
  58. 'M',
  59. null,
  60. this._onKeyboardShortcut,
  61. 'keyboardShortcuts.mute');
  62. }
  63. /**
  64. * Unregisters the keyboard shortcut that toggles the audio muting.
  65. *
  66. * @inheritdoc
  67. * @returns {void}
  68. */
  69. componentWillUnmount() {
  70. typeof APP === 'undefined'
  71. || APP.keyboardshortcut.unregisterShortcut('M');
  72. }
  73. /**
  74. * Indicates if audio is currently muted or not.
  75. *
  76. * @override
  77. * @protected
  78. * @returns {boolean}
  79. */
  80. _isAudioMuted() {
  81. return this.props._audioMuted;
  82. }
  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. // Ignore keyboard shortcuts if the audio button is disabled.
  92. if (this._isDisabled()) {
  93. return;
  94. }
  95. sendAnalytics(
  96. createShortcutEvent(
  97. AUDIO_MUTE,
  98. ACTION_SHORTCUT_TRIGGERED,
  99. { enable: !this._isAudioMuted() }));
  100. AbstractButton.prototype._onClick.call(this);
  101. }
  102. /**
  103. * Changes the muted state.
  104. *
  105. * @param {boolean} audioMuted - Whether audio should be muted or not.
  106. * @protected
  107. * @returns {void}
  108. */
  109. _setAudioMuted(audioMuted: boolean) {
  110. this.props.dispatch(muteLocal(audioMuted, MEDIA_TYPE.AUDIO));
  111. }
  112. /**
  113. * Return a boolean value indicating if this button is disabled or not.
  114. *
  115. * @returns {boolean}
  116. */
  117. _isDisabled() {
  118. return this.props._disabled;
  119. }
  120. }
  121. /**
  122. * Maps (parts of) the redux state to the associated props for the
  123. * {@code AudioMuteButton} component.
  124. *
  125. * @param {Object} state - The Redux state.
  126. * @private
  127. * @returns {{
  128. * _audioMuted: boolean,
  129. * _disabled: boolean
  130. * }}
  131. */
  132. function _mapStateToProps(state: IReduxState) {
  133. const _audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  134. const _disabled = isAudioMuteButtonDisabled(state);
  135. const enabledFlag = getFeatureFlag(state, AUDIO_MUTE_BUTTON_ENABLED, true);
  136. return {
  137. _audioMuted,
  138. _disabled,
  139. visible: enabledFlag
  140. };
  141. }
  142. export default translate(connect(_mapStateToProps)(AudioMuteButton));