Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DesktopPicker.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* global config */
  2. import Tabs from '@atlaskit/tabs';
  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 CONFIGURED_TYPES = config.desktopSharingChromeSources || [];
  32. const VALID_TYPES = TAB_CONFIGURATIONS.map(c => c.type);
  33. const TABS_TO_POPULATE
  34. = TAB_CONFIGURATIONS.filter(
  35. c => CONFIGURED_TYPES.includes(c.type) && VALID_TYPES.includes(c.type));
  36. const TYPES_TO_FETCH = TABS_TO_POPULATE.map(c => c.type);
  37. /**
  38. * React component for DesktopPicker.
  39. *
  40. * @extends Component
  41. */
  42. class DesktopPicker extends Component {
  43. /**
  44. * DesktopPicker component's property types.
  45. *
  46. * @static
  47. */
  48. static propTypes = {
  49. /**
  50. * Used to request DesktopCapturerSources.
  51. */
  52. dispatch: React.PropTypes.func,
  53. /**
  54. * The callback to be invoked when the component is closed or when
  55. * a DesktopCapturerSource has been chosen.
  56. */
  57. onSourceChoose: React.PropTypes.func,
  58. /**
  59. * An object with arrays of DesktopCapturerSources. The key should be
  60. * the source type.
  61. */
  62. sources: React.PropTypes.object,
  63. /**
  64. * Used to obtain translations.
  65. */
  66. t: React.PropTypes.func
  67. };
  68. /**
  69. * Initializes a new DesktopPicker instance.
  70. *
  71. * @param {Object} props - The read-only properties with which the new
  72. * instance is to be initialized.
  73. */
  74. constructor(props) {
  75. super(props);
  76. this.state = {
  77. selectedSource: {}
  78. };
  79. this._poller = null;
  80. this._onCloseModal = this._onCloseModal.bind(this);
  81. this._onPreviewClick = this._onPreviewClick.bind(this);
  82. this._onSubmit = this._onSubmit.bind(this);
  83. this._updateSources = this._updateSources.bind(this);
  84. }
  85. /**
  86. * Perform an immediate update request for DesktopCapturerSources and begin
  87. * requesting updates at an interval.
  88. *
  89. * @inheritdoc
  90. */
  91. componentWillMount() {
  92. this._updateSources();
  93. this._startPolling();
  94. }
  95. /**
  96. * Notifies this mounted React Component that it will receive new props.
  97. * Sets a default selected source if one is not already set.
  98. *
  99. * @inheritdoc
  100. * @param {Object} nextProps - The read-only React Component props that this
  101. * instance will receive.
  102. * @returns {void}
  103. */
  104. componentWillReceiveProps(nextProps) {
  105. if (!this.state.selectedSource.id
  106. && nextProps.sources.screen.length) {
  107. this.setState({
  108. selectedSource: {
  109. id: nextProps.sources.screen[0].id,
  110. type: 'screen'
  111. }
  112. });
  113. }
  114. }
  115. /**
  116. * Clean up component and DesktopCapturerSource store state.
  117. *
  118. * @inheritdoc
  119. */
  120. componentWillUnmount() {
  121. this._stopPolling();
  122. this.props.dispatch(resetDesktopSources());
  123. }
  124. /**
  125. * Implements React's {@link Component#render()}.
  126. *
  127. * @inheritdoc
  128. */
  129. render() {
  130. return (
  131. <Dialog
  132. isModal = { false }
  133. okTitleKey = 'dialog.Share'
  134. onCancel = { this._onCloseModal }
  135. onSubmit = { this._onSubmit }
  136. titleKey = 'dialog.shareYourScreen'
  137. width = 'medium' >
  138. { this._renderTabs() }
  139. </Dialog>
  140. );
  141. }
  142. /**
  143. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  144. * callback with a selectedSource, if any.
  145. *
  146. * @param {string} id - The id of the DesktopCapturerSource to pass into the
  147. * onSourceChoose callback.
  148. * @param {string} type - The type of the DesktopCapturerSource to pass into
  149. * the onSourceChoose callback.
  150. * @returns {void}
  151. */
  152. _onCloseModal(id, type) {
  153. this.props.onSourceChoose(id, type);
  154. this.props.dispatch(hideDialog());
  155. }
  156. /**
  157. * Sets the currently selected DesktopCapturerSource.
  158. *
  159. * @param {string} id - The id of DesktopCapturerSource.
  160. * @param {string} type - The type of DesktopCapturerSource.
  161. * @returns {void}
  162. */
  163. _onPreviewClick(id, type) {
  164. this.setState({
  165. selectedSource: {
  166. id,
  167. type
  168. }
  169. });
  170. }
  171. /**
  172. * Request to close the modal and execute callbacks with the selected source
  173. * id.
  174. *
  175. * @returns {void}
  176. */
  177. _onSubmit() {
  178. const { id, type } = this.state.selectedSource;
  179. this._onCloseModal(id, type);
  180. }
  181. /**
  182. * Configures and renders the tabs for display.
  183. *
  184. * @private
  185. * @returns {ReactElement}
  186. */
  187. _renderTabs() {
  188. const { selectedSource } = this.state;
  189. const { sources, t } = this.props;
  190. const tabs
  191. = TABS_TO_POPULATE.map(({ defaultSelected, label, type }) => {
  192. return {
  193. content: <DesktopPickerPane
  194. key = { type }
  195. onClick = { this._onPreviewClick }
  196. onDoubleClick = { this._onCloseModal }
  197. selectedSourceId = { selectedSource.id }
  198. sources = { sources[type] || [] }
  199. type = { type } />,
  200. defaultSelected,
  201. label: t(label)
  202. };
  203. });
  204. return <Tabs tabs = { tabs } />;
  205. }
  206. /**
  207. * Create an interval to update knwon available DesktopCapturerSources.
  208. *
  209. * @private
  210. * @returns {void}
  211. */
  212. _startPolling() {
  213. this._stopPolling();
  214. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  215. }
  216. /**
  217. * Cancels the interval to update DesktopCapturerSources.
  218. *
  219. * @private
  220. * @returns {void}
  221. */
  222. _stopPolling() {
  223. window.clearInterval(this._poller);
  224. this._poller = null;
  225. }
  226. /**
  227. * Dispatches an action to get currently available DesktopCapturerSources.
  228. *
  229. * @private
  230. * @returns {void}
  231. */
  232. _updateSources() {
  233. this.props.dispatch(obtainDesktopSources(
  234. TYPES_TO_FETCH,
  235. {
  236. THUMBNAIL_SIZE
  237. }
  238. ));
  239. }
  240. }
  241. /**
  242. * Maps (parts of) the Redux state to the associated DesktopPicker's props.
  243. *
  244. * @param {Object} state - Redux state.
  245. * @private
  246. * @returns {{
  247. * sources: Object
  248. * }}
  249. */
  250. function _mapStateToProps(state) {
  251. return {
  252. sources: state['features/desktop-picker']
  253. };
  254. }
  255. export default translate(connect(_mapStateToProps)(DesktopPicker));