Add extending Layout Inspector docs

Summary:
Please give feedback on the sidebar placement.

This is only the Android part. I'll put up a second diff copying the iOS thing over
but hope someone can commandeer that and check for accuracy.

Reviewed By: priteshrnandgaonkar

Differential Revision: D15181454

fbshipit-source-id: d32081feefbfb0ffc38890e835a7d5f6b78667ab
This commit is contained in:
Pascal Hartig
2019-05-02 08:40:55 -07:00
committed by Facebook Github Bot
parent 367bb9a502
commit df46c0c63e
4 changed files with 57 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

View File

@@ -0,0 +1,53 @@
---
id: layout-inspector
title: Extending Layout Inspector
---
The Layout Inspector plugin can be extended to support new kinds of UI components. You can also extend it to customize the data made available in the sidebar.
Depending on whether you want to expose new data on Android or iOS, there are different interfaces you can use.
![Layout Inspector](/docs/assets/layoutinspector.png)
## Android
### Node Descriptor
To expose an object to the Layout Inspector in Flipper you have to implement a `NodeDescriptor` which describes your object. For example the `ViewDescriptor` describes `View` objects and the `FragmentDescriptor` describe `Fragment` instances. These descriptors have a set of callbacks used to expose children and data associated with the object they describe. See [`NodeDescriptor`](https://github.com/facebook/flipper/blob/b0d2983bd440dc41ec67089e11acd394e6566b8f/android/src/main/java/com/facebook/flipper/plugins/inspector/NodeDescriptor.java) for the full API.
`NodeDescriptor` implementations should not subclass other `NodeDescriptor` implementations. Instead to re-use existing behavior from a more generic descriptor, you should prefer to use delegate.
**Don't**
```java
class ViewGroupDescriptor extends ViewDescriptor<ViewGroup> {
public String getName(ViewGroup node) {
return super.getName(node);
}
}
```
**Do**
```java
class ViewGroupDescriptor extends NodeDescriptor<ViewGroup> {
public String getName(ViewGroup node) {
NodeDescriptor descriptor = descriptorForClass(View.class);
return descriptor.getName(node);
}
}
```
### Register a Descriptor
Register your descriptor in the `DescriptorMapping` used to instantiate the `InspectorFlipperPlugin`.
```java
final FlipperClient client = FlipperClient.createInstance(mContext);
final DescriptorMapping descriptorMapping = DescriptorMapping.withDefaults();
descriptorMapping.register(MyObject.class, new MyObjectDescriptor());
client.addPlugin(new InspectorFlipperPlugin(mContext, descriptorMapping));
```
### Extending an existing Descriptor
You may not need to create a whole new descriptor but instead you may just want to change extend an existing one to expose some new piece of data. In that case just locate the correct descriptor and edit its `getData`, `getAttributes` and perhaps `setData` methods.

View File

@@ -34,6 +34,9 @@
"extending/js-setup": { "extending/js-setup": {
"title": "JavaScript Setup" "title": "JavaScript Setup"
}, },
"extending/layout-inspector": {
"title": "Extending the Layout Inspector"
},
"extending/new-clients": { "extending/new-clients": {
"title": "Implementing a Flipper Client" "title": "Implementing a Flipper Client"
}, },

View File

@@ -29,7 +29,7 @@
"Advanced Usage": ["custom-ports", "stetho"] "Advanced Usage": ["custom-ports", "stetho"]
}, },
"extending": { "extending": {
"Extending Flipper": ["extending/index"], "Extending Flipper": ["extending/index", "extending/layout-inspector"],
"Plugin Development": [ "Plugin Development": [
"extending/js-setup", "extending/js-setup",
"extending/writing-a-plugin", "extending/writing-a-plugin",