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

Commit 8629723

Browse files
authored
Fix issue with percent handling (#60)
1 parent 5644ade commit 8629723

18 files changed

+613
-4
lines changed

constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidget.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public class ConstraintWidget {
7272
private boolean OPTIMIZE_WRAP = false;
7373
private boolean OPTIMIZE_WRAP_ON_RESOLVED = true;
7474

75+
private int mWidthOverride = -1;
76+
private int mHeightOverride = -1;
77+
7578
public WidgetRun getRun(int orientation) {
7679
if (orientation == HORIZONTAL) {
7780
return horizontalRun;
@@ -513,6 +516,8 @@ public void reset() {
513516
mMeasureRequested = true;
514517
mResolvedMatchConstraintDefault[HORIZONTAL] = 0;
515518
mResolvedMatchConstraintDefault[VERTICAL] = 0;
519+
mWidthOverride = -1;
520+
mHeightOverride = -1;
516521
}
517522

518523
public int horizontalGroup = -1;
@@ -533,6 +538,10 @@ public boolean oppositeDimensionsTied() {
533538
&& mListDimensionBehaviors[VERTICAL] == MATCH_CONSTRAINT);
534539
}
535540

541+
public boolean hasDimensionOverride() {
542+
return mWidthOverride != -1 || mHeightOverride != -1;
543+
}
544+
536545
/*-----------------------------------------------------------------------*/
537546
// Creation
538547
/*-----------------------------------------------------------------------*/
@@ -1435,6 +1444,18 @@ public void setFrame(int left, int top, int right, int bottom) {
14351444
if (mWidth < mMinWidth) {
14361445
mWidth = mMinWidth;
14371446
}
1447+
if (mMatchConstraintMaxWidth > 0 && mListDimensionBehaviors[HORIZONTAL] == MATCH_CONSTRAINT) {
1448+
mWidth = Math.min(mWidth, mMatchConstraintMaxWidth);
1449+
}
1450+
if (mMatchConstraintMaxHeight > 0 && mListDimensionBehaviors[VERTICAL] == MATCH_CONSTRAINT) {
1451+
mHeight = Math.min(mHeight, mMatchConstraintMaxHeight);
1452+
}
1453+
if (w != mWidth) {
1454+
mWidthOverride = mWidth;
1455+
}
1456+
if (h != mHeight) {
1457+
mHeightOverride = mHeight;
1458+
}
14381459

14391460
if (LinearSystem.FULL_DEBUG) {
14401461
System.out.println("update from solver " + mDebugName + " " + mX + ":" + mY + " - " + mWidth + " x " + mHeight);
@@ -2694,6 +2715,24 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal,
26942715
break;
26952716
}
26962717

2718+
2719+
if (mWidthOverride != -1 && isHorizontal) {
2720+
if (FULL_DEBUG) {
2721+
System.out.println("OVERRIDE WIDTH to " + mWidthOverride);
2722+
}
2723+
variableSize = false;
2724+
dimension = mWidthOverride;
2725+
mWidthOverride = -1;
2726+
}
2727+
if (mHeightOverride != -1 && !isHorizontal) {
2728+
if (FULL_DEBUG) {
2729+
System.out.println("OVERRIDE HEIGHT to " + mHeightOverride);
2730+
}
2731+
variableSize = false;
2732+
dimension = mHeightOverride;
2733+
mHeightOverride = -1;
2734+
}
2735+
26972736
if (mVisibility == ConstraintWidget.GONE) {
26982737
dimension = 0;
26992738
variableSize = false;
@@ -2783,7 +2822,9 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal,
27832822
percentEnd = system.createObjectVariable(mParent.getAnchor(ConstraintAnchor.Type.RIGHT));
27842823
}
27852824
system.addConstraint(system.createRow().createRowDimensionRatio(end, begin, percentEnd, percentBegin, matchPercentDimension));
2786-
variableSize = false;
2825+
if (parentWrapContent) {
2826+
variableSize = false;
2827+
}
27872828
} else {
27882829
isTerminal = true;
27892830
}
@@ -2884,6 +2925,15 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal,
28842925
if (beginWidget instanceof Barrier || endWidget instanceof Barrier) {
28852926
boundsCheckStrength = SolverVariable.STRENGTH_HIGHEST;
28862927
}
2928+
} else if (matchConstraintDefault == MATCH_CONSTRAINT_PERCENT) {
2929+
applyCentering = true;
2930+
rangeCheckStrength = SolverVariable.STRENGTH_EQUALITY;
2931+
boundsCheckStrength = SolverVariable.STRENGTH_EQUALITY;
2932+
applyBoundsCheck = true;
2933+
applyRangeCheck = true;
2934+
if (beginWidget instanceof Barrier || endWidget instanceof Barrier) {
2935+
boundsCheckStrength = SolverVariable.STRENGTH_HIGHEST;
2936+
}
28872937
} else if (matchConstraintDefault == MATCH_CONSTRAINT_WRAP) {
28882938
applyCentering = true;
28892939
applyRangeCheck = true;
@@ -2975,6 +3025,7 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal,
29753025
applyBoundsCheck = false;
29763026
parentWrapContent = false;
29773027
}
3028+
29783029
system.addCentering(begin, beginTarget, beginAnchor.getMargin(),
29793030
bias, endTarget, end, endAnchor.getMargin(), centeringStrength);
29803031
}

constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidgetContainer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,20 @@ public boolean addChildrenToSolver(LinearSystem system) {
419419
*
420420
* @param system the solver we get the values from.
421421
*/
422-
public void updateChildrenFromSolver(LinearSystem system, boolean flags[]) {
422+
public boolean updateChildrenFromSolver(LinearSystem system, boolean flags[]) {
423423
flags[Optimizer.FLAG_RECOMPUTE_BOUNDS] = false;
424424
boolean optimize = optimizeFor(Optimizer.OPTIMIZATION_GRAPH);
425425
updateFromSolver(system, optimize);
426426
final int count = mChildren.size();
427+
boolean hasOverride = false;
427428
for (int i = 0; i < count; i++) {
428429
ConstraintWidget widget = mChildren.get(i);
429430
widget.updateFromSolver(system, optimize);
431+
if (widget.hasDimensionOverride()) {
432+
hasOverride = true;
433+
}
430434
}
435+
return hasOverride;
431436
}
432437

433438
@Override
@@ -802,15 +807,15 @@ public void layout() {
802807
System.out.println("EXCEPTION : " + e);
803808
}
804809
if (needsSolving) {
805-
updateChildrenFromSolver(mSystem, Optimizer.flags);
810+
needsSolving = updateChildrenFromSolver(mSystem, Optimizer.flags);
806811
} else {
807812
updateFromSolver(mSystem, optimize);
808813
for (int i = 0; i < count; i++) {
809814
ConstraintWidget widget = mChildren.get(i);
810815
widget.updateFromSolver(mSystem, optimize);
811816
}
817+
needsSolving = false;
812818
}
813-
needsSolving = false;
814819

815820
if (hasWrapContent && countSolve < MAX_ITERATIONS
816821
&& Optimizer.flags[Optimizer.FLAG_RECOMPUTE_BOUNDS]) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"duration": "128000",
3+
"layout": {
4+
"children": [{
5+
"bounds": {
6+
"top": "648",
7+
"left": "315",
8+
"bottom": "889",
9+
"right": "765"
10+
},
11+
"id": "textview1",
12+
"class": "MaterialTextView"
13+
}],
14+
"bounds": {
15+
"top": "0",
16+
"left": "0",
17+
"bottom": "1536",
18+
"right": "1080"
19+
},
20+
"id": "root",
21+
"class": "ConstraintLayout"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"duration": "178000",
3+
"layout": {
4+
"children": [{
5+
"bounds": {
6+
"top": "0",
7+
"left": "315",
8+
"bottom": "241",
9+
"right": "765"
10+
},
11+
"id": "textview1",
12+
"class": "MaterialTextView"
13+
}],
14+
"bounds": {
15+
"top": "0",
16+
"left": "0",
17+
"bottom": "241",
18+
"right": "1080"
19+
},
20+
"id": "root",
21+
"class": "ConstraintLayout"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"duration": "176000",
3+
"layout": {
4+
"children": [{
5+
"bounds": {
6+
"top": "88",
7+
"left": "0",
8+
"bottom": "1449",
9+
"right": "0"
10+
},
11+
"id": "textview1",
12+
"class": "MaterialTextView"
13+
}],
14+
"bounds": {
15+
"top": "0",
16+
"left": "0",
17+
"bottom": "1536",
18+
"right": "0"
19+
},
20+
"id": "root",
21+
"class": "ConstraintLayout"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"duration": "221000",
3+
"layout": {
4+
"children": [{
5+
"bounds": {
6+
"top": "0",
7+
"left": "0",
8+
"bottom": "1361",
9+
"right": "0"
10+
},
11+
"id": "textview1",
12+
"class": "MaterialTextView"
13+
}],
14+
"bounds": {
15+
"top": "0",
16+
"left": "0",
17+
"bottom": "1361",
18+
"right": "0"
19+
},
20+
"id": "root",
21+
"class": "ConstraintLayout"
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"duration": "255000",
3+
"layout": {
4+
"children": [
5+
{
6+
"bounds": {
7+
"top": "591",
8+
"left": "500",
9+
"bottom": "648",
10+
"right": "581"
11+
},
12+
"id": "title",
13+
"class": "MaterialTextView"
14+
},
15+
{
16+
"bounds": {
17+
"top": "648",
18+
"left": "315",
19+
"bottom": "889",
20+
"right": "765"
21+
},
22+
"id": "text",
23+
"class": "MaterialTextView"
24+
}
25+
],
26+
"bounds": {
27+
"top": "0",
28+
"left": "0",
29+
"bottom": "1536",
30+
"right": "1080"
31+
},
32+
"id": "root",
33+
"class": "ConstraintLayout"
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"duration": "760500",
3+
"layout": {
4+
"children": [
5+
{
6+
"bounds": {
7+
"top": "0",
8+
"left": "500",
9+
"bottom": "57",
10+
"right": "581"
11+
},
12+
"id": "title",
13+
"class": "MaterialTextView"
14+
},
15+
{
16+
"bounds": {
17+
"top": "57",
18+
"left": "315",
19+
"bottom": "298",
20+
"right": "765"
21+
},
22+
"id": "text",
23+
"class": "MaterialTextView"
24+
}
25+
],
26+
"bounds": {
27+
"top": "0",
28+
"left": "0",
29+
"bottom": "355",
30+
"right": "1080"
31+
},
32+
"id": "root",
33+
"class": "ConstraintLayout"
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"duration": "603000",
3+
"layout": {
4+
"children": [
5+
{
6+
"bounds": {
7+
"top": "199",
8+
"left": "41",
9+
"bottom": "256",
10+
"right": "122"
11+
},
12+
"id": "title",
13+
"class": "MaterialTextView"
14+
},
15+
{
16+
"bounds": {
17+
"top": "256",
18+
"left": "41",
19+
"bottom": "1281",
20+
"right": "122"
21+
},
22+
"id": "text",
23+
"class": "MaterialTextView"
24+
}
25+
],
26+
"bounds": {
27+
"top": "0",
28+
"left": "0",
29+
"bottom": "1536",
30+
"right": "162"
31+
},
32+
"id": "root",
33+
"class": "ConstraintLayout"
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"duration": "577500",
3+
"layout": {
4+
"children": [
5+
{
6+
"bounds": {
7+
"top": "0",
8+
"left": "41",
9+
"bottom": "57",
10+
"right": "122"
11+
},
12+
"id": "title",
13+
"class": "MaterialTextView"
14+
},
15+
{
16+
"bounds": {
17+
"top": "57",
18+
"left": "41",
19+
"bottom": "1082",
20+
"right": "122"
21+
},
22+
"id": "text",
23+
"class": "MaterialTextView"
24+
}
25+
],
26+
"bounds": {
27+
"top": "0",
28+
"left": "0",
29+
"bottom": "1139",
30+
"right": "162"
31+
},
32+
"id": "root",
33+
"class": "ConstraintLayout"
34+
}
35+
}

0 commit comments

Comments
 (0)