[ReactNative] refactor the inspector
Summary:
The `InspectorOverlay` component was getting unwieldy, so I broke it into three components:
- Inspector
- InspectorOverlay
- InspectorPanel
and added @flow types.
The inspector was also living under the `ReactIOS` directory, and I moved it
up into the `Libraries` directory, as the inspector will soon be usable [on
Android](https://phabricator.fb.com/D2138319).
All features of the inspector should remain functional, with the addition of
one feature:
- you can toggle "touch to inspect" by tapping the "Inspect" button at the
bottom of the inspection panel. When inspection is disabled, the panel remains, but you can interact with
the app normally without touches being intercepted
@public
Test Plan:
Open the inspector:
- touch to inspect things, verify that margin, padding, size and position are
reported correctly, and that the component hierarchy is navigable.
- tap the "Inspect" button, and verify that you can interact with the app
normally.
{F22548949}
[Video of toggling inspection](https://www.latest.facebook.com/pxlcld/mrs9)
2015-06-11 20:50:48 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule InspectorOverlay
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Dimensions = require('Dimensions');
|
|
|
|
var InspectorUtils = require('InspectorUtils');
|
|
|
|
var React = require('React');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var UIManager = require('NativeModules').UIManager;
|
|
|
|
var View = require('View');
|
|
|
|
var ElementBox = require('ElementBox');
|
|
|
|
|
|
|
|
var PropTypes = React.PropTypes;
|
|
|
|
|
|
|
|
type EventLike = {
|
|
|
|
nativeEvent: Object;
|
|
|
|
};
|
|
|
|
|
|
|
|
var InspectorOverlay = React.createClass({
|
|
|
|
propTypes: {
|
2015-07-14 19:03:17 +00:00
|
|
|
inspected: PropTypes.shape({
|
|
|
|
frame: PropTypes.object,
|
|
|
|
style: PropTypes.any,
|
|
|
|
}),
|
|
|
|
inspectedViewTag: PropTypes.number,
|
[ReactNative] refactor the inspector
Summary:
The `InspectorOverlay` component was getting unwieldy, so I broke it into three components:
- Inspector
- InspectorOverlay
- InspectorPanel
and added @flow types.
The inspector was also living under the `ReactIOS` directory, and I moved it
up into the `Libraries` directory, as the inspector will soon be usable [on
Android](https://phabricator.fb.com/D2138319).
All features of the inspector should remain functional, with the addition of
one feature:
- you can toggle "touch to inspect" by tapping the "Inspect" button at the
bottom of the inspection panel. When inspection is disabled, the panel remains, but you can interact with
the app normally without touches being intercepted
@public
Test Plan:
Open the inspector:
- touch to inspect things, verify that margin, padding, size and position are
reported correctly, and that the component hierarchy is navigable.
- tap the "Inspect" button, and verify that you can interact with the app
normally.
{F22548949}
[Video of toggling inspection](https://www.latest.facebook.com/pxlcld/mrs9)
2015-06-11 20:50:48 +00:00
|
|
|
onTouchInstance: PropTypes.func.isRequired,
|
|
|
|
},
|
|
|
|
|
|
|
|
findViewForTouchEvent: function(e: EventLike) {
|
|
|
|
var {locationX, locationY} = e.nativeEvent.touches[0];
|
|
|
|
UIManager.findSubviewIn(
|
|
|
|
this.props.inspectedViewTag,
|
|
|
|
[locationX, locationY],
|
|
|
|
(nativeViewTag, left, top, width, height) => {
|
|
|
|
var instance = InspectorUtils.findInstanceByNativeTag(this.props.rootTag, nativeViewTag);
|
|
|
|
if (!instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onTouchInstance(instance, {left, top, width, height}, locationY);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
shouldSetResponser: function(e: EventLike): bool {
|
|
|
|
this.findViewForTouchEvent(e);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var content = null;
|
|
|
|
if (this.props.inspected) {
|
|
|
|
content = <ElementBox frame={this.props.inspected.frame} style={this.props.inspected.style} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
onStartShouldSetResponder={this.shouldSetResponser}
|
|
|
|
onResponderMove={this.findViewForTouchEvent}
|
|
|
|
style={[styles.inspector, {height: Dimensions.get('window').height}]}>
|
|
|
|
{content}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
inspector: {
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = InspectorOverlay;
|