Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Row & Column feature parity experiment #802

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
horizontalGap: Dp = 0.dp,
columnWeights: IntArray = intArrayOf(),
padding: Dp = 0.dp,
horizontalChainStyle: ChainStyle = ChainStyle.None,
): ConstrainedLayoutReference {
return createGrid(
elements = elements,
Expand All @@ -752,6 +753,7 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
paddingTop = padding,
paddingRight = padding,
paddingBottom = padding,
chainStyle = horizontalChainStyle,
)
}

Expand Down Expand Up @@ -808,6 +810,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
columnWeights: IntArray = intArrayOf(),
paddingHorizontal: Dp = 0.dp,
paddingVertical: Dp = 0.dp,
horizontalChainStyle: ChainStyle = ChainStyle.None,
horizontalChainBias: Float = 0.5f,
): ConstrainedLayoutReference {
return createGrid(
elements = elements,
Expand All @@ -820,6 +824,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
paddingTop = paddingVertical,
paddingRight = paddingHorizontal,
paddingBottom = paddingVertical,
chainStyle = horizontalChainStyle,
chainBias = horizontalChainBias
)
}

Expand Down Expand Up @@ -873,6 +879,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
rowWeights: IntArray = intArrayOf(),
verticalGap: Dp = 0.dp,
padding: Dp = 0.dp,
verticalChainStyle: ChainStyle = ChainStyle.None,
verticalChainBias: Float = 0.5f,
): ConstrainedLayoutReference {
return createGrid(
elements = elements,
Expand All @@ -885,6 +893,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
paddingTop = padding,
paddingRight = padding,
paddingBottom = padding,
chainStyle = verticalChainStyle,
chainBias = verticalChainBias
)
}

Expand Down Expand Up @@ -940,6 +950,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
rowWeights: IntArray = intArrayOf(),
paddingHorizontal: Dp = 0.dp,
paddingVertical: Dp = 0.dp,
verticalChainStyle: ChainStyle = ChainStyle.Spread,
verticalChainBias: Float = 0.5f
): ConstrainedLayoutReference {
return createGrid(
elements = elements,
Expand All @@ -952,6 +964,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
paddingTop = paddingVertical,
paddingRight = paddingHorizontal,
paddingBottom = paddingVertical,
chainStyle = verticalChainStyle,
chainBias = verticalChainBias,
)
}

Expand Down Expand Up @@ -1299,6 +1313,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
paddingRight: Dp = 0.dp,
paddingBottom: Dp = 0.dp,
flags: Array<GridFlag> = arrayOf(),
chainStyle: ChainStyle = ChainStyle.None,
chainBias: Float = 0.5f
): ConstrainedLayoutReference {
val ref = ConstrainedLayoutReference(createHelperId())
val elementArray = CLArray(charArrayOf())
Expand Down Expand Up @@ -1338,6 +1354,8 @@ abstract class ConstraintLayoutBaseScope internal constructor(extendFrom: CLObje
putString("skips", skips)
putString("spans", spans)
put("flags", flagArray)
put("chainStyle", CLString.from(chainStyle.name))
put("chainBias", CLNumber(chainBias))
}

return ref
Expand Down Expand Up @@ -1798,6 +1816,12 @@ class ChainStyle internal constructor(
internal val bias: Float? = null
) {
companion object {
/**
* Not apply chain style
*/
@Stable
val None = ChainStyle("none")

/**
* A chain style that evenly distributes the contained layouts.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,18 @@ private static void parseGridType(String gridType,
}
}
break;
case "chainStyle":
String chainStyle = element.get(param).content();
if (chainStyle != null && chainStyle.length() > 0) {
grid.setChainStyle(chainStyle);
}
break;
case "chainBias":
float chainBias = element.get(param).getFloat();
if (chainBias >= 0 && chainBias <= 1) {
grid.setChainBias(chainBias);
}
break;
default:
ConstraintReference reference = state.constraints(name);
applyAttribute(state, layoutVariables, reference, element, param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ public GridReference(@NonNull State state, @NonNull State.Helper type) {
*/
private int[] mFlags;

public String getChainStyle() {
return mChainStyle;
}

public void setChainStyle(String chainStyle) {
mChainStyle = chainStyle;
}

private String mChainStyle;


public float getChainBias() {
return mChainBias;
}

public void setChainBias(float chainBias) {
mChainBias = chainBias;
}

private float mChainBias;

/**
* get padding left
* @return padding left
Expand Down Expand Up @@ -454,6 +475,14 @@ public void apply() {
mGrid.setFlags(mFlags);
}

if (mChainStyle != null) {
mGrid.setChainStyle(mChainStyle);
}

if (mChainBias >= 0 && mChainBias <= 1) {
mGrid.setChainBias(mChainBias);
}

// General attributes of a widget
applyBase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class GridCore extends VirtualLayout {
private static final int MAX_ROWS = 50; // maximum number of rows can be specified.
private static final int MAX_COLUMNS = 50; // maximum number of columns can be specified.

// Values of the chain styles
public static final String CHAIN_SPREAD = "spread";
public static final String CHAIN_SPREAD_INSIDE = "spread_inside";
public static final String CHAIN_PACKED = "packed";

/**
* Container for all the ConstraintWidgets
*/
Expand Down Expand Up @@ -162,6 +167,27 @@ public class GridCore extends VirtualLayout {
*/
private boolean mSubGridByColRow = false;

public String getChainStyle() {
return mChainStyle;
}

public void setChainStyle(String chainStyle) {
mChainStyle = chainStyle;
}

public float getChainBias() {
return mChainBias;
}

public void setChainBias(float chainBias) {
mChainBias = chainBias;
}

private String mChainStyle;


private float mChainBias;

public GridCore() {
updateActualRowsAndColumns();
initMatrices();
Expand Down Expand Up @@ -781,9 +807,64 @@ private void setBoxWidgetVerticalChains() {
* Chains the boxWidgets and add constraints to the widgets
*/
private void addConstraints() {
setBoxWidgetVerticalChains();
setBoxWidgetHorizontalChains();
arrangeWidgets();
// setBoxWidgetVerticalChains();
// setBoxWidgetHorizontalChains();
// arrangeWidgets();

int style = -1;
if (mChainStyle.equals(CHAIN_SPREAD)) {
style = 0;
} else if (mChainStyle.equals(CHAIN_SPREAD_INSIDE)) {
style = 1;
} else if (mChainStyle.equals(CHAIN_PACKED)) {
style = 2;
}

if (mRows == 1 && style != -1) {
for (int i = 0; i < mWidgetsCount; i++) {
if (i == 0) {
mWidgets[i].mLeft.connect(mLeft, 0);
mWidgets[i].setHorizontalChainStyle(style);
mWidgets[i].setHorizontalBiasPercent(mChainBias);
} else {
mWidgets[i].mLeft.connect(mWidgets[i - 1].mRight, 0);
}
if (i == mWidgetsCount - 1) {
mWidgets[i].mRight.connect(mRight, 0);
} else {
mWidgets[i].mRight.connect(mWidgets[i + 1].mLeft, 0);
}

if (mWidgets[i].mTop.mTarget == null && mWidgets[i].mBottom.mTarget == null) {
mWidgets[i].mTop.connect(mTop, 0);
mWidgets[i].mBottom.connect(mBottom, 0);
}
}
} else if (mColumns == 1 && style != -1) {
for (int i = 0; i < mWidgetsCount; i++) {
if (i == 0) {
mWidgets[i].mTop.connect(mTop, 0);
mWidgets[i].setVerticalChainStyle(style);
mWidgets[i].setVerticalBiasPercent(mChainBias);
} else {
mWidgets[i].mTop.connect(mWidgets[i - 1].mBottom, 0);
}
if (i == mWidgetsCount - 1) {
mWidgets[i].mBottom.connect(mBottom, 0);
} else {
mWidgets[i].mBottom.connect(mWidgets[i + 1].mTop, 0);
}

if (mWidgets[i].mLeft.mTarget == null && mWidgets[i].mRight.mTarget == null) {
mWidgets[i].mLeft.connect(mLeft, 0);
mWidgets[i].mRight.connect(mRight, 0);
}
}
} else {
setBoxWidgetVerticalChains();
setBoxWidgetHorizontalChains();
arrangeWidgets();
}
}

/**
Expand Down
Loading