use of com.android.tools.idea.uibuilder.model.NlModel in project android by JetBrains.
the class AnchorTarget method mouseRelease.
/**
* On mouseRelease, we can either disconnect the current anchor (if the mouse release is on ourselve)
* or connect the anchor to a given target. Modifications are applied first in memory then commited
* to the XML model.
*
* @param x
* @param y
* @param closestTarget
*/
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
myLastX = -1;
myLastY = -1;
if (myComponent.getParent() != null) {
myComponent.getParent().setExpandTargetArea(false);
}
if (closestTarget != null && closestTarget instanceof AnchorTarget && !(((AnchorTarget) closestTarget).isConnected(this))) {
NlComponent component = myComponent.getNlComponent();
if (closestTarget == this) {
disconnectMe(component);
} else {
String attribute = getAttribute(closestTarget);
if (attribute != null) {
AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
AttributesTransaction attributes = connectMe(component, attribute, targetComponent);
NlModel nlModel = component.getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Constraint Connected";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
}
}
}
}
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);
}
Aggregations