Search in sources :

Example 6 with NlModel

use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.

the class GuidelineCycleTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    AttributesTransaction attributes = myComponent.getNlComponent().startAttributeTransaction();
    String begin = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
    String end = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
    String percent = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT);
    SceneComponent parent = myComponent.getParent();
    int value = myComponent.getDrawY() - parent.getDrawY();
    int dimension = parent.getDrawHeight();
    if (!myIsHorizontal) {
        value = myComponent.getDrawX() - parent.getDrawX();
        dimension = parent.getDrawWidth();
    }
    if (begin != null) {
        setEnd(attributes, dimension - value);
    } else if (end != null) {
        setPercent(attributes, value / (float) dimension);
    } else if (percent != null) {
        setBegin(attributes, value);
    }
    attributes.apply();
    NlModel nlModel = myComponent.getNlComponent().getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Cycle Guideline";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            attributes.commit();
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 7 with NlModel

use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.

the class IconPreviewFactoryTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Palette palette = loadPalette();
    List<Palette.Item> items = new ArrayList<>();
    palette.accept(items::add);
    myItem = items.get(0);
    NlModel model = createModel();
    myScreenView = surface().screen(model).getScreen();
    myFactory = new IconPreviewFactory();
    myFacet.setRenderService(new MyRenderService(myFacet));
    myFactory.myRenderTimeoutSeconds = Long.MAX_VALUE;
}
Also used : ArrayList(java.util.ArrayList) NlModel(com.android.tools.idea.uibuilder.model.NlModel)

Example 8 with NlModel

use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.

the class DesignSurfaceFixture method findView.

/**
   * Searches for the nth occurrence of a given view in the layout. The ordering of widgets of the same
   * type is by visual order, first vertically, then horizontally (and finally by XML source offset, if they exactly overlap
   * as for example would happen in a {@code <merge>}
   *
   * @param tag the view tag to search for, e.g. "Button" or "TextView"
   * @param occurrence the index of the occurrence of the tag, e.g. 0 for the first TextView in the layout
   */
@NotNull
public NlComponentFixture findView(@NotNull final String tag, int occurrence) {
    waitForRenderToFinish();
    ScreenView screenView = target().getCurrentScreenView();
    assertNotNull(screenView);
    final NlModel model = screenView.getModel();
    final java.util.List<NlComponent> components = Lists.newArrayList();
    model.getComponents().forEach(component -> addComponents(tag, component, components));
    // Sort by visual order
    components.sort((component1, component2) -> {
        int delta = component1.y - component2.y;
        if (delta != -1) {
            return delta;
        }
        delta = component1.x - component2.x;
        if (delta != -1) {
            return delta;
        }
        // Unlikely
        return component1.getTag().getTextOffset() - component2.getTag().getTextOffset();
    });
    assertTrue("Only " + components.size() + " found, not enough for occurrence #" + occurrence, components.size() > occurrence);
    NlComponent component = components.get(occurrence);
    return createComponentFixture(component);
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with NlModel

use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.

the class ConvertToConstraintLayoutAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView == null) {
        return;
    }
    NlComponent target = findTarget(screenView);
    if (target == null) {
        // Shouldn't happen, enforced by update(AnActionEvent)
        return;
    }
    // Step #1: UI
    Project project = mySurface.getProject();
    ConvertToConstraintLayoutForm dialog = new ConvertToConstraintLayoutForm(project);
    if (!dialog.showAndGet()) {
        return;
    }
    boolean flatten = dialog.getFlattenHierarchy();
    boolean includeIds = dialog.getFlattenReferenced();
    // Step #2: Ensure ConstraintLayout is available in the project
    GradleDependencyManager manager = GradleDependencyManager.getInstance(project);
    GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(SdkConstants.CONSTRAINT_LAYOUT_LIB_ARTIFACT + ":+");
    if (!manager.ensureLibraryIsIncluded(screenView.getModel().getModule(), Collections.singletonList(coordinate), null)) {
        return;
    }
    // Step #3: Migrate
    NlModel model = screenView.getModel();
    @SuppressWarnings("ConstantConditions") ConstraintLayoutConverter converter = new ConstraintLayoutConverter(screenView, target, flatten, includeIds);
    converter.execute();
    // Finally we need to apply the infer constraints action; we can't do that from the above action
    // since we're holding the write lock
    inferConstraints(model);
}
Also used : Project(com.intellij.openapi.project.Project) ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) NlModel(com.android.tools.idea.uibuilder.model.NlModel) GradleDependencyManager(com.android.tools.idea.gradle.dependencies.GradleDependencyManager)

Example 10 with NlModel

use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.

the class RelativeLayoutHandlerTest method createModel.

@NotNull
private NlModel createModel() {
    ModelBuilder builder = model("relative.xml", component(RELATIVE_LAYOUT).withBounds(0, 0, 1000, 1000).matchParentWidth().matchParentHeight().children(component(BUTTON).withBounds(100, 100, 100, 100).id("@id/button").width("100dp").height("100dp").withAttribute("android:layout_alignParentTop", "true").withAttribute("android:layout_alignParentLeft", "true").withAttribute("android:layout_alignParentStart", "true").withAttribute("android:layout_marginTop", "100dp").withAttribute("android:layout_marginLeft", "100dp").withAttribute("android:layout_marginStart", "100dp"), component(CHECK_BOX).withBounds(300, 300, 20, 20).viewObject(mockViewWithBaseline(17)).id("@id/checkbox").width("20dp").height("20dp").withAttribute("android:layout_below", "@id/button").withAttribute("android:layout_toRightOf", "@id/button").withAttribute("android:layout_marginLeft", "100dp").withAttribute("android:layout_marginTop", "100dp"), component(TEXT_VIEW).withBounds(400, 400, 100, 100).viewObject(mockViewWithBaseline(70)).id("@id/textView").width("100dp").height("100dp").withAttribute("android:layout_below", "@id/checkbox").withAttribute("android:layout_toRightOf", "@id/checkbox").withAttribute("android:layout_marginLeft", "80dp").withAttribute("android:layout_marginTop", "80dp")));
    NlModel model = builder.build();
    assertEquals(1, model.getComponents().size());
    assertEquals("NlComponent{tag=<RelativeLayout>, bounds=[0,0:1000x1000}\n" + "    NlComponent{tag=<Button>, bounds=[100,100:100x100}\n" + "    NlComponent{tag=<CheckBox>, bounds=[300,300:20x20}\n" + "    NlComponent{tag=<TextView>, bounds=[400,400:100x100}", NlTreeDumper.dumpTree(model.getComponents()));
    format(model.getFile());
    assertEquals("<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\">\n" + "\n" + "    <Button\n" + "        android:id=\"@id/button\"\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\"\n" + "        android:layout_alignParentTop=\"true\"\n" + "        android:layout_alignParentLeft=\"true\"\n" + "        android:layout_alignParentStart=\"true\"\n" + "        android:layout_marginTop=\"100dp\"\n" + "        android:layout_marginLeft=\"100dp\"\n" + "        android:layout_marginStart=\"100dp\" />\n" + "\n" + "    <CheckBox\n" + "        android:id=\"@id/checkbox\"\n" + "        android:layout_width=\"20dp\"\n" + "        android:layout_height=\"20dp\"\n" + "        android:layout_below=\"@id/button\"\n" + "        android:layout_toRightOf=\"@id/button\"\n" + "        android:layout_marginLeft=\"100dp\"\n" + "        android:layout_marginTop=\"100dp\" />\n" + "\n" + "    <TextView\n" + "        android:id=\"@id/textView\"\n" + "        android:layout_width=\"100dp\"\n" + "        android:layout_height=\"100dp\"\n" + "        android:layout_below=\"@id/checkbox\"\n" + "        android:layout_toRightOf=\"@id/checkbox\"\n" + "        android:layout_marginLeft=\"80dp\"\n" + "        android:layout_marginTop=\"80dp\" />\n" + "\n" + "</RelativeLayout>\n", model.getFile().getText());
    return model;
}
Also used : ModelBuilder(com.android.tools.idea.uibuilder.fixtures.ModelBuilder) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NlModel (com.android.tools.idea.uibuilder.model.NlModel)71 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)33 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)19 NotNull (org.jetbrains.annotations.NotNull)18 XmlFile (com.intellij.psi.xml.XmlFile)14 Project (com.intellij.openapi.project.Project)12 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)11 Result (com.intellij.openapi.application.Result)11 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)11 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)6 ComponentDescriptor (com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)5 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)5 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)5 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)5 Configuration (com.android.tools.idea.configurations.Configuration)4 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 SelectionModel (com.android.tools.idea.uibuilder.model.SelectionModel)2 XmlTag (com.intellij.psi.xml.XmlTag)2 BufferedImage (java.awt.image.BufferedImage)2