use of com.android.ide.common.resources.ResourceResolver 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;
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class TextViewDecorator method addContent.
@Override
public void addContent(@NotNull DisplayList list, long time, @NotNull SceneContext sceneContext, @NotNull SceneComponent component) {
Rectangle rect = new Rectangle();
component.fillDrawRect(time, rect);
int l = sceneContext.getSwingX(rect.x);
int t = sceneContext.getSwingY(rect.y);
int w = sceneContext.getSwingDimension(rect.width);
int h = sceneContext.getSwingDimension(rect.height);
String text = ConstraintUtilities.getResolvedText(component.getNlComponent());
NlComponent nlc = component.getNlComponent();
Configuration configuration = nlc.getModel().getConfiguration();
ResourceResolver resourceResolver = configuration.getResourceResolver();
Integer size = null;
if (resourceResolver != null) {
String textSize = nlc.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, DEFAULT_DIM, configuration);
// temporary
size = (int) (0.8 * size);
}
String alignment = nlc.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_TEXT_ALIGNMENT);
int align = ConstraintUtilities.getAlignment(alignment);
String single = nlc.getAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_SINGLE_LINE);
boolean singleLine = Boolean.parseBoolean(single);
int baseLineOffset = sceneContext.getSwingDimension(component.getBaseline());
int scaleSize = sceneContext.getSwingDimension(size);
list.add(new DrawTextRegion(l, t, w, h, baseLineOffset, text, singleLine, false, align, DrawTextRegion.TEXT_ALIGNMENT_VIEW_START, scaleSize));
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class NlOldPalettePanel method setColors.
private void setColors() {
Color background;
Color foreground;
Configuration configuration = null;
if (myDesignSurface != null) {
configuration = myDesignSurface.getConfiguration();
}
ResourceResolver resolver = null;
if (configuration != null) {
resolver = configuration.getResourceResolver();
}
if (resolver == null || myMode != Mode.PREVIEW) {
foreground = UIUtil.getTreeForeground();
background = UIUtil.getTreeBackground();
} else {
ResourceValue windowBackground = resolver.findItemInTheme("colorBackground", true);
background = ResourceHelper.resolveColor(resolver, windowBackground, myProject);
if (background == null) {
background = UIUtil.getTreeBackground();
}
ResourceValue textForeground = resolver.findItemInTheme("colorForeground", true);
foreground = ResourceHelper.resolveColor(resolver, textForeground, myProject);
if (foreground == null) {
foreground = UIUtil.getTreeForeground();
}
// Ensure the colors can be differentiated:
if (Math.abs(ImageUtils.getBrightness(background.getRGB()) - ImageUtils.getBrightness(foreground.getRGB())) < 64) {
if (ImageUtils.getBrightness(background.getRGB()) < 128) {
foreground = JBColor.WHITE;
} else {
foreground = JBColor.BLACK;
}
}
}
myPaletteTree.setBackground(background);
myPaletteTree.setForeground(foreground);
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class NlBooleanRenderer method customizeRenderContent.
@Override
public void customizeRenderContent(@NotNull JTable table, @NotNull NlProperty p, boolean selected, boolean hasFocus, int row, int col) {
myCheckbox.setEnabled(true);
myLabel.clear();
String propValue = p.getValue();
ThreeStateCheckBox.State state = getState(propValue);
if (state == null && propValue != null) {
ResourceResolver resourceResolver = p.getResolver();
if (resourceResolver != null) {
myLabel.append(propValue, modifyAttributes(selected, SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES));
String resolvedValue = resourceResolver.findResValue(propValue, false).getValue();
state = getState(resolvedValue);
}
}
if (state != null) {
myCheckbox.setEnabled(true);
myCheckbox.setState(state);
} else {
myCheckbox.setEnabled(false);
}
}
use of com.android.ide.common.resources.ResourceResolver in project android by JetBrains.
the class NlDefaultRenderer method getIcon.
@Nullable
public static Icon getIcon(@NotNull NlProperty property) {
String text = property.getResolvedValue();
if (text == null) {
return null;
}
if (isColorValue(text)) {
return getColorIcon(text);
}
Configuration configuration = property.getModel().getConfiguration();
//noinspection ConstantConditions
if (configuration == null) {
// happens in unit test
return null;
}
ResourceResolver resolver = configuration.getResourceResolver();
if (resolver == null) {
return null;
}
if (text.startsWith(SdkConstants.COLOR_RESOURCE_PREFIX) || text.startsWith(SdkConstants.ANDROID_COLOR_RESOURCE_PREFIX)) {
return getColorIcon(resolver, property, text);
}
if (text.startsWith(SdkConstants.DRAWABLE_PREFIX) || text.startsWith(SdkConstants.ANDROID_DRAWABLE_PREFIX) || text.startsWith(SdkConstants.MIPMAP_PREFIX)) {
return getDrawableIcon(resolver, property, text);
}
return null;
}
Aggregations