use of com.android.tools.sherpa.drawing.decorator.TextWidget in project android by JetBrains.
the class ConstraintUtilities method updateWidgetFromComponent.
/**
* Update the constraint widget with the component information (coming from XML)
*
* @param scene the scene of components. Can be null.
* @param constraintModel the constraint model we are working with
* @param widget constraint widget
* @param component the model component
* @return true if need to save the xml
*/
static boolean updateWidgetFromComponent(@Nullable Scene scene, @NotNull ConstraintModel constraintModel, @Nullable ConstraintWidget widget, @Nullable NlComponent component) {
if (component == null || widget == null) {
return false;
}
if (!(widget instanceof Guideline)) {
widget.setVisibility(component.getAndroidViewVisibility());
}
widget.setDebugName(component.getId());
Insets padding = component.getPadding(true);
WidgetsScene widgetsScene = constraintModel.getScene();
if (scene != null) {
// If the scene exists, use the bounds from it instead of from the NlComponent.
// This gives us animation on layout changes.
// Note: this is temporary, once the Scene interaction / painting is fully done we'll switch to it.
long time = System.currentTimeMillis();
SceneComponent sceneComponent = scene.getSceneComponent(component);
if (sceneComponent != null) {
widget.setDrawOrigin(sceneComponent.getDrawX(time), sceneComponent.getDrawY(time));
int w = sceneComponent.getDrawWidth(time);
int h = sceneComponent.getDrawHeight(time);
widget.setDimension(w, h);
return false;
}
}
if (widget instanceof ConstraintWidgetContainer) {
int paddingLeft = constraintModel.pxToDp(padding.left);
int paddingTop = constraintModel.pxToDp(padding.top);
int paddingRight = constraintModel.pxToDp(padding.right);
int paddingBottom = constraintModel.pxToDp(padding.bottom);
((ConstraintWidgetContainer) widget).setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
widget.setDimension(constraintModel.pxToDp(component.w) - paddingLeft - paddingRight, constraintModel.pxToDp(component.h) - paddingTop - paddingBottom);
} else {
widget.setDimension(constraintModel.pxToDp(component.w), constraintModel.pxToDp(component.h));
}
String absoluteWidth = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_WIDTH);
if (absoluteWidth != null) {
Configuration configuration = component.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteWidth, configuration);
size = constraintModel.pxToDp(size);
widget.setWidth(size);
}
String absoluteHeight = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_HEIGHT);
if (absoluteHeight != null) {
Configuration configuration = component.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
int size = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteHeight, configuration);
size = constraintModel.pxToDp(size);
widget.setHeight(size);
}
widget.setMinWidth(constraintModel.pxToDp(component.getMinimumWidth()));
widget.setMinHeight(constraintModel.pxToDp(component.getMinimumHeight()));
NlComponent parent = component.getParent();
NlModel model = component.getModel();
if (parent != null) {
ConstraintWidget parentWidget = widgetsScene.getWidget(parent);
if (parentWidget instanceof WidgetContainer) {
WidgetContainer parentContainerWidget = (WidgetContainer) parentWidget;
if (widget.getParent() != parentContainerWidget) {
parentContainerWidget.add(widget);
}
}
}
// First set the origin of the widget
int x = constraintModel.pxToDp(component.x);
int y = constraintModel.pxToDp(component.y);
if (widget instanceof ConstraintWidgetContainer) {
x += constraintModel.pxToDp(padding.left);
y += constraintModel.pxToDp(padding.top);
}
WidgetContainer parentContainer = (WidgetContainer) widget.getParent();
if (parentContainer != null) {
if (!(parentContainer instanceof ConstraintWidgetContainer)) {
x = constraintModel.pxToDp(component.x - component.getParent().x);
y = constraintModel.pxToDp(component.y - component.getParent().y);
} else {
x -= parentContainer.getDrawX();
y -= parentContainer.getDrawY();
}
}
String absoluteX = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_X);
if (absoluteX != null) {
Configuration configuration = component.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteX, configuration);
x = constraintModel.pxToDp(position);
}
String absoluteY = component.getAttribute(SdkConstants.TOOLS_URI, ConvertToConstraintLayoutAction.ATTR_LAYOUT_CONVERSION_ABSOLUTE_Y);
if (absoluteY != null) {
Configuration configuration = component.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
int position = ViewEditor.resolveDimensionPixelSize(resourceResolver, absoluteY, configuration);
y = constraintModel.pxToDp(position);
}
if (scene == null) {
if (widget.getX() != x || widget.getY() != y) {
widget.setOrigin(x, y);
widget.forceUpdateDrawPosition();
}
}
boolean overrideDimension = false;
// FIXME: need to agree on the correct magic value for this rather than simply using zero.
String layout_width = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH);
if (component.w == 0 || getLayoutDimensionDpValue(component, layout_width) == 0) {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
if (APPLY_MINIMUM_SIZE && widget.getWidth() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
widget.setWidth(MINIMUM_SIZE);
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
overrideDimension = true;
} else {
widget.setWrapWidth(widget.getWidth());
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
}
} else if (layout_width != null && layout_width.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
if (isWidgetInsideConstraintLayout(widget)) {
if (widget.getAnchor(ConstraintAnchor.Type.LEFT).isConnected() && widget.getAnchor(ConstraintAnchor.Type.RIGHT).isConnected()) {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
widget.setWidth(MINIMUM_SIZE_EXPAND);
int height = widget.getHeight();
ConstraintWidget.DimensionBehaviour verticalBehaviour = widget.getVerticalDimensionBehaviour();
if (height <= 1 && widget instanceof WidgetContainer) {
widget.setHeight(MINIMUM_SIZE_EXPAND);
}
ArrayList<ConstraintWidget> widgets = new ArrayList<>();
widgets.add(widget);
Scout.arrangeWidgets(Scout.Arrange.ExpandHorizontally, widgets, true);
widget.setHeight(height);
widget.setVerticalDimensionBehaviour(verticalBehaviour);
overrideDimension = true;
}
}
} else {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
}
String layout_height = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT);
if (component.h == 0 || getLayoutDimensionDpValue(component, layout_height) == 0) {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_WRAP_CONTENT)) {
if (APPLY_MINIMUM_SIZE && widget.getHeight() < MINIMUM_SIZE && widget instanceof WidgetContainer && ((WidgetContainer) widget).getChildren().size() == 0) {
widget.setHeight(MINIMUM_SIZE);
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
overrideDimension = true;
} else {
widget.setWrapHeight(widget.getHeight());
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
}
} else if (layout_height != null && layout_height.equalsIgnoreCase(SdkConstants.VALUE_MATCH_PARENT)) {
if (isWidgetInsideConstraintLayout(widget)) {
if ((widget.getAnchor(ConstraintAnchor.Type.TOP).isConnected() && widget.getAnchor(ConstraintAnchor.Type.BOTTOM).isConnected())) {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
widget.setHeight(MINIMUM_SIZE_EXPAND);
int width = widget.getWidth();
ConstraintWidget.DimensionBehaviour horizontalBehaviour = widget.getHorizontalDimensionBehaviour();
if (width <= 1 && widget instanceof WidgetContainer) {
widget.setWidth(MINIMUM_SIZE_EXPAND);
}
ArrayList<ConstraintWidget> widgets = new ArrayList<>();
widgets.add(widget);
Scout.arrangeWidgets(Scout.Arrange.ExpandVertically, widgets, true);
widget.setWidth(width);
widget.setHorizontalDimensionBehaviour(horizontalBehaviour);
overrideDimension = true;
}
}
} else {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
}
widget.setBaselineDistance(constraintModel.pxToDp(component.getBaseline()));
widget.resetAnchors();
String left1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_LEFT_OF);
String left2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_LEFT_TO_RIGHT_OF);
String right1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_LEFT_OF);
String right2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_RIGHT_TO_RIGHT_OF);
String top1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_TOP_OF);
String top2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_TOP_TO_BOTTOM_OF);
String bottom1 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_TOP_OF);
String bottom2 = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BOTTOM_TO_BOTTOM_OF);
String baseline = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BASELINE_TO_BASELINE_OF);
String ratio = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO);
WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
companion.getWidgetProperties().clear();
companion.getWidgetProperties().setDimensionRatio(ratio);
setMarginType(ConstraintAnchor.Type.LEFT, component, widget);
setMarginType(ConstraintAnchor.Type.RIGHT, component, widget);
setMarginType(ConstraintAnchor.Type.TOP, component, widget);
setMarginType(ConstraintAnchor.Type.BOTTOM, component, widget);
setTarget(model, widgetsScene, left1, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.LEFT);
setStartMargin(left1, component, widget);
setTarget(model, widgetsScene, left2, widget, ConstraintAnchor.Type.LEFT, ConstraintAnchor.Type.RIGHT);
setStartMargin(left2, component, widget);
setTarget(model, widgetsScene, right1, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.LEFT);
setEndMargin(right1, component, widget);
setTarget(model, widgetsScene, right2, widget, ConstraintAnchor.Type.RIGHT, ConstraintAnchor.Type.RIGHT);
setEndMargin(right2, component, widget);
setTarget(model, widgetsScene, top1, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.TOP);
setTopMargin(top1, component, widget);
setTarget(model, widgetsScene, top2, widget, ConstraintAnchor.Type.TOP, ConstraintAnchor.Type.BOTTOM);
setTopMargin(top2, component, widget);
setTarget(model, widgetsScene, bottom1, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.TOP);
setBottomMargin(bottom1, component, widget);
setTarget(model, widgetsScene, bottom2, widget, ConstraintAnchor.Type.BOTTOM, ConstraintAnchor.Type.BOTTOM);
setBottomMargin(bottom2, component, widget);
setTarget(model, widgetsScene, baseline, widget, ConstraintAnchor.Type.BASELINE, ConstraintAnchor.Type.BASELINE);
setBias(SdkConstants.ATTR_LAYOUT_HORIZONTAL_BIAS, component, widget);
setBias(SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS, component, widget);
setDimensionRatio(SdkConstants.ATTR_LAYOUT_DIMENSION_RATIO, component, widget);
setChainStyle(SdkConstants.ATTR_LAYOUT_HORIZONTAL_CHAIN_STYLE, component, widget);
setChainStyle(SdkConstants.ATTR_LAYOUT_VERTICAL_CHAIN_STYLE, component, widget);
setChainWeight(SdkConstants.ATTR_LAYOUT_HORIZONTAL_WEIGHT, component, widget);
setChainWeight(SdkConstants.ATTR_LAYOUT_VERTICAL_WEIGHT, component, widget);
if (widget instanceof Guideline) {
Guideline guideline = (Guideline) widget;
setGuideline(component, guideline);
}
// Update text decorator
WidgetDecorator decorator = companion.getWidgetDecorator(WidgetDecorator.BLUEPRINT_STYLE);
if (decorator != null && decorator instanceof TextWidget) {
TextWidget textWidget = (TextWidget) decorator;
textWidget.setText(getResolvedText(component));
Configuration configuration = component.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
Integer size = null;
if (resourceResolver != null) {
String textSize = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_SIZE);
if (textSize != null) {
size = ViewEditor.resolveDimensionPixelSize(resourceResolver, textSize, configuration);
}
}
if (size == null) {
// With the specified string, this method cannot return null
//noinspection ConstantConditions
size = ViewEditor.resolveDimensionPixelSize(resourceResolver, "15sp", configuration);
}
String alignment = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_ALIGNMENT);
textWidget.setTextAlignment((alignment == null) ? TextWidget.TEXT_ALIGNMENT_VIEW_START : alignmentMap.get(alignment));
String single = component.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_SINGLE_LINE);
textWidget.setSingleLine(Boolean.parseBoolean(single));
// Cannot be null, see previous condition
//noinspection ConstantConditions
textWidget.setTextSize(constraintModel.pxToDp(size));
}
// if true, need to update the XML
return overrideDimension;
}
Aggregations