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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 { obtainDesktopSources, resetDesktopSources } from '../actions';
  9. import DesktopPickerPane from './DesktopPickerPane';
  10. const THUMBNAIL_SIZE = {
  11. height: 300,
  12. width: 300
  13. };
  14. const UPDATE_INTERVAL = 1000;
  15. type TabConfiguration = {
  16. defaultSelected?: boolean,
  17. label: string,
  18. type: string
  19. };
  20. const TAB_CONFIGURATIONS: Array<TabConfiguration> = [
  21. {
  22. /**
  23. * The indicator which determines whether this tab configuration is
  24. * selected by default.
  25. *
  26. * @type {boolean}
  27. */
  28. defaultSelected: true,
  29. label: 'dialog.yourEntireScreen',
  30. type: 'screen'
  31. },
  32. {
  33. label: 'dialog.applicationWindow',
  34. type: 'window'
  35. }
  36. ];
  37. const VALID_TYPES = TAB_CONFIGURATIONS.map(c => c.type);
  38. /**
  39. * React component for DesktopPicker.
  40. *
  41. * @extends Component
  42. */
  43. class DesktopPicker extends Component {
  44. /**
  45. * Default values for DesktopPicker component's properties.
  46. *
  47. * @static
  48. */
  49. static defaultProps = {
  50. options: {}
  51. };
  52. /**
  53. * DesktopPicker component's property types.
  54. *
  55. * @static
  56. */
  57. static propTypes = {
  58. /**
  59. * Used to request DesktopCapturerSources.
  60. */
  61. dispatch: PropTypes.func,
  62. /**
  63. * The callback to be invoked when the component is closed or when
  64. * a DesktopCapturerSource has been chosen.
  65. */
  66. onSourceChoose: PropTypes.func,
  67. /**
  68. * An object with options related to desktop sharing.
  69. */
  70. options: PropTypes.object,
  71. /**
  72. * An object with arrays of DesktopCapturerSources. The key should be
  73. * the source type.
  74. */
  75. sources: PropTypes.object,
  76. /**
  77. * Used to obtain translations.
  78. */
  79. t: PropTypes.func
  80. };
  81. _poller = null;
  82. state = {
  83. selectedSource: {},
  84. tabsToPopulate: [],
  85. typesToFetch: []
  86. };
  87. /**
  88. * Initializes a new DesktopPicker instance.
  89. *
  90. * @param {Object} props - The read-only properties with which the new
  91. * instance is to be initialized.
  92. */
  93. constructor(props) {
  94. super(props);
  95. // Bind event handlers so they are only bound once per instance.
  96. this._onCloseModal = this._onCloseModal.bind(this);
  97. this._onPreviewClick = this._onPreviewClick.bind(this);
  98. this._onSubmit = this._onSubmit.bind(this);
  99. this._updateSources = this._updateSources.bind(this);
  100. }
  101. /**
  102. * Perform an immediate update request for DesktopCapturerSources and begin
  103. * requesting updates at an interval.
  104. *
  105. * @inheritdoc
  106. */
  107. componentWillMount() {
  108. const { desktopSharingSources } = this.props.options;
  109. this._onSourceTypesConfigChanged(
  110. desktopSharingSources);
  111. this._updateSources();
  112. this._startPolling();
  113. }
  114. /**
  115. * Notifies this mounted React Component that it will receive new props.
  116. * Sets a default selected source if one is not already set.
  117. *
  118. * @inheritdoc
  119. * @param {Object} nextProps - The read-only React Component props that this
  120. * instance will receive.
  121. * @returns {void}
  122. */
  123. componentWillReceiveProps(nextProps) {
  124. if (!this.state.selectedSource.id
  125. && nextProps.sources.screen.length) {
  126. this.setState({
  127. selectedSource: {
  128. id: nextProps.sources.screen[0].id,
  129. type: 'screen'
  130. }
  131. });
  132. }
  133. const { desktopSharingSources } = this.props.options;
  134. this._onSourceTypesConfigChanged(
  135. desktopSharingSources);
  136. }
  137. /**
  138. * Clean up component and DesktopCapturerSource store state.
  139. *
  140. * @inheritdoc
  141. */
  142. componentWillUnmount() {
  143. this._stopPolling();
  144. this.props.dispatch(resetDesktopSources());
  145. }
  146. /**
  147. * Implements React's {@link Component#render()}.
  148. *
  149. * @inheritdoc
  150. */
  151. render() {
  152. return (
  153. <Dialog
  154. isModal = { false }
  155. okTitleKey = 'dialog.Share'
  156. onCancel = { this._onCloseModal }
  157. onSubmit = { this._onSubmit }
  158. titleKey = 'dialog.shareYourScreen'
  159. width = 'medium' >
  160. { this._renderTabs() }
  161. </Dialog>
  162. );
  163. }
  164. _onCloseModal: (?string, string) => void;
  165. /**
  166. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  167. * callback with a selectedSource, if any.
  168. *
  169. * @param {string} [id] - The id of the DesktopCapturerSource to pass into
  170. * the onSourceChoose callback.
  171. * @param {string} type - The type of the DesktopCapturerSource to pass into
  172. * the onSourceChoose callback.
  173. * @returns {void}
  174. */
  175. _onCloseModal(id = '', type) {
  176. this.props.onSourceChoose(id, type);
  177. this.props.dispatch(hideDialog());
  178. }
  179. _onPreviewClick: (string, string) => void;
  180. /**
  181. * Sets the currently selected DesktopCapturerSource.
  182. *
  183. * @param {string} id - The id of DesktopCapturerSource.
  184. * @param {string} type - The type of DesktopCapturerSource.
  185. * @returns {void}
  186. */
  187. _onPreviewClick(id, type) {
  188. this.setState({
  189. selectedSource: {
  190. id,
  191. type
  192. }
  193. });
  194. }
  195. /**
  196. * Handles changing of allowed desktop sharing source types.
  197. *
  198. * @param {Array<string>} desktopSharingSourceTypes - The types that will be
  199. * fetched and displayed.
  200. * @returns {void}
  201. */
  202. _onSourceTypesConfigChanged(desktopSharingSourceTypes = []) {
  203. const tabsToPopulate
  204. = TAB_CONFIGURATIONS.filter(({ type }) =>
  205. desktopSharingSourceTypes.includes(type)
  206. && VALID_TYPES.includes(type));
  207. this.setState({
  208. tabsToPopulate,
  209. typesToFetch: tabsToPopulate.map(c => c.type)
  210. });
  211. }
  212. _onSubmit: () => void;
  213. /**
  214. * Request to close the modal and execute callbacks with the selected source
  215. * id.
  216. *
  217. * @returns {void}
  218. */
  219. _onSubmit() {
  220. const { id, type } = this.state.selectedSource;
  221. this._onCloseModal(id, type);
  222. }
  223. /**
  224. * Configures and renders the tabs for display.
  225. *
  226. * @private
  227. * @returns {ReactElement}
  228. */
  229. _renderTabs() {
  230. const { selectedSource } = this.state;
  231. const { sources, t } = this.props;
  232. const tabs
  233. = this.state.tabsToPopulate.map(
  234. ({ defaultSelected, label, type }) => {
  235. return {
  236. content: <DesktopPickerPane
  237. key = { type }
  238. onClick = { this._onPreviewClick }
  239. onDoubleClick = { this._onCloseModal }
  240. selectedSourceId = { selectedSource.id }
  241. sources = { sources[type] || [] }
  242. type = { type } />,
  243. defaultSelected,
  244. label: t(label)
  245. };
  246. });
  247. return <Tabs tabs = { tabs } />;
  248. }
  249. /**
  250. * Create an interval to update knwon available DesktopCapturerSources.
  251. *
  252. * @private
  253. * @returns {void}
  254. */
  255. _startPolling() {
  256. this._stopPolling();
  257. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  258. }
  259. /**
  260. * Cancels the interval to update DesktopCapturerSources.
  261. *
  262. * @private
  263. * @returns {void}
  264. */
  265. _stopPolling() {
  266. window.clearInterval(this._poller);
  267. this._poller = null;
  268. }
  269. _updateSources: () => void;
  270. /**
  271. * Dispatches an action to get currently available DesktopCapturerSources.
  272. *
  273. * @private
  274. * @returns {void}
  275. */
  276. _updateSources() {
  277. this.props.dispatch(obtainDesktopSources(
  278. this.state.typesToFetch,
  279. {
  280. THUMBNAIL_SIZE
  281. }
  282. ));
  283. }
  284. }
  285. /**
  286. * Maps (parts of) the Redux state to the associated DesktopPicker's props.
  287. *
  288. * @param {Object} state - Redux state.
  289. * @private
  290. * @returns {{
  291. * sources: Object
  292. * }}
  293. */
  294. function _mapStateToProps(state) {
  295. return {
  296. sources: state['features/desktop-picker']
  297. };
  298. }
  299. export default translate(connect(_mapStateToProps)(DesktopPicker));