Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

[Compose] Fix margin for Chain first and last elements #573

Merged
merged 2 commits into from
Apr 21, 2022
Merged
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 @@ -76,15 +76,16 @@ public interface ConstraintReferenceFactory {
protected int mMarginRight = 0;
protected int mMarginStart = 0;
protected int mMarginEnd = 0;
int mMarginTop = 0;
int mMarginBottom = 0;

int mMarginLeftGone = 0;
int mMarginRightGone = 0;
int mMarginStartGone = 0;
int mMarginEndGone = 0;
int mMarginTopGone = 0;
int mMarginBottomGone = 0;
protected int mMarginTop = 0;
protected int mMarginBottom = 0;

protected int mMarginLeftGone = 0;
protected int mMarginRightGone = 0;
protected int mMarginStartGone = 0;
protected int mMarginEndGone = 0;
protected int mMarginTopGone = 0;
protected int mMarginBottomGone = 0;

int mMarginBaseline = 0;
int mMarginBaselineGone = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ public ChainReference bias(float bias) {
mBias = bias;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ public void apply() {
if (first == null) {
first = reference;
if (mStartToStart != null) {
first.startToStart(mStartToStart).margin(mMarginStart);
first.startToStart(mStartToStart)
.margin(mMarginStart)
.marginGone(mMarginStartGone);
} else if (mStartToEnd != null) {
first.startToEnd(mStartToEnd).margin(mMarginStart);
first.startToEnd(mStartToEnd).margin(mMarginStart).marginGone(mMarginStartGone);
} else if (mLeftToLeft != null) {
// TODO: Hack until we support RTL properly
first.startToStart(mLeftToLeft).margin(mMarginLeft);
first.startToStart(mLeftToLeft).margin(mMarginLeft).marginGone(mMarginLeftGone);
} else if (mLeftToRight != null) {
// TODO: Hack until we support RTL properly
first.startToEnd(mLeftToRight).margin(mMarginLeft);
first.startToEnd(mLeftToRight).margin(mMarginLeft).marginGone(mMarginLeftGone);
} else {
// No constraint declared, default to Parent.
first.startToStart(State.PARENT);
}
}
Expand All @@ -64,16 +67,17 @@ public void apply() {

if (previous != null) {
if (mEndToStart != null) {
previous.endToStart(mEndToStart).margin(mMarginEnd);
previous.endToStart(mEndToStart).margin(mMarginEnd).marginGone(mMarginEndGone);
} else if (mEndToEnd != null) {
previous.endToEnd(mEndToEnd).margin(mMarginEnd);
previous.endToEnd(mEndToEnd).margin(mMarginEnd).marginGone(mMarginEndGone);
} else if (mRightToLeft != null) {
// TODO: Hack until we support RTL properly
previous.endToStart(mRightToLeft).margin(mMarginRight);
previous.endToStart(mRightToLeft).margin(mMarginRight).marginGone(mMarginRightGone);
} else if (mRightToRight != null) {
// TODO: Hack until we support RTL properly
previous.endToEnd(mRightToRight).margin(mMarginRight);
previous.endToEnd(mRightToRight).margin(mMarginRight).marginGone(mMarginRightGone);
} else {
// No constraint declared, default to Parent.
previous.endToEnd(State.PARENT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public void apply() {
if (first == null) {
first = reference;
if (mTopToTop != null) {
first.topToTop(mTopToTop);
first.topToTop(mTopToTop).margin(mMarginTop).marginGone(mMarginTopGone);
} else if (mTopToBottom != null) {
first.topToBottom(mTopToBottom);
first.topToBottom(mTopToBottom).margin(mMarginTop).marginGone(mMarginTopGone);
} else {
// No constraint declared, default to Parent.
first.topToTop(State.PARENT);
}
}
Expand All @@ -58,10 +59,15 @@ public void apply() {

if (previous != null) {
if (mBottomToTop != null) {
previous.bottomToTop(mBottomToTop);
previous.bottomToTop(mBottomToTop)
.margin(mMarginBottom)
.marginGone(mMarginBottomGone);
} else if (mBottomToBottom != null) {
previous.bottomToBottom(mBottomToBottom);
previous.bottomToBottom(mBottomToBottom)
.margin(mMarginBottom)
.marginGone(mMarginBottomGone);
} else {
// No constraint declared, default to Parent.
previous.bottomToBottom(State.PARENT);
}
}
Expand All @@ -86,5 +92,4 @@ public void apply() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.constraintlayout.compose.ChainStyle
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension
import androidx.constraintlayout.compose.Visibility

@Preview
@Composable
Expand Down Expand Up @@ -114,7 +115,42 @@ fun Test12() { // Constraint an horizontal chain
start.linkTo(parent.start, 12.dp)
}
constrain(chain1) {
start.linkTo(box3.end)
start.linkTo(box3.end, 10.dp)
}
}
ThreeBoxChainLayout(constraintSet = constraintSet)
}

@Preview
@Composable
fun Test18() { // Constraint an horizontal chain with gone margins
val constraintSet = ConstraintSet {
val box1 = createRefFor("box1")
val box2 = createRefFor("box2")
val box3 = createRefFor("box3")
val chain1 = createHorizontalChain(box1, box2, chainStyle = ChainStyle.Spread)

constrain(box1) {
width = Dimension.value(20.dp)
height = Dimension.value(20.dp)
centerVerticallyTo(parent)
}
constrain(box2) {
width = Dimension.value(20.dp)
height = Dimension.value(20.dp)
centerVerticallyTo(box1)
}
constrain(box3) {
width = Dimension.value(200.dp)
height = Dimension.value(20.dp)
top.linkTo(parent.top, 12.dp)
start.linkTo(parent.start, 12.dp)
visibility = Visibility.Gone
}
constrain(chain1) {
start.linkTo(box3.end, 10.dp, 100.dp)
// gone margin with parent should not matter
end.linkTo(parent.end, 10.dp, 200.dp)
}
}
ThreeBoxChainLayout(constraintSet = constraintSet)
Expand Down Expand Up @@ -146,7 +182,43 @@ fun Test13() { // Constrain a vertical chain
start.linkTo(parent.start, 12.dp)
}
constrain(chain1) {
top.linkTo(box3.bottom)
top.linkTo(box3.bottom, 10.dp)
}
}
ThreeBoxChainLayout(constraintSet = constraintSet)
}

@Preview
@Composable
fun Test17() { // Constrain a vertical chain with gone margins
val constraintSet = ConstraintSet {
val box1 = createRefFor("box1")
val box2 = createRefFor("box2")
val box3 = createRefFor("box3")
val chain1 = createVerticalChain(box1, box2, chainStyle = ChainStyle.Spread)

constrain(box1) {
width = Dimension.value(20.dp)
height = Dimension.value(20.dp)
centerHorizontallyTo(parent)
}
constrain(box2) {
width = Dimension.value(20.dp)
height = Dimension.value(20.dp)
centerHorizontallyTo(box1)
}
constrain(box3) {
width = Dimension.value(20.dp)
height = Dimension.value(200.dp)
top.linkTo(parent.top, 12.dp)
start.linkTo(parent.start, 12.dp)
visibility = Visibility.Gone
}
constrain(chain1) {
// With the gone margin it should basically layout the same as if box3 was visible
top.linkTo(box3.bottom, 10.dp, 100.dp)
// gone margin with parent should not matter
bottom.linkTo(parent.bottom, 10.dp, 200.dp)
}
}
ThreeBoxChainLayout(constraintSet = constraintSet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test3={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":809,"top":794,"right":892,"bottom":877},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":499,"top":794,"right":582,"bottom":1069},"isHelper":false,"isRoot":false,"helperReferences":[]},"1000":{"viewId":"1000","box":{"left":892,"top":0,"right":892,"bottom":0},"isHelper":true,"isRoot":false,"helperReferences":["box1","box2"]},"box2":{"viewId":"box2","box":{"left":754,"top":1091,"right":892,"bottom":1229},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test17={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":0,"top":0,"right":0,"bottom":0},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":513,"top":758,"right":568,"bottom":813},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":513,"top":1297,"right":568,"bottom":1352},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test2={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":471,"top":1251,"right":609,"bottom":1389},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":499,"top":794,"right":582,"bottom":1069},"isHelper":false,"isRoot":false,"helperReferences":[]},"1000":{"viewId":"1000","box":{"left":0,"top":1229,"right":0,"bottom":1229},"isHelper":true,"isRoot":false,"helperReferences":["box1","box2"]},"box2":{"viewId":"box2","box":{"left":471,"top":1091,"right":609,"bottom":1229},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test18={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":0,"top":0,"right":0,"bottom":0},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":497,"top":904,"right":552,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":775,"top":904,"right":830,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test5={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":0,"top":284,"right":1080,"bottom":1580},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test4={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":243,"top":1246,"right":326,"bottom":1521},"isHelper":false,"isRoot":false,"helperReferences":[]},"1000":{"viewId":"1000","box":{"left":0,"top":1835,"right":1080,"bottom":1835},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":471,"top":1543,"right":609,"bottom":1681},"isHelper":false,"isRoot":false,"helperReferences":[]},"1001":{"viewId":"1001","box":{"left":0,"top":932,"right":1080,"bottom":932},"isHelper":false,"isRoot":false,"helperReferences":[]},"1002":{"viewId":"1002","box":{"left":28,"top":0,"right":28,"bottom":1863},"isHelper":false,"isRoot":false,"helperReferences":[]},"1003":{"viewId":"1003","box":{"left":540,"top":0,"right":540,"bottom":1863},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test7={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":513,"top":890,"right":568,"bottom":973},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":527,"top":995,"right":555,"bottom":1133},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
Expand All @@ -8,8 +10,8 @@ com.example.constraintlayout.verification.dsl.DslVerificationKt#Test9={"type":"C
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test8={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":499,"top":890,"right":582,"bottom":973},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":435,"top":995,"right":647,"bottom":1047},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test10={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":0,"top":904,"right":810,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":810,"top":904,"right":1080,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test11={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":513,"top":0,"right":568,"bottom":1397},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":513,"top":1397,"right":568,"bottom":1863},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test12={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":33,"top":33,"right":583,"bottom":88},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":712,"top":904,"right":767,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":896,"top":904,"right":951,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test13={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":33,"top":33,"right":88,"bottom":583},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":513,"top":973,"right":568,"bottom":1028},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":513,"top":1418,"right":568,"bottom":1473},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test12={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":33,"top":33,"right":583,"bottom":88},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":731,"top":904,"right":786,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":905,"top":904,"right":960,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test13={"type":"CONSTRAINTS","version":1,"content":{"box3":{"viewId":"box3","box":{"left":33,"top":33,"right":88,"bottom":583},"isHelper":false,"isRoot":false,"helperReferences":[]},"box1":{"viewId":"box1","box":{"left":513,"top":992,"right":568,"bottom":1047},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":513,"top":1427,"right":568,"bottom":1482},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test14={"type":"CONSTRAINTS","version":1,"content":{"1":{"viewId":"1","box":{"left":712,"top":904,"right":767,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"2":{"viewId":"2","box":{"left":896,"top":904,"right":951,"bottom":959},"isHelper":false,"isRoot":false,"helperReferences":[]},"3":{"viewId":"3","box":{"left":33,"top":33,"right":583,"bottom":88},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test15={"type":"CONSTRAINTS","version":1,"content":{"box1":{"viewId":"box1","box":{"left":540,"top":932,"right":540,"bottom":932},"isHelper":false,"isRoot":false,"helperReferences":[]},"box2":{"viewId":"box2","box":{"left":471,"top":1207,"right":609,"bottom":1345},"isHelper":false,"isRoot":false,"helperReferences":[]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1863},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
com.example.constraintlayout.verification.dsl.DslVerificationKt#Test16={"type":"CONSTRAINTS","version":1,"content":{"1":{"viewId":"1","box":{"left":33,"top":480,"right":111,"bottom":532},"isHelper":false,"isRoot":false,"helperReferences":[]},"2":{"viewId":"2","box":{"left":33,"top":667,"right":203,"bottom":719},"isHelper":false,"isRoot":false,"helperReferences":[]},"3":{"viewId":"3","box":{"left":225,"top":429,"right":1047,"bottom":583},"isHelper":false,"isRoot":false,"helperReferences":[]},"4":{"viewId":"4","box":{"left":225,"top":616,"right":1047,"bottom":770},"isHelper":false,"isRoot":false,"helperReferences":[]},"5":{"viewId":"5","box":{"left":375,"top":66,"right":705,"bottom":396},"isHelper":false,"isRoot":false,"helperReferences":[]},"6":{"viewId":"6","box":{"left":640,"top":803,"right":823,"bottom":902},"isHelper":false,"isRoot":false,"helperReferences":[]},"7":{"viewId":"7","box":{"left":845,"top":803,"right":1047,"bottom":902},"isHelper":false,"isRoot":false,"helperReferences":[]},"1000":{"viewId":"1000","box":{"left":33,"top":0,"right":33,"bottom":1962},"isHelper":false,"isRoot":false,"helperReferences":[]},"1001":{"viewId":"1001","box":{"left":1047,"top":0,"right":1047,"bottom":1962},"isHelper":false,"isRoot":false,"helperReferences":[]},"1002":{"viewId":"1002","box":{"left":0,"top":583,"right":0,"bottom":583},"isHelper":true,"isRoot":false,"helperReferences":["1","3"]},"1003":{"viewId":"1003","box":{"left":203,"top":0,"right":203,"bottom":0},"isHelper":true,"isRoot":false,"helperReferences":["1","2"]},"1004":{"viewId":"1004","box":{"left":0,"top":770,"right":0,"bottom":770},"isHelper":true,"isRoot":false,"helperReferences":["2","4"]},"0":{"viewId":"0","box":{"left":0,"top":0,"right":1080,"bottom":1962},"isHelper":false,"isRoot":true,"helperReferences":[]}}};
Expand Down