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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. * Initializes a new DesktopPicker instance.
  110. *
  111. * @param {Object} props - The read-only properties with which the new
  112. * instance is to be initialized.
  113. */
  114. constructor(props: IProps) {
  115. super(props);
  116. // Bind event handlers so they are only bound once per instance.
  117. this._onCloseModal = this._onCloseModal.bind(this);
  118. this._onPreviewClick = this._onPreviewClick.bind(this);
  119. this._onShareAudioChecked = this._onShareAudioChecked.bind(this);
  120. this._onSubmit = this._onSubmit.bind(this);
  121. this._onTabSelected = this._onTabSelected.bind(this);
  122. this._updateSources = this._updateSources.bind(this);
  123. this.state.types
  124. = DesktopPicker._getValidTypes(this.props.desktopSharingSources);
  125. }
  126. /**
  127. * Starts polling.
  128. *
  129. * @inheritdoc
  130. * @returns {void}
  131. */
  132. componentDidMount() {
  133. this._startPolling();
  134. }
  135. /**
  136. * Clean up component and DesktopCapturerSource store state.
  137. *
  138. * @inheritdoc
  139. */
  140. componentWillUnmount() {
  141. this._stopPolling();
  142. }
  143. /**
  144. * Implements React's {@link Component#render()}.
  145. *
  146. * @inheritdoc
  147. */
  148. render() {
  149. const { selectedTab, selectedSource, sources, types } = this.state;
  150. return (
  151. <Dialog
  152. ok = {{
  153. disabled: Boolean(!this.state.selectedSource.id),
  154. translationKey: 'dialog.Share'
  155. }}
  156. onCancel = { this._onCloseModal }
  157. onSubmit = { this._onSubmit }
  158. size = 'large'
  159. titleKey = 'dialog.shareYourScreen'>
  160. { this._renderTabs() }
  161. {types.map(type => (
  162. <div
  163. aria-labelledby = { `${type}-button` }
  164. className = { selectedTab === type ? undefined : 'hide' }
  165. id = { `${type}-panel` }
  166. key = { type }
  167. role = 'tabpanel'
  168. tabIndex = { 0 }>
  169. {selectedTab === type && (
  170. <DesktopPickerPane
  171. key = { selectedTab }
  172. onClick = { this._onPreviewClick }
  173. onDoubleClick = { this._onSubmit }
  174. onShareAudioChecked = { this._onShareAudioChecked }
  175. selectedSourceId = { selectedSource.id }
  176. sources = { sources[selectedTab as keyof typeof sources] }
  177. type = { selectedTab } />
  178. )}
  179. </div>
  180. ))}
  181. </Dialog>
  182. );
  183. }
  184. /**
  185. * Computes the selected source.
  186. *
  187. * @param {Object} sources - The available sources.
  188. * @param {string} selectedTab - The selected tab.
  189. * @returns {Object} The selectedSource value.
  190. */
  191. _getSelectedSource(sources: any = {}, selectedTab?: string) {
  192. const { selectedSource } = this.state;
  193. const currentSelectedTab = selectedTab ?? this.state.selectedTab;
  194. /**
  195. * If there are no sources for this type (or no sources for any type)
  196. * we can't select anything.
  197. */
  198. if (!Array.isArray(sources[currentSelectedTab as keyof typeof sources])
  199. || sources[currentSelectedTab as keyof typeof sources].length <= 0) {
  200. return {};
  201. }
  202. /**
  203. * Select the first available source for this type in the following
  204. * scenarios:
  205. * 1) Nothing is yet selected.
  206. * 2) Tab change.
  207. * 3) The selected source is no longer available.
  208. */
  209. if (!selectedSource // scenario 1)
  210. || selectedSource.type !== currentSelectedTab // scenario 2)
  211. || !sources[currentSelectedTab].some( // scenario 3)
  212. (source: any) => source.id === selectedSource.id)) {
  213. return {
  214. id: sources[currentSelectedTab][0].id,
  215. type: currentSelectedTab
  216. };
  217. }
  218. /**
  219. * For all other scenarios don't change the selection.
  220. */
  221. return selectedSource;
  222. }
  223. /**
  224. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  225. * callback with a selectedSource, if any.
  226. *
  227. * @param {string} [id] - The id of the DesktopCapturerSource to pass into
  228. * the onSourceChoose callback.
  229. * @param {string} type - The type of the DesktopCapturerSource to pass into
  230. * the onSourceChoose callback.
  231. * @param {boolean} screenShareAudio - Whether or not to add system audio to
  232. * screen sharing session.
  233. * @returns {void}
  234. */
  235. _onCloseModal(id = '', type?: string, screenShareAudio = false) {
  236. this.props.onSourceChoose(id, type, screenShareAudio);
  237. this.props.dispatch(hideDialog());
  238. }
  239. /**
  240. * Sets the currently selected DesktopCapturerSource.
  241. *
  242. * @param {string} id - The id of DesktopCapturerSource.
  243. * @param {string} type - The type of DesktopCapturerSource.
  244. * @returns {void}
  245. */
  246. _onPreviewClick(id: string, type: string) {
  247. this.setState({
  248. selectedSource: {
  249. id,
  250. type
  251. }
  252. });
  253. }
  254. /**
  255. * Request to close the modal and execute callbacks with the selected source
  256. * id.
  257. *
  258. * @returns {void}
  259. */
  260. _onSubmit() {
  261. const { selectedSource: { id, type }, screenShareAudio } = this.state;
  262. this._onCloseModal(id, type, screenShareAudio);
  263. }
  264. /**
  265. * Stores the selected tab and updates the selected source via
  266. * {@code _getSelectedSource}.
  267. *
  268. * @param {string} id - The id of the newly selected tab.
  269. * @returns {void}
  270. */
  271. _onTabSelected(id: string) {
  272. const { sources } = this.state;
  273. // When we change tabs also reset the screenShareAudio state so we don't
  274. // use the option from one tab when sharing from another.
  275. this.setState({
  276. screenShareAudio: false,
  277. selectedSource: this._getSelectedSource(sources, id),
  278. // select type `window` or `screen` from id
  279. selectedTab: id
  280. });
  281. }
  282. /**
  283. * Set the screenSharingAudio state indicating whether or not to also share
  284. * system audio.
  285. *
  286. * @param {boolean} checked - Share audio or not.
  287. * @returns {void}
  288. */
  289. _onShareAudioChecked(checked: boolean) {
  290. this.setState({ screenShareAudio: checked });
  291. }
  292. /**
  293. * Configures and renders the tabs for display.
  294. *
  295. * @private
  296. * @returns {ReactElement}
  297. */
  298. _renderTabs() {
  299. const { types } = this.state;
  300. const { t } = this.props;
  301. const tabs
  302. = types.map(
  303. type => {
  304. return {
  305. accessibilityLabel: t(TAB_LABELS[type as keyof typeof TAB_LABELS]),
  306. id: `${type}`,
  307. controlsId: `${type}-panel`,
  308. label: t(TAB_LABELS[type as keyof typeof TAB_LABELS])
  309. };
  310. });
  311. return (
  312. <Tabs
  313. accessibilityLabel = { t('dialog.sharingTabs') }
  314. className = 'desktop-picker-tabs-container'
  315. onChange = { this._onTabSelected }
  316. selected = { `${this.state.selectedTab}` }
  317. tabs = { tabs } />
  318. );
  319. }
  320. /**
  321. * Create an interval to update known available DesktopCapturerSources.
  322. *
  323. * @private
  324. * @returns {void}
  325. */
  326. _startPolling() {
  327. this._stopPolling();
  328. this._updateSources();
  329. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  330. }
  331. /**
  332. * Cancels the interval to update DesktopCapturerSources.
  333. *
  334. * @private
  335. * @returns {void}
  336. */
  337. _stopPolling() {
  338. window.clearInterval(this._poller);
  339. this._poller = null;
  340. }
  341. /**
  342. * Obtains the desktop sources and updates state with them.
  343. *
  344. * @private
  345. * @returns {void}
  346. */
  347. _updateSources() {
  348. const { types } = this.state;
  349. const options = {
  350. types,
  351. thumbnailSize: THUMBNAIL_SIZE
  352. };
  353. if (types.length > 0) {
  354. obtainDesktopSources(options)
  355. .then((sources: any) => {
  356. const selectedSource = this._getSelectedSource(sources);
  357. this.setState({
  358. selectedSource,
  359. sources
  360. });
  361. })
  362. .catch((error: any) => logger.log(error));
  363. }
  364. }
  365. }
  366. export default translate(connect()(DesktopPicker));