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

Commit 3fcc360

Browse files
committed
[Compose] Fix margin for Chain first and last elements
Fixes #572 The margin for constraints that can be declared for chains would not be applied for VerticalChains. Also applies the gone margin, which was ignored for both kinds of Chains.
1 parent f0c2409 commit 3fcc360

File tree

6 files changed

+111
-27
lines changed

6 files changed

+111
-27
lines changed

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/ConstraintReference.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ public interface ConstraintReferenceFactory {
7676
protected int mMarginRight = 0;
7777
protected int mMarginStart = 0;
7878
protected int mMarginEnd = 0;
79-
int mMarginTop = 0;
80-
int mMarginBottom = 0;
81-
82-
int mMarginLeftGone = 0;
83-
int mMarginRightGone = 0;
84-
int mMarginStartGone = 0;
85-
int mMarginEndGone = 0;
86-
int mMarginTopGone = 0;
87-
int mMarginBottomGone = 0;
79+
protected int mMarginTop = 0;
80+
protected int mMarginBottom = 0;
81+
82+
protected int mMarginLeftGone = 0;
83+
protected int mMarginRightGone = 0;
84+
protected int mMarginStartGone = 0;
85+
protected int mMarginEndGone = 0;
86+
protected int mMarginTopGone = 0;
87+
protected int mMarginBottomGone = 0;
88+
8889
int mMarginBaseline = 0;
8990
int mMarginBaselineGone = 0;
9091

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/helpers/ChainReference.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.constraintlayout.core.state.helpers;
1818

19+
import androidx.constraintlayout.core.state.ConstraintReference;
1920
import androidx.constraintlayout.core.state.HelperReference;
2021
import androidx.constraintlayout.core.state.State;
2122

@@ -54,5 +55,4 @@ public ChainReference bias(float bias) {
5455
mBias = bias;
5556
return this;
5657
}
57-
5858
}

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/helpers/HorizontalChainReference.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ public void apply() {
4242
if (first == null) {
4343
first = reference;
4444
if (mStartToStart != null) {
45-
first.startToStart(mStartToStart).margin(mMarginStart);
45+
first.startToStart(mStartToStart)
46+
.margin(mMarginStart)
47+
.marginGone(mMarginStartGone);
4648
} else if (mStartToEnd != null) {
47-
first.startToEnd(mStartToEnd).margin(mMarginStart);
49+
first.startToEnd(mStartToEnd).margin(mMarginStart).marginGone(mMarginStartGone);
4850
} else if (mLeftToLeft != null) {
4951
// TODO: Hack until we support RTL properly
50-
first.startToStart(mLeftToLeft).margin(mMarginLeft);
52+
first.startToStart(mLeftToLeft).margin(mMarginLeft).marginGone(mMarginLeftGone);
5153
} else if (mLeftToRight != null) {
5254
// TODO: Hack until we support RTL properly
53-
first.startToEnd(mLeftToRight).margin(mMarginLeft);
55+
first.startToEnd(mLeftToRight).margin(mMarginLeft).marginGone(mMarginLeftGone);
5456
} else {
57+
// No constraint declared, default to Parent.
5558
first.startToStart(State.PARENT);
5659
}
5760
}
@@ -64,16 +67,17 @@ public void apply() {
6467

6568
if (previous != null) {
6669
if (mEndToStart != null) {
67-
previous.endToStart(mEndToStart).margin(mMarginEnd);
70+
previous.endToStart(mEndToStart).margin(mMarginEnd).marginGone(mMarginEndGone);
6871
} else if (mEndToEnd != null) {
69-
previous.endToEnd(mEndToEnd).margin(mMarginEnd);
72+
previous.endToEnd(mEndToEnd).margin(mMarginEnd).marginGone(mMarginEndGone);
7073
} else if (mRightToLeft != null) {
7174
// TODO: Hack until we support RTL properly
72-
previous.endToStart(mRightToLeft).margin(mMarginRight);
75+
previous.endToStart(mRightToLeft).margin(mMarginRight).marginGone(mMarginRightGone);
7376
} else if (mRightToRight != null) {
7477
// TODO: Hack until we support RTL properly
75-
previous.endToEnd(mRightToRight).margin(mMarginRight);
78+
previous.endToEnd(mRightToRight).margin(mMarginRight).marginGone(mMarginRightGone);
7679
} else {
80+
// No constraint declared, default to Parent.
7781
previous.endToEnd(State.PARENT);
7882
}
7983
}

constraintlayout/core/src/main/java/androidx/constraintlayout/core/state/helpers/VerticalChainReference.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public void apply() {
4242
if (first == null) {
4343
first = reference;
4444
if (mTopToTop != null) {
45-
first.topToTop(mTopToTop);
45+
first.topToTop(mTopToTop).margin(mMarginTop).marginGone(mMarginTopGone);
4646
} else if (mTopToBottom != null) {
47-
first.topToBottom(mTopToBottom);
47+
first.topToBottom(mTopToBottom).margin(mMarginTop).marginGone(mMarginTopGone);
4848
} else {
49+
// No constraint declared, default to Parent.
4950
first.topToTop(State.PARENT);
5051
}
5152
}
@@ -58,10 +59,15 @@ public void apply() {
5859

5960
if (previous != null) {
6061
if (mBottomToTop != null) {
61-
previous.bottomToTop(mBottomToTop);
62+
previous.bottomToTop(mBottomToTop)
63+
.margin(mMarginBottom)
64+
.marginGone(mMarginBottomGone);
6265
} else if (mBottomToBottom != null) {
63-
previous.bottomToBottom(mBottomToBottom);
66+
previous.bottomToBottom(mBottomToBottom)
67+
.margin(mMarginBottom)
68+
.marginGone(mMarginBottomGone);
6469
} else {
70+
// No constraint declared, default to Parent.
6571
previous.bottomToBottom(State.PARENT);
6672
}
6773
}
@@ -86,5 +92,4 @@ public void apply() {
8692
}
8793
}
8894
}
89-
9095
}

projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/verification/dsl/Chains.kt

+74-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import androidx.constraintlayout.compose.ChainStyle
3535
import androidx.constraintlayout.compose.ConstraintLayout
3636
import androidx.constraintlayout.compose.ConstraintSet
3737
import androidx.constraintlayout.compose.Dimension
38+
import androidx.constraintlayout.compose.Visibility
3839

3940
@Preview
4041
@Composable
@@ -114,7 +115,42 @@ fun Test12() { // Constraint an horizontal chain
114115
start.linkTo(parent.start, 12.dp)
115116
}
116117
constrain(chain1) {
117-
start.linkTo(box3.end)
118+
start.linkTo(box3.end, 10.dp)
119+
}
120+
}
121+
ThreeBoxChainLayout(constraintSet = constraintSet)
122+
}
123+
124+
@Preview
125+
@Composable
126+
fun Test18() { // Constraint an horizontal chain with gone margins
127+
val constraintSet = ConstraintSet {
128+
val box1 = createRefFor("box1")
129+
val box2 = createRefFor("box2")
130+
val box3 = createRefFor("box3")
131+
val chain1 = createHorizontalChain(box1, box2, chainStyle = ChainStyle.Spread)
132+
133+
constrain(box1) {
134+
width = Dimension.value(20.dp)
135+
height = Dimension.value(20.dp)
136+
centerVerticallyTo(parent)
137+
}
138+
constrain(box2) {
139+
width = Dimension.value(20.dp)
140+
height = Dimension.value(20.dp)
141+
centerVerticallyTo(box1)
142+
}
143+
constrain(box3) {
144+
width = Dimension.value(200.dp)
145+
height = Dimension.value(20.dp)
146+
top.linkTo(parent.top, 12.dp)
147+
start.linkTo(parent.start, 12.dp)
148+
visibility = Visibility.Gone
149+
}
150+
constrain(chain1) {
151+
start.linkTo(box3.end, 10.dp, 100.dp)
152+
// gone margin with parent should not matter
153+
end.linkTo(parent.end, 10.dp, 200.dp)
118154
}
119155
}
120156
ThreeBoxChainLayout(constraintSet = constraintSet)
@@ -146,7 +182,43 @@ fun Test13() { // Constrain a vertical chain
146182
start.linkTo(parent.start, 12.dp)
147183
}
148184
constrain(chain1) {
149-
top.linkTo(box3.bottom)
185+
top.linkTo(box3.bottom, 10.dp)
186+
}
187+
}
188+
ThreeBoxChainLayout(constraintSet = constraintSet)
189+
}
190+
191+
@Preview
192+
@Composable
193+
fun Test17() { // Constrain a vertical chain with gone margins
194+
val constraintSet = ConstraintSet {
195+
val box1 = createRefFor("box1")
196+
val box2 = createRefFor("box2")
197+
val box3 = createRefFor("box3")
198+
val chain1 = createVerticalChain(box1, box2, chainStyle = ChainStyle.Spread)
199+
200+
constrain(box1) {
201+
width = Dimension.value(20.dp)
202+
height = Dimension.value(20.dp)
203+
centerHorizontallyTo(parent)
204+
}
205+
constrain(box2) {
206+
width = Dimension.value(20.dp)
207+
height = Dimension.value(20.dp)
208+
centerHorizontallyTo(box1)
209+
}
210+
constrain(box3) {
211+
width = Dimension.value(20.dp)
212+
height = Dimension.value(200.dp)
213+
top.linkTo(parent.top, 12.dp)
214+
start.linkTo(parent.start, 12.dp)
215+
visibility = Visibility.Gone
216+
}
217+
constrain(chain1) {
218+
// With the gone margin it should basically layout the same as if box3 was visible
219+
top.linkTo(box3.bottom, 10.dp, 100.dp)
220+
// gone margin with parent should not matter
221+
bottom.linkTo(parent.bottom, 10.dp, 200.dp)
150222
}
151223
}
152224
ThreeBoxChainLayout(constraintSet = constraintSet)

0 commit comments

Comments
 (0)