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

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