您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DesktopPicker.tsx 12KB

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