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

Labels.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // @flow
  2. import React from 'react';
  3. import { TouchableOpacity, View } from 'react-native';
  4. import { JitsiRecordingConstants } from '../../../base/lib-jitsi-meet';
  5. import { connect } from '../../../base/redux';
  6. import {
  7. isNarrowAspectRatio,
  8. makeAspectRatioAware
  9. } from '../../../base/responsive-ui';
  10. import {
  11. RecordingExpandedLabel
  12. } from '../../../recording';
  13. import { TranscribingExpandedLabel } from '../../../transcribing';
  14. import { VideoQualityExpandedLabel } from '../../../video-quality';
  15. import AbstractLabels, {
  16. _abstractMapStateToProps,
  17. type Props as AbstractLabelsProps
  18. } from '../AbstractLabels';
  19. import { shouldDisplayNotifications } from '../../functions';
  20. import InsecureRoomNameExpandedLabel from './InsecureRoomNameExpandedLabel';
  21. import styles from './styles';
  22. /**
  23. * The type of the React {@code Component} props of {@link Labels}.
  24. */
  25. type Props = AbstractLabelsProps & {
  26. /**
  27. * Function to translate i18n labels.
  28. */
  29. t: Function,
  30. /**
  31. * True if the labels should be visible, false otherwise.
  32. */
  33. _visible: boolean
  34. };
  35. type State = {
  36. /**
  37. * Layout object of the outermost container. For stucture please see:
  38. * https://facebook.github.io/react-native/docs/view#onlayout
  39. */
  40. containerLayout: ?Object,
  41. /**
  42. * Layout objects of the individual labels. This data type contains the same
  43. * structure as the layout is defined here:
  44. * https://facebook.github.io/react-native/docs/view#onlayout
  45. * but keyed with the ID of the label its layout it contains. E.g.
  46. *
  47. * {
  48. * transcribing: {
  49. * { layout: { x, y, width, height } }
  50. * },
  51. * ...
  52. * }
  53. */
  54. labelLayouts: Object,
  55. /**
  56. * Position of the label to render the {@code ExpandedLabel} to.
  57. */
  58. parentPosition: ?number,
  59. /**
  60. * String to show which {@code ExpandedLabel} to be shown. (Equals to the
  61. * label IDs below.)
  62. */
  63. visibleExpandedLabel: ?string
  64. }
  65. const LABEL_ID_QUALITY = 'quality';
  66. const LABEL_ID_RECORDING = 'recording';
  67. const LABEL_ID_STREAMING = 'streaming';
  68. const LABEL_ID_TRANSCRIBING = 'transcribing';
  69. const LABEL_ID_INSECURE_ROOM_NAME = 'insecure-room-name';
  70. /**
  71. * The {@code ExpandedLabel} components to be rendered for the individual
  72. * {@code Label}s.
  73. */
  74. const EXPANDED_LABELS = {
  75. quality: VideoQualityExpandedLabel,
  76. recording: {
  77. component: RecordingExpandedLabel,
  78. props: {
  79. mode: JitsiRecordingConstants.mode.FILE
  80. }
  81. },
  82. streaming: {
  83. component: RecordingExpandedLabel,
  84. props: {
  85. mode: JitsiRecordingConstants.mode.STREAM
  86. }
  87. },
  88. transcribing: TranscribingExpandedLabel,
  89. 'insecure-room-name': InsecureRoomNameExpandedLabel
  90. };
  91. /**
  92. * Timeout to hide the {@ExpandedLabel}.
  93. */
  94. const EXPANDED_LABEL_TIMEOUT = 5000;
  95. /**
  96. * A container that renders the conference indicators, if any.
  97. */
  98. class Labels extends AbstractLabels<Props, State> {
  99. /**
  100. * Timeout for the expanded labels to disappear.
  101. */
  102. expandedLabelTimeout: TimeoutID;
  103. /**
  104. * Instantiates a new instance of {@code Labels}.
  105. *
  106. * @inheritdoc
  107. */
  108. constructor(props: Props) {
  109. super(props);
  110. this.state = {
  111. containerLayout: undefined,
  112. labelLayouts: {},
  113. parentPosition: undefined,
  114. visibleExpandedLabel: undefined
  115. };
  116. this._onTopViewLayout = this._onTopViewLayout.bind(this);
  117. }
  118. /**
  119. * Implements React {@code Component}'s componentWillUnmount.
  120. *
  121. * @inheritdoc
  122. */
  123. componentWillUnmount() {
  124. clearTimeout(this.expandedLabelTimeout);
  125. }
  126. /**
  127. * Implements React {@code Component}'s render.
  128. *
  129. * @inheritdoc
  130. */
  131. render() {
  132. if (!this.props._visible) {
  133. return null;
  134. }
  135. const wide = !isNarrowAspectRatio(this);
  136. const { _filmstripVisible } = this.props;
  137. return (
  138. <View
  139. pointerEvents = 'box-none'
  140. style = { styles.labelWrapper }>
  141. <View
  142. onLayout = { this._onTopViewLayout }
  143. pointerEvents = 'box-none'
  144. style = { [
  145. styles.indicatorContainer,
  146. wide && _filmstripVisible
  147. && styles.indicatorContainerWide
  148. ] }>
  149. <TouchableOpacity
  150. onLayout = { this._createOnLayout(LABEL_ID_RECORDING) }
  151. onPress = { this._createOnPress(LABEL_ID_RECORDING) } >
  152. {
  153. this._renderRecordingLabel(
  154. JitsiRecordingConstants.mode.FILE)
  155. }
  156. </TouchableOpacity>
  157. <TouchableOpacity
  158. onLayout = { this._createOnLayout(LABEL_ID_STREAMING) }
  159. onPress = { this._createOnPress(LABEL_ID_STREAMING) } >
  160. {
  161. this._renderRecordingLabel(
  162. JitsiRecordingConstants.mode.STREAM)
  163. }
  164. </TouchableOpacity>
  165. <TouchableOpacity
  166. onLayout = {
  167. this._createOnLayout(LABEL_ID_TRANSCRIBING)
  168. }
  169. onPress = {
  170. this._createOnPress(LABEL_ID_TRANSCRIBING)
  171. } >
  172. {
  173. this._renderTranscribingLabel()
  174. }
  175. </TouchableOpacity>
  176. <TouchableOpacity
  177. onLayout = {
  178. this._createOnLayout(LABEL_ID_INSECURE_ROOM_NAME)
  179. }
  180. onPress = {
  181. this._createOnPress(LABEL_ID_INSECURE_ROOM_NAME)
  182. } >
  183. {
  184. this._renderInsecureRoomNameLabel()
  185. }
  186. </TouchableOpacity>
  187. <TouchableOpacity
  188. onLayout = {
  189. this._createOnLayout(LABEL_ID_QUALITY) }
  190. onPress = {
  191. this._createOnPress(LABEL_ID_QUALITY) } >
  192. { this._renderVideoQualityLabel() }
  193. </TouchableOpacity>
  194. </View>
  195. <View
  196. style = { [
  197. styles.indicatorContainer,
  198. wide && _filmstripVisible
  199. && styles.indicatorContainerWide
  200. ] }>
  201. {
  202. this._renderExpandedLabel()
  203. }
  204. </View>
  205. </View>
  206. );
  207. }
  208. /**
  209. * Creates a function to be invoked when the onLayout of the touchables are
  210. * triggered.
  211. *
  212. * @param {string} label - The identifier of the label that's onLayout is
  213. * triggered.
  214. * @returns {Function}
  215. */
  216. _createOnLayout(label) {
  217. return ({ nativeEvent: { layout } }) => {
  218. const { labelLayouts } = this.state;
  219. const updatedLayout = {};
  220. updatedLayout[label] = layout;
  221. this.setState({
  222. labelLayouts: {
  223. ...labelLayouts,
  224. ...updatedLayout
  225. }
  226. });
  227. };
  228. }
  229. /**
  230. * Creates a function to be invoked when the onPress of the touchables are
  231. * triggered.
  232. *
  233. * @param {string} label - The identifier of the label that's onLayout is
  234. * triggered.
  235. * @returns {Function}
  236. */
  237. _createOnPress(label) {
  238. return () => {
  239. const {
  240. containerLayout,
  241. labelLayouts
  242. } = this.state;
  243. let { visibleExpandedLabel } = this.state;
  244. if (containerLayout) {
  245. const labelLayout = labelLayouts[label];
  246. // This calculation has to be changed if the labels are not
  247. // positioned right anymore.
  248. const right = containerLayout.width - labelLayout.x;
  249. visibleExpandedLabel
  250. = visibleExpandedLabel === label ? undefined : label;
  251. clearTimeout(this.expandedLabelTimeout);
  252. this.setState({
  253. parentPosition: right,
  254. visibleExpandedLabel
  255. });
  256. if (visibleExpandedLabel) {
  257. this.expandedLabelTimeout = setTimeout(() => {
  258. this.setState({
  259. visibleExpandedLabel: undefined
  260. });
  261. }, EXPANDED_LABEL_TIMEOUT);
  262. }
  263. }
  264. };
  265. }
  266. _onTopViewLayout: Object => void
  267. /**
  268. * Invoked when the View containing the {@code Label}s is laid out.
  269. *
  270. * @param {Object} layout - The native layout object.
  271. * @returns {void}
  272. */
  273. _onTopViewLayout({ nativeEvent: { layout } }) {
  274. this.setState({
  275. containerLayout: layout
  276. });
  277. }
  278. /**
  279. * Rendes the expanded (explaining) label for the label that was touched.
  280. *
  281. * @returns {React$Element}
  282. */
  283. _renderExpandedLabel() {
  284. const { parentPosition, visibleExpandedLabel } = this.state;
  285. if (visibleExpandedLabel) {
  286. const expandedLabel = EXPANDED_LABELS[visibleExpandedLabel];
  287. if (expandedLabel) {
  288. const component = expandedLabel.component || expandedLabel;
  289. const expandedLabelProps = expandedLabel.props || {};
  290. return React.createElement(component, {
  291. ...expandedLabelProps,
  292. parentPosition
  293. });
  294. }
  295. }
  296. return null;
  297. }
  298. _renderRecordingLabel: string => React$Element<any>;
  299. _renderTranscribingLabel: () => React$Element<any>;
  300. _renderInsecureRoomNameLabel: () => React$Element<any>;
  301. _renderVideoQualityLabel: () => React$Element<any>;
  302. }
  303. /**
  304. * Maps (parts of) the redux state to the associated
  305. * {@code Labels}'s props.
  306. *
  307. * @param {Object} state - The redux state.
  308. * @private
  309. * @returns {Props}
  310. */
  311. function _mapStateToProps(state) {
  312. return {
  313. ..._abstractMapStateToProps(state),
  314. _visible: !shouldDisplayNotifications(state)
  315. };
  316. }
  317. export default connect(_mapStateToProps)(makeAspectRatioAware(Labels));