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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. avatarURL: string,
  57. displayName: string,
  58. isVideoDisplayed: boolean,
  59. videoAvailable: boolean,
  60. videoMuted: boolean,
  61. visible: boolean
  62. };
  63. /**
  64. * Represents the always on top page.
  65. *
  66. * @class AlwaysOnTop
  67. * @extends Component
  68. */
  69. export default class AlwaysOnTop extends Component<*, State> {
  70. _hovered: boolean;
  71. /**
  72. * Initializes new AlwaysOnTop instance.
  73. *
  74. * @param {*} props - The read-only properties with which the new instance
  75. * is to be initialized.
  76. */
  77. constructor(props: *) {
  78. super(props);
  79. this.state = {
  80. visible: true,
  81. audioMuted: false,
  82. videoMuted: false,
  83. audioAvailable: false,
  84. videoAvailable: false,
  85. displayName: '',
  86. isVideoDisplayed: true,
  87. avatarURL: ''
  88. };
  89. // Bind event handlers so they are only bound once per instance.
  90. this._audioAvailabilityListener
  91. = this._audioAvailabilityListener.bind(this);
  92. this._audioMutedListener = this._audioMutedListener.bind(this);
  93. this._avatarChangedListener = this._avatarChangedListener.bind(this);
  94. this._largeVideoChangedListener
  95. = this._largeVideoChangedListener.bind(this);
  96. this._displayNameChangedListener
  97. = this._displayNameChangedListener.bind(this);
  98. this._mouseMove = this._mouseMove.bind(this);
  99. this._onMouseOut = this._onMouseOut.bind(this);
  100. this._onMouseOver = this._onMouseOver.bind(this);
  101. this._videoAvailabilityListener
  102. = this._videoAvailabilityListener.bind(this);
  103. this._videoMutedListener = this._videoMutedListener.bind(this);
  104. }
  105. _audioAvailabilityListener: ({ available: boolean }) => void;
  106. /**
  107. * Handles audio available api events.
  108. *
  109. * @param {{ available: boolean }} status - The new available status.
  110. * @returns {void}
  111. */
  112. _audioAvailabilityListener({ available }) {
  113. this.setState({ audioAvailable: available });
  114. }
  115. _audioMutedListener: ({ muted: boolean }) => void;
  116. /**
  117. * Handles audio muted api events.
  118. *
  119. * @param {{ muted: boolean }} status - The new muted status.
  120. * @returns {void}
  121. */
  122. _audioMutedListener({ muted }) {
  123. this.setState({ audioMuted: muted });
  124. }
  125. _avatarChangedListener: () => void;
  126. /**
  127. * Handles avatar changed api events.
  128. *
  129. * @returns {void}
  130. */
  131. _avatarChangedListener({ avatarURL, id }) {
  132. if (api._getOnStageParticipant() !== id) {
  133. return;
  134. }
  135. if (avatarURL !== this.state.avatarURL) {
  136. this.setState({ avatarURL });
  137. }
  138. }
  139. _displayNameChangedListener: () => void;
  140. /**
  141. * Handles display name changed api events.
  142. *
  143. * @returns {void}
  144. */
  145. _displayNameChangedListener({ formattedDisplayName, id }) {
  146. if (api._getOnStageParticipant() !== id) {
  147. return;
  148. }
  149. if (formattedDisplayName !== this.state.displayName) {
  150. this.setState({ displayName: formattedDisplayName });
  151. }
  152. }
  153. /**
  154. * Hides the toolbar after a timeout.
  155. *
  156. * @returns {void}
  157. */
  158. _hideToolbarAfterTimeout() {
  159. setTimeout(() => {
  160. if (this._hovered) {
  161. this._hideToolbarAfterTimeout();
  162. return;
  163. }
  164. this.setState({ visible: false });
  165. }, TOOLBAR_TIMEOUT);
  166. }
  167. _largeVideoChangedListener: () => void;
  168. /**
  169. * Handles large video changed api events.
  170. *
  171. * @returns {void}
  172. */
  173. _largeVideoChangedListener() {
  174. const userID = api._getOnStageParticipant();
  175. const displayName = api._getFormattedDisplayName(userID);
  176. const avatarURL = api.getAvatarURL(userID);
  177. const isVideoDisplayed = Boolean(api._getLargeVideo());
  178. this.setState({
  179. avatarURL,
  180. displayName,
  181. isVideoDisplayed
  182. });
  183. }
  184. _mouseMove: () => void;
  185. /**
  186. * Handles mouse move events.
  187. *
  188. * @returns {void}
  189. */
  190. _mouseMove() {
  191. if (!this.state.visible) {
  192. this.setState({ visible: true });
  193. }
  194. }
  195. _onMouseOut: () => void;
  196. /**
  197. * Toolbar mouse out handler.
  198. *
  199. * @returns {void}
  200. */
  201. _onMouseOut() {
  202. this._hovered = false;
  203. }
  204. _onMouseOver: () => void;
  205. /**
  206. * Toolbar mouse over handler.
  207. *
  208. * @returns {void}
  209. */
  210. _onMouseOver() {
  211. this._hovered = true;
  212. }
  213. _videoAvailabilityListener: ({ available: boolean }) => void;
  214. /**
  215. * Renders display name and avatar for the on stage participant.
  216. *
  217. * @returns {ReactElement}
  218. */
  219. _renderVideoNotAvailableScreen() {
  220. const { avatarURL, displayName, isVideoDisplayed } = this.state;
  221. if (isVideoDisplayed) {
  222. return null;
  223. }
  224. return (
  225. <div id = 'videoNotAvailableScreen'>
  226. {
  227. avatarURL
  228. ? <div id = 'avatarContainer'>
  229. <img
  230. id = 'avatar'
  231. src = { avatarURL } />
  232. </div>
  233. : null
  234. }
  235. <div
  236. className = 'displayname'
  237. id = 'displayname'>
  238. { displayName }
  239. </div>
  240. </div>
  241. );
  242. }
  243. /**
  244. * Handles audio available api events.
  245. *
  246. * @param {{ available: boolean }} status - The new available status.
  247. * @returns {void}
  248. */
  249. _videoAvailabilityListener({ available }) {
  250. this.setState({ videoAvailable: available });
  251. }
  252. _videoMutedListener: ({ muted: boolean }) => void;
  253. /**
  254. * Handles video muted api events.
  255. *
  256. * @param {{ muted: boolean }} status - The new muted status.
  257. * @returns {void}
  258. */
  259. _videoMutedListener({ muted }) {
  260. this.setState({ videoMuted: muted });
  261. }
  262. /**
  263. * Sets mouse move listener and initial toolbar timeout.
  264. *
  265. * @inheritdoc
  266. * @returns {void}
  267. */
  268. componentDidMount() {
  269. api.on('audioMuteStatusChanged', this._audioMutedListener);
  270. api.on('videoMuteStatusChanged', this._videoMutedListener);
  271. api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
  272. api.on('videoAvailabilityChanged', this._videoAvailabilityListener);
  273. api.on('largeVideoChanged', this._largeVideoChangedListener);
  274. api.on('displayNameChange', this._displayNameChangedListener);
  275. api.on('avatarChanged', this._avatarChangedListener);
  276. this._largeVideoChangedListener();
  277. Promise.all([
  278. api.isAudioMuted(),
  279. api.isVideoMuted(),
  280. api.isAudioAvailable(),
  281. api.isVideoAvailable()
  282. ])
  283. .then(([
  284. audioMuted = false,
  285. videoMuted = false,
  286. audioAvailable = false,
  287. videoAvailable = false
  288. ]) =>
  289. this.setState({
  290. audioMuted,
  291. videoMuted,
  292. audioAvailable,
  293. videoAvailable
  294. })
  295. )
  296. .catch(console.error);
  297. window.addEventListener('mousemove', this._mouseMove);
  298. this._hideToolbarAfterTimeout();
  299. }
  300. /**
  301. * Removes all listeners.
  302. *
  303. * @inheritdoc
  304. * @returns {void}
  305. */
  306. componentWillUnmount() {
  307. api.removeListener('audioMuteStatusChanged',
  308. this._audioMutedListener);
  309. api.removeListener('videoMuteStatusChanged',
  310. this._videoMutedListener);
  311. api.removeListener('audioAvailabilityChanged',
  312. this._audioAvailabilityListener);
  313. api.removeListener('videoAvailabilityChanged',
  314. this._videoAvailabilityListener);
  315. api.removeListener('largeVideoChanged',
  316. this._largeVideoChangedListener);
  317. api.removeListener('displayNameChange',
  318. this._displayNameChangedListener);
  319. api.removeListener('avatarChanged', this._avatarChangedListener);
  320. window.removeEventListener('mousemove', this._mouseMove);
  321. }
  322. /**
  323. * Sets a timeout to hide the toolbar when the toolbar is shown.
  324. *
  325. * @inheritdoc
  326. * @returns {void}
  327. */
  328. componentWillUpdate(nextProps: *, nextState: State) {
  329. if (!this.state.visible && nextState.visible) {
  330. this._hideToolbarAfterTimeout();
  331. }
  332. }
  333. /**
  334. * Implements React's {@link Component#render()}.
  335. *
  336. * @inheritdoc
  337. * @returns {ReactElement}
  338. */
  339. render() {
  340. const className
  341. = `toolbar_primary always-on-top ${
  342. this.state.visible ? 'fadeIn' : 'fadeOut'}`;
  343. return (
  344. <div id = 'alwaysOnTop'>
  345. <StatelessToolbar
  346. className = { className }
  347. onMouseOut = { this._onMouseOut }
  348. onMouseOver = { this._onMouseOver }>
  349. {
  350. Object.entries(TOOLBAR_BUTTONS).map(
  351. ([ key, button ]) => {
  352. // XXX The following silences a couple of flow
  353. // errors:
  354. if (button === null
  355. || typeof button !== 'object') {
  356. return null;
  357. }
  358. const { onClick } = button;
  359. let enabled = false;
  360. let toggled = false;
  361. switch (key) {
  362. case 'microphone':
  363. enabled = this.state.audioAvailable;
  364. toggled = enabled
  365. ? this.state.audioMuted : true;
  366. break;
  367. case 'camera':
  368. enabled = this.state.videoAvailable;
  369. toggled = enabled
  370. ? this.state.videoMuted : true;
  371. break;
  372. default: // hangup button
  373. toggled = false;
  374. enabled = true;
  375. }
  376. const updatedButton = {
  377. ...button,
  378. enabled,
  379. toggled
  380. };
  381. return (
  382. <StatelessToolbarButton
  383. button = { updatedButton }
  384. key = { key }
  385. onClick = { onClick } />
  386. );
  387. }
  388. )
  389. }
  390. </StatelessToolbar>
  391. {
  392. this._renderVideoNotAvailableScreen()
  393. }
  394. </div>
  395. );
  396. }
  397. }