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.

VideoMuteButton.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @flow
  2. import { AbstractVideoMuteButton } 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 VideoMuteButton}.
  7. */
  8. type State = {
  9. /**
  10. * Whether video is available is not.
  11. */
  12. videoAvailable: boolean,
  13. /**
  14. * Whether video is muted or not.
  15. */
  16. videoMuted: boolean
  17. };
  18. /**
  19. * Stateless "mute/unmute video" button for the Always-on-Top windows.
  20. */
  21. export default class VideoMuteButton
  22. extends AbstractVideoMuteButton<Props, State> {
  23. /**
  24. * Initializes a new {@code VideoMuteButton} instance.
  25. *
  26. * @param {Props} props - The React {@code Component} props to initialize
  27. * the new {@code VideoMuteButton} instance with.
  28. */
  29. constructor(props: Props) {
  30. super(props);
  31. this.state = {
  32. videoAvailable: false,
  33. videoMuted: true
  34. };
  35. // Bind event handlers so they are only bound once per instance.
  36. this._videoAvailabilityListener
  37. = this._videoAvailabilityListener.bind(this);
  38. this._videoMutedListener = this._videoMutedListener.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('videoAvailabilityChanged', this._videoAvailabilityListener);
  48. api.on('videoMuteStatusChanged', this._videoMutedListener);
  49. Promise.all([
  50. api.isVideoAvailable(),
  51. api.isVideoMuted()
  52. ])
  53. .then(([ videoAvailable, videoMuted ]) =>
  54. this.setState({
  55. videoAvailable,
  56. videoMuted
  57. }))
  58. .catch(console.error);
  59. }
  60. /**
  61. * Removes all listeners.
  62. *
  63. * @inheritdoc
  64. * @returns {void}
  65. */
  66. componentWillUnmount() {
  67. api.removeListener(
  68. 'videoAvailabilityChanged',
  69. this._videoAvailabilityListener);
  70. api.removeListener(
  71. 'videoMuteStatusChanged',
  72. this._videoMutedListener);
  73. }
  74. /**
  75. * Indicates whether this button is disabled or not.
  76. *
  77. * @override
  78. * @protected
  79. * @returns {boolean}
  80. */
  81. _isDisabled() {
  82. return !this.state.videoAvailable;
  83. }
  84. /**
  85. * Indicates if video is currently muted ot nor.
  86. *
  87. * @override
  88. * @protected
  89. * @returns {boolean}
  90. */
  91. _isVideoMuted() {
  92. return this.state.videoMuted;
  93. }
  94. /**
  95. * Changes the muted state.
  96. *
  97. * @override
  98. * @param {boolean} videoMuted - Whether video should be muted or not.
  99. * @protected
  100. * @returns {void}
  101. */
  102. _setVideoMuted(videoMuted: boolean) { // eslint-disable-line no-unused-vars
  103. this.state.videoAvailable && api.executeCommand('toggleVideo');
  104. }
  105. _videoAvailabilityListener: ({ available: boolean }) => void;
  106. /**
  107. * Handles video available api events.
  108. *
  109. * @param {{ available: boolean }} status - The new available status.
  110. * @returns {void}
  111. */
  112. _videoAvailabilityListener({ available }) {
  113. this.setState({ videoAvailable: available });
  114. }
  115. _videoMutedListener: ({ muted: boolean }) => void;
  116. /**
  117. * Handles video muted api events.
  118. *
  119. * @param {{ muted: boolean }} status - The new muted status.
  120. * @returns {void}
  121. */
  122. _videoMutedListener({ muted }) {
  123. this.setState({ videoMuted: muted });
  124. }
  125. }