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

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