Search in sources :

Example 11 with RenderResult

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

the class ConvertToConstraintLayoutAction method inferConstraints.

private static void inferConstraints(NlModel model) {
    RenderResult result = model.getRenderResult();
    if (result == null || !result.getRenderResult().isSuccess()) {
        return;
    }
    ApplicationManager.getApplication().invokeLater(() -> {
        final ConstraintModel constraintModel = ConstraintModel.getConstraintModel(model);
        if (constraintModel != null) {
            int dpi = model.getConfiguration().getDensity().getDpiValue();
            constraintModel.setDpiValue(dpi);
            constraintModel.updateNlModel(null, model.getComponents(), true);
            // Infer new constraints
            WidgetsScene scene = constraintModel.getScene();
            try {
                Scout.inferConstraints(scene);
            } catch (Throwable t) {
                Logger.getInstance(ConvertToConstraintLayoutAction.class).warn(t);
            }
            WriteCommandAction.runWriteCommandAction(model.getProject(), "Infer Constraints", null, new Runnable() {

                @Override
                public void run() {
                    constraintModel.saveToXML(true);
                    constraintModel.setNeedsAnimateConstraints(ConstraintAnchor.SCOUT_CREATOR);
                    // Finally remove the conversion x/y/width/height attributes
                    List<NlComponent> components = model.getComponents();
                    for (NlComponent root : components) {
                        removeAbsolutePositionAndSizes(root);
                    }
                }
            }, model.getFile());
        }
    }, ModalityState.any());
}
Also used : WidgetsScene(com.android.tools.sherpa.structure.WidgetsScene) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) RenderResult(com.android.tools.idea.rendering.RenderResult) List(java.util.List) ConstraintModel(com.android.tools.idea.uibuilder.handlers.constraint.ConstraintModel)

Aggregations

RenderResult (com.android.tools.idea.rendering.RenderResult)11 UsageTracker (com.android.tools.analytics.UsageTracker)2 Configuration (com.android.tools.idea.configurations.Configuration)2 RenderLogger (com.android.tools.idea.rendering.RenderLogger)2 RenderErrorModel (com.android.tools.idea.rendering.errors.ui.RenderErrorModel)2 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)2 PsiFile (com.intellij.psi.PsiFile)2 Update (com.intellij.util.ui.update.Update)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 Result (com.android.ide.common.rendering.api.Result)1 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 State (com.android.sdklib.devices.State)1 HtmlLinkManager (com.android.tools.idea.rendering.HtmlLinkManager)1 RenderErrorModelFactory (com.android.tools.idea.rendering.RenderErrorModelFactory)1 ConstraintModel (com.android.tools.idea.uibuilder.handlers.constraint.ConstraintModel)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 WidgetsScene (com.android.tools.sherpa.structure.WidgetsScene)1 Cache (com.google.common.cache.Cache)1