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.

Filmstrip.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* @flow */
  2. import React, { PureComponent } from 'react';
  3. import { FixedSizeList, FixedSizeGrid } from 'react-window';
  4. import type { Dispatch } from 'redux';
  5. import {
  6. createShortcutEvent,
  7. createToolbarEvent,
  8. sendAnalytics
  9. } from '../../../analytics';
  10. import { getToolbarButtons } from '../../../base/config';
  11. import { translate } from '../../../base/i18n';
  12. import { Icon, IconMenuDown, IconMenuUp } from '../../../base/icons';
  13. import { connect } from '../../../base/redux';
  14. import { showToolbox } from '../../../toolbox/actions.web';
  15. import { isButtonEnabled, isToolboxVisible } from '../../../toolbox/functions.web';
  16. import { LAYOUTS, getCurrentLayout } from '../../../video-layout';
  17. import { setFilmstripVisible, setVisibleRemoteParticipants } from '../../actions';
  18. import { TILE_HORIZONTAL_MARGIN, TILE_VERTICAL_MARGIN, TOOLBAR_HEIGHT } from '../../constants';
  19. import { shouldRemoteVideosBeVisible } from '../../functions';
  20. import AudioTracksContainer from './AudioTracksContainer';
  21. import Thumbnail from './Thumbnail';
  22. import ThumbnailWrapper from './ThumbnailWrapper';
  23. declare var APP: Object;
  24. declare var interfaceConfig: Object;
  25. /**
  26. * The type of the React {@code Component} props of {@link Filmstrip}.
  27. */
  28. type Props = {
  29. /**
  30. * Additional CSS class names top add to the root.
  31. */
  32. _className: string,
  33. /**
  34. * The current layout of the filmstrip.
  35. */
  36. _currentLayout: string,
  37. /**
  38. * The number of columns in tile view.
  39. */
  40. _columns: number,
  41. /**
  42. * The width of the filmstrip.
  43. */
  44. _filmstripWidth: number,
  45. /**
  46. * The height of the filmstrip.
  47. */
  48. _filmstripHeight: number,
  49. /**
  50. * Whether the filmstrip button is enabled.
  51. */
  52. _isFilmstripButtonEnabled: boolean,
  53. /**
  54. * The participants in the call.
  55. */
  56. _remoteParticipants: Array<Object>,
  57. /**
  58. * The length of the remote participants array.
  59. */
  60. _remoteParticipantsLength: number,
  61. /**
  62. * The number of rows in tile view.
  63. */
  64. _rows: number,
  65. /**
  66. * The height of the thumbnail.
  67. */
  68. _thumbnailHeight: number,
  69. /**
  70. * The width of the thumbnail.
  71. */
  72. _thumbnailWidth: number,
  73. /**
  74. * Additional CSS class names to add to the container of all the thumbnails.
  75. */
  76. _videosClassName: string,
  77. /**
  78. * Whether or not the filmstrip videos should currently be displayed.
  79. */
  80. _visible: boolean,
  81. /**
  82. * Whether or not the toolbox is displayed.
  83. */
  84. _isToolboxVisible: Boolean,
  85. /**
  86. * The redux {@code dispatch} function.
  87. */
  88. dispatch: Dispatch<any>,
  89. /**
  90. * Invoked to obtain translated strings.
  91. */
  92. t: Function
  93. };
  94. /**
  95. * Implements a React {@link Component} which represents the filmstrip on
  96. * Web/React.
  97. *
  98. * @extends Component
  99. */
  100. class Filmstrip extends PureComponent <Props> {
  101. /**
  102. * Initializes a new {@code Filmstrip} instance.
  103. *
  104. * @param {Object} props - The read-only properties with which the new
  105. * instance is to be initialized.
  106. */
  107. constructor(props: Props) {
  108. super(props);
  109. // Bind event handlers so they are only bound once for every instance.
  110. this._onShortcutToggleFilmstrip = this._onShortcutToggleFilmstrip.bind(this);
  111. this._onToolbarToggleFilmstrip = this._onToolbarToggleFilmstrip.bind(this);
  112. this._onTabIn = this._onTabIn.bind(this);
  113. this._gridItemKey = this._gridItemKey.bind(this);
  114. this._listItemKey = this._listItemKey.bind(this);
  115. this._onGridItemsRendered = this._onGridItemsRendered.bind(this);
  116. this._onListItemsRendered = this._onListItemsRendered.bind(this);
  117. }
  118. /**
  119. * Implements React's {@link Component#componentDidMount}.
  120. *
  121. * @inheritdoc
  122. */
  123. componentDidMount() {
  124. APP.keyboardshortcut.registerShortcut(
  125. 'F',
  126. 'filmstripPopover',
  127. this._onShortcutToggleFilmstrip,
  128. 'keyboardShortcuts.toggleFilmstrip'
  129. );
  130. }
  131. /**
  132. * Implements React's {@link Component#componentDidUpdate}.
  133. *
  134. * @inheritdoc
  135. */
  136. componentWillUnmount() {
  137. APP.keyboardshortcut.unregisterShortcut('F');
  138. }
  139. /**
  140. * Implements React's {@link Component#render()}.
  141. *
  142. * @inheritdoc
  143. * @returns {ReactElement}
  144. */
  145. render() {
  146. const filmstripStyle = { };
  147. const { _currentLayout } = this.props;
  148. const tileViewActive = _currentLayout === LAYOUTS.TILE_VIEW;
  149. switch (_currentLayout) {
  150. case LAYOUTS.VERTICAL_FILMSTRIP_VIEW:
  151. // Adding 18px for the 2px margins, 2px borders on the left and right and 5px padding on the left and right.
  152. // Also adding 7px for the scrollbar.
  153. filmstripStyle.maxWidth = (interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120) + 25;
  154. break;
  155. }
  156. let toolbar = null;
  157. if (this.props._isFilmstripButtonEnabled) {
  158. toolbar = this._renderToggleButton();
  159. }
  160. return (
  161. <div
  162. className = { `filmstrip ${this.props._className}` }
  163. style = { filmstripStyle }>
  164. { toolbar }
  165. <div
  166. className = { this.props._videosClassName }
  167. id = 'remoteVideos'>
  168. <div
  169. className = 'filmstrip__videos'
  170. id = 'filmstripLocalVideo'>
  171. <div id = 'filmstripLocalVideoThumbnail'>
  172. {
  173. !tileViewActive && <Thumbnail
  174. key = 'local' />
  175. }
  176. </div>
  177. </div>
  178. {
  179. this._renderRemoteParticipants()
  180. }
  181. </div>
  182. <AudioTracksContainer />
  183. </div>
  184. );
  185. }
  186. _onTabIn: () => void;
  187. /**
  188. * Toggle the toolbar visibility when tabbing into it.
  189. *
  190. * @returns {void}
  191. */
  192. _onTabIn() {
  193. if (!this.props._isToolboxVisible && this.props._visible) {
  194. this.props.dispatch(showToolbox());
  195. }
  196. }
  197. _listItemKey: number => string;
  198. /**
  199. * The key to be used for every ThumbnailWrapper element in stage view.
  200. *
  201. * @param {number} index - The index of the ThumbnailWrapper instance.
  202. * @returns {string} - The key.
  203. */
  204. _listItemKey(index) {
  205. const { _remoteParticipants, _remoteParticipantsLength } = this.props;
  206. if (typeof index !== 'number' || _remoteParticipantsLength <= index) {
  207. return `empty-${index}`;
  208. }
  209. return _remoteParticipants[index];
  210. }
  211. _gridItemKey: Object => string;
  212. /**
  213. * The key to be used for every ThumbnailWrapper element in tile views.
  214. *
  215. * @param {Object} data - An object with the indexes identifying the ThumbnailWrapper instance.
  216. * @returns {string} - The key.
  217. */
  218. _gridItemKey({ columnIndex, rowIndex }) {
  219. const { _columns, _remoteParticipants, _remoteParticipantsLength } = this.props;
  220. const index = (rowIndex * _columns) + columnIndex;
  221. if (index > _remoteParticipantsLength) {
  222. return `empty-${index}`;
  223. }
  224. if (index === _remoteParticipantsLength) {
  225. return 'local';
  226. }
  227. return _remoteParticipants[index];
  228. }
  229. _onListItemsRendered: Object => void;
  230. /**
  231. * Handles items rendered changes in stage view.
  232. *
  233. * @param {Object} data - Information about the rendered items.
  234. * @returns {void}
  235. */
  236. _onListItemsRendered({ overscanStartIndex, overscanStopIndex }) {
  237. const { dispatch } = this.props;
  238. dispatch(setVisibleRemoteParticipants(overscanStartIndex, overscanStopIndex));
  239. }
  240. _onGridItemsRendered: Object => void;
  241. /**
  242. * Handles items rendered changes in tile view.
  243. *
  244. * @param {Object} data - Information about the rendered items.
  245. * @returns {void}
  246. */
  247. _onGridItemsRendered({
  248. overscanColumnStartIndex,
  249. overscanColumnStopIndex,
  250. overscanRowStartIndex,
  251. overscanRowStopIndex
  252. }) {
  253. const { _columns, dispatch } = this.props;
  254. const startIndex = (overscanRowStartIndex * _columns) + overscanColumnStartIndex;
  255. const endIndex = (overscanRowStopIndex * _columns) + overscanColumnStopIndex;
  256. dispatch(setVisibleRemoteParticipants(startIndex, endIndex));
  257. }
  258. /**
  259. * Renders the thumbnails for remote participants.
  260. *
  261. * @returns {ReactElement}
  262. */
  263. _renderRemoteParticipants() {
  264. const {
  265. _columns,
  266. _currentLayout,
  267. _filmstripHeight,
  268. _filmstripWidth,
  269. _remoteParticipantsLength,
  270. _rows,
  271. _thumbnailHeight,
  272. _thumbnailWidth
  273. } = this.props;
  274. if (!_thumbnailWidth || isNaN(_thumbnailWidth) || !_thumbnailHeight
  275. || isNaN(_thumbnailHeight) || !_filmstripHeight || isNaN(_filmstripHeight) || !_filmstripWidth
  276. || isNaN(_filmstripWidth)) {
  277. return null;
  278. }
  279. if (_currentLayout === LAYOUTS.TILE_VIEW) {
  280. return (
  281. <FixedSizeGrid
  282. className = 'filmstrip__videos remote-videos'
  283. columnCount = { _columns }
  284. columnWidth = { _thumbnailWidth + TILE_HORIZONTAL_MARGIN }
  285. height = { _filmstripHeight }
  286. initialScrollLeft = { 0 }
  287. initialScrollTop = { 0 }
  288. itemKey = { this._gridItemKey }
  289. onItemsRendered = { this._onGridItemsRendered }
  290. rowCount = { _rows }
  291. rowHeight = { _thumbnailHeight + TILE_VERTICAL_MARGIN }
  292. width = { _filmstripWidth }>
  293. {
  294. ThumbnailWrapper
  295. }
  296. </FixedSizeGrid>
  297. );
  298. }
  299. const props = {
  300. itemCount: _remoteParticipantsLength,
  301. className: 'filmstrip__videos remote-videos',
  302. height: _filmstripHeight,
  303. itemKey: this._listItemKey,
  304. itemSize: 0,
  305. onItemsRendered: this._onListItemsRendered,
  306. width: _filmstripWidth,
  307. style: {
  308. willChange: 'auto'
  309. }
  310. };
  311. if (_currentLayout === LAYOUTS.HORIZONTAL_FILMSTRIP_VIEW) {
  312. const itemSize = _thumbnailWidth + TILE_HORIZONTAL_MARGIN;
  313. const isNotOverflowing = (_remoteParticipantsLength * itemSize) <= _filmstripWidth;
  314. props.itemSize = itemSize;
  315. // $FlowFixMe
  316. props.layout = 'horizontal';
  317. if (isNotOverflowing) {
  318. props.className += ' is-not-overflowing';
  319. }
  320. } else if (_currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  321. const itemSize = _thumbnailHeight + TILE_VERTICAL_MARGIN;
  322. const isNotOverflowing = (_remoteParticipantsLength * itemSize) <= _filmstripHeight;
  323. if (isNotOverflowing) {
  324. props.className += ' is-not-overflowing';
  325. }
  326. props.itemSize = itemSize;
  327. }
  328. return (
  329. <FixedSizeList { ...props }>
  330. {
  331. ThumbnailWrapper
  332. }
  333. </FixedSizeList>
  334. );
  335. }
  336. /**
  337. * Dispatches an action to change the visibility of the filmstrip.
  338. *
  339. * @private
  340. * @returns {void}
  341. */
  342. _doToggleFilmstrip() {
  343. this.props.dispatch(setFilmstripVisible(!this.props._visible));
  344. }
  345. _onShortcutToggleFilmstrip: () => void;
  346. /**
  347. * Creates an analytics keyboard shortcut event and dispatches an action for
  348. * toggling filmstrip visibility.
  349. *
  350. * @private
  351. * @returns {void}
  352. */
  353. _onShortcutToggleFilmstrip() {
  354. sendAnalytics(createShortcutEvent(
  355. 'toggle.filmstrip',
  356. {
  357. enable: this.props._visible
  358. }));
  359. this._doToggleFilmstrip();
  360. }
  361. _onToolbarToggleFilmstrip: () => void;
  362. /**
  363. * Creates an analytics toolbar event and dispatches an action for opening
  364. * the speaker stats modal.
  365. *
  366. * @private
  367. * @returns {void}
  368. */
  369. _onToolbarToggleFilmstrip() {
  370. sendAnalytics(createToolbarEvent(
  371. 'toggle.filmstrip.button',
  372. {
  373. enable: this.props._visible
  374. }));
  375. this._doToggleFilmstrip();
  376. }
  377. /**
  378. * Creates a React Element for changing the visibility of the filmstrip when
  379. * clicked.
  380. *
  381. * @private
  382. * @returns {ReactElement}
  383. */
  384. _renderToggleButton() {
  385. const icon = this.props._visible ? IconMenuDown : IconMenuUp;
  386. const { t } = this.props;
  387. return (
  388. <div
  389. className = 'filmstrip__toolbar'>
  390. <button
  391. aria-expanded = { this.props._visible }
  392. aria-label = { t('toolbar.accessibilityLabel.toggleFilmstrip') }
  393. id = 'toggleFilmstripButton'
  394. onClick = { this._onToolbarToggleFilmstrip }
  395. onFocus = { this._onTabIn }
  396. tabIndex = { 0 }>
  397. <Icon
  398. aria-label = { t('toolbar.accessibilityLabel.toggleFilmstrip') }
  399. src = { icon } />
  400. </button>
  401. </div>
  402. );
  403. }
  404. }
  405. /**
  406. * Maps (parts of) the Redux state to the associated {@code Filmstrip}'s props.
  407. *
  408. * @param {Object} state - The Redux state.
  409. * @private
  410. * @returns {Props}
  411. */
  412. function _mapStateToProps(state) {
  413. const toolbarButtons = getToolbarButtons(state);
  414. const { visible, remoteParticipants } = state['features/filmstrip'];
  415. const reduceHeight = state['features/toolbox'].visible && toolbarButtons.length;
  416. const remoteVideosVisible = shouldRemoteVideosBeVisible(state);
  417. const { isOpen: shiftRight } = state['features/chat'];
  418. const className = `${remoteVideosVisible ? '' : 'hide-videos'} ${
  419. reduceHeight ? 'reduce-height' : ''
  420. } ${shiftRight ? 'shift-right' : ''}`.trim();
  421. const videosClassName = `filmstrip__videos${visible ? '' : ' hidden'}`;
  422. const {
  423. gridDimensions = {},
  424. filmstripHeight,
  425. filmstripWidth,
  426. thumbnailSize: tileViewThumbnailSize
  427. } = state['features/filmstrip'].tileViewDimensions;
  428. const _currentLayout = getCurrentLayout(state);
  429. let _thumbnailSize, remoteFilmstripHeight, remoteFilmstripWidth;
  430. switch (_currentLayout) {
  431. case LAYOUTS.TILE_VIEW:
  432. _thumbnailSize = tileViewThumbnailSize;
  433. remoteFilmstripHeight = filmstripHeight;
  434. remoteFilmstripWidth = filmstripWidth;
  435. break;
  436. case LAYOUTS.VERTICAL_FILMSTRIP_VIEW: {
  437. const { remote, remoteVideosContainer } = state['features/filmstrip'].verticalViewDimensions;
  438. _thumbnailSize = remote;
  439. remoteFilmstripHeight = remoteVideosContainer?.height - (reduceHeight ? TOOLBAR_HEIGHT : 0);
  440. remoteFilmstripWidth = remoteVideosContainer?.width;
  441. break;
  442. }
  443. case LAYOUTS.HORIZONTAL_FILMSTRIP_VIEW: {
  444. const { remote, remoteVideosContainer } = state['features/filmstrip'].horizontalViewDimensions;
  445. _thumbnailSize = remote;
  446. remoteFilmstripHeight = remoteVideosContainer?.height;
  447. remoteFilmstripWidth = remoteVideosContainer?.width;
  448. break;
  449. }
  450. }
  451. return {
  452. _className: className,
  453. _columns: gridDimensions.columns,
  454. _currentLayout,
  455. _filmstripHeight: remoteFilmstripHeight,
  456. _filmstripWidth: remoteFilmstripWidth,
  457. _isFilmstripButtonEnabled: isButtonEnabled('filmstrip', state),
  458. _remoteParticipantsLength: remoteParticipants.length,
  459. _remoteParticipants: remoteParticipants,
  460. _rows: gridDimensions.rows,
  461. _thumbnailWidth: _thumbnailSize?.width,
  462. _thumbnailHeight: _thumbnailSize?.height,
  463. _videosClassName: videosClassName,
  464. _visible: visible,
  465. _isToolboxVisible: isToolboxVisible(state)
  466. };
  467. }
  468. export default translate(connect(_mapStateToProps)(Filmstrip));