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.

DesktopPicker.tsx 12KB

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