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

DesktopPicker.js 10.0KB

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