From 1a049c14e52176e35707d83a3f0cdb5e1900ce84 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 8 Dec 2015 20:52:44 -0800 Subject: [PATCH] Catch un-normalized BoxConstraints Add BoxConstraints.isNormalized feature. Use this feature in asserts in all the intrinsic dimension methods, in various relevant BoxConstraints methods, and in layout(). Wrap the _DebugSize logic in BoxConstraints.constrain() in an assert block to avoid a branch in release mode. Remove the logic in BoxConstraints.isSatisfiedBy() that dealt with non-normalized values. Add BoxConstraints.normalize(). Make RenderCustomOneChildLayoutBox.performLayout() only set parentUsesSize on the child if the constraints aren't tight. --- examples/rendering/lib/sector_layout.dart | 2 + packages/flutter/lib/src/rendering/block.dart | 8 ++++ packages/flutter/lib/src/rendering/box.dart | 43 ++++++++++++++++--- .../lib/src/rendering/custom_layout.dart | 2 + .../lib/src/rendering/editable_paragraph.dart | 1 + packages/flutter/lib/src/rendering/flex.dart | 1 + packages/flutter/lib/src/rendering/grid.dart | 4 ++ packages/flutter/lib/src/rendering/image.dart | 4 ++ .../flutter/lib/src/rendering/object.dart | 4 ++ .../flutter/lib/src/rendering/overflow.dart | 8 ++++ .../flutter/lib/src/rendering/paragraph.dart | 3 ++ .../flutter/lib/src/rendering/proxy_box.dart | 23 ++++++++++ .../lib/src/rendering/shifted_box.dart | 23 +++++++--- packages/flutter/lib/src/rendering/stack.dart | 4 ++ .../lib/src/rendering/statistics_box.dart | 4 ++ .../flutter/lib/src/rendering/viewport.dart | 4 ++ packages/unit/test/rendering/flex_test.dart | 2 +- 17 files changed, 127 insertions(+), 13 deletions(-) diff --git a/examples/rendering/lib/sector_layout.dart b/examples/rendering/lib/sector_layout.dart index c0b3bf204a..a51b8a16e3 100644 --- a/examples/rendering/lib/sector_layout.dart +++ b/examples/rendering/lib/sector_layout.dart @@ -37,6 +37,8 @@ class SectorConstraints extends Constraints { } bool get isTight => minDeltaTheta >= maxDeltaTheta && minDeltaTheta >= maxDeltaTheta; + + bool get isNormalized => minDeltaRadius <= maxDeltaRadius && minDeltaTheta <= maxDeltaTheta; } class SectorDimensions { diff --git a/packages/flutter/lib/src/rendering/block.dart b/packages/flutter/lib/src/rendering/block.dart index 9c95effd54..d8975aa395 100644 --- a/packages/flutter/lib/src/rendering/block.dart +++ b/packages/flutter/lib/src/rendering/block.dart @@ -167,6 +167,7 @@ class RenderBlock extends RenderBlockBase { } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) { return _getIntrinsicCrossAxis( constraints, @@ -177,6 +178,7 @@ class RenderBlock extends RenderBlockBase { } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) { return _getIntrinsicCrossAxis( constraints, @@ -187,6 +189,7 @@ class RenderBlock extends RenderBlockBase { } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) return _getIntrinsicMainAxis(constraints); return _getIntrinsicCrossAxis( @@ -196,6 +199,7 @@ class RenderBlock extends RenderBlockBase { } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) return _getIntrinsicMainAxis(constraints); return _getIntrinsicCrossAxis( @@ -361,24 +365,28 @@ class RenderBlockViewport extends RenderBlockBase { } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) return _getIntrinsicDimension(constraints, minCrossAxisExtentCallback, constraints.constrainWidth); return constraints.constrainWidth(minExtent); } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (isVertical) return _getIntrinsicDimension(constraints, maxCrossAxisExtentCallback, constraints.constrainWidth); return _getIntrinsicDimension(constraints, totalExtentCallback, new BoxConstraints(minWidth: minExtent).enforce(constraints).constrainWidth); } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (!isVertical) return _getIntrinsicDimension(constraints, minCrossAxisExtentCallback, constraints.constrainHeight); return constraints.constrainHeight(0.0); } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (!isVertical) return _getIntrinsicDimension(constraints, maxCrossAxisExtentCallback, constraints.constrainHeight); return _getIntrinsicDimension(constraints, totalExtentCallback, new BoxConstraints(minHeight: minExtent).enforce(constraints).constrainHeight); diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index bca944336d..f13114859d 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -88,6 +88,7 @@ class BoxConstraints extends Constraints { /// Returns new box constraints that are smaller by the given edge dimensions BoxConstraints deflate(EdgeDims edges) { assert(edges != null); + assert(isNormalized); double horizontal = edges.left + edges.right; double vertical = edges.top + edges.bottom; double deflatedMinWidth = math.max(0.0, minWidth - horizontal); @@ -102,6 +103,7 @@ class BoxConstraints extends Constraints { /// Returns new box constraints that remove the minimum width and height requirements BoxConstraints loosen() { + assert(isNormalized); return new BoxConstraints( minWidth: 0.0, maxWidth: maxWidth, @@ -144,19 +146,24 @@ class BoxConstraints extends Constraints { /// Returns the width that both satisfies the constraints and is as close as possible to the given width double constrainWidth([double width = double.INFINITY]) { + assert(isNormalized); return clamp(min: minWidth, max: maxWidth, value: width); } /// Returns the height that both satisfies the constraints and is as close as possible to the given height double constrainHeight([double height = double.INFINITY]) { + assert(isNormalized); return clamp(min: minHeight, max: maxHeight, value: height); } /// Returns the size that both satisfies the constraints and is as close as possible to the given size Size constrain(Size size) { Size result = new Size(constrainWidth(size.width), constrainHeight(size.height)); - if (size is _DebugSize) - result = new _DebugSize(result, size._owner, size._canBeUsedByParent); + assert(() { + if (size is _DebugSize) + result = new _DebugSize(result, size._owner, size._canBeUsedByParent); + return true; + }); return result; } @@ -177,8 +184,9 @@ class BoxConstraints extends Constraints { /// Whether the given size satisfies the constraints bool isSatisfiedBy(Size size) { - return (minWidth <= size.width) && (size.width <= math.max(minWidth, maxWidth)) && - (minHeight <= size.height) && (size.height <= math.max(minHeight, maxHeight)); + assert(isNormalized); + return (minWidth <= size.width) && (size.width <= maxWidth) && + (minHeight <= size.height) && (size.height <= maxHeight); } BoxConstraints operator*(double other) { @@ -227,6 +235,8 @@ class BoxConstraints extends Constraints { return b * t; if (b == null) return a * (1.0 - t); + assert(a.isNormalized); + assert(b.isNormalized); return new BoxConstraints( minWidth: ui.lerpDouble(a.minWidth, b.minWidth, t), maxWidth: ui.lerpDouble(a.maxWidth, b.maxWidth, t), @@ -235,12 +245,25 @@ class BoxConstraints extends Constraints { ); } + bool get isNormalized => minWidth <= maxWidth && minHeight <= maxHeight; + + BoxConstraints normalize() { + return new BoxConstraints( + minWidth: minWidth, + maxWidth: minWidth > maxWidth ? minWidth : maxWidth, + minHeight: minHeight, + maxHeight: minHeight > maxHeight ? minHeight : maxHeight + ); + } + bool operator ==(dynamic other) { + assert(isNormalized); if (identical(this, other)) return true; if (other is! BoxConstraints) return false; final BoxConstraints typedOther = other; + assert(typedOther.isNormalized); return minWidth == typedOther.minWidth && maxWidth == typedOther.maxWidth && minHeight == typedOther.minHeight && @@ -248,6 +271,7 @@ class BoxConstraints extends Constraints { } int get hashCode { + assert(isNormalized); int value = 373; value = 37 * value + minWidth.hashCode; value = 37 * value + maxWidth.hashCode; @@ -257,11 +281,12 @@ class BoxConstraints extends Constraints { } String toString() { + String annotation = isNormalized ? '' : '; NOT NORMALIZED'; if (minWidth == double.INFINITY && minHeight == double.INFINITY) - return 'BoxConstraints(biggest)'; + return 'BoxConstraints(biggest$annotation)'; if (minWidth == 0 && maxWidth == double.INFINITY && minHeight == 0 && maxHeight == double.INFINITY) - return 'BoxConstraints(unconstrained)'; + return 'BoxConstraints(unconstrained$annotation)'; String describe(double min, double max, String dim) { if (min == max) return '$dim=${min.toStringAsFixed(1)}'; @@ -269,7 +294,7 @@ class BoxConstraints extends Constraints { } final String width = describe(minWidth, maxWidth, 'w'); final String height = describe(minHeight, maxHeight, 'h'); - return 'BoxConstraints($width, $height)'; + return 'BoxConstraints($width, $height$annotation)'; } } @@ -334,6 +359,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.constrainWidth(0.0); } @@ -342,6 +368,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.constrainWidth(0.0); } @@ -350,6 +377,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.constrainHeight(0.0); } @@ -362,6 +390,7 @@ abstract class RenderBox extends RenderObject { /// /// Override in subclasses that implement [performLayout]. double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.constrainHeight(0.0); } diff --git a/packages/flutter/lib/src/rendering/custom_layout.dart b/packages/flutter/lib/src/rendering/custom_layout.dart index 5c04b61a6d..38c86d5b29 100644 --- a/packages/flutter/lib/src/rendering/custom_layout.dart +++ b/packages/flutter/lib/src/rendering/custom_layout.dart @@ -41,6 +41,7 @@ abstract class MultiChildLayoutDelegate { 'A MultiChildLayoutDelegate cannot layout the same child more than once.'; return _debugChildrenNeedingLayout.remove(child); }); + assert(constraints.isNormalized); child.layout(constraints, parentUsesSize: true); return child.size; } @@ -130,6 +131,7 @@ class RenderCustomMultiChildLayoutBox extends RenderBox } Size _getSize(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.constrain(_delegate.getSize(constraints)); } diff --git a/packages/flutter/lib/src/rendering/editable_paragraph.dart b/packages/flutter/lib/src/rendering/editable_paragraph.dart index 74b1451abb..bfc6ea3c72 100644 --- a/packages/flutter/lib/src/rendering/editable_paragraph.dart +++ b/packages/flutter/lib/src/rendering/editable_paragraph.dart @@ -60,6 +60,7 @@ class RenderEditableParagraph extends RenderParagraph { } BoxConstraints _getTextContraints(BoxConstraints constraints) { + assert(constraints.isNormalized); return new BoxConstraints( minWidth: 0.0, maxWidth: double.INFINITY, diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index 4be08aae1d..566541652c 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -148,6 +148,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin { - - // Abstract class for one-child-layout render boxes - RenderShiftedBox(RenderBox child) { this.child = child; } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (child != null) return child.getMinIntrinsicWidth(constraints); return super.getMinIntrinsicWidth(constraints); } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); if (child != null) return child.getMaxIntrinsicWidth(constraints); return super.getMaxIntrinsicWidth(constraints); } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (child != null) return child.getMinIntrinsicHeight(constraints); return super.getMinIntrinsicHeight(constraints); } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); if (child != null) return child.getMaxIntrinsicHeight(constraints); return super.getMaxIntrinsicHeight(constraints); @@ -100,6 +103,7 @@ class RenderPadding extends RenderShiftedBox { } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); double totalPadding = padding.left + padding.right; if (child != null) return child.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding; @@ -107,6 +111,7 @@ class RenderPadding extends RenderShiftedBox { } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); double totalPadding = padding.left + padding.right; if (child != null) return child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding; @@ -114,6 +119,7 @@ class RenderPadding extends RenderShiftedBox { } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); double totalPadding = padding.top + padding.bottom; if (child != null) return child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding; @@ -121,6 +127,7 @@ class RenderPadding extends RenderShiftedBox { } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); double totalPadding = padding.top + padding.bottom; if (child != null) return child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding; @@ -282,18 +289,22 @@ class RenderCustomOneChildLayoutBox extends RenderShiftedBox { } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return _getSize(constraints).width; } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return _getSize(constraints).width; } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return _getSize(constraints).height; } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return _getSize(constraints).height; } @@ -305,9 +316,11 @@ class RenderCustomOneChildLayoutBox extends RenderShiftedBox { void performLayout() { if (child != null) { - child.layout(delegate.getConstraintsForChild(constraints), parentUsesSize: true); + BoxConstraints childConstraints = delegate.getConstraintsForChild(constraints); + assert(childConstraints.isNormalized); + child.layout(childConstraints, parentUsesSize: !childConstraints.isTight); final BoxParentData childParentData = child.parentData; - childParentData.position = delegate.getPositionForChild(size, child.size); + childParentData.position = delegate.getPositionForChild(size, childConstraints.isTight ? childConstraints.smallest : child.size); } } } diff --git a/packages/flutter/lib/src/rendering/stack.dart b/packages/flutter/lib/src/rendering/stack.dart index b89f17afd0..16121ecb8d 100644 --- a/packages/flutter/lib/src/rendering/stack.dart +++ b/packages/flutter/lib/src/rendering/stack.dart @@ -222,6 +222,7 @@ abstract class RenderStackBase extends RenderBox } double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); double width = constraints.minWidth; RenderBox child = firstChild; while (child != null) { @@ -236,6 +237,7 @@ abstract class RenderStackBase extends RenderBox } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); bool hasNonPositionedChildren = false; double width = constraints.minWidth; RenderBox child = firstChild; @@ -255,6 +257,7 @@ abstract class RenderStackBase extends RenderBox } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); double height = constraints.minHeight; RenderBox child = firstChild; while (child != null) { @@ -269,6 +272,7 @@ abstract class RenderStackBase extends RenderBox } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); bool hasNonPositionedChildren = false; double height = constraints.minHeight; RenderBox child = firstChild; diff --git a/packages/flutter/lib/src/rendering/statistics_box.dart b/packages/flutter/lib/src/rendering/statistics_box.dart index 34ef1c1308..f5f4b90893 100644 --- a/packages/flutter/lib/src/rendering/statistics_box.dart +++ b/packages/flutter/lib/src/rendering/statistics_box.dart @@ -34,18 +34,22 @@ class RenderStatisticsBox extends RenderBox { bool get sizedByParent => true; double getMinIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.minWidth; } double getMaxIntrinsicWidth(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.maxWidth; } double getMinIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.minHeight; } double getMaxIntrinsicHeight(BoxConstraints constraints) { + assert(constraints.isNormalized); return constraints.maxHeight; } diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 18157c68aa..eee9cb65ed 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -98,24 +98,28 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin[box]); layout(flex, constraints: const BoxConstraints( - minWidth: 200.0, maxWidth: 100.0, minHeight: 200.0, maxHeight: 100.0) + minWidth: 200.0, maxWidth: 200.0, minHeight: 200.0, maxHeight: 200.0) ); expect(flex.size.width, equals(200.0), reason: "flex width");