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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // @flow
  2. import React, { Component } from 'react';
  3. // We need to reference these files directly to avoid loading things that are not available
  4. // in this environment (e.g. JitsiMeetJS or interfaceConfig)
  5. import StatelessAvatar from '../base/avatar/components/web/StatelessAvatar';
  6. import { getInitials } from '../base/avatar/functions';
  7. import Toolbar from './Toolbar';
  8. const { api } = window.alwaysOnTop;
  9. /**
  10. * The timeout in ms for hiding the toolbar.
  11. */
  12. const TOOLBAR_TIMEOUT = 4000;
  13. /**
  14. * The type of the React {@code Component} state of {@link AlwaysOnTop}.
  15. */
  16. type State = {
  17. avatarURL: string,
  18. displayName: string,
  19. formattedDisplayName: string,
  20. isVideoDisplayed: boolean,
  21. visible: boolean
  22. };
  23. /**
  24. * Represents the always on top page.
  25. *
  26. * @class AlwaysOnTop
  27. * @extends Component
  28. */
  29. export default class AlwaysOnTop extends Component<*, State> {
  30. _hovered: boolean;
  31. /**
  32. * Initializes a new {@code AlwaysOnTop} instance.
  33. *
  34. * @param {*} props - The read-only properties with which the new instance
  35. * is to be initialized.
  36. */
  37. constructor(props: *) {
  38. super(props);
  39. this.state = {
  40. avatarURL: '',
  41. displayName: '',
  42. formattedDisplayName: '',
  43. isVideoDisplayed: true,
  44. visible: true
  45. };
  46. // Bind event handlers so they are only bound once per instance.
  47. this._avatarChangedListener = this._avatarChangedListener.bind(this);
  48. this._displayNameChangedListener
  49. = this._displayNameChangedListener.bind(this);
  50. this._largeVideoChangedListener
  51. = this._largeVideoChangedListener.bind(this);
  52. this._mouseMove = this._mouseMove.bind(this);
  53. this._onMouseOut = this._onMouseOut.bind(this);
  54. this._onMouseOver = this._onMouseOver.bind(this);
  55. }
  56. _avatarChangedListener: () => void;
  57. /**
  58. * Handles avatar changed api events.
  59. *
  60. * @returns {void}
  61. */
  62. _avatarChangedListener({ avatarURL, id }) {
  63. if (api._getOnStageParticipant() === id
  64. && avatarURL !== this.state.avatarURL) {
  65. this.setState({ avatarURL });
  66. }
  67. }
  68. _displayNameChangedListener: () => void;
  69. /**
  70. * Handles display name changed api events.
  71. *
  72. * @returns {void}
  73. */
  74. _displayNameChangedListener({ displayname, formattedDisplayName, id }) {
  75. if (api._getOnStageParticipant() === id
  76. && (formattedDisplayName !== this.state.formattedDisplayName
  77. || displayname !== this.state.displayName)) {
  78. // I think the API has a typo using lowercase n for the displayname
  79. this.setState({
  80. displayName: displayname,
  81. formattedDisplayName
  82. });
  83. }
  84. }
  85. /**
  86. * Hides the toolbar after a timeout.
  87. *
  88. * @returns {void}
  89. */
  90. _hideToolbarAfterTimeout() {
  91. setTimeout(
  92. () => {
  93. if (this._hovered) {
  94. this._hideToolbarAfterTimeout();
  95. } else {
  96. this.setState({ visible: false });
  97. }
  98. },
  99. TOOLBAR_TIMEOUT);
  100. }
  101. _largeVideoChangedListener: () => void;
  102. /**
  103. * Handles large video changed api events.
  104. *
  105. * @returns {void}
  106. */
  107. _largeVideoChangedListener() {
  108. const userID = api._getOnStageParticipant();
  109. const avatarURL = api.getAvatarURL(userID);
  110. const displayName = api.getDisplayName(userID);
  111. const formattedDisplayName = api._getFormattedDisplayName(userID);
  112. const isVideoDisplayed = Boolean(api._getLargeVideo());
  113. this.setState({
  114. avatarURL,
  115. displayName,
  116. formattedDisplayName,
  117. isVideoDisplayed
  118. });
  119. }
  120. _mouseMove: () => void;
  121. /**
  122. * Handles mouse move events.
  123. *
  124. * @returns {void}
  125. */
  126. _mouseMove() {
  127. this.state.visible || this.setState({ visible: true });
  128. }
  129. _onMouseOut: () => void;
  130. /**
  131. * Toolbar mouse out handler.
  132. *
  133. * @returns {void}
  134. */
  135. _onMouseOut() {
  136. this._hovered = false;
  137. }
  138. _onMouseOver: () => void;
  139. /**
  140. * Toolbar mouse over handler.
  141. *
  142. * @returns {void}
  143. */
  144. _onMouseOver() {
  145. this._hovered = true;
  146. }
  147. /**
  148. * Renders display name and avatar for the on stage participant.
  149. *
  150. * @returns {ReactElement}
  151. */
  152. _renderVideoNotAvailableScreen() {
  153. const { avatarURL, displayName, formattedDisplayName, isVideoDisplayed } = this.state;
  154. if (isVideoDisplayed) {
  155. return null;
  156. }
  157. return (
  158. <div id = 'videoNotAvailableScreen'>
  159. <div id = 'avatarContainer'>
  160. <StatelessAvatar
  161. id = 'avatar'
  162. initials = { getInitials(displayName) }
  163. url = { avatarURL } />)
  164. </div>
  165. <div
  166. className = 'displayname'
  167. id = 'displayname'>
  168. { formattedDisplayName }
  169. </div>
  170. </div>
  171. );
  172. }
  173. /**
  174. * Sets mouse move listener and initial toolbar timeout.
  175. *
  176. * @inheritdoc
  177. * @returns {void}
  178. */
  179. componentDidMount() {
  180. api.on('avatarChanged', this._avatarChangedListener);
  181. api.on('displayNameChange', this._displayNameChangedListener);
  182. api.on('largeVideoChanged', this._largeVideoChangedListener);
  183. this._largeVideoChangedListener();
  184. window.addEventListener('mousemove', this._mouseMove);
  185. this._hideToolbarAfterTimeout();
  186. }
  187. /**
  188. * Sets a timeout to hide the toolbar when the toolbar is shown.
  189. *
  190. * @inheritdoc
  191. * @returns {void}
  192. */
  193. componentDidUpdate(prevProps: *, prevState: State) {
  194. if (!prevState.visible && this.state.visible) {
  195. this._hideToolbarAfterTimeout();
  196. }
  197. }
  198. /**
  199. * Removes all listeners.
  200. *
  201. * @inheritdoc
  202. * @returns {void}
  203. */
  204. componentWillUnmount() {
  205. api.removeListener('avatarChanged', this._avatarChangedListener);
  206. api.removeListener(
  207. 'displayNameChange',
  208. this._displayNameChangedListener);
  209. api.removeListener(
  210. 'largeVideoChanged',
  211. this._largeVideoChangedListener);
  212. window.removeEventListener('mousemove', this._mouseMove);
  213. }
  214. /**
  215. * Implements React's {@link Component#render()}.
  216. *
  217. * @inheritdoc
  218. * @returns {ReactElement}
  219. */
  220. render() {
  221. return (
  222. <div id = 'alwaysOnTop'>
  223. <Toolbar
  224. className = { this.state.visible ? 'fadeIn' : 'fadeOut' }
  225. onMouseOut = { this._onMouseOut }
  226. onMouseOver = { this._onMouseOver } />
  227. { this._renderVideoNotAvailableScreen() }
  228. </div>
  229. );
  230. }
  231. }