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.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // @flow
  2. import Tabs from '@atlaskit/tabs';
  3. import React, { PureComponent } from 'react';
  4. import type { Dispatch } from 'redux';
  5. import { Dialog, hideDialog } from '../../base/dialog';
  6. import { translate } from '../../base/i18n';
  7. import { connect } from '../../base/redux';
  8. import { obtainDesktopSources } from '../functions';
  9. import DesktopPickerPane from './DesktopPickerPane';
  10. /**
  11. * The size of the requested thumbnails.
  12. *
  13. * @type {Object}
  14. */
  15. const THUMBNAIL_SIZE = {
  16. height: 300,
  17. width: 300
  18. };
  19. /**
  20. * The sources polling interval in ms.
  21. *
  22. * @type {int}
  23. */
  24. const UPDATE_INTERVAL = 2000;
  25. /**
  26. * The default selected tab.
  27. *
  28. * @type {string}
  29. */
  30. const DEFAULT_TAB_TYPE = 'screen';
  31. const TAB_LABELS = {
  32. screen: 'dialog.yourEntireScreen',
  33. window: 'dialog.applicationWindow'
  34. };
  35. const VALID_TYPES = Object.keys(TAB_LABELS);
  36. /**
  37. * The type of the React {@code Component} props of {@link DesktopPicker}.
  38. */
  39. type Props = {
  40. /**
  41. * An array with desktop sharing sources to be displayed.
  42. */
  43. desktopSharingSources: Array<string>,
  44. /**
  45. * Used to request DesktopCapturerSources.
  46. */
  47. dispatch: Dispatch<any>,
  48. /**
  49. * The callback to be invoked when the component is closed or when a
  50. * DesktopCapturerSource has been chosen.
  51. */
  52. onSourceChoose: Function,
  53. /**
  54. * Used to obtain translations.
  55. */
  56. t: Function
  57. };
  58. /**
  59. * The type of the React {@code Component} state of {@link DesktopPicker}.
  60. */
  61. type State = {
  62. /**
  63. * The state of the audio screen share checkbox.
  64. */
  65. screenShareAudio: boolean,
  66. /**
  67. * The currently highlighted DesktopCapturerSource.
  68. */
  69. selectedSource: Object,
  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. * @extends Component
  87. */
  88. class DesktopPicker extends PureComponent<Props, State> {
  89. /**
  90. * Implements React's {@link Component#getDerivedStateFromProps()}.
  91. *
  92. * @inheritdoc
  93. */
  94. static getDerivedStateFromProps(props) {
  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 = []) {
  107. return types.filter(
  108. type => VALID_TYPES.includes(type));
  109. }
  110. _poller = null;
  111. state = {
  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: Props) {
  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. isModal = { false }
  168. okDisabled = { Boolean(!this.state.selectedSource.id) }
  169. okKey = 'dialog.Share'
  170. onCancel = { this._onCloseModal }
  171. onSubmit = { this._onSubmit }
  172. titleKey = 'dialog.shareYourScreen'
  173. width = 'medium' >
  174. { this._renderTabs() }
  175. </Dialog>
  176. );
  177. }
  178. /**
  179. * Computates the selected source.
  180. *
  181. * @param {Object} sources - The available sources.
  182. * @returns {Object} The selectedSource value.
  183. */
  184. _getSelectedSource(sources = {}) {
  185. const { selectedSource } = this.state;
  186. /**
  187. * If there are no sources for this type (or no sources for any type)
  188. * we can't select anything.
  189. */
  190. if (!Array.isArray(sources[this._selectedTabType])
  191. || sources[this._selectedTabType].length <= 0) {
  192. return {};
  193. }
  194. /**
  195. * Select the first available source for this type in the following
  196. * scenarios:
  197. * 1) Nothing is yet selected.
  198. * 2) Tab change.
  199. * 3) The selected source is no longer available.
  200. */
  201. if (!selectedSource // scenario 1)
  202. || selectedSource.type !== this._selectedTabType // scenario 2)
  203. || !sources[this._selectedTabType].some( // scenario 3)
  204. source => source.id === selectedSource.id)) {
  205. return {
  206. id: sources[this._selectedTabType][0].id,
  207. type: this._selectedTabType
  208. };
  209. }
  210. /**
  211. * For all other scenarios don't change the selection.
  212. */
  213. return selectedSource;
  214. }
  215. _onCloseModal: (?string, string, ?boolean) => void;
  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, screenShareAudio = false) {
  229. this.props.onSourceChoose(id, type, screenShareAudio);
  230. this.props.dispatch(hideDialog());
  231. }
  232. _onPreviewClick: (string, string) => void;
  233. /**
  234. * Sets the currently selected DesktopCapturerSource.
  235. *
  236. * @param {string} id - The id of DesktopCapturerSource.
  237. * @param {string} type - The type of DesktopCapturerSource.
  238. * @returns {void}
  239. */
  240. _onPreviewClick(id, type) {
  241. this.setState({
  242. selectedSource: {
  243. id,
  244. type
  245. }
  246. });
  247. }
  248. _onSubmit: () => void;
  249. /**
  250. * Request to close the modal and execute callbacks with the selected source
  251. * id.
  252. *
  253. * @returns {void}
  254. */
  255. _onSubmit() {
  256. const { selectedSource: { id, type }, screenShareAudio } = this.state;
  257. this._onCloseModal(id, type, screenShareAudio);
  258. }
  259. _onTabSelected: () => void;
  260. /**
  261. * Stores the selected tab and updates the selected source via
  262. * {@code _getSelectedSource}.
  263. *
  264. * @param {Object} tab - The configuration passed into atlaskit tabs to
  265. * describe how to display the selected tab.
  266. * @param {number} tabIndex - The index of the tab within the array of
  267. * displayed tabs.
  268. * @returns {void}
  269. */
  270. _onTabSelected(tab, tabIndex) { // eslint-disable-line no-unused-vars
  271. const { types, sources } = this.state;
  272. this._selectedTabType = types[tabIndex];
  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),
  278. selectedTab: tabIndex
  279. });
  280. }
  281. _onShareAudioChecked: (boolean) => void;
  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) {
  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 { selectedSource, sources, types } = this.state;
  300. const { t } = this.props;
  301. const tabs
  302. = types.map(
  303. type => {
  304. return {
  305. content: <DesktopPickerPane
  306. key = { type }
  307. onClick = { this._onPreviewClick }
  308. onDoubleClick = { this._onSubmit }
  309. onShareAudioChecked = { this._onShareAudioChecked }
  310. selectedSourceId = { selectedSource.id }
  311. sources = { sources[type] }
  312. type = { type } />,
  313. label: t(TAB_LABELS[type])
  314. };
  315. });
  316. return (
  317. <Tabs
  318. onSelect = { this._onTabSelected }
  319. selected = { this.state.selectedTab }
  320. tabs = { tabs } />);
  321. }
  322. /**
  323. * Create an interval to update known available DesktopCapturerSources.
  324. *
  325. * @private
  326. * @returns {void}
  327. */
  328. _startPolling() {
  329. this._stopPolling();
  330. this._updateSources();
  331. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  332. }
  333. /**
  334. * Cancels the interval to update DesktopCapturerSources.
  335. *
  336. * @private
  337. * @returns {void}
  338. */
  339. _stopPolling() {
  340. window.clearInterval(this._poller);
  341. this._poller = null;
  342. }
  343. _updateSources: () => void;
  344. /**
  345. * Obtains the desktop sources and updates state with them.
  346. *
  347. * @private
  348. * @returns {void}
  349. */
  350. _updateSources() {
  351. const { types } = this.state;
  352. if (types.length > 0) {
  353. obtainDesktopSources(
  354. this.state.types,
  355. { thumbnailSize: THUMBNAIL_SIZE }
  356. )
  357. .then(sources => {
  358. const selectedSource = this._getSelectedSource(sources);
  359. // TODO: Maybe check if we have stopped the timer and unmounted
  360. // the component.
  361. this.setState({
  362. sources,
  363. selectedSource
  364. });
  365. })
  366. .catch(() => { /* ignore */ });
  367. }
  368. }
  369. }
  370. export default translate(connect()(DesktopPicker));