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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. selectedSourceId: ''
  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.selectedSourceId
  106. && nextProps.sources.screen.length) {
  107. this.setState({
  108. selectedSourceId: nextProps.sources.screen[0].id
  109. });
  110. }
  111. }
  112. /**
  113. * Clean up component and DesktopCapturerSource store state.
  114. *
  115. * @inheritdoc
  116. */
  117. componentWillUnmount() {
  118. this._stopPolling();
  119. this.props.dispatch(resetDesktopSources());
  120. }
  121. /**
  122. * Implements React's {@link Component#render()}.
  123. *
  124. * @inheritdoc
  125. */
  126. render() {
  127. return (
  128. <Dialog
  129. isModal = { false }
  130. okTitleKey = 'dialog.Share'
  131. onCancel = { this._onCloseModal }
  132. onSubmit = { this._onSubmit }
  133. titleKey = 'dialog.shareYourScreen'
  134. width = 'medium' >
  135. { this._renderTabs() }
  136. </Dialog>
  137. );
  138. }
  139. /**
  140. * Dispatches an action to hide the DesktopPicker and invokes the passed in
  141. * callback with a selectedSourceId, if any.
  142. *
  143. * @param {string} id - The id of the DesktopCapturerSource to pass into the
  144. * onSourceChoose callback.
  145. * @returns {void}
  146. */
  147. _onCloseModal(id = '') {
  148. this.props.onSourceChoose(id);
  149. this.props.dispatch(hideDialog());
  150. }
  151. /**
  152. * Sets the currently selected DesktopCapturerSource.
  153. *
  154. * @param {string} id - The id of DesktopCapturerSource.
  155. * @returns {void}
  156. */
  157. _onPreviewClick(id) {
  158. this.setState({ selectedSourceId: id });
  159. }
  160. /**
  161. * Request to close the modal and execute callbacks with the selected source
  162. * id.
  163. *
  164. * @returns {void}
  165. */
  166. _onSubmit() {
  167. this._onCloseModal(this.state.selectedSourceId);
  168. }
  169. /**
  170. * Configures and renders the tabs for display.
  171. *
  172. * @private
  173. * @returns {ReactElement}
  174. */
  175. _renderTabs() {
  176. const { selectedSourceId } = this.state;
  177. const { sources, t } = this.props;
  178. const tabs
  179. = TABS_TO_POPULATE.map(({ defaultSelected, label, type }) => {
  180. return {
  181. content: <DesktopPickerPane
  182. key = { type }
  183. onClick = { this._onPreviewClick }
  184. onDoubleClick = { this._onCloseModal }
  185. selectedSourceId = { selectedSourceId }
  186. sources = { sources[type] || [] }
  187. type = { type } />,
  188. defaultSelected,
  189. label: t(label)
  190. };
  191. });
  192. return <Tabs tabs = { tabs } />;
  193. }
  194. /**
  195. * Create an interval to update knwon available DesktopCapturerSources.
  196. *
  197. * @private
  198. * @returns {void}
  199. */
  200. _startPolling() {
  201. this._stopPolling();
  202. this._poller = window.setInterval(this._updateSources, UPDATE_INTERVAL);
  203. }
  204. /**
  205. * Cancels the interval to update DesktopCapturerSources.
  206. *
  207. * @private
  208. * @returns {void}
  209. */
  210. _stopPolling() {
  211. window.clearInterval(this._poller);
  212. this._poller = null;
  213. }
  214. /**
  215. * Dispatches an action to get currently available DesktopCapturerSources.
  216. *
  217. * @private
  218. * @returns {void}
  219. */
  220. _updateSources() {
  221. this.props.dispatch(obtainDesktopSources(
  222. TYPES_TO_FETCH,
  223. {
  224. THUMBNAIL_SIZE
  225. }
  226. ));
  227. }
  228. }
  229. /**
  230. * Maps (parts of) the Redux state to the associated DesktopPicker's props.
  231. *
  232. * @param {Object} state - Redux state.
  233. * @private
  234. * @returns {{
  235. * sources: Object
  236. * }}
  237. */
  238. function _mapStateToProps(state) {
  239. return {
  240. sources: state['features/desktop-picker']
  241. };
  242. }
  243. export default translate(connect(_mapStateToProps)(DesktopPicker));