Commit c21b565f authored by Ian Hickson's avatar Ian Hickson

tightenWidth(), tightenHeight() => tighten(width:, height:)

This makes it more consistent with tightFor(), and also makes it
easier to tighten both directions at once when you're not sure you
will always do so (e.g. if you have a height and width that might be
null, and want to tighten whichever ones aren't null).
parent a0338203
...@@ -39,7 +39,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { ...@@ -39,7 +39,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
// in this case the toolbar appears -after- the body in the stacking order, // in this case the toolbar appears -after- the body in the stacking order,
// so the toolbar's shadow is drawn on top of the body. // so the toolbar's shadow is drawn on top of the body.
final BoxConstraints fullWidthConstraints = looseConstraints.tightenWidth(size.width); final BoxConstraints fullWidthConstraints = looseConstraints.tighten(width: size.width);
Size toolBarSize = Size.zero; Size toolBarSize = Size.zero;
if (isChild(_Child.toolBar)) { if (isChild(_Child.toolBar)) {
...@@ -49,7 +49,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { ...@@ -49,7 +49,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
if (isChild(_Child.body)) { if (isChild(_Child.body)) {
final double bodyHeight = size.height - toolBarSize.height; final double bodyHeight = size.height - toolBarSize.height;
final BoxConstraints bodyConstraints = fullWidthConstraints.tightenHeight(bodyHeight); final BoxConstraints bodyConstraints = fullWidthConstraints.tighten(height: bodyHeight);
layoutChild(_Child.body, bodyConstraints); layoutChild(_Child.body, bodyConstraints);
positionChild(_Child.body, new Offset(0.0, toolBarSize.height)); positionChild(_Child.body, new Offset(0.0, toolBarSize.height));
} }
......
...@@ -124,22 +124,14 @@ class BoxConstraints extends Constraints { ...@@ -124,22 +124,14 @@ class BoxConstraints extends Constraints {
); );
} }
/// Returns new box constraints with a tight width as close to the given width /// Returns new box constraints with a tight width and/or height as close to
/// as possible while still respecting the original box constraints. /// the given width and height as possible while still respecting the original
BoxConstraints tightenWidth(double width) { /// box constraints.
return new BoxConstraints(minWidth: math.max(math.min(maxWidth, width), minWidth), BoxConstraints tighten({ double width, double height }) {
maxWidth: math.max(math.min(maxWidth, width), minWidth), return new BoxConstraints(minWidth: width == null ? minWidth : math.max(math.min(maxWidth, width), minWidth),
minHeight: minHeight, maxWidth: width == null ? maxWidth : math.max(math.min(maxWidth, width), minWidth),
maxHeight: maxHeight); minHeight: height == null ? minHeight : math.max(math.min(maxHeight, height), minHeight),
} maxHeight: height == null ? maxHeight : math.max(math.min(maxHeight, height), minHeight));
/// Returns new box constraints with a tight height as close to the given
/// height as possible while still respecting the original box constraints.
BoxConstraints tightenHeight(double height) {
return new BoxConstraints(minWidth: minWidth,
maxWidth: maxWidth,
minHeight: math.max(math.min(maxHeight, height), minHeight),
maxHeight: math.max(math.min(maxHeight, height), minHeight));
} }
/// Returns box constraints with the same width constraints but with /// Returns box constraints with the same width constraints but with
......
...@@ -415,7 +415,7 @@ class RenderIntrinsicWidth extends RenderProxyBox { ...@@ -415,7 +415,7 @@ class RenderIntrinsicWidth extends RenderProxyBox {
return constraints; return constraints;
double width = child.getMaxIntrinsicWidth(constraints); double width = child.getMaxIntrinsicWidth(constraints);
assert(width == constraints.constrainWidth(width)); assert(width == constraints.constrainWidth(width));
return constraints.tightenWidth(_applyStep(width, _stepWidth)); return constraints.tighten(width: _applyStep(width, _stepWidth));
} }
double getMinIntrinsicWidth(BoxConstraints constraints) { double getMinIntrinsicWidth(BoxConstraints constraints) {
...@@ -451,7 +451,7 @@ class RenderIntrinsicWidth extends RenderProxyBox { ...@@ -451,7 +451,7 @@ class RenderIntrinsicWidth extends RenderProxyBox {
if (child != null) { if (child != null) {
BoxConstraints childConstraints = _getInnerConstraints(constraints); BoxConstraints childConstraints = _getInnerConstraints(constraints);
if (_stepHeight != null) if (_stepHeight != null)
childConstraints.tightenHeight(getMaxIntrinsicHeight(childConstraints)); childConstraints.tighten(height: getMaxIntrinsicHeight(childConstraints));
child.layout(childConstraints, parentUsesSize: true); child.layout(childConstraints, parentUsesSize: true);
size = child.size; size = child.size;
} else { } else {
...@@ -485,7 +485,7 @@ class RenderIntrinsicHeight extends RenderProxyBox { ...@@ -485,7 +485,7 @@ class RenderIntrinsicHeight extends RenderProxyBox {
return constraints; return constraints;
double height = child.getMaxIntrinsicHeight(constraints); double height = child.getMaxIntrinsicHeight(constraints);
assert(height == constraints.constrainHeight(height)); assert(height == constraints.constrainHeight(height));
return constraints.tightenHeight(height); return constraints.tighten(height: height);
} }
double getMinIntrinsicWidth(BoxConstraints constraints) { double getMinIntrinsicWidth(BoxConstraints constraints) {
......
...@@ -333,14 +333,14 @@ abstract class RenderStackBase extends RenderBox ...@@ -333,14 +333,14 @@ abstract class RenderStackBase extends RenderBox
BoxConstraints childConstraints = const BoxConstraints(); BoxConstraints childConstraints = const BoxConstraints();
if (childParentData.left != null && childParentData.right != null) if (childParentData.left != null && childParentData.right != null)
childConstraints = childConstraints.tightenWidth(size.width - childParentData.right - childParentData.left); childConstraints = childConstraints.tighten(width: size.width - childParentData.right - childParentData.left);
else if (childParentData.width != null) else if (childParentData.width != null)
childConstraints = childConstraints.tightenWidth(childParentData.width); childConstraints = childConstraints.tighten(width: childParentData.width);
if (childParentData.top != null && childParentData.bottom != null) if (childParentData.top != null && childParentData.bottom != null)
childConstraints = childConstraints.tightenHeight(size.height - childParentData.bottom - childParentData.top); childConstraints = childConstraints.tighten(height: size.height - childParentData.bottom - childParentData.top);
else if (childParentData.height != null) else if (childParentData.height != null)
childConstraints = childConstraints.tightenHeight(childParentData.height); childConstraints = childConstraints.tighten(height: childParentData.height);
child.layout(childConstraints, parentUsesSize: true); child.layout(childConstraints, parentUsesSize: true);
......
...@@ -509,12 +509,7 @@ class SizedBox extends OneChildRenderObjectWidget { ...@@ -509,12 +509,7 @@ class SizedBox extends OneChildRenderObjectWidget {
); );
BoxConstraints get _additionalConstraints { BoxConstraints get _additionalConstraints {
BoxConstraints result = const BoxConstraints(); return new BoxConstraints.tightFor(width: width, height: height);
if (width != null)
result = result.tightenWidth(width);
if (height != null)
result = result.tightenHeight(height);
return result;
} }
void updateRenderObject(RenderConstrainedBox renderObject, SizedBox oldWidget) { void updateRenderObject(RenderConstrainedBox renderObject, SizedBox oldWidget) {
......
...@@ -25,7 +25,7 @@ void main() { ...@@ -25,7 +25,7 @@ void main() {
test('Flex and padding', () { test('Flex and padding', () {
RenderBox size = new RenderConstrainedBox( RenderBox size = new RenderConstrainedBox(
additionalConstraints: new BoxConstraints().tightenHeight(100.0) additionalConstraints: new BoxConstraints().tighten(height: 100.0)
); );
RenderBox inner = new RenderDecoratedBox( RenderBox inner = new RenderDecoratedBox(
decoration: new BoxDecoration( decoration: new BoxDecoration(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment