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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. accessibilityLabel = 'Audio mute';
  24. /**
  25. * Initializes a new {@code AudioMuteButton} instance.
  26. *
  27. * @param {Props} props - The React {@code Component} props to initialize
  28. * the new {@code AudioMuteButton} instance with.
  29. */
  30. constructor(props: Props) {
  31. super(props);
  32. this.state = {
  33. audioAvailable: false,
  34. audioMuted: true
  35. };
  36. // Bind event handlers so they are only bound once per instance.
  37. this._audioAvailabilityListener
  38. = this._audioAvailabilityListener.bind(this);
  39. this._audioMutedListener = this._audioMutedListener.bind(this);
  40. }
  41. /**
  42. * Sets mouse move listener and initial toolbar timeout.
  43. *
  44. * @inheritdoc
  45. * @returns {void}
  46. */
  47. componentDidMount() {
  48. api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
  49. api.on('audioMuteStatusChanged', this._audioMutedListener);
  50. Promise.all([
  51. api.isAudioAvailable(),
  52. api.isAudioMuted()
  53. ])
  54. .then(([ audioAvailable, audioMuted ]) =>
  55. this.setState({
  56. audioAvailable,
  57. audioMuted
  58. }))
  59. .catch(console.error);
  60. }
  61. /**
  62. * Removes all listeners.
  63. *
  64. * @inheritdoc
  65. * @returns {void}
  66. */
  67. componentWillUnmount() {
  68. api.removeListener(
  69. 'audioAvailabilityChanged',
  70. this._audioAvailabilityListener);
  71. api.removeListener(
  72. 'audioMuteStatusChanged',
  73. this._audioMutedListener);
  74. }
  75. _audioAvailabilityListener: ({ available: boolean }) => void;
  76. /**
  77. * Handles audio available api events.
  78. *
  79. * @param {{ available: boolean }} status - The new available status.
  80. * @returns {void}
  81. */
  82. _audioAvailabilityListener({ available }) {
  83. this.setState({ audioAvailable: available });
  84. }
  85. _audioMutedListener: ({ muted: boolean }) => void;
  86. /**
  87. * Handles audio muted api events.
  88. *
  89. * @param {{ muted: boolean }} status - The new muted status.
  90. * @returns {void}
  91. */
  92. _audioMutedListener({ muted }) {
  93. this.setState({ audioMuted: muted });
  94. }
  95. /**
  96. * Indicates if audio is currently muted ot nor.
  97. *
  98. * @override
  99. * @protected
  100. * @returns {boolean}
  101. */
  102. _isAudioMuted() {
  103. return this.state.audioMuted;
  104. }
  105. /**
  106. * Indicates whether this button is disabled or not.
  107. *
  108. * @override
  109. * @protected
  110. * @returns {boolean}
  111. */
  112. _isDisabled() {
  113. return !this.state.audioAvailable;
  114. }
  115. /**
  116. * Changes the muted state.
  117. *
  118. * @override
  119. * @param {boolean} audioMuted - Whether audio should be muted or not.
  120. * @protected
  121. * @returns {void}
  122. */
  123. _setAudioMuted(audioMuted: boolean) { // eslint-disable-line no-unused-vars
  124. this.state.audioAvailable && api.executeCommand('toggleAudio');
  125. }
  126. }