Search in sources :

Example 6 with RenderLogger

use of com.android.tools.idea.rendering.RenderLogger in project android by JetBrains.

the class ConstraintModel method updateConstraintLayoutRoots.

/**
   * Traverse the hierarchy to find all ConstraintLayout instances
   * and update them. We set all the wrap_content sizes of the ConstraintLayout children
   * from layout lib
   *  @param container
   *
   */
private void updateConstraintLayoutRoots(WidgetContainer container) {
    if (container == null) {
        return;
    }
    Map<NlComponent, Dimension> wrapContentSizes = Maps.newHashMap();
    if (container instanceof ConstraintWidgetContainer) {
        NlComponent component = (NlComponent) ((WidgetCompanion) container.getCompanionWidget()).getWidgetModel();
        Insets padding = component.getPadding(true);
        container.setDimension(pxToDp(component.w - padding.width()), pxToDp(component.h - padding.height()));
        int x = pxToDp(component.x);
        int y = pxToDp(component.y);
        x += pxToDp(padding.left);
        y += pxToDp(padding.top);
        WidgetContainer parentContainer = (WidgetContainer) container.getParent();
        if (parentContainer != null) {
            if (!(parentContainer instanceof ConstraintWidgetContainer)) {
                x = pxToDp(component.x - component.getParent().x);
                y = pxToDp(component.y - component.getParent().y);
            } else {
                x -= parentContainer.getDrawX();
                y -= parentContainer.getDrawY();
            }
        }
        if (container.getX() != x || container.getY() != y) {
            container.setOrigin(x, y);
            container.forceUpdateDrawPosition();
        }
    }
    if (!(container instanceof ConstraintWidgetContainer)) {
        NlComponent component = (NlComponent) ((WidgetCompanion) container.getCompanionWidget()).getWidgetModel();
        container.setDimension(pxToDp(component.w), pxToDp(component.h));
    }
    if (container instanceof ConstraintWidgetContainer && container.getChildren().size() > 0) {
        NlComponent root = (NlComponent) ((WidgetCompanion) container.getCompanionWidget()).getWidgetModel();
        XmlTag parentTag = root.getTag();
        if (parentTag.isValid()) {
            Map<XmlTag, NlComponent> tagToComponent = Maps.newHashMapWithExpectedSize(root.getChildCount());
            for (NlComponent child : root.getChildren()) {
                tagToComponent.put(child.getTag(), child);
            }
            XmlFile xmlFile = myNlModel.getFile();
            AndroidFacet facet = myNlModel.getFacet();
            RenderService renderService = RenderService.get(facet);
            RenderLogger logger = renderService.createLogger();
            final RenderTask task = renderService.createTask(xmlFile, myNlModel.getConfiguration(), logger, null);
            if (task != null) {
                // Measure wrap_content bounds
                Map<XmlTag, ViewInfo> map = task.measureChildren(parentTag, new RenderTask.AttributeFilter() {

                    @Override
                    public String getAttribute(@NotNull XmlTag n, @Nullable String namespace, @NotNull String localName) {
                        // Change attributes to wrap_content
                        if (ATTR_LAYOUT_WIDTH.equals(localName) && ANDROID_URI.equals(namespace)) {
                            return VALUE_WRAP_CONTENT;
                        }
                        if (ATTR_LAYOUT_HEIGHT.equals(localName) && ANDROID_URI.equals(namespace)) {
                            return VALUE_WRAP_CONTENT;
                        }
                        return null;
                    }
                });
                task.dispose();
                if (map != null) {
                    for (Map.Entry<XmlTag, ViewInfo> entry : map.entrySet()) {
                        ViewInfo viewInfo = entry.getValue();
                        viewInfo = RenderService.getSafeBounds(viewInfo);
                        Dimension size = new Dimension(viewInfo.getRight() - viewInfo.getLeft(), viewInfo.getBottom() - viewInfo.getTop());
                        NlComponent child = tagToComponent.get(entry.getKey());
                        if (child != null) {
                            wrapContentSizes.put(child, size);
                        }
                    }
                }
            }
        }
    }
    for (ConstraintWidget child : container.getChildren()) {
        NlComponent component = (NlComponent) ((WidgetCompanion) child.getCompanionWidget()).getWidgetModel();
        Dimension dimension = wrapContentSizes.get(component);
        if (dimension != null) {
            child.setWrapWidth(pxToDp((int) dimension.getWidth()));
            child.setWrapHeight(pxToDp((int) dimension.getHeight()));
        }
        if (child instanceof WidgetContainer) {
            updateConstraintLayoutRoots((WidgetContainer) child);
        }
    }
}
Also used : Insets(com.android.tools.idea.uibuilder.model.Insets) RenderLogger(com.android.tools.idea.rendering.RenderLogger) XmlFile(com.intellij.psi.xml.XmlFile) RenderTask(com.android.tools.idea.rendering.RenderTask) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) RenderService(com.android.tools.idea.rendering.RenderService) Map(java.util.Map) WeakHashMap(com.intellij.util.containers.WeakHashMap) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

RenderLogger (com.android.tools.idea.rendering.RenderLogger)6 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 RenderResult (com.android.tools.idea.rendering.RenderResult)2 RenderService (com.android.tools.idea.rendering.RenderService)2 RenderTask (com.android.tools.idea.rendering.RenderTask)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 Result (com.android.ide.common.rendering.api.Result)1 UsageTracker (com.android.tools.analytics.UsageTracker)1 Configuration (com.android.tools.idea.configurations.Configuration)1 HtmlLinkManager (com.android.tools.idea.rendering.HtmlLinkManager)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 Insets (com.android.tools.idea.uibuilder.model.Insets)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)1 AndroidStudioEvent (com.google.wireless.android.sdk.stats.AndroidStudioEvent)1 LayoutEditorRenderResult (com.google.wireless.android.sdk.stats.LayoutEditorRenderResult)1 MockModule (com.intellij.mock.MockModule)1