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

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