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.3KB

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