From bb7c8a152a1ca04a60c0e4e4face87d9265ae5c7 Mon Sep 17 00:00:00 2001 From: Daniel Abramowitz Date: Wed, 19 Dec 2018 09:40:29 -0800 Subject: [PATCH] Generic layout inspector section Summary: `PropWithNamedFlipperObject` is an interface that can be implemented by a Litho component prop to display a new section in the Flipper layout inspector. Reviewed By: danielbuechele Differential Revision: D13419918 fbshipit-source-id: be2ade160d6381944b1b68a7645b5b23f1d142a1 --- .../litho/DebugComponentDescriptor.java | 24 ++++++++++++++----- .../litho/PropWithInspectorSection.java | 15 ++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 android/src/main/java/com/facebook/flipper/plugins/litho/PropWithInspectorSection.java diff --git a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java index 5b87f5c96..04dde4dc2 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java +++ b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java @@ -41,6 +41,7 @@ import com.facebook.yoga.YogaJustify; import com.facebook.yoga.YogaPositionType; import com.facebook.yoga.YogaValue; import java.lang.reflect.Field; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -158,10 +159,7 @@ public class DebugComponentDescriptor extends NodeDescriptor { data.add(new Named<>("Layout", layoutData)); } - final FlipperObject propData = getPropData(node); - if (propData != null) { - data.add(new Named<>("Props", propData)); - } + data.addAll(getPropData(node)); final FlipperObject stateData = getStateData(node); if (stateData != null) { @@ -261,13 +259,14 @@ public class DebugComponentDescriptor extends NodeDescriptor { } @Nullable - private static FlipperObject getPropData(DebugComponent node) { + private static List> getPropData(DebugComponent node) { if (node.canResolve()) { return null; } final Component component = node.getComponent(); final FlipperObject.Builder props = new FlipperObject.Builder(); + List> data = new ArrayList<>(); boolean hasProps = false; for (Field f : component.getClass().getDeclaredFields()) { @@ -276,6 +275,15 @@ public class DebugComponentDescriptor extends NodeDescriptor { final Prop annotation = f.getAnnotation(Prop.class); if (annotation != null) { + if (f.get(component) != null + && PropWithInspectorSection.class.isAssignableFrom(f.get(component).getClass())) { + final AbstractMap.SimpleEntry datum = + ((PropWithInspectorSection) f.get(component)).getFlipperLayoutInspectorSection(); + if (datum != null) { + data.add(new Named<>(datum.getKey(), new FlipperObject(datum.getValue()))); + } + } + switch (annotation.resType()) { case COLOR: props.put(f.getName(), fromColor((Integer) f.get(component))); @@ -316,7 +324,11 @@ public class DebugComponentDescriptor extends NodeDescriptor { } } - return hasProps ? props.build() : null; + if (hasProps) { + data.add(new Named<>("Props", props.build())); + } + + return data; } @Nullable diff --git a/android/src/main/java/com/facebook/flipper/plugins/litho/PropWithInspectorSection.java b/android/src/main/java/com/facebook/flipper/plugins/litho/PropWithInspectorSection.java new file mode 100644 index 000000000..b2eb211a6 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/litho/PropWithInspectorSection.java @@ -0,0 +1,15 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +package com.facebook.flipper.plugins.litho; + +import java.util.AbstractMap; +import javax.annotation.CheckForNull; + +public interface PropWithInspectorSection { + @CheckForNull + AbstractMap.SimpleEntry getFlipperLayoutInspectorSection(); +}