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

DesktopPicker.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import React, { PureComponent } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { IStore } from '../../app/types';
  4. import { hideDialog } from '../../base/dialog/actions';
  5. import { translate } from '../../base/i18n/functions';
  6. import { connect } from '../../base/redux/functions';
  7. import Dialog from '../../base/ui/components/web/Dialog';
  8. import Tabs from '../../base/ui/components/web/Tabs';
  9. // eslint-disable-next-line lines-around-comment
  10. // @ts-ignore
  11. import { obtainDesktopSources } from '../functions';
  12. // @ts-ignore
  13. import DesktopPickerPane from './DesktopPickerPane';
  14. /**
  15. * The size of the requested thumbnails.
  16. *
  17. * @type {Object}
  18. */
  19. const THUMBNAIL_SIZE = {
  20. height: 300,
  21. width: 300
  22. };
  23. /**
  24. * The sources polling interval in ms.
  25. *
  26. * @type {int}
  27. */
  28. const UPDATE_INTERVAL = 2000;
  29. /**
  30. * The default selected tab.
  31. *
  32. * @type {string}
  33. */
  34. const DEFAULT_TAB_TYPE = 'screen';
  35. const TAB_LABELS = {
  36. screen: 'dialog.yourEntireScreen',
  37. window: 'dialog.applicationWindow'
  38. };
  39. const VALID_TYPES = Object.keys(TAB_LABELS);
  40. /**
  41. * The type of the React {@code Component} props of {@link DesktopPicker}.
  42. */
  43. interface IProps extends WithTranslation {
  44. /**
  45. * An array with desktop sharing sources to be displayed.
  46. */
  47. desktopSharingSources: Array<string>;
  48. /**
  49. * Used to request DesktopCapturerSources.
  50. */
  51. dispatch: IStore['dispatch'];
  52. /**
  53. * The callback to be invoked when the component is closed or when a
  54. * DesktopCapturerSource has been chosen.
  55. */
  56. onSourceChoose: Function;
  57. }
  58. /**
  59. * The type of the React {@code Component} state of {@link DesktopPicker}.
  60. */
  61. interface IState {
  62. /**
  63. * The state of the audio screen share checkbox.
  64. */
  65. screenShareAudio: boolean;
  66. /**
  67. * The currently highlighted DesktopCapturerSource.
  68. */
  69. selectedSource: any;
  70. /**
  71. * The desktop source type currently being displayed.
  72. */
  73. selectedTab: string;
  74. /**
  75. * An object containing all the DesktopCapturerSources.
  76. */
  77. sources: Object;
  78. /**
  79. * The desktop source types to fetch previews for.
  80. */
  81. types: Array<string>;
  82. }
  83. /**
  84. * React component for DesktopPicker.
  85. *
  86. * @augments Component
  87. */
  88. class DesktopPicker extends PureComponent<IProps, IState> {
  89. /**
  90. * Implements React's {@link Component#getDerivedStateFromProps()}.
  91. *
  92. * @inheritdoc
  93. */
  94. static getDerivedStateFromProps(props: IProps) {
  95. return {
  96. types: DesktopPicker._getValidTypes(props.desktopSharingSources)
  97. };
  98. }
  99. /**
  100. * Extracts only the valid types from the passed {@code types}.
  101. *
  102. * @param {Array<string>} types - The types to filter.
  103. * @private
  104. * @returns {Array<string>} The filtered types.
  105. */
  106. static _getValidTypes(types: string[] = []) {
  107. return types.filter(
  108. type => VALID_TYPES.includes(type));
  109. }
  110. _poller: any = null;
  111. state: IState = {
  112. screenShareAudio: false,
  113. selectedSource: {},
  114. selectedTab: DEFAULT_TAB_TYPE,
  115. sources: {},
  116. types: []
  117. };
  118. /**
  119. * Stores the type of the selected tab.
  120. *
  121. * @type {string}
  122. */
  123. _selectedTabType = DEFAULT_TAB_TYPE;
  124. /**
  125. * Initializes a new DesktopPicker instance.
  126. *
  127. * @param {Object} props - The read-only properties with which the new
  128. * instance is to be initialized.
  129. */
  130. constructor(props: IProps) {
  131. super(props);
  132. // Bind event handlers so they are only bound once per instance.
  133. this._onCloseModal = this._onCloseModal.bind(this);
  134. this._onPreviewClick = this._onPreviewClick.bind(this);
  135. this._onShareAudioChecked = this._onShareAudioChecked.bind(this);
  136. this._onSubmit = this._onSubmit.bind(this);
  137. this._onTabSelected = this._onTabSelected.bind(this);
  138. this._updateSources = this._updateSources.bind(this);
  139. this.state.types
  140. = DesktopPicker._getValidTypes(this.props.desktopSharingSources);
  141. }
  142. /**
  143. * Starts polling.
  144. *
  145. * @inheritdoc
  146. * @returns {void}
  147. */
  148. componentDidMount() {
  149. this._startPolling();
  150. }
  151. /**
  152. * Clean up component and DesktopCapturerSource store state.
  153. *
  154. * @inheritdoc
  155. */
  156. componentWillUnmount() {
  157. this._stopPolling();
  158. }
  159. /**
  160. * Implements React's {@link Component#render()}.
  161. *
  162. * @inheritdoc
  163. */
  164. render() {
  165. const { selectedTab, selectedSource, sources, types } = this.state;
  166. return (
  167. <Dialog
  168. ok = {{
  169. disabled: Boolean(!this.state.selectedSource.id),
  170. translationKey: 'dialog.Share'
  171. }}
  172. onCancel = { this._onCloseModal }
  173. onSubmit = { this._onSubmit }
  174. size = 'large'
  175. titleKey = 'dialog.shareYourScreen'>
  176. { this._renderTabs() }
  177. {types.map(type => (
  178. <div
  179. aria-labelledby = { `${type}-button` }
  180. className = { selectedTab === type ? undefined : 'hide' }
  181. id = { `${type}-panel` }
  182. key = { type }
  183. role = 'tabpanel'
  184. tabIndex = { 0 }>
  185. {selectedTab === type && (
  186. <DesktopPickerPane
  187. key = { selectedTab }
  188. onClick = { this._onPreviewClick }
  189. onDoubleClick = { this._onSubmit }
  190. onShareAudioChecked = { this._onShareAudioChecked }
  191. selectedSourceId = { selectedSource.id }
  192. sources = { sources[selectedTab as keyof typeof sources] }
  193. type = { selectedTab } />
  194. )}
  195. </div>
  196. ))}
  197. </Dialog>
  198. );
  199. }
  200. /**
  201. * Computes the selected source.
  202. *
  203. * @param {Object} sources - The available sources.
  204. * @returns {Object} The selectedSource value.
  205. */
  206. _getSelectedSource(sources: any = {}) {
  207. const { selectedSource } = this.state;
  208. /**
  209. * If there are no sources for this type (or no sources for any type)
  210. * we can't select anything.
  211. */
  212. if (!Array.isArray(sources[this._selectedTabType as keyof typeof sources])
  213. || sources[this._selectedTabType as keyof typeof sources].length <= 0) {
  214. return {};
  215. }
  216. /**
  217. * Select the first available source for this type in the following
  218. * scenarios:
  219. * 1) Nothing is yet selected.
  220. * 2) Tab change.
  221. * 3) The selected source is no longer available.
  222. */
  223. if (!selectedSource // scenario 1)
  224. || selectedSource.type !== this._selectedTabType // scenario 2)
  225. || !sources[this._selectedTabType].some( // scenario 3)
  226. (source: any) => source.id === selectedSource.id)) {
  227. return {
  228. id: sources[this._selectedTabType][0].id,
  229. type: this._selectedTabType
  230. };
  231. }
  232. /**
  233. * For all other scenarios don't change the selection.
  234. */
  235. return selectedSource;
  236. }
  237. /**
  238. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  239. * callback with a selectedSource, if any.
  240. *
  241. * @param {string} [id] - The id of the DesktopCapturerSource to pass into
  242. * the onSourceChoose callback.
  243. * @param {string} type - The type of the DesktopCapturerSource to pass into
  244. * the onSourceChoose callback.
  245. * @param {boolean} screenShareAudio - Whether or not to add system audio to
  246. * screen sharing session.
  247. * @returns {void}
  248. */
  249. _onCloseModal(id = '', type?: string, screenShareAudio = false) {
  250. this.props.onSourceChoose(id, type, screenShareAudio);
  251. this.props.dispatch(hideDialog());
  252. }
  253. /**
  254. * Sets the currently selected DesktopCapturerSource.
  255. *
  256. * @param {string} id - The id of DesktopCapturerSource.
  257. * @param {string} type - The type of DesktopCapturerSource.
  258. * @returns {void}
  259. */
  260. _onPreviewClick(id: string, type: string) {
  261. this.setState({
  262. selectedSource: {
  263. id,
  264. type
  265. }
  266. });
  267. }
  268. /**
  269. * Request to close the modal and execute callbacks with the selected source
  270. * id.
  271. *
  272. * @returns {void}
  273. */
  274. _onSubmit() {
  275. const { selectedSource: { id, type }, screenShareAudio } = this.state;
  276. this._onCloseModal(id, type, screenShareAudio);
  277. }
  278. /**
  279. * Stores the selected tab and updates the selected source via
  280. * {@code _getSelectedSource}.
  281. *
  282. * @param {string} id - The id of the newly selected tab.
  283. * @returns {void}
  284. */
  285. _onTabSelected(id: string) {
  286. const { sources } = this.state;
  287. this._selectedTabType = id;
  288. // When we change tabs also reset the screenShareAudio state so we don't
  289. // use the option from one tab when sharing from another.
  290. this.setState({
  291. screenShareAudio: false,
  292. selectedSource: this._getSelectedSource(sources),
  293. selectedTab: id
  294. });
  295. }
  296. /**
  297. * Set the screenSharingAudio state indicating whether or not to also share
  298. * system audio.
  299. *
  300. * @param {boolean} checked - Share audio or not.
  301. * @returns {void}
  302. */
  303. _onShareAudioChecked(checked: boolean) {
  304. this.setState({ screenShareAudio: checked });
  305. }
  306. /**
  307. * Configures and renders the tabs for display.
  308. *
  309. * @private
  310. * @returns {ReactElement}
  311. */
  312. _renderTabs() {
  313. const { types } = this.state;
  314. const { t } = this.props;
  315. const tabs
  316. = types.map(
  317. type => {
  318. return {
  319. accessibilityLabel: t(TAB_LABELS[type as keyof typeof TAB_LABELS]),
  320. id: `${type}-tab`,
  321. controlsId: `${type}-panel`,
  322. label: t(TAB_LABELS[type as keyof typeof TAB_LABELS])
  323. };
  324. });
  325. return (
  326. <Tabs
  327. accessibilityLabel = { t('dialog.sharingTabs') }
  328. className = 'desktop-picker-tabs-container'
  329. onChange = { this._onTabSelected }
  330. selected = { `${this.state.selectedTab}-tab` }
  331. tabs = { tabs } />);
  332. }
  333. /**
  334. * Create an interval to update known available DesktopCapturerSources.
  335. *
  336. * @private
  337. * @returns {void}
  338. */
  339. _startPolling() {
  340. this._stopPolling();
  341. this._updateSources();
  342. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  343. }
  344. /**
  345. * Cancels the interval to update DesktopCapturerSources.
  346. *
  347. * @private
  348. * @returns {void}
  349. */
  350. _stopPolling() {
  351. window.clearInterval(this._poller);
  352. this._poller = null;
  353. }
  354. /**
  355. * Obtains the desktop sources and updates state with them.
  356. *
  357. * @private
  358. * @returns {void}
  359. */
  360. _updateSources() {
  361. const { types } = this.state;
  362. if (types.length > 0) {
  363. obtainDesktopSources(
  364. this.state.types,
  365. { thumbnailSize: THUMBNAIL_SIZE }
  366. )
  367. .then((sources: any) => {
  368. const selectedSource = this._getSelectedSource(sources);
  369. // TODO: Maybe check if we have stopped the timer and unmounted
  370. // the component.
  371. this.setState({
  372. sources,
  373. selectedSource
  374. });
  375. })
  376. .catch(() => { /* ignore */ });
  377. }
  378. }
  379. }
  380. export default translate(connect()(DesktopPicker));