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.

Toolbox.js 8.5KB

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