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.

AlwaysOnTop.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // @flow
  2. import React, { Component } from 'react';
  3. import StatelessToolbar from '../toolbox/components/StatelessToolbar';
  4. import StatelessToolbarButton
  5. from '../toolbox/components/StatelessToolbarButton';
  6. const { api } = window.alwaysOnTop;
  7. /**
  8. * Map with toolbar button descriptors.
  9. */
  10. const TOOLBAR_BUTTONS = {
  11. /**
  12. * The descriptor of the camera toolbar button.
  13. */
  14. camera: {
  15. classNames: [ 'button', 'icon-camera' ],
  16. enabled: true,
  17. id: 'toolbar_button_camera',
  18. onClick() {
  19. api.executeCommand('toggleVideo');
  20. }
  21. },
  22. /**
  23. * The descriptor of the toolbar button which hangs up the call/conference.
  24. */
  25. hangup: {
  26. classNames: [ 'button', 'icon-hangup', 'button_hangup' ],
  27. enabled: true,
  28. id: 'toolbar_button_hangup',
  29. onClick() {
  30. api.executeCommand('hangup');
  31. window.close();
  32. }
  33. },
  34. /**
  35. * The descriptor of the microphone toolbar button.
  36. */
  37. microphone: {
  38. classNames: [ 'button', 'icon-microphone' ],
  39. enabled: true,
  40. id: 'toolbar_button_mute',
  41. onClick() {
  42. api.executeCommand('toggleAudio');
  43. }
  44. }
  45. };
  46. /**
  47. * The timeout in ms for hidding the toolbar.
  48. */
  49. const TOOLBAR_TIMEOUT = 4000;
  50. /**
  51. * The type of the React {@code Component} state of {@link FeedbackButton}.
  52. */
  53. type State = {
  54. audioAvailable: boolean,
  55. audioMuted: boolean,
  56. videoAvailable: boolean,
  57. videoMuted: boolean,
  58. visible: boolean
  59. };
  60. /**
  61. * Represents the always on top page.
  62. *
  63. * @class AlwaysOnTop
  64. * @extends Component
  65. */
  66. export default class AlwaysOnTop extends Component<*, State> {
  67. _hovered: boolean;
  68. /**
  69. * Initializes new AlwaysOnTop instance.
  70. *
  71. * @param {*} props - The read-only properties with which the new instance
  72. * is to be initialized.
  73. */
  74. constructor(props: *) {
  75. super(props);
  76. this.state = {
  77. visible: true,
  78. audioMuted: false,
  79. videoMuted: false,
  80. audioAvailable: false,
  81. videoAvailable: false
  82. };
  83. // Bind event handlers so they are only bound once per instance.
  84. this._audioAvailabilityListener
  85. = this._audioAvailabilityListener.bind(this);
  86. this._audioMutedListener = this._audioMutedListener.bind(this);
  87. this._mouseMove = this._mouseMove.bind(this);
  88. this._onMouseOut = this._onMouseOut.bind(this);
  89. this._onMouseOver = this._onMouseOver.bind(this);
  90. this._videoAvailabilityListener
  91. = this._videoAvailabilityListener.bind(this);
  92. this._videoMutedListener = this._videoMutedListener.bind(this);
  93. }
  94. _audioAvailabilityListener: ({ available: boolean }) => void;
  95. /**
  96. * Handles audio available api events.
  97. *
  98. * @param {{ available: boolean }} status - The new available status.
  99. * @returns {void}
  100. */
  101. _audioAvailabilityListener({ available }) {
  102. this.setState({ audioAvailable: available });
  103. }
  104. _audioMutedListener: ({ muted: boolean }) => void;
  105. /**
  106. * Handles audio muted api events.
  107. *
  108. * @param {{ muted: boolean }} status - The new muted status.
  109. * @returns {void}
  110. */
  111. _audioMutedListener({ muted }) {
  112. this.setState({ audioMuted: muted });
  113. }
  114. /**
  115. * Hides the toolbar after a timeout.
  116. *
  117. * @returns {void}
  118. */
  119. _hideToolbarAfterTimeout() {
  120. setTimeout(() => {
  121. if (this._hovered) {
  122. this._hideToolbarAfterTimeout();
  123. return;
  124. }
  125. this.setState({ visible: false });
  126. }, TOOLBAR_TIMEOUT);
  127. }
  128. _mouseMove: () => void;
  129. /**
  130. * Handles mouse move events.
  131. *
  132. * @returns {void}
  133. */
  134. _mouseMove() {
  135. if (!this.state.visible) {
  136. this.setState({ visible: true });
  137. }
  138. }
  139. _onMouseOut: () => void;
  140. /**
  141. * Toolbar mouse out handler.
  142. *
  143. * @returns {void}
  144. */
  145. _onMouseOut() {
  146. this._hovered = false;
  147. }
  148. _onMouseOver: () => void;
  149. /**
  150. * Toolbar mouse over handler.
  151. *
  152. * @returns {void}
  153. */
  154. _onMouseOver() {
  155. this._hovered = true;
  156. }
  157. _videoAvailabilityListener: ({ available: boolean }) => void;
  158. /**
  159. * Handles audio available api events.
  160. *
  161. * @param {{ available: boolean }} status - The new available status.
  162. * @returns {void}
  163. */
  164. _videoAvailabilityListener({ available }) {
  165. this.setState({ videoAvailable: available });
  166. }
  167. _videoMutedListener: ({ muted: boolean }) => void;
  168. /**
  169. * Handles video muted api events.
  170. *
  171. * @param {{ muted: boolean }} status - The new muted status.
  172. * @returns {void}
  173. */
  174. _videoMutedListener({ muted }) {
  175. this.setState({ videoMuted: muted });
  176. }
  177. /**
  178. * Sets mouse move listener and initial toolbar timeout.
  179. *
  180. * @inheritdoc
  181. * @returns {void}
  182. */
  183. componentDidMount() {
  184. api.on('audioMuteStatusChanged', this._audioMutedListener);
  185. api.on('videoMuteStatusChanged', this._videoMutedListener);
  186. api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
  187. api.on('videoAvailabilityChanged', this._videoAvailabilityListener);
  188. Promise.all([
  189. api.isAudioMuted(),
  190. api.isVideoMuted(),
  191. api.isAudioAvailable(),
  192. api.isVideoAvailable()
  193. ])
  194. .then(([
  195. audioMuted = false,
  196. videoMuted = false,
  197. audioAvailable = false,
  198. videoAvailable = false
  199. ]) =>
  200. this.setState({
  201. audioMuted,
  202. videoMuted,
  203. audioAvailable,
  204. videoAvailable
  205. })
  206. )
  207. .catch(console.error);
  208. window.addEventListener('mousemove', this._mouseMove);
  209. this._hideToolbarAfterTimeout();
  210. }
  211. /**
  212. * Removes all listeners.
  213. *
  214. * @inheritdoc
  215. * @returns {void}
  216. */
  217. componentWillUnmount() {
  218. api.removeListener('audioMuteStatusChanged',
  219. this._audioMutedListener);
  220. api.removeListener('videoMuteStatusChanged',
  221. this._videoMutedListener);
  222. api.removeListener('audioAvailabilityChanged',
  223. this._audioAvailabilityListener);
  224. api.removeListener('videoAvailabilityChanged',
  225. this._videoAvailabilityListener);
  226. window.removeEventListener('mousemove', this._mouseMove);
  227. }
  228. /**
  229. * Sets a timeout to hide the toolbar when the toolbar is shown.
  230. *
  231. * @inheritdoc
  232. * @returns {void}
  233. */
  234. componentWillUpdate(nextProps: *, nextState: State) {
  235. if (!this.state.visible && nextState.visible) {
  236. this._hideToolbarAfterTimeout();
  237. }
  238. }
  239. /**
  240. * Implements React's {@link Component#render()}.
  241. *
  242. * @inheritdoc
  243. * @returns {ReactElement}
  244. */
  245. render() {
  246. const className
  247. = `toolbar_primary always-on-top ${
  248. this.state.visible ? 'fadeIn' : 'fadeOut'}`;
  249. return (
  250. <StatelessToolbar
  251. className = { className }
  252. onMouseOut = { this._onMouseOut }
  253. onMouseOver = { this._onMouseOver }>
  254. {
  255. Object.entries(TOOLBAR_BUTTONS).map(([ key, button ]) => {
  256. // XXX The following silences a couple of flow errors:
  257. if (button === null || typeof button !== 'object') {
  258. return null;
  259. }
  260. const { onClick } = button;
  261. let enabled = false;
  262. let toggled = false;
  263. switch (key) {
  264. case 'microphone':
  265. enabled = this.state.audioAvailable;
  266. toggled = enabled ? this.state.audioMuted : true;
  267. break;
  268. case 'camera':
  269. enabled = this.state.videoAvailable;
  270. toggled = enabled ? this.state.videoMuted : true;
  271. break;
  272. default: // hangup button
  273. toggled = false;
  274. enabled = true;
  275. }
  276. const updatedButton = {
  277. ...button,
  278. enabled,
  279. toggled
  280. };
  281. return (
  282. <StatelessToolbarButton
  283. button = { updatedButton }
  284. key = { key }
  285. onClick = { onClick } />
  286. );
  287. })
  288. }
  289. </StatelessToolbar>
  290. );
  291. }
  292. }