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

DesktopPicker.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* eslint-disable lines-around-comment */
  2. import Tabs from '@atlaskit/tabs';
  3. import React, { PureComponent } from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { IStore } from '../../app/types';
  6. import { hideDialog } from '../../base/dialog/actions';
  7. import { translate } from '../../base/i18n/functions';
  8. import { connect } from '../../base/redux/functions';
  9. import Dialog from '../../base/ui/components/web/Dialog';
  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: number;
  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: 0,
  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. return (
  166. <Dialog
  167. ok = {{
  168. disabled: Boolean(!this.state.selectedSource.id),
  169. translationKey: 'dialog.Share'
  170. }}
  171. onCancel = { this._onCloseModal }
  172. onSubmit = { this._onSubmit }
  173. size = 'large'
  174. titleKey = 'dialog.shareYourScreen'>
  175. { this._renderTabs() }
  176. </Dialog>
  177. );
  178. }
  179. /**
  180. * Computes the selected source.
  181. *
  182. * @param {Object} sources - The available sources.
  183. * @returns {Object} The selectedSource value.
  184. */
  185. _getSelectedSource(sources: any = {}) {
  186. const { selectedSource } = this.state;
  187. /**
  188. * If there are no sources for this type (or no sources for any type)
  189. * we can't select anything.
  190. */
  191. if (!Array.isArray(sources[this._selectedTabType as keyof typeof sources])
  192. || sources[this._selectedTabType as keyof typeof sources].length <= 0) {
  193. return {};
  194. }
  195. /**
  196. * Select the first available source for this type in the following
  197. * scenarios:
  198. * 1) Nothing is yet selected.
  199. * 2) Tab change.
  200. * 3) The selected source is no longer available.
  201. */
  202. if (!selectedSource // scenario 1)
  203. || selectedSource.type !== this._selectedTabType // scenario 2)
  204. || !sources[this._selectedTabType].some( // scenario 3)
  205. (source: any) => source.id === selectedSource.id)) {
  206. return {
  207. id: sources[this._selectedTabType][0].id,
  208. type: this._selectedTabType
  209. };
  210. }
  211. /**
  212. * For all other scenarios don't change the selection.
  213. */
  214. return selectedSource;
  215. }
  216. /**
  217. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  218. * callback with a selectedSource, if any.
  219. *
  220. * @param {string} [id] - The id of the DesktopCapturerSource to pass into
  221. * the onSourceChoose callback.
  222. * @param {string} type - The type of the DesktopCapturerSource to pass into
  223. * the onSourceChoose callback.
  224. * @param {boolean} screenShareAudio - Whether or not to add system audio to
  225. * screen sharing session.
  226. * @returns {void}
  227. */
  228. _onCloseModal(id = '', type?: string, screenShareAudio = false) {
  229. this.props.onSourceChoose(id, type, screenShareAudio);
  230. this.props.dispatch(hideDialog());
  231. }
  232. /**
  233. * Sets the currently selected DesktopCapturerSource.
  234. *
  235. * @param {string} id - The id of DesktopCapturerSource.
  236. * @param {string} type - The type of DesktopCapturerSource.
  237. * @returns {void}
  238. */
  239. _onPreviewClick(id: string, type: string) {
  240. this.setState({
  241. selectedSource: {
  242. id,
  243. type
  244. }
  245. });
  246. }
  247. /**
  248. * Request to close the modal and execute callbacks with the selected source
  249. * id.
  250. *
  251. * @returns {void}
  252. */
  253. _onSubmit() {
  254. const { selectedSource: { id, type }, screenShareAudio } = this.state;
  255. this._onCloseModal(id, type, screenShareAudio);
  256. }
  257. /**
  258. * Stores the selected tab and updates the selected source via
  259. * {@code _getSelectedSource}.
  260. *
  261. * @param {Object} _tab - The configuration passed into atlaskit tabs to
  262. * describe how to display the selected tab.
  263. * @param {number} tabIndex - The index of the tab within the array of
  264. * displayed tabs.
  265. * @returns {void}
  266. */
  267. _onTabSelected(_tab: Object, tabIndex: number) {
  268. const { types, sources } = this.state;
  269. this._selectedTabType = types[tabIndex];
  270. // When we change tabs also reset the screenShareAudio state so we don't
  271. // use the option from one tab when sharing from another.
  272. this.setState({
  273. screenShareAudio: false,
  274. selectedSource: this._getSelectedSource(sources),
  275. selectedTab: tabIndex
  276. });
  277. }
  278. /**
  279. * Set the screenSharingAudio state indicating whether or not to also share
  280. * system audio.
  281. *
  282. * @param {boolean} checked - Share audio or not.
  283. * @returns {void}
  284. */
  285. _onShareAudioChecked(checked: boolean) {
  286. this.setState({ screenShareAudio: checked });
  287. }
  288. /**
  289. * Configures and renders the tabs for display.
  290. *
  291. * @private
  292. * @returns {ReactElement}
  293. */
  294. _renderTabs() {
  295. const { selectedSource, sources, types } = this.state;
  296. const { t } = this.props;
  297. const tabs
  298. = types.map(
  299. type => {
  300. return {
  301. content: <DesktopPickerPane
  302. key = { type }
  303. onClick = { this._onPreviewClick }
  304. onDoubleClick = { this._onSubmit }
  305. onShareAudioChecked = { this._onShareAudioChecked }
  306. selectedSourceId = { selectedSource.id }
  307. sources = { sources[type as keyof typeof sources] }
  308. type = { type } />,
  309. label: t(TAB_LABELS[type as keyof typeof TAB_LABELS])
  310. };
  311. });
  312. return (
  313. <Tabs
  314. onSelect = { this._onTabSelected }
  315. selected = { this.state.selectedTab }
  316. tabs = { tabs } />);
  317. }
  318. /**
  319. * Create an interval to update known available DesktopCapturerSources.
  320. *
  321. * @private
  322. * @returns {void}
  323. */
  324. _startPolling() {
  325. this._stopPolling();
  326. this._updateSources();
  327. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  328. }
  329. /**
  330. * Cancels the interval to update DesktopCapturerSources.
  331. *
  332. * @private
  333. * @returns {void}
  334. */
  335. _stopPolling() {
  336. window.clearInterval(this._poller);
  337. this._poller = null;
  338. }
  339. /**
  340. * Obtains the desktop sources and updates state with them.
  341. *
  342. * @private
  343. * @returns {void}
  344. */
  345. _updateSources() {
  346. const { types } = this.state;
  347. if (types.length > 0) {
  348. obtainDesktopSources(
  349. this.state.types,
  350. { thumbnailSize: THUMBNAIL_SIZE }
  351. )
  352. .then((sources: any) => {
  353. const selectedSource = this._getSelectedSource(sources);
  354. // TODO: Maybe check if we have stopped the timer and unmounted
  355. // the component.
  356. this.setState({
  357. sources,
  358. selectedSource
  359. });
  360. })
  361. .catch(() => { /* ignore */ });
  362. }
  363. }
  364. }
  365. export default translate(connect()(DesktopPicker));