Skip to content

Commit

Permalink
Add ability to change background from stetho
Browse files Browse the repository at this point in the history
Summary: Add ability to set background and foreground colors of components via stetho

Reviewed By: passy

Differential Revision: D4842989

fbshipit-source-id: 6cd4f331a8db43a2f818c58c314a730668cf9ff9
  • Loading branch information
Emil Sjolander authored and facebook-github-bot committed Apr 6, 2017
1 parent 4128e43 commit e014d02
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.lang.reflect.Field;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.util.SimpleArrayMap;

Expand Down Expand Up @@ -137,6 +138,17 @@ private static void storeYogaValue(
}
}

private static void storeDrawable(
StyleAccumulator accumulator,
SimpleArrayMap<String, String> overrides,
String key) {
if (overrides.containsKey(key)) {
accumulator.store(key, overrides.get(key), false);
} else {
accumulator.store(key, "<drawable>", false);
}
}

void getStyles(ComponentStethoNode stethoNode, StyleAccumulator accumulator) {
final YogaNodeAPI yogaNode = stethoNode.node.mYogaNode;
final YogaNodeAPI defaults = ComponentsPools.acquireYogaNode();
Expand All @@ -147,6 +159,9 @@ void getStyles(ComponentStethoNode stethoNode, StyleAccumulator accumulator) {
mStyleOverrides.put(stethoNode.key, overrides);
}

storeDrawable(accumulator, overrides, "background");
storeDrawable(accumulator, overrides, "foreground");

storeEnum(accumulator, overrides, "direction", yogaNode.getStyleDirection());
storeEnum(accumulator, overrides, "flex-direction", yogaNode.getFlexDirection());
storeEnum(accumulator, overrides, "justify-content", yogaNode.getJustifyContent());
Expand Down Expand Up @@ -188,6 +203,22 @@ void getStyles(ComponentStethoNode stethoNode, StyleAccumulator accumulator) {
ComponentsPools.release(defaults);
}

private static int parseColor(String color) {
if (color == null || color.length() == 0) {
return Color.TRANSPARENT;
}

// Color.parse does not handle hax code with 3 ints e.g. #123
if (color.length() == 4) {
final char r = color.charAt(1);
final char g = color.charAt(2);
final char b = color.charAt(3);
color = "#" + r + r + g + g + b + b;
}

return Color.parseColor(color);
}

public void applyOverrides(InternalNode node) {
final String nodeKey = getGlobalKey(node, 0); // We only override the root

Expand All @@ -198,6 +229,14 @@ public void applyOverrides(InternalNode node) {
final String value = styles.get(key);

try {
if (key.equals("background")) {
node.backgroundColor(parseColor(value));
}

if (key.equals("foreground")) {
node.foregroundColor(parseColor(value));
}

if (key.equals("direction")) {
node.layoutDirection(YogaDirection.valueOf(toEnumString(value)));
}
Expand Down

0 comments on commit e014d02

Please sign in to comment.