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

AudioRouteButton.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // @flow
  2. import React from 'react';
  3. import {
  4. findNodeHandle,
  5. NativeModules,
  6. requireNativeComponent,
  7. View
  8. } from 'react-native';
  9. import { connect } from 'react-redux';
  10. import { openDialog } from '../../../base/dialog';
  11. import { translate } from '../../../base/i18n';
  12. import { AbstractButton } from '../../../base/toolbox';
  13. import type { AbstractButtonProps } from '../../../base/toolbox';
  14. import AudioRoutePickerDialog from './AudioRoutePickerDialog';
  15. /**
  16. * The {@code MPVolumeView} React {@code Component}. It will only be available
  17. * on iOS.
  18. */
  19. const MPVolumeView
  20. = NativeModules.MPVolumeViewManager
  21. && requireNativeComponent('MPVolumeView', null);
  22. /**
  23. * The style required to hide the {@code MPVolumeView}, since it's displayed
  24. * programmatically.
  25. */
  26. const HIDE_VIEW_STYLE = { display: 'none' };
  27. type Props = AbstractButtonProps & {
  28. /**
  29. * The redux {@code dispatch} function used to open/show the
  30. * {@code AudioRoutePickerDialog}.
  31. */
  32. dispatch: Function
  33. };
  34. /**
  35. * A toolbar button which triggers an audio route picker when pressed.
  36. */
  37. class AudioRouteButton extends AbstractButton<Props, *> {
  38. accessibilityLabel = 'toolbar.accessibilityLabel.audioRoute';
  39. iconName = 'icon-volume';
  40. label = 'toolbar.audioRoute';
  41. _volumeComponent: ?Object;
  42. /**
  43. * Initializes a new {@code AudioRouteButton} instance.
  44. *
  45. * @param {Props} props - The React {@code Component} props to initialize
  46. * the new {@code AudioRouteButton} instance with.
  47. */
  48. constructor(props: Props) {
  49. super(props);
  50. /**
  51. * The internal reference to the React {@code MPVolumeView} for
  52. * showing the volume control view.
  53. *
  54. * @private
  55. * @type {ReactElement}
  56. */
  57. this._volumeComponent = null;
  58. // Bind event handlers so they are only bound once per instance.
  59. this._setVolumeComponent = this._setVolumeComponent.bind(this);
  60. }
  61. /**
  62. * Handles clicking / pressing the button, and opens the appropriate dialog.
  63. *
  64. * @private
  65. * @returns {void}
  66. */
  67. _handleClick() {
  68. if (MPVolumeView) {
  69. NativeModules.MPVolumeViewManager.show(
  70. findNodeHandle(this._volumeComponent));
  71. } else if (AudioRoutePickerDialog) {
  72. this.props.dispatch(openDialog(AudioRoutePickerDialog));
  73. }
  74. }
  75. _setVolumeComponent: (?Object) => void;
  76. /**
  77. * Sets the internal reference to the React Component wrapping the
  78. * {@code MPVolumeView} component.
  79. *
  80. * @param {ReactElement} component - React Component.
  81. * @private
  82. * @returns {void}
  83. */
  84. _setVolumeComponent(component) {
  85. this._volumeComponent = component;
  86. }
  87. /**
  88. * Implements React's {@link Component#render()}.
  89. *
  90. * @inheritdoc
  91. * @returns {React$Node}
  92. */
  93. render() {
  94. if (!MPVolumeView && !AudioRoutePickerDialog) {
  95. return null;
  96. }
  97. const element = super.render();
  98. return (
  99. <View>
  100. { element }
  101. {
  102. MPVolumeView
  103. && <MPVolumeView
  104. ref = { this._setVolumeComponent }
  105. style = { HIDE_VIEW_STYLE } />
  106. }
  107. </View>
  108. );
  109. }
  110. }
  111. export default translate(connect()(AudioRouteButton));