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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // @flow
  2. // We need to reference these files directly to avoid loading things that are not available
  3. // in this environment (e.g. JitsiMeetJS or interfaceConfig)
  4. import AbstractAudioMuteButton from '../base/toolbox/components/AbstractAudioMuteButton';
  5. import type { Props } from '../base/toolbox/components/AbstractButton';
  6. const { api } = window.alwaysOnTop;
  7. const logger = require('jitsi-meet-logger').getLogger(__filename);
  8. /**
  9. * The type of the React {@code Component} state of {@link AudioMuteButton}.
  10. */
  11. type State = {
  12. /**
  13. * Whether audio is available is not.
  14. */
  15. audioAvailable: boolean,
  16. /**
  17. * Whether audio is muted or not.
  18. */
  19. audioMuted: boolean
  20. };
  21. /**
  22. * Stateless "mute/unmute audio" button for the Always-on-Top windows.
  23. */
  24. export default class AudioMuteButton
  25. extends AbstractAudioMuteButton<Props, State> {
  26. accessibilityLabel = 'Audio mute';
  27. /**
  28. * Initializes a new {@code AudioMuteButton} instance.
  29. *
  30. * @param {Props} props - The React {@code Component} props to initialize
  31. * the new {@code AudioMuteButton} instance with.
  32. */
  33. constructor(props: Props) {
  34. super(props);
  35. this.state = {
  36. audioAvailable: false,
  37. audioMuted: true
  38. };
  39. // Bind event handlers so they are only bound once per instance.
  40. this._audioAvailabilityListener
  41. = this._audioAvailabilityListener.bind(this);
  42. this._audioMutedListener = this._audioMutedListener.bind(this);
  43. }
  44. /**
  45. * Sets mouse move listener and initial toolbar timeout.
  46. *
  47. * @inheritdoc
  48. * @returns {void}
  49. */
  50. componentDidMount() {
  51. api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
  52. api.on('audioMuteStatusChanged', this._audioMutedListener);
  53. Promise.all([
  54. api.isAudioAvailable(),
  55. api.isAudioMuted()
  56. ])
  57. .then(([ audioAvailable, audioMuted ]) =>
  58. this.setState({
  59. audioAvailable,
  60. audioMuted
  61. }))
  62. .catch(logger.error);
  63. }
  64. /**
  65. * Removes all listeners.
  66. *
  67. * @inheritdoc
  68. * @returns {void}
  69. */
  70. componentWillUnmount() {
  71. api.removeListener(
  72. 'audioAvailabilityChanged',
  73. this._audioAvailabilityListener);
  74. api.removeListener(
  75. 'audioMuteStatusChanged',
  76. this._audioMutedListener);
  77. }
  78. _audioAvailabilityListener: ({ available: boolean }) => void;
  79. /**
  80. * Handles audio available api events.
  81. *
  82. * @param {{ available: boolean }} status - The new available status.
  83. * @returns {void}
  84. */
  85. _audioAvailabilityListener({ available }) {
  86. this.setState({ audioAvailable: available });
  87. }
  88. _audioMutedListener: ({ muted: boolean }) => void;
  89. /**
  90. * Handles audio muted api events.
  91. *
  92. * @param {{ muted: boolean }} status - The new muted status.
  93. * @returns {void}
  94. */
  95. _audioMutedListener({ muted }) {
  96. this.setState({ audioMuted: muted });
  97. }
  98. /**
  99. * Indicates if audio is currently muted ot nor.
  100. *
  101. * @override
  102. * @protected
  103. * @returns {boolean}
  104. */
  105. _isAudioMuted() {
  106. return this.state.audioMuted;
  107. }
  108. /**
  109. * Indicates whether this button is disabled or not.
  110. *
  111. * @override
  112. * @protected
  113. * @returns {boolean}
  114. */
  115. _isDisabled() {
  116. return !this.state.audioAvailable;
  117. }
  118. /**
  119. * Changes the muted state.
  120. *
  121. * @override
  122. * @param {boolean} audioMuted - Whether audio should be muted or not.
  123. * @protected
  124. * @returns {void}
  125. */
  126. _setAudioMuted(audioMuted: boolean) { // eslint-disable-line no-unused-vars
  127. this.state.audioAvailable && api.executeCommand('toggleAudio');
  128. }
  129. }