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

Toolbox.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { View } from 'react-native';
  4. import { ColorSchemeRegistry } from '../../../base/color-scheme';
  5. import { CHAT_ENABLED, getFeatureFlag } from '../../../base/flags';
  6. import { Container } from '../../../base/react';
  7. import { connect } from '../../../base/redux';
  8. import { StyleType } from '../../../base/styles';
  9. import { ChatButton } from '../../../chat';
  10. import { InfoDialogButton } from '../../../invite';
  11. import { isToolboxVisible } from '../../functions';
  12. import { HANGUP_BUTTON_SIZE } from '../../constants';
  13. import AudioMuteButton from '../AudioMuteButton';
  14. import HangupButton from '../HangupButton';
  15. import OverflowMenuButton from './OverflowMenuButton';
  16. import styles from './styles';
  17. import VideoMuteButton from '../VideoMuteButton';
  18. /**
  19. * The number of buttons other than {@link HangupButton} to render in
  20. * {@link Toolbox}.
  21. *
  22. * @private
  23. * @type number
  24. */
  25. const _BUTTON_COUNT = 4;
  26. /**
  27. * Factor relating the hangup button and other toolbar buttons.
  28. *
  29. * @private
  30. * @type number
  31. */
  32. const _BUTTON_SIZE_FACTOR = 0.85;
  33. /**
  34. * The type of {@link Toolbox}'s React {@code Component} props.
  35. */
  36. type Props = {
  37. /**
  38. * Whether the chat feature has been enabled. The meeting info button will be displayed in its place when disabled.
  39. */
  40. _chatEnabled: boolean,
  41. /**
  42. * The color-schemed stylesheet of the feature.
  43. */
  44. _styles: StyleType,
  45. /**
  46. * The indicator which determines whether the toolbox is visible.
  47. */
  48. _visible: boolean,
  49. /**
  50. * The redux {@code dispatch} function.
  51. */
  52. dispatch: Function
  53. };
  54. /**
  55. * The type of {@link Toolbox}'s React {@code Component} state.
  56. */
  57. type State = {
  58. /**
  59. * The detected width for this component.
  60. */
  61. width: number
  62. };
  63. /**
  64. * Implements the conference toolbox on React Native.
  65. */
  66. class Toolbox extends Component<Props, State> {
  67. state = {
  68. width: 0
  69. };
  70. /**
  71. * Initializes a new {@code Toolbox} instance.
  72. *
  73. * @inheritdoc
  74. */
  75. constructor(props: Props) {
  76. super(props);
  77. // Bind event handlers so they are only bound once per instance.
  78. this._onLayout = this._onLayout.bind(this);
  79. }
  80. /**
  81. * Implements React's {@link Component#render()}.
  82. *
  83. * @inheritdoc
  84. * @returns {ReactElement}
  85. */
  86. render() {
  87. return (
  88. <Container
  89. onLayout = { this._onLayout }
  90. style = { styles.toolbox }
  91. visible = { this.props._visible }>
  92. { this._renderToolbar() }
  93. </Container>
  94. );
  95. }
  96. /**
  97. * Calculates how large our toolbar buttons can be, given the available
  98. * width. In the future we might want to have a size threshold, and once
  99. * it's passed a completely different style could be used, akin to the web.
  100. *
  101. * @private
  102. * @returns {number}
  103. */
  104. _calculateButtonSize() {
  105. const { _styles } = this.props;
  106. const { width } = this.state;
  107. if (width <= 0) {
  108. // We don't know how much space is allocated to the toolbar yet.
  109. return width;
  110. }
  111. const hangupButtonSize = HANGUP_BUTTON_SIZE;
  112. const { style } = _styles.buttonStyles;
  113. let buttonSize
  114. = (width
  115. // Account for HangupButton without its margin which is not
  116. // included in _BUTTON_COUNT:
  117. - hangupButtonSize
  118. // Account for the horizontal margins of all buttons:
  119. - ((_BUTTON_COUNT + 1) * style.marginHorizontal * 2))
  120. / _BUTTON_COUNT;
  121. // Well, don't return a non-positive button size.
  122. if (buttonSize <= 0) {
  123. buttonSize = style.width;
  124. }
  125. // The button should be at most _BUTTON_SIZE_FACTOR of the hangup
  126. // button's size.
  127. buttonSize
  128. = Math.min(buttonSize, hangupButtonSize * _BUTTON_SIZE_FACTOR);
  129. // Make sure it's an even number.
  130. return 2 * Math.round(buttonSize / 2);
  131. }
  132. /**
  133. * Constructs the toggled style of the chat button. This cannot be done by
  134. * simple style inheritance due to the size calculation done in this
  135. * component.
  136. *
  137. * @param {Object} baseStyle - The base style that was originally
  138. * calculated.
  139. * @returns {Object | Array}
  140. */
  141. _getChatButtonToggledStyle(baseStyle) {
  142. const { _styles } = this.props;
  143. if (Array.isArray(baseStyle.style)) {
  144. return {
  145. ...baseStyle,
  146. style: [
  147. ...baseStyle.style,
  148. _styles.chatButtonOverride.toggled
  149. ]
  150. };
  151. }
  152. return {
  153. ...baseStyle,
  154. style: [
  155. baseStyle.style,
  156. _styles.chatButtonOverride.toggled
  157. ]
  158. };
  159. }
  160. _onLayout: (Object) => void;
  161. /**
  162. * Handles the "on layout" View's event and stores the width as state.
  163. *
  164. * @param {Object} event - The "on layout" event object/structure passed
  165. * by react-native.
  166. * @private
  167. * @returns {void}
  168. */
  169. _onLayout({ nativeEvent: { layout: { width } } }) {
  170. this.setState({ width });
  171. }
  172. /**
  173. * Renders the toolbar. In order to avoid a weird visual effect in which the
  174. * toolbar is (visually) rendered and then visibly changes its size, it is
  175. * rendered only after we've figured out the width available to the toolbar.
  176. *
  177. * @returns {React$Node}
  178. */
  179. _renderToolbar() {
  180. const { _chatEnabled, _styles } = this.props;
  181. const buttonSize = this._calculateButtonSize();
  182. let { buttonStyles, toggledButtonStyles } = _styles;
  183. if (buttonSize > 0) {
  184. const extraButtonStyle = {
  185. borderRadius: buttonSize / 2,
  186. height: buttonSize,
  187. width: buttonSize
  188. };
  189. // XXX The following width equality checks attempt to minimize
  190. // unnecessary objects and possibly re-renders.
  191. if (buttonStyles.style.width !== extraButtonStyle.width) {
  192. buttonStyles = {
  193. ...buttonStyles,
  194. style: [ buttonStyles.style, extraButtonStyle ]
  195. };
  196. }
  197. if (toggledButtonStyles.style.width !== extraButtonStyle.width) {
  198. toggledButtonStyles = {
  199. ...toggledButtonStyles,
  200. style: [ toggledButtonStyles.style, extraButtonStyle ]
  201. };
  202. }
  203. } else {
  204. // XXX In order to avoid a weird visual effect in which the toolbar
  205. // is (visually) rendered and then visibly changes its size, it is
  206. // rendered only after we've figured out the width available to the
  207. // toolbar.
  208. return null;
  209. }
  210. return (
  211. <View
  212. pointerEvents = 'box-none'
  213. style = { styles.toolbar }>
  214. {
  215. _chatEnabled
  216. && <ChatButton
  217. styles = { buttonStyles }
  218. toggledStyles = {
  219. this._getChatButtonToggledStyle(toggledButtonStyles)
  220. } />
  221. }
  222. {
  223. !_chatEnabled
  224. && <InfoDialogButton
  225. styles = { buttonStyles }
  226. toggledStyles = { toggledButtonStyles } />
  227. }
  228. <AudioMuteButton
  229. styles = { buttonStyles }
  230. toggledStyles = { toggledButtonStyles } />
  231. <HangupButton
  232. styles = { _styles.hangupButtonStyles } />
  233. <VideoMuteButton
  234. styles = { buttonStyles }
  235. toggledStyles = { toggledButtonStyles } />
  236. <OverflowMenuButton
  237. styles = { buttonStyles }
  238. toggledStyles = { toggledButtonStyles } />
  239. </View>
  240. );
  241. }
  242. }
  243. /**
  244. * Maps parts of the redux state to {@link Toolbox} (React {@code Component})
  245. * props.
  246. *
  247. * @param {Object} state - The redux state of which parts are to be mapped to
  248. * {@code Toolbox} props.
  249. * @private
  250. * @returns {{
  251. * _chatEnabled: boolean,
  252. * _styles: StyleType,
  253. * _visible: boolean
  254. * }}
  255. */
  256. function _mapStateToProps(state: Object): Object {
  257. return {
  258. _chatEnabled: getFeatureFlag(state, CHAT_ENABLED, true),
  259. _styles: ColorSchemeRegistry.get(state, 'Toolbox'),
  260. _visible: isToolboxVisible(state)
  261. };
  262. }
  263. export default connect(_mapStateToProps)(Toolbox);