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

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