Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Filmstrip.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. * Fixes case in which context menu overflows and creates a scroll on the whole filmstrip videos pane.
  34. */
  35. const TILEVIEW_VIDEO_PANES_STYLE = { overflow: 'visible' };
  36. /**
  37. * The type of the React {@code Component} props of {@link Filmstrip}.
  38. */
  39. type Props = {
  40. /**
  41. * Additional CSS class names top add to the root.
  42. */
  43. _className: string,
  44. /**
  45. * The current layout of the filmstrip.
  46. */
  47. _currentLayout: string,
  48. /**
  49. * The number of columns in tile view.
  50. */
  51. _columns: number,
  52. /**
  53. * The width of the filmstrip.
  54. */
  55. _filmstripWidth: number,
  56. /**
  57. * The height of the filmstrip.
  58. */
  59. _filmstripHeight: number,
  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, _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 (_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 { _columns, _remoteParticipants, _remoteParticipantsLength, _thumbnailsReordered } = this.props;
  259. const index = (rowIndex * _columns) + columnIndex;
  260. // When the thumbnails are reordered, local participant is inserted at index 0.
  261. const localIndex = _thumbnailsReordered ? 0 : _remoteParticipantsLength;
  262. const remoteIndex = _thumbnailsReordered ? index - 1 : index;
  263. if (index > _remoteParticipantsLength) {
  264. return `empty-${index}`;
  265. }
  266. if (index === localIndex) {
  267. return 'local';
  268. }
  269. return _remoteParticipants[remoteIndex];
  270. }
  271. _onListItemsRendered: Object => void;
  272. /**
  273. * Handles items rendered changes in stage view.
  274. *
  275. * @param {Object} data - Information about the rendered items.
  276. * @returns {void}
  277. */
  278. _onListItemsRendered({ visibleStartIndex, visibleStopIndex }) {
  279. const { dispatch } = this.props;
  280. const { startIndex, stopIndex } = this._calculateIndices(visibleStartIndex, visibleStopIndex);
  281. dispatch(setVisibleRemoteParticipants(startIndex, stopIndex));
  282. }
  283. _onGridItemsRendered: Object => void;
  284. /**
  285. * Handles items rendered changes in tile view.
  286. *
  287. * @param {Object} data - Information about the rendered items.
  288. * @returns {void}
  289. */
  290. _onGridItemsRendered({
  291. visibleColumnStartIndex,
  292. visibleColumnStopIndex,
  293. visibleRowStartIndex,
  294. visibleRowStopIndex
  295. }) {
  296. const { _columns, dispatch } = this.props;
  297. const start = (visibleRowStartIndex * _columns) + visibleColumnStartIndex;
  298. const stop = (visibleRowStopIndex * _columns) + visibleColumnStopIndex;
  299. const { startIndex, stopIndex } = this._calculateIndices(start, stop);
  300. dispatch(setVisibleRemoteParticipants(startIndex, stopIndex));
  301. }
  302. /**
  303. * Renders the thumbnails for remote participants.
  304. *
  305. * @returns {ReactElement}
  306. */
  307. _renderRemoteParticipants() {
  308. const {
  309. _columns,
  310. _currentLayout,
  311. _filmstripHeight,
  312. _filmstripWidth,
  313. _remoteParticipantsLength,
  314. _rows,
  315. _thumbnailHeight,
  316. _thumbnailWidth
  317. } = this.props;
  318. if (!_thumbnailWidth || isNaN(_thumbnailWidth) || !_thumbnailHeight
  319. || isNaN(_thumbnailHeight) || !_filmstripHeight || isNaN(_filmstripHeight) || !_filmstripWidth
  320. || isNaN(_filmstripWidth)) {
  321. return null;
  322. }
  323. if (_currentLayout === LAYOUTS.TILE_VIEW) {
  324. return (
  325. <FixedSizeGrid
  326. className = 'filmstrip__videos remote-videos'
  327. columnCount = { _columns }
  328. columnWidth = { _thumbnailWidth + TILE_HORIZONTAL_MARGIN }
  329. height = { _filmstripHeight }
  330. initialScrollLeft = { 0 }
  331. initialScrollTop = { 0 }
  332. itemKey = { this._gridItemKey }
  333. onItemsRendered = { this._onGridItemsRendered }
  334. overscanRowCount = { 1 }
  335. rowCount = { _rows }
  336. rowHeight = { _thumbnailHeight + TILE_VERTICAL_MARGIN }
  337. style = { TILEVIEW_VIDEO_PANES_STYLE }
  338. width = { _filmstripWidth }>
  339. {
  340. ThumbnailWrapper
  341. }
  342. </FixedSizeGrid>
  343. );
  344. }
  345. const props = {
  346. itemCount: _remoteParticipantsLength,
  347. className: 'filmstrip__videos remote-videos',
  348. height: _filmstripHeight,
  349. itemKey: this._listItemKey,
  350. itemSize: 0,
  351. onItemsRendered: this._onListItemsRendered,
  352. overscanCount: 1,
  353. width: _filmstripWidth,
  354. style: {
  355. willChange: 'auto'
  356. }
  357. };
  358. if (_currentLayout === LAYOUTS.HORIZONTAL_FILMSTRIP_VIEW) {
  359. const itemSize = _thumbnailWidth + TILE_HORIZONTAL_MARGIN;
  360. const isNotOverflowing = (_remoteParticipantsLength * itemSize) <= _filmstripWidth;
  361. props.itemSize = itemSize;
  362. // $FlowFixMe
  363. props.layout = 'horizontal';
  364. if (isNotOverflowing) {
  365. props.className += ' is-not-overflowing';
  366. }
  367. } else if (_currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  368. const itemSize = _thumbnailHeight + TILE_VERTICAL_MARGIN;
  369. const isNotOverflowing = (_remoteParticipantsLength * itemSize) <= _filmstripHeight;
  370. if (isNotOverflowing) {
  371. props.className += ' is-not-overflowing';
  372. }
  373. props.itemSize = itemSize;
  374. }
  375. return (
  376. <FixedSizeList { ...props }>
  377. {
  378. ThumbnailWrapper
  379. }
  380. </FixedSizeList>
  381. );
  382. }
  383. /**
  384. * Dispatches an action to change the visibility of the filmstrip.
  385. *
  386. * @private
  387. * @returns {void}
  388. */
  389. _doToggleFilmstrip() {
  390. this.props.dispatch(setFilmstripVisible(!this.props._visible));
  391. }
  392. _onShortcutToggleFilmstrip: () => void;
  393. /**
  394. * Creates an analytics keyboard shortcut event and dispatches an action for
  395. * toggling filmstrip visibility.
  396. *
  397. * @private
  398. * @returns {void}
  399. */
  400. _onShortcutToggleFilmstrip() {
  401. sendAnalytics(createShortcutEvent(
  402. 'toggle.filmstrip',
  403. {
  404. enable: this.props._visible
  405. }));
  406. this._doToggleFilmstrip();
  407. }
  408. _onToolbarToggleFilmstrip: () => void;
  409. /**
  410. * Creates an analytics toolbar event and dispatches an action for opening
  411. * the speaker stats modal.
  412. *
  413. * @private
  414. * @returns {void}
  415. */
  416. _onToolbarToggleFilmstrip() {
  417. sendAnalytics(createToolbarEvent(
  418. 'toggle.filmstrip.button',
  419. {
  420. enable: this.props._visible
  421. }));
  422. this._doToggleFilmstrip();
  423. }
  424. /**
  425. * Creates a React Element for changing the visibility of the filmstrip when
  426. * clicked.
  427. *
  428. * @private
  429. * @returns {ReactElement}
  430. */
  431. _renderToggleButton() {
  432. const icon = this.props._visible ? IconMenuDown : IconMenuUp;
  433. const { t } = this.props;
  434. return (
  435. <div
  436. className = 'filmstrip__toolbar'>
  437. <button
  438. aria-expanded = { this.props._visible }
  439. aria-label = { t('toolbar.accessibilityLabel.toggleFilmstrip') }
  440. id = 'toggleFilmstripButton'
  441. onClick = { this._onToolbarToggleFilmstrip }
  442. onFocus = { this._onTabIn }
  443. tabIndex = { 0 }>
  444. <Icon
  445. aria-label = { t('toolbar.accessibilityLabel.toggleFilmstrip') }
  446. src = { icon } />
  447. </button>
  448. </div>
  449. );
  450. }
  451. }
  452. /**
  453. * Maps (parts of) the Redux state to the associated {@code Filmstrip}'s props.
  454. *
  455. * @param {Object} state - The Redux state.
  456. * @private
  457. * @returns {Props}
  458. */
  459. function _mapStateToProps(state) {
  460. const toolbarButtons = getToolbarButtons(state);
  461. const { testing = {} } = state['features/base/config'];
  462. const enableThumbnailReordering = testing.enableThumbnailReordering ?? true;
  463. const { visible, remoteParticipants } = state['features/filmstrip'];
  464. const reduceHeight = state['features/toolbox'].visible && toolbarButtons.length;
  465. const remoteVideosVisible = shouldRemoteVideosBeVisible(state);
  466. const { isOpen: shiftRight } = state['features/chat'];
  467. const {
  468. gridDimensions = {},
  469. filmstripHeight,
  470. filmstripWidth,
  471. thumbnailSize: tileViewThumbnailSize
  472. } = state['features/filmstrip'].tileViewDimensions;
  473. const _currentLayout = getCurrentLayout(state);
  474. const { clientHeight, clientWidth } = state['features/base/responsive-ui'];
  475. const availableSpace = clientHeight - filmstripHeight;
  476. let filmstripPadding = 0;
  477. if (availableSpace > 0) {
  478. const paddingValue = TOOLBAR_HEIGHT_MOBILE - availableSpace;
  479. if (paddingValue > 0) {
  480. filmstripPadding = paddingValue;
  481. }
  482. } else {
  483. filmstripPadding = TOOLBAR_HEIGHT_MOBILE;
  484. }
  485. const collapseTileView = reduceHeight
  486. && isMobileBrowser()
  487. && clientWidth <= ASPECT_RATIO_BREAKPOINT;
  488. const className = `${remoteVideosVisible ? '' : 'hide-videos'} ${
  489. reduceHeight ? 'reduce-height' : ''
  490. } ${shiftRight ? 'shift-right' : ''} ${collapseTileView ? 'collapse' : ''}`.trim();
  491. const videosClassName = `filmstrip__videos${visible ? '' : ' hidden'}`;
  492. let _thumbnailSize, remoteFilmstripHeight, remoteFilmstripWidth;
  493. switch (_currentLayout) {
  494. case LAYOUTS.TILE_VIEW:
  495. _thumbnailSize = tileViewThumbnailSize;
  496. remoteFilmstripHeight = filmstripHeight - (collapseTileView && filmstripPadding > 0 ? filmstripPadding : 0);
  497. remoteFilmstripWidth = filmstripWidth;
  498. break;
  499. case LAYOUTS.VERTICAL_FILMSTRIP_VIEW: {
  500. const { remote, remoteVideosContainer } = state['features/filmstrip'].verticalViewDimensions;
  501. _thumbnailSize = remote;
  502. remoteFilmstripHeight = remoteVideosContainer?.height - (reduceHeight ? TOOLBAR_HEIGHT : 0);
  503. remoteFilmstripWidth = remoteVideosContainer?.width;
  504. break;
  505. }
  506. case LAYOUTS.HORIZONTAL_FILMSTRIP_VIEW: {
  507. const { remote, remoteVideosContainer } = state['features/filmstrip'].horizontalViewDimensions;
  508. _thumbnailSize = remote;
  509. remoteFilmstripHeight = remoteVideosContainer?.height;
  510. remoteFilmstripWidth = remoteVideosContainer?.width;
  511. break;
  512. }
  513. }
  514. return {
  515. _className: className,
  516. _columns: gridDimensions.columns,
  517. _currentLayout,
  518. _filmstripHeight: remoteFilmstripHeight,
  519. _filmstripWidth: remoteFilmstripWidth,
  520. _isFilmstripButtonEnabled: isButtonEnabled('filmstrip', state),
  521. _remoteParticipantsLength: remoteParticipants.length,
  522. _remoteParticipants: remoteParticipants,
  523. _rows: gridDimensions.rows,
  524. _thumbnailWidth: _thumbnailSize?.width,
  525. _thumbnailHeight: _thumbnailSize?.height,
  526. _thumbnailsReordered: enableThumbnailReordering,
  527. _videosClassName: videosClassName,
  528. _visible: visible,
  529. _isToolboxVisible: isToolboxVisible(state)
  530. };
  531. }
  532. export default translate(connect(_mapStateToProps)(Filmstrip));