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

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