use of com.android.tools.idea.uibuilder.model.NlComponent 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.NlComponent in project android by JetBrains.
the class DragHandler method insertComponents.
/**
* Insert the components being dragged into this layout.
*
* @param insertIndex the position to drop the dragged components at, or -1 to append them at the end.
* The index refers to the position of the children <b>before</b> the drag, which
* matters if some of the existing children in the layout are being dragged.
* @param insertType the type of move/insert
*/
protected final void insertComponents(int insertIndex, @NotNull InsertType insertType) {
NlComponent before = null;
if (insertIndex != -1 && insertIndex < layout.getChildCount()) {
before = layout.getChild(insertIndex);
}
editor.getModel().addComponents(components, layout, before, insertType);
}
use of com.android.tools.idea.uibuilder.model.NlComponent 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.NlComponent in project android by JetBrains.
the class ConvertToConstraintLayoutAction method update.
@Override
public void update(AnActionEvent e) {
Presentation presentation = e.getPresentation();
ScreenView screenView = mySurface.getCurrentScreenView();
NlComponent target = findTarget(screenView);
if (target != null) {
String tagName = target.getTagName();
// Don't show action if it's already a ConstraintLayout
if (target.isOrHasSuperclass(CONSTRAINT_LAYOUT)) {
presentation.setVisible(false);
return;
}
presentation.setVisible(true);
tagName = tagName.substring(tagName.lastIndexOf('.') + 1);
presentation.setText("Convert " + tagName + " to ConstraintLayout");
presentation.setEnabled(true);
} else {
presentation.setText(TITLE);
presentation.setEnabled(false);
presentation.setVisible(true);
}
}
use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class GenerateLayoutTestSkeletonAction method getBounds.
@NotNull
private static Rectangle getBounds(@NotNull NlComponent component) {
NlComponent parent = component.getParent();
Rectangle parentBounds = parent != null ? getBounds(parent) : new Rectangle(0, 0, 1000, 1000);
ViewInfo viewInfo = component.viewInfo;
if (viewInfo == null) {
return new Rectangle(parentBounds.x, parentBounds.y, 100, 100);
}
return new Rectangle(viewInfo.getLeft() + parentBounds.x, viewInfo.getTop() + parentBounds.y, viewInfo.getRight() - viewInfo.getLeft(), viewInfo.getBottom() - viewInfo.getTop());
}
Aggregations