InspectorPanel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. */
  10. 'use strict';
  11. const ElementProperties = require('./ElementProperties');
  12. const NetworkOverlay = require('./NetworkOverlay');
  13. const PerformanceOverlay = require('./PerformanceOverlay');
  14. const React = require('react');
  15. const ScrollView = require('../Components/ScrollView/ScrollView');
  16. const StyleSheet = require('../StyleSheet/StyleSheet');
  17. const Text = require('../Text/Text');
  18. const TouchableHighlight = require('../Components/Touchable/TouchableHighlight');
  19. const View = require('../Components/View/View');
  20. import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
  21. type Props = $ReadOnly<{|
  22. devtoolsIsOpen: boolean,
  23. inspecting: boolean,
  24. setInspecting: (val: boolean) => void,
  25. perfing: boolean,
  26. setPerfing: (val: boolean) => void,
  27. touchTargeting: boolean,
  28. setTouchTargeting: (val: boolean) => void,
  29. networking: boolean,
  30. setNetworking: (val: boolean) => void,
  31. hierarchy?: ?Array<{|name: string|}>,
  32. selection?: ?number,
  33. setSelection: number => mixed,
  34. inspected?: ?$ReadOnly<{|
  35. style?: ?ViewStyleProp,
  36. frame?: ?$ReadOnly<{|
  37. top?: ?number,
  38. left?: ?number,
  39. width?: ?number,
  40. height: ?number,
  41. |}>,
  42. source?: ?{|
  43. fileName?: string,
  44. lineNumber?: number,
  45. |},
  46. |}>,
  47. |}>;
  48. class InspectorPanel extends React.Component<Props> {
  49. renderWaiting(): React.Node {
  50. if (this.props.inspecting) {
  51. return (
  52. <Text style={styles.waitingText}>Tap something to inspect it</Text>
  53. );
  54. }
  55. return <Text style={styles.waitingText}>Nothing is inspected</Text>;
  56. }
  57. render(): React.Node {
  58. let contents;
  59. if (this.props.inspected) {
  60. contents = (
  61. <ScrollView style={styles.properties}>
  62. <ElementProperties
  63. style={this.props.inspected.style}
  64. frame={this.props.inspected.frame}
  65. source={this.props.inspected.source}
  66. // $FlowFixMe: Hierarchy should be non-nullable
  67. hierarchy={this.props.hierarchy}
  68. selection={this.props.selection}
  69. setSelection={this.props.setSelection}
  70. />
  71. </ScrollView>
  72. );
  73. } else if (this.props.perfing) {
  74. contents = <PerformanceOverlay />;
  75. } else if (this.props.networking) {
  76. contents = <NetworkOverlay />;
  77. } else {
  78. contents = <View style={styles.waiting}>{this.renderWaiting()}</View>;
  79. }
  80. return (
  81. <View style={styles.container}>
  82. {!this.props.devtoolsIsOpen && contents}
  83. <View style={styles.buttonRow}>
  84. <InspectorPanelButton
  85. title={'Inspect'}
  86. pressed={this.props.inspecting}
  87. onClick={this.props.setInspecting}
  88. />
  89. <InspectorPanelButton
  90. title={'Perf'}
  91. pressed={this.props.perfing}
  92. onClick={this.props.setPerfing}
  93. />
  94. <InspectorPanelButton
  95. title={'Network'}
  96. pressed={this.props.networking}
  97. onClick={this.props.setNetworking}
  98. />
  99. <InspectorPanelButton
  100. title={'Touchables'}
  101. pressed={this.props.touchTargeting}
  102. onClick={this.props.setTouchTargeting}
  103. />
  104. </View>
  105. </View>
  106. );
  107. }
  108. }
  109. type InspectorPanelButtonProps = $ReadOnly<{|
  110. onClick: (val: boolean) => void,
  111. pressed: boolean,
  112. title: string,
  113. |}>;
  114. class InspectorPanelButton extends React.Component<InspectorPanelButtonProps> {
  115. render() {
  116. return (
  117. <TouchableHighlight
  118. onPress={() => this.props.onClick(!this.props.pressed)}
  119. style={[styles.button, this.props.pressed && styles.buttonPressed]}>
  120. <Text style={styles.buttonText}>{this.props.title}</Text>
  121. </TouchableHighlight>
  122. );
  123. }
  124. }
  125. const styles = StyleSheet.create({
  126. buttonRow: {
  127. flexDirection: 'row',
  128. },
  129. button: {
  130. backgroundColor: 'rgba(0, 0, 0, 0.3)',
  131. margin: 2,
  132. height: 30,
  133. justifyContent: 'center',
  134. alignItems: 'center',
  135. },
  136. buttonPressed: {
  137. backgroundColor: 'rgba(255, 255, 255, 0.3)',
  138. },
  139. buttonText: {
  140. textAlign: 'center',
  141. color: 'white',
  142. margin: 5,
  143. },
  144. container: {
  145. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  146. },
  147. properties: {
  148. height: 200,
  149. },
  150. waiting: {
  151. height: 100,
  152. },
  153. waitingText: {
  154. fontSize: 20,
  155. textAlign: 'center',
  156. marginVertical: 20,
  157. color: 'white',
  158. },
  159. });
  160. module.exports = InspectorPanel;