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

Commit 6fdd704

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 2932b0d commit 6fdd704

File tree

6 files changed

+116
-31
lines changed

6 files changed

+116
-31
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ public interface ConstraintReferenceFactory {
7171
protected int mMarginRight = 0;
7272
protected int mMarginStart = 0;
7373
protected int mMarginEnd = 0;
74-
int mMarginTop = 0;
75-
int mMarginBottom = 0;
76-
77-
int mMarginLeftGone = 0;
78-
int mMarginRightGone = 0;
79-
int mMarginStartGone = 0;
80-
int mMarginEndGone = 0;
81-
int mMarginTopGone = 0;
82-
int mMarginBottomGone = 0;
74+
protected int mMarginTop = 0;
75+
protected int mMarginBottom = 0;
76+
77+
protected int mMarginLeftGone = 0;
78+
protected int mMarginRightGone = 0;
79+
protected int mMarginStartGone = 0;
80+
protected int mMarginEndGone = 0;
81+
protected int mMarginTopGone = 0;
82+
protected int mMarginBottomGone = 0;
83+
8384
int mMarginBaseline = 0;
8485
int mMarginBaselineGone = 0;
8586

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ public ChainReference style(State.Chain style) {
3838
public ChainReference bias(float bias) { mBias = bias;
3939
return this;
4040
}
41-
4241
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

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

19-
import androidx.constraintlayout.core.widgets.ConstraintWidget;
2019
import androidx.constraintlayout.core.state.ConstraintReference;
2120
import androidx.constraintlayout.core.state.State;
21+
import androidx.constraintlayout.core.widgets.ConstraintWidget;
2222

2323
public class HorizontalChainReference extends ChainReference {
2424

@@ -39,16 +39,19 @@ public void apply() {
3939
if (first == null) {
4040
first = reference;
4141
if (mStartToStart != null) {
42-
first.startToStart(mStartToStart).margin(mMarginStart);
42+
first.startToStart(mStartToStart)
43+
.margin(mMarginStart)
44+
.marginGone(mMarginStartGone);
4345
} else if (mStartToEnd != null) {
44-
first.startToEnd(mStartToEnd).margin(mMarginStart);
46+
first.startToEnd(mStartToEnd).margin(mMarginStart).marginGone(mMarginStartGone);
4547
} else if (mLeftToLeft != null) {
4648
// TODO: Hack until we support RTL properly
47-
first.startToStart(mLeftToLeft).margin(mMarginLeft);
49+
first.startToStart(mLeftToLeft).margin(mMarginLeft).marginGone(mMarginLeftGone);
4850
} else if (mLeftToRight != null) {
4951
// TODO: Hack until we support RTL properly
50-
first.startToEnd(mLeftToRight).margin(mMarginLeft);
52+
first.startToEnd(mLeftToRight).margin(mMarginLeft).marginGone(mMarginLeftGone);
5153
} else {
54+
// No constraint declared, default to Parent.
5255
first.startToStart(State.PARENT);
5356
}
5457
}
@@ -61,16 +64,17 @@ public void apply() {
6164

6265
if (previous != null) {
6366
if (mEndToStart != null) {
64-
previous.endToStart(mEndToStart).margin(mMarginEnd);
67+
previous.endToStart(mEndToStart).margin(mMarginEnd).marginGone(mMarginEndGone);
6568
} else if (mEndToEnd != null) {
66-
previous.endToEnd(mEndToEnd).margin(mMarginEnd);
69+
previous.endToEnd(mEndToEnd).margin(mMarginEnd).marginGone(mMarginEndGone);
6770
} else if (mRightToLeft != null) {
6871
// TODO: Hack until we support RTL properly
69-
previous.endToStart(mRightToLeft).margin(mMarginRight);
70-
}else if (mRightToRight != null) {
72+
previous.endToStart(mRightToLeft).margin(mMarginRight).marginGone(mMarginRightGone);
73+
} else if (mRightToRight != null) {
7174
// TODO: Hack until we support RTL properly
72-
previous.endToEnd(mRightToRight).margin(mMarginRight);
75+
previous.endToEnd(mRightToRight).margin(mMarginRight).marginGone(mMarginRightGone);
7376
} else {
77+
// No constraint declared, default to Parent.
7478
previous.endToEnd(State.PARENT);
7579
}
7680
}
@@ -86,10 +90,12 @@ public void apply() {
8690
switch (mStyle) {
8791
case SPREAD: {
8892
first.setHorizontalChainStyle(ConstraintWidget.CHAIN_SPREAD);
89-
} break;
93+
}
94+
break;
9095
case SPREAD_INSIDE: {
9196
first.setHorizontalChainStyle(ConstraintWidget.CHAIN_SPREAD_INSIDE);
92-
} break;
97+
}
98+
break;
9399
case PACKED: {
94100
first.setHorizontalChainStyle(ConstraintWidget.CHAIN_PACKED);
95101
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ public void apply() {
3939
if (first == null) {
4040
first = reference;
4141
if (mTopToTop != null) {
42-
first.topToTop(mTopToTop);
42+
first.topToTop(mTopToTop).margin(mMarginTop).marginGone(mMarginTopGone);
4343
} else if (mTopToBottom != null) {
44-
first.topToBottom(mTopToBottom);
44+
first.topToBottom(mTopToBottom).margin(mMarginTop).marginGone(mMarginTopGone);
4545
} else {
46+
// No constraint declared, default to Parent.
4647
first.topToTop(State.PARENT);
4748
}
4849
}
@@ -55,10 +56,15 @@ public void apply() {
5556

5657
if (previous != null) {
5758
if (mBottomToTop != null) {
58-
previous.bottomToTop(mBottomToTop);
59+
previous.bottomToTop(mBottomToTop)
60+
.margin(mMarginBottom)
61+
.marginGone(mMarginBottomGone);
5962
} else if (mBottomToBottom != null) {
60-
previous.bottomToBottom(mBottomToBottom);
63+
previous.bottomToBottom(mBottomToBottom)
64+
.margin(mMarginBottom)
65+
.marginGone(mMarginBottomGone);
6166
} else {
67+
// No constraint declared, default to Parent.
6268
previous.bottomToBottom(State.PARENT);
6369
}
6470
}
@@ -83,5 +89,4 @@ public void apply() {
8389
}
8490
}
8591
}
86-
8792
}

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

Lines changed: 74 additions & 2 deletions
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)