Search in sources :

Example 1 with ConstraintModel

use of com.android.tools.idea.uibuilder.handlers.constraint.ConstraintModel 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)1 ConstraintModel (com.android.tools.idea.uibuilder.handlers.constraint.ConstraintModel)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 WidgetsScene (com.android.tools.sherpa.structure.WidgetsScene)1 List (java.util.List)1