use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.
the class WidgetDecorator method getConstraintHandle.
/**
* Utility function to return a ConstraintHandle instance associated with the anchor
*
* @param anchor the given anchor
* @return returns the associated ConstraintHandle
*/
public static ConstraintHandle getConstraintHandle(ConstraintAnchor anchor) {
ConstraintWidget widget = anchor.getOwner();
WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
WidgetInteractionTargets interactionTargets = companion.getWidgetInteractionTargets();
return interactionTargets.getConstraintHandle(anchor);
}
use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.
the class SceneDraw method getDecorator.
/**
* Utility function to get the decorator of the widget and set the correct
* anchor and resize indicators
*
* @param widget the widget to draw
* @param selectedWidget the selected widget if any
* @param selectedAnchor the selected anchor if any
* @param selectedResizeHandle the selected resize handle if any
* @return the widget decorator
*/
private WidgetDecorator getDecorator(ConstraintWidget widget, ConstraintWidget selectedWidget, ConstraintAnchor selectedAnchor, ResizeHandle selectedResizeHandle) {
WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
WidgetDecorator decorator = companion.getWidgetDecorator(getCurrentStyle());
if (!decorator.isSelected()) {
decorator.updateShowAnchorsPolicy(selectedWidget, selectedAnchor);
} else {
decorator.setShowResizeHandles(mWidgetMotion.needToShowDecorations());
decorator.setShowSizeIndicator(selectedResizeHandle != null);
}
if (mMoveOnlyMode) {
decorator.setShowResizeHandles(false);
}
return decorator;
}
use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.
the class ConstraintModel method onSelectionChanged.
/**
* Something has changed in our selection
*
* @param selection our internal selection object
*/
@Override
public void onSelectionChanged(Selection selection) {
SelectionModel selectionModel = myNlModel.getSelectionModel();
if (selection.isEmpty()) {
selectionModel.clear();
return;
}
List<NlComponent> components = new ArrayList<>();
for (Selection.Element selectedElement : mySelection.getElements()) {
if (selectedElement.widget == myDragDropWidget) {
continue;
}
WidgetCompanion companion = (WidgetCompanion) selectedElement.widget.getCompanionWidget();
NlComponent component = (NlComponent) companion.getWidgetModel();
components.add(component);
}
if (!components.isEmpty()) {
selectionModel.setSelection(components);
} else {
selectionModel.clear();
}
}
use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.
the class ConstraintModel method createSolverWidgetFromComponent.
/**
* Create the widget associated to a component if necessary.
*
* @param component the component we want to represent
* @return true if we need to save the XML
*/
private boolean createSolverWidgetFromComponent(@NotNull NlComponent component) {
ConstraintWidget widget = myWidgetsScene.getWidget(component);
boolean saveXML = false;
if (widget != null && isConstraintLayout(component)) {
if (!(widget instanceof ConstraintWidgetContainer)) {
if (widget instanceof WidgetContainer) {
ConstraintWidgetContainer container = new ConstraintWidgetContainer();
myWidgetsScene.transformContainerToContainer((WidgetContainer) widget, container);
saveXML = setupConstraintWidget(component, container);
widget = container;
} else {
myWidgetsScene.removeWidget(widget);
widget = null;
}
}
}
if (widget == null) {
boolean dropWidget = false;
if (USE_GUIDELINES_DURING_DND) {
if (myDragDropWidget != null) {
WidgetCompanion companion = (WidgetCompanion) myDragDropWidget.getCompanionWidget();
if (companion.getWidgetModel() == component) {
widget = myDragDropWidget;
if (component.isOrHasSuperclass(CLASS_VIEWGROUP) || isDataBindingLayout(component)) {
widget = new WidgetContainer();
}
widget.setCompanionWidget(null);
dropWidget = true;
}
}
}
if (widget == null) {
if (isConstraintLayout(component)) {
widget = new ConstraintWidgetContainer();
} else if (component.getTagName().equalsIgnoreCase(SdkConstants.CONSTRAINT_LAYOUT_GUIDELINE)) {
widget = new Guideline();
String orientation = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_ORIENTATION);
if (orientation != null) {
if (SdkConstants.ATTR_GUIDELINE_ORIENTATION_VERTICAL.equalsIgnoreCase(orientation)) {
Guideline guideline = (Guideline) widget;
guideline.setOrientation(Guideline.VERTICAL);
}
}
} else {
if (component.isOrHasSuperclass(CLASS_VIEWGROUP) || isDataBindingLayout(component) || component.getChildCount() > 0) {
widget = new WidgetContainer();
} else {
widget = new ConstraintWidget();
}
}
}
saveXML |= setupConstraintWidget(component, widget);
myWidgetsScene.setWidget(widget);
if (USE_GUIDELINES_DURING_DND) {
if (dropWidget) {
connectDroppedWidget();
myDragDropWidget = null;
mySelection.add(widget);
}
}
}
for (NlComponent child : component.getChildren()) {
saveXML |= createSolverWidgetFromComponent(child);
}
return saveXML;
}
use of com.android.tools.sherpa.structure.WidgetCompanion in project android by JetBrains.
the class ConstraintUtilities method setConnection.
/**
* Update the attributes given a new anchor
*
* @param attributes the attributes we work on
* @param anchor the anchor we want to update from
*/
static void setConnection(@NotNull NlAttributesHolder attributes, @NotNull ConstraintAnchor anchor) {
resetAnchor(attributes, anchor.getType());
String attribute = getConnectionAttribute(anchor, anchor.getTarget());
String marginAttribute = getConnectionAttributeMargin(anchor);
String marginAttributeRtl = getConnectionRtlAttributeMargin(anchor);
if (anchor.isConnected() && attribute != null) {
ConstraintWidget owner = anchor.getOwner();
WidgetCompanion ownerCompanion = (WidgetCompanion) owner.getCompanionWidget();
ConstraintWidget target = anchor.getTarget().getOwner();
WidgetCompanion companion = (WidgetCompanion) target.getCompanionWidget();
NlComponent targetComponent = (NlComponent) companion.getWidgetModel();
String targetId;
boolean use_parent_ref = useParentReference(targetComponent.getModel());
if (owner.getParent() == target && use_parent_ref) {
targetId = SdkConstants.ATTR_PARENT;
} else {
targetId = SdkConstants.NEW_ID_PREFIX + targetComponent.ensureId();
}
attributes.setAttribute(SdkConstants.SHERPA_URI, attribute, targetId);
String margin = ownerCompanion.getWidgetProperties().getMarginValue(anchor);
String marginRtl = ownerCompanion.getWidgetProperties().getMarginRtlValue(anchor);
String marginValue = null;
if (anchor.getMargin() > 0) {
marginValue = String.format(SdkConstants.VALUE_N_DP, anchor.getMargin());
}
if (margin != null && !ownerCompanion.getWidgetProperties().isMarginReference(anchor)) {
margin = marginValue;
}
if (marginRtl != null && !ownerCompanion.getWidgetProperties().isMarginRtlReference(anchor)) {
marginRtl = marginValue;
}
NlModel model = targetComponent.getModel();
if (supportsStartEnd(anchor, model)) {
// If we need to set RTL attributes
if (marginRtl == null) {
if (margin != null) {
marginRtl = margin;
} else {
marginRtl = marginValue;
}
}
if (requiresRightLeft(model)) {
// If in addition to RTL attribute we need right/left...
if (margin == null) {
margin = marginRtl;
}
}
} else if (margin == null) {
margin = marginValue;
}
if (marginAttribute != null && margin != null) {
attributes.setAttribute(SdkConstants.NS_RESOURCES, marginAttribute, margin);
}
if (marginAttributeRtl != null && marginRtl != null) {
attributes.setAttribute(SdkConstants.NS_RESOURCES, marginAttributeRtl, marginRtl);
}
String attributeCreator = getConnectionAttributeCreator(anchor);
if (anchor.getConnectionCreator() != 0) {
attributes.setAttribute(SdkConstants.TOOLS_URI, attributeCreator, String.valueOf(anchor.getConnectionCreator()));
} else {
attributes.setAttribute(SdkConstants.TOOLS_URI, attributeCreator, null);
}
}
}
Aggregations