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();
}
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;
}
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);
}
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);
}
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;
}
Aggregations