diff --git a/app/lib/components/Filmstrip.jsx b/app/lib/components/Filmstrip.jsx index 5e2f870..01552e4 100644 --- a/app/lib/components/Filmstrip.jsx +++ b/app/lib/components/Filmstrip.jsx @@ -1,15 +1,92 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; +import ResizeObserver from 'resize-observer-polyfill'; import { connect } from 'react-redux'; import classnames from 'classnames'; import Peer from './Peer'; class Filmstrip extends Component { + constructor(props) + { + super(props); + + this.activePeerContainer = React.createRef(); + } + state = { - selectedPeerName : null + selectedPeerName : null, + width : 400 }; + // Find the name of the peer which is currently speaking. This is either + // the latest active speaker, or the manually selected peer. + getActivePeerName = () => + this.state.selectedPeerName || this.props.activeSpeakerName; + + isSharingCamera = (peerName) => this.props.peers[peerName] && + this.props.peers[peerName].consumers.some((consumer) => + this.props.consumers[consumer].source === 'screen'); + + getRatio = () => + { + let ratio = 4 / 3; + + if (this.isSharingCamera(this.getActivePeerName())) + { + ratio *= 2; + } + + return ratio; + }; + + updateDimensions = () => + { + const container = this.activePeerContainer.current; + + if (container) + { + const ratio = this.getRatio(); + + let width = container.clientWidth; + + if (width / ratio > container.clientHeight) + { + width = container.clientHeight * ratio; + } + + this.setState({ + width + }); + } + }; + + componentDidMount() + { + this.updateDimensions(); + } + + componentDidMount() + { + window.addEventListener('resize', this.updateDimensions); + const observer = new ResizeObserver(this.updateDimensions); + + observer.observe(this.activePeerContainer.current); + } + + componentWillUnmount() + { + window.removeEventListener('resize', this.updateDimensions); + } + + componentDidUpdate(prevProps) + { + if (prevProps !== this.props) + { + this.updateDimensions(); + } + } + handleSelectPeer = (selectedPeerName) => { this.setState((oldState) => ({ @@ -22,39 +99,44 @@ class Filmstrip extends Component { const { activeSpeakerName, peers, advancedMode } = this.props; - // Find the name of the peer which is currently speaking. This is either - // the latest active speaker, or the manually selected peer. - const activePeerName = this.state.selectedPeerName || activeSpeakerName; + const activePeerName = this.getActivePeerName(); return (