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 10KB

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