2018-04-17 20:18:02 +00:00
|
|
|
// @flow
|
|
|
|
|
2022-04-04 10:38:49 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2019-07-19 13:26:32 +00:00
|
|
|
// We need to reference these files directly to avoid loading things that are not available
|
|
|
|
// in this environment (e.g. JitsiMeetJS or interfaceConfig)
|
2022-04-04 10:38:49 +00:00
|
|
|
import { IconMicrophoneEmpty, IconMicrophoneEmptySlash } from '../base/icons';
|
2019-07-19 13:26:32 +00:00
|
|
|
import type { Props } from '../base/toolbox/components/AbstractButton';
|
2018-04-17 20:18:02 +00:00
|
|
|
|
2022-04-04 10:38:49 +00:00
|
|
|
import ToolbarButton from './ToolbarButton';
|
|
|
|
|
2018-04-17 20:18:02 +00:00
|
|
|
const { api } = window.alwaysOnTop;
|
|
|
|
|
2018-05-03 17:36:29 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link AudioMuteButton}.
|
|
|
|
*/
|
2018-04-17 20:18:02 +00:00
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether audio is available is not.
|
|
|
|
*/
|
|
|
|
audioAvailable: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether audio is muted or not.
|
|
|
|
*/
|
|
|
|
audioMuted: boolean
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-05-03 17:36:29 +00:00
|
|
|
* Stateless "mute/unmute audio" button for the Always-on-Top windows.
|
2018-04-17 20:18:02 +00:00
|
|
|
*/
|
2022-04-04 10:38:49 +00:00
|
|
|
export default class AudioMuteButton extends Component<Props, State> {
|
|
|
|
icon = IconMicrophoneEmpty;
|
|
|
|
toggledIcon = IconMicrophoneEmptySlash;
|
2018-06-07 20:32:18 +00:00
|
|
|
accessibilityLabel = 'Audio mute';
|
|
|
|
|
2018-04-17 20:18:02 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code AudioMuteButton} instance.
|
|
|
|
*
|
|
|
|
* @param {Props} props - The React {@code Component} props to initialize
|
|
|
|
* the new {@code AudioMuteButton} instance with.
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
audioAvailable: false,
|
|
|
|
audioMuted: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once per instance.
|
|
|
|
this._audioAvailabilityListener
|
|
|
|
= this._audioAvailabilityListener.bind(this);
|
|
|
|
this._audioMutedListener = this._audioMutedListener.bind(this);
|
2022-04-04 10:38:49 +00:00
|
|
|
this._onClick = this._onClick.bind(this);
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets mouse move listener and initial toolbar timeout.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
|
|
|
|
api.on('audioMuteStatusChanged', this._audioMutedListener);
|
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
api.isAudioAvailable(),
|
2022-04-18 13:19:19 +00:00
|
|
|
api.isAudioMuted(),
|
2022-04-26 12:32:18 +00:00
|
|
|
api.isAudioDisabled?.() || Promise.resolve(false)
|
2018-04-17 20:18:02 +00:00
|
|
|
])
|
2022-04-26 12:32:18 +00:00
|
|
|
.then(([ audioAvailable, audioMuted, audioDisabled ]) =>
|
2018-04-17 20:18:02 +00:00
|
|
|
this.setState({
|
2022-04-26 12:32:18 +00:00
|
|
|
audioAvailable: audioAvailable && !audioDisabled,
|
2018-04-17 20:18:02 +00:00
|
|
|
audioMuted
|
2018-05-03 17:36:29 +00:00
|
|
|
}))
|
2019-09-06 11:52:40 +00:00
|
|
|
.catch(console.error);
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all listeners.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
2018-05-03 17:36:29 +00:00
|
|
|
api.removeListener(
|
|
|
|
'audioAvailabilityChanged',
|
2018-04-17 20:18:02 +00:00
|
|
|
this._audioAvailabilityListener);
|
2018-05-03 17:36:29 +00:00
|
|
|
api.removeListener(
|
|
|
|
'audioMuteStatusChanged',
|
2018-04-17 20:18:02 +00:00
|
|
|
this._audioMutedListener);
|
|
|
|
}
|
|
|
|
|
2018-05-03 17:36:29 +00:00
|
|
|
_audioAvailabilityListener: ({ available: boolean }) => void;
|
|
|
|
|
2018-04-17 20:18:02 +00:00
|
|
|
/**
|
2018-05-03 17:36:29 +00:00
|
|
|
* Handles audio available api events.
|
2018-04-17 20:18:02 +00:00
|
|
|
*
|
2018-05-03 17:36:29 +00:00
|
|
|
* @param {{ available: boolean }} status - The new available status.
|
|
|
|
* @returns {void}
|
2018-04-17 20:18:02 +00:00
|
|
|
*/
|
2018-05-03 17:36:29 +00:00
|
|
|
_audioAvailabilityListener({ available }) {
|
|
|
|
this.setState({ audioAvailable: available });
|
|
|
|
}
|
|
|
|
|
|
|
|
_audioMutedListener: ({ muted: boolean }) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles audio muted api events.
|
|
|
|
*
|
|
|
|
* @param {{ muted: boolean }} status - The new muted status.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_audioMutedListener({ muted }) {
|
|
|
|
this.setState({ audioMuted: muted });
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-16 15:59:33 +00:00
|
|
|
* Indicates if audio is currently muted or not.
|
2018-04-17 20:18:02 +00:00
|
|
|
*
|
|
|
|
* @override
|
2018-05-11 02:10:26 +00:00
|
|
|
* @protected
|
2018-04-17 20:18:02 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_isAudioMuted() {
|
|
|
|
return this.state.audioMuted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-03 17:36:29 +00:00
|
|
|
* Indicates whether this button is disabled or not.
|
2018-04-17 20:18:02 +00:00
|
|
|
*
|
2018-05-03 17:36:29 +00:00
|
|
|
* @override
|
2018-05-11 02:10:26 +00:00
|
|
|
* @protected
|
2018-05-03 17:36:29 +00:00
|
|
|
* @returns {boolean}
|
2018-04-17 20:18:02 +00:00
|
|
|
*/
|
2018-05-03 17:36:29 +00:00
|
|
|
_isDisabled() {
|
|
|
|
return !this.state.audioAvailable;
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-03 17:36:29 +00:00
|
|
|
* Changes the muted state.
|
2018-04-17 20:18:02 +00:00
|
|
|
*
|
2018-05-11 02:10:26 +00:00
|
|
|
* @override
|
2018-05-03 17:36:29 +00:00
|
|
|
* @param {boolean} audioMuted - Whether audio should be muted or not.
|
2018-05-11 02:10:26 +00:00
|
|
|
* @protected
|
2018-04-17 20:18:02 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-05-03 17:36:29 +00:00
|
|
|
_setAudioMuted(audioMuted: boolean) { // eslint-disable-line no-unused-vars
|
|
|
|
this.state.audioAvailable && api.executeCommand('toggleAudio');
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|
2022-04-04 10:38:49 +00:00
|
|
|
|
|
|
|
_onClick: () => {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles clicking / pressing the button, and toggles the audio mute state
|
|
|
|
* accordingly.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onClick() {
|
|
|
|
this._setAudioMuted(!this._isAudioMuted());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const toggled = this._isAudioMuted();
|
|
|
|
|
|
|
|
return (<ToolbarButton
|
|
|
|
accessibilityLabel = { this.accessibilityLabel }
|
|
|
|
disabled = { this._isDisabled() }
|
|
|
|
icon = { toggled ? this.toggledIcon : this.icon }
|
|
|
|
onClick = { this._onClick }
|
|
|
|
toggled = { toggled } />);
|
|
|
|
}
|
2018-04-17 20:18:02 +00:00
|
|
|
}
|