From a6609e6751db242602aecec801267b5787163461 Mon Sep 17 00:00:00 2001 From: Nicolas Roard Date: Thu, 17 Dec 2020 13:01:05 -0800 Subject: [PATCH] Fix issue with percent handling --- .../core/widgets/ConstraintWidget.java | 53 +++++++++++++++++- .../widgets/ConstraintWidgetContainer.java | 11 +++- .../references/check_417_MATCH_MATCH.json | 23 ++++++++ .../references/check_417_MATCH_WRAP.json | 23 ++++++++ .../references/check_417_WRAP_MATCH.json | 23 ++++++++ .../references/check_417_WRAP_WRAP.json | 23 ++++++++ .../references/check_418_MATCH_MATCH.json | 35 ++++++++++++ .../references/check_418_MATCH_WRAP.json | 35 ++++++++++++ .../references/check_418_WRAP_MATCH.json | 35 ++++++++++++ .../references/check_418_WRAP_WRAP.json | 35 ++++++++++++ .../references/check_419_MATCH_MATCH.json | 45 +++++++++++++++ .../references/check_419_MATCH_WRAP.json | 45 +++++++++++++++ .../references/check_419_WRAP_MATCH.json | 45 +++++++++++++++ .../references/check_419_WRAP_WRAP.json | 45 +++++++++++++++ .../app/src/main/res/layout/check_416.xml | 2 + .../app/src/main/res/layout/check_417.xml | 28 ++++++++++ .../app/src/main/res/layout/check_418.xml | 55 ++++++++++++++++++ .../app/src/main/res/layout/check_419.xml | 56 +++++++++++++++++++ 18 files changed, 613 insertions(+), 4 deletions(-) create mode 100644 desktop/ValidationTool/references/check_417_MATCH_MATCH.json create mode 100644 desktop/ValidationTool/references/check_417_MATCH_WRAP.json create mode 100644 desktop/ValidationTool/references/check_417_WRAP_MATCH.json create mode 100644 desktop/ValidationTool/references/check_417_WRAP_WRAP.json create mode 100644 desktop/ValidationTool/references/check_418_MATCH_MATCH.json create mode 100644 desktop/ValidationTool/references/check_418_MATCH_WRAP.json create mode 100644 desktop/ValidationTool/references/check_418_WRAP_MATCH.json create mode 100644 desktop/ValidationTool/references/check_418_WRAP_WRAP.json create mode 100644 desktop/ValidationTool/references/check_419_MATCH_MATCH.json create mode 100644 desktop/ValidationTool/references/check_419_MATCH_WRAP.json create mode 100644 desktop/ValidationTool/references/check_419_WRAP_MATCH.json create mode 100644 desktop/ValidationTool/references/check_419_WRAP_WRAP.json create mode 100644 projects/ConstraintLayoutValidation/app/src/main/res/layout/check_417.xml create mode 100644 projects/ConstraintLayoutValidation/app/src/main/res/layout/check_418.xml create mode 100644 projects/ConstraintLayoutValidation/app/src/main/res/layout/check_419.xml diff --git a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidget.java b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidget.java index 3613c0f0c..063e52616 100644 --- a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidget.java +++ b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidget.java @@ -72,6 +72,9 @@ public class ConstraintWidget { private boolean OPTIMIZE_WRAP = false; private boolean OPTIMIZE_WRAP_ON_RESOLVED = true; + private int mWidthOverride = -1; + private int mHeightOverride = -1; + public WidgetRun getRun(int orientation) { if (orientation == HORIZONTAL) { return horizontalRun; @@ -513,6 +516,8 @@ public void reset() { mMeasureRequested = true; mResolvedMatchConstraintDefault[HORIZONTAL] = 0; mResolvedMatchConstraintDefault[VERTICAL] = 0; + mWidthOverride = -1; + mHeightOverride = -1; } public int horizontalGroup = -1; @@ -533,6 +538,10 @@ public boolean oppositeDimensionsTied() { && mListDimensionBehaviors[VERTICAL] == MATCH_CONSTRAINT); } + public boolean hasDimensionOverride() { + return mWidthOverride != -1 || mHeightOverride != -1; + } + /*-----------------------------------------------------------------------*/ // Creation /*-----------------------------------------------------------------------*/ @@ -1435,6 +1444,18 @@ public void setFrame(int left, int top, int right, int bottom) { if (mWidth < mMinWidth) { mWidth = mMinWidth; } + if (mMatchConstraintMaxWidth > 0 && mListDimensionBehaviors[HORIZONTAL] == MATCH_CONSTRAINT) { + mWidth = Math.min(mWidth, mMatchConstraintMaxWidth); + } + if (mMatchConstraintMaxHeight > 0 && mListDimensionBehaviors[VERTICAL] == MATCH_CONSTRAINT) { + mHeight = Math.min(mHeight, mMatchConstraintMaxHeight); + } + if (w != mWidth) { + mWidthOverride = mWidth; + } + if (h != mHeight) { + mHeightOverride = mHeight; + } if (LinearSystem.FULL_DEBUG) { System.out.println("update from solver " + mDebugName + " " + mX + ":" + mY + " - " + mWidth + " x " + mHeight); @@ -2694,6 +2715,24 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal, break; } + + if (mWidthOverride != -1 && isHorizontal) { + if (FULL_DEBUG) { + System.out.println("OVERRIDE WIDTH to " + mWidthOverride); + } + variableSize = false; + dimension = mWidthOverride; + mWidthOverride = -1; + } + if (mHeightOverride != -1 && !isHorizontal) { + if (FULL_DEBUG) { + System.out.println("OVERRIDE HEIGHT to " + mHeightOverride); + } + variableSize = false; + dimension = mHeightOverride; + mHeightOverride = -1; + } + if (mVisibility == ConstraintWidget.GONE) { dimension = 0; variableSize = false; @@ -2783,7 +2822,9 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal, percentEnd = system.createObjectVariable(mParent.getAnchor(ConstraintAnchor.Type.RIGHT)); } system.addConstraint(system.createRow().createRowDimensionRatio(end, begin, percentEnd, percentBegin, matchPercentDimension)); - variableSize = false; + if (parentWrapContent) { + variableSize = false; + } } else { isTerminal = true; } @@ -2884,6 +2925,15 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal, if (beginWidget instanceof Barrier || endWidget instanceof Barrier) { boundsCheckStrength = SolverVariable.STRENGTH_HIGHEST; } + } else if (matchConstraintDefault == MATCH_CONSTRAINT_PERCENT) { + applyCentering = true; + rangeCheckStrength = SolverVariable.STRENGTH_EQUALITY; + boundsCheckStrength = SolverVariable.STRENGTH_EQUALITY; + applyBoundsCheck = true; + applyRangeCheck = true; + if (beginWidget instanceof Barrier || endWidget instanceof Barrier) { + boundsCheckStrength = SolverVariable.STRENGTH_HIGHEST; + } } else if (matchConstraintDefault == MATCH_CONSTRAINT_WRAP) { applyCentering = true; applyRangeCheck = true; @@ -2975,6 +3025,7 @@ private void applyConstraints(LinearSystem system, boolean isHorizontal, applyBoundsCheck = false; parentWrapContent = false; } + system.addCentering(begin, beginTarget, beginAnchor.getMargin(), bias, endTarget, end, endAnchor.getMargin(), centeringStrength); } diff --git a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidgetContainer.java b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidgetContainer.java index 04be948e3..687ad0c10 100644 --- a/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidgetContainer.java +++ b/constraintlayout/core/src/main/java/androidx/constraintlayout/core/widgets/ConstraintWidgetContainer.java @@ -419,15 +419,20 @@ public boolean addChildrenToSolver(LinearSystem system) { * * @param system the solver we get the values from. */ - public void updateChildrenFromSolver(LinearSystem system, boolean flags[]) { + public boolean updateChildrenFromSolver(LinearSystem system, boolean flags[]) { flags[Optimizer.FLAG_RECOMPUTE_BOUNDS] = false; boolean optimize = optimizeFor(Optimizer.OPTIMIZATION_GRAPH); updateFromSolver(system, optimize); final int count = mChildren.size(); + boolean hasOverride = false; for (int i = 0; i < count; i++) { ConstraintWidget widget = mChildren.get(i); widget.updateFromSolver(system, optimize); + if (widget.hasDimensionOverride()) { + hasOverride = true; + } } + return hasOverride; } @Override @@ -802,15 +807,15 @@ public void layout() { System.out.println("EXCEPTION : " + e); } if (needsSolving) { - updateChildrenFromSolver(mSystem, Optimizer.flags); + needsSolving = updateChildrenFromSolver(mSystem, Optimizer.flags); } else { updateFromSolver(mSystem, optimize); for (int i = 0; i < count; i++) { ConstraintWidget widget = mChildren.get(i); widget.updateFromSolver(mSystem, optimize); } + needsSolving = false; } - needsSolving = false; if (hasWrapContent && countSolve < MAX_ITERATIONS && Optimizer.flags[Optimizer.FLAG_RECOMPUTE_BOUNDS]) { diff --git a/desktop/ValidationTool/references/check_417_MATCH_MATCH.json b/desktop/ValidationTool/references/check_417_MATCH_MATCH.json new file mode 100644 index 000000000..e74020c2c --- /dev/null +++ b/desktop/ValidationTool/references/check_417_MATCH_MATCH.json @@ -0,0 +1,23 @@ +{ + "duration": "128000", + "layout": { + "children": [{ + "bounds": { + "top": "648", + "left": "315", + "bottom": "889", + "right": "765" + }, + "id": "textview1", + "class": "MaterialTextView" + }], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_417_MATCH_WRAP.json b/desktop/ValidationTool/references/check_417_MATCH_WRAP.json new file mode 100644 index 000000000..9e61e8962 --- /dev/null +++ b/desktop/ValidationTool/references/check_417_MATCH_WRAP.json @@ -0,0 +1,23 @@ +{ + "duration": "178000", + "layout": { + "children": [{ + "bounds": { + "top": "0", + "left": "315", + "bottom": "241", + "right": "765" + }, + "id": "textview1", + "class": "MaterialTextView" + }], + "bounds": { + "top": "0", + "left": "0", + "bottom": "241", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_417_WRAP_MATCH.json b/desktop/ValidationTool/references/check_417_WRAP_MATCH.json new file mode 100644 index 000000000..fb2fce1a6 --- /dev/null +++ b/desktop/ValidationTool/references/check_417_WRAP_MATCH.json @@ -0,0 +1,23 @@ +{ + "duration": "176000", + "layout": { + "children": [{ + "bounds": { + "top": "88", + "left": "0", + "bottom": "1449", + "right": "0" + }, + "id": "textview1", + "class": "MaterialTextView" + }], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "0" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_417_WRAP_WRAP.json b/desktop/ValidationTool/references/check_417_WRAP_WRAP.json new file mode 100644 index 000000000..9c2862884 --- /dev/null +++ b/desktop/ValidationTool/references/check_417_WRAP_WRAP.json @@ -0,0 +1,23 @@ +{ + "duration": "221000", + "layout": { + "children": [{ + "bounds": { + "top": "0", + "left": "0", + "bottom": "1361", + "right": "0" + }, + "id": "textview1", + "class": "MaterialTextView" + }], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1361", + "right": "0" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_418_MATCH_MATCH.json b/desktop/ValidationTool/references/check_418_MATCH_MATCH.json new file mode 100644 index 000000000..271ba1e3a --- /dev/null +++ b/desktop/ValidationTool/references/check_418_MATCH_MATCH.json @@ -0,0 +1,35 @@ +{ + "duration": "255000", + "layout": { + "children": [ + { + "bounds": { + "top": "591", + "left": "500", + "bottom": "648", + "right": "581" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "648", + "left": "315", + "bottom": "889", + "right": "765" + }, + "id": "text", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_418_MATCH_WRAP.json b/desktop/ValidationTool/references/check_418_MATCH_WRAP.json new file mode 100644 index 000000000..3b3d3b257 --- /dev/null +++ b/desktop/ValidationTool/references/check_418_MATCH_WRAP.json @@ -0,0 +1,35 @@ +{ + "duration": "760500", + "layout": { + "children": [ + { + "bounds": { + "top": "0", + "left": "500", + "bottom": "57", + "right": "581" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "57", + "left": "315", + "bottom": "298", + "right": "765" + }, + "id": "text", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "355", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_418_WRAP_MATCH.json b/desktop/ValidationTool/references/check_418_WRAP_MATCH.json new file mode 100644 index 000000000..f7e0f4361 --- /dev/null +++ b/desktop/ValidationTool/references/check_418_WRAP_MATCH.json @@ -0,0 +1,35 @@ +{ + "duration": "603000", + "layout": { + "children": [ + { + "bounds": { + "top": "199", + "left": "41", + "bottom": "256", + "right": "122" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "256", + "left": "41", + "bottom": "1281", + "right": "122" + }, + "id": "text", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "162" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_418_WRAP_WRAP.json b/desktop/ValidationTool/references/check_418_WRAP_WRAP.json new file mode 100644 index 000000000..54641df01 --- /dev/null +++ b/desktop/ValidationTool/references/check_418_WRAP_WRAP.json @@ -0,0 +1,35 @@ +{ + "duration": "577500", + "layout": { + "children": [ + { + "bounds": { + "top": "0", + "left": "41", + "bottom": "57", + "right": "122" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "57", + "left": "41", + "bottom": "1082", + "right": "122" + }, + "id": "text", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1139", + "right": "162" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_419_MATCH_MATCH.json b/desktop/ValidationTool/references/check_419_MATCH_MATCH.json new file mode 100644 index 000000000..1c42261e2 --- /dev/null +++ b/desktop/ValidationTool/references/check_419_MATCH_MATCH.json @@ -0,0 +1,45 @@ +{ + "duration": "490000", + "layout": { + "children": [ + { + "bounds": { + "top": "648", + "left": "315", + "bottom": "889", + "right": "765" + }, + "id": "text", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "591", + "left": "500", + "bottom": "648", + "right": "581" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "855", + "left": "315", + "bottom": "1096", + "right": "765" + }, + "id": "text2", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_419_MATCH_WRAP.json b/desktop/ValidationTool/references/check_419_MATCH_WRAP.json new file mode 100644 index 000000000..2e3306455 --- /dev/null +++ b/desktop/ValidationTool/references/check_419_MATCH_WRAP.json @@ -0,0 +1,45 @@ +{ + "duration": "869000", + "layout": { + "children": [ + { + "bounds": { + "top": "57", + "left": "315", + "bottom": "298", + "right": "765" + }, + "id": "text", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "0", + "left": "500", + "bottom": "57", + "right": "581" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "75", + "left": "315", + "bottom": "316", + "right": "765" + }, + "id": "text2", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "355", + "right": "1080" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_419_WRAP_MATCH.json b/desktop/ValidationTool/references/check_419_WRAP_MATCH.json new file mode 100644 index 000000000..69822ebde --- /dev/null +++ b/desktop/ValidationTool/references/check_419_WRAP_MATCH.json @@ -0,0 +1,45 @@ +{ + "duration": "514000", + "layout": { + "children": [ + { + "bounds": { + "top": "256", + "left": "41", + "bottom": "1281", + "right": "122" + }, + "id": "text", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "199", + "left": "41", + "bottom": "256", + "right": "122" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "337", + "left": "41", + "bottom": "1362", + "right": "122" + }, + "id": "text2", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1536", + "right": "162" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/desktop/ValidationTool/references/check_419_WRAP_WRAP.json b/desktop/ValidationTool/references/check_419_WRAP_WRAP.json new file mode 100644 index 000000000..0e1ac199c --- /dev/null +++ b/desktop/ValidationTool/references/check_419_WRAP_WRAP.json @@ -0,0 +1,45 @@ +{ + "duration": "907000", + "layout": { + "children": [ + { + "bounds": { + "top": "57", + "left": "41", + "bottom": "1082", + "right": "122" + }, + "id": "text", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "0", + "left": "41", + "bottom": "57", + "right": "122" + }, + "id": "title", + "class": "MaterialTextView" + }, + { + "bounds": { + "top": "75", + "left": "41", + "bottom": "1100", + "right": "122" + }, + "id": "text2", + "class": "MaterialTextView" + } + ], + "bounds": { + "top": "0", + "left": "0", + "bottom": "1139", + "right": "162" + }, + "id": "root", + "class": "ConstraintLayout" + } +} \ No newline at end of file diff --git a/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_416.xml b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_416.xml index 625e3fbd6..3ebd97036 100644 --- a/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_416.xml +++ b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_416.xml @@ -7,6 +7,8 @@ android:layout_height="match_parent" tools:ignore="MissingConstraints"> + + + + + + + + + diff --git a/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_418.xml b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_418.xml new file mode 100644 index 000000000..f067311ab --- /dev/null +++ b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_418.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_419.xml b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_419.xml new file mode 100644 index 000000000..687578ee6 --- /dev/null +++ b/projects/ConstraintLayoutValidation/app/src/main/res/layout/check_419.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + +