shifted_box.dart 23.9 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:math' as math;
6 7

import 'box.dart';
8
import 'debug.dart';
9
import 'object.dart';
10

11 12
/// Abstract class for one-child-layout render boxes that provide control over
/// the child's position.
13 14 15 16 17 18
abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
  RenderShiftedBox(RenderBox child) {
    this.child = child;
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
19
    assert(constraints.debugAssertIsNormalized);
20 21 22 23 24 25
    if (child != null)
      return child.getMinIntrinsicWidth(constraints);
    return super.getMinIntrinsicWidth(constraints);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
26
    assert(constraints.debugAssertIsNormalized);
27 28 29 30 31 32
    if (child != null)
      return child.getMaxIntrinsicWidth(constraints);
    return super.getMaxIntrinsicWidth(constraints);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
33
    assert(constraints.debugAssertIsNormalized);
34 35 36 37 38 39
    if (child != null)
      return child.getMinIntrinsicHeight(constraints);
    return super.getMinIntrinsicHeight(constraints);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
40
    assert(constraints.debugAssertIsNormalized);
41 42 43 44 45 46 47 48 49 50
    if (child != null)
      return child.getMaxIntrinsicHeight(constraints);
    return super.getMaxIntrinsicHeight(constraints);
  }

  double computeDistanceToActualBaseline(TextBaseline baseline) {
    double result;
    if (child != null) {
      assert(!needsLayout);
      result = child.getDistanceToActualBaseline(baseline);
Hixie's avatar
Hixie committed
51
      final BoxParentData childParentData = child.parentData;
52
      if (result != null)
53
        result += childParentData.offset.dy;
54 55 56 57 58 59 60
    } else {
      result = super.computeDistanceToActualBaseline(baseline);
    }
    return result;
  }

  void paint(PaintingContext context, Offset offset) {
Hixie's avatar
Hixie committed
61 62
    if (child != null) {
      final BoxParentData childParentData = child.parentData;
Adam Barth's avatar
Adam Barth committed
63
      context.paintChild(child, childParentData.offset + offset);
Hixie's avatar
Hixie committed
64
    }
65 66
  }

Adam Barth's avatar
Adam Barth committed
67
  bool hitTestChildren(HitTestResult result, { Point position }) {
68
    if (child != null) {
Hixie's avatar
Hixie committed
69
      final BoxParentData childParentData = child.parentData;
70 71
      final Point childPosition = new Point(position.x - childParentData.offset.dx,
                                            position.y - childParentData.offset.dy);
Adam Barth's avatar
Adam Barth committed
72
      return child.hitTest(result, position: childPosition);
73
    }
Adam Barth's avatar
Adam Barth committed
74
    return false;
75 76 77 78
  }

}

79 80 81 82 83 84
/// Insets its child by the given padding.
///
/// When passing layout constraints to its child, padding shrinks the
/// constraints by the given padding, causing the child to layout at a smaller
/// size. Padding then sizes itself to its child's size, inflated by the
/// padding, effectively creating empty space around the child.
85
class RenderPadding extends RenderShiftedBox {
86 87 88 89
  RenderPadding({
    EdgeDims padding,
    RenderBox child
  }) : _padding = padding, super(child) {
90
    assert(padding != null);
91
    assert(padding.isNonNegative);
92 93
  }

94
  /// The amount to pad the child in each dimension.
95
  EdgeDims get padding => _padding;
96
  EdgeDims _padding;
97 98
  void set padding (EdgeDims value) {
    assert(value != null);
99
    assert(value.isNonNegative);
100 101 102 103 104 105 106
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
107
    assert(constraints.debugAssertIsNormalized);
108 109
    double totalPadding = padding.left + padding.right;
    if (child != null)
110
      return constraints.constrainWidth(child.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding);
111 112 113 114
    return constraints.constrainWidth(totalPadding);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
115
    assert(constraints.debugAssertIsNormalized);
116 117
    double totalPadding = padding.left + padding.right;
    if (child != null)
118
      return constraints.constrainWidth(child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding);
119 120 121 122
    return constraints.constrainWidth(totalPadding);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
123
    assert(constraints.debugAssertIsNormalized);
124 125
    double totalPadding = padding.top + padding.bottom;
    if (child != null)
126
      return constraints.constrainHeight(child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding);
127 128 129 130
    return constraints.constrainHeight(totalPadding);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
131
    assert(constraints.debugAssertIsNormalized);
132 133
    double totalPadding = padding.top + padding.bottom;
    if (child != null)
134
      return constraints.constrainHeight(child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding);
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    return constraints.constrainHeight(totalPadding);
  }

  void performLayout() {
    assert(padding != null);
    if (child == null) {
      size = constraints.constrain(new Size(
        padding.left + padding.right,
        padding.top + padding.bottom
      ));
      return;
    }
    BoxConstraints innerConstraints = constraints.deflate(padding);
    child.layout(innerConstraints, parentUsesSize: true);
Hixie's avatar
Hixie committed
149
    final BoxParentData childParentData = child.parentData;
150
    childParentData.offset = new Offset(padding.left, padding.top);
151 152 153 154 155 156
    size = constraints.constrain(new Size(
      padding.left + child.size.width + padding.right,
      padding.top + child.size.height + padding.bottom
    ));
  }

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  void debugPaintSize(PaintingContext context, Offset offset) {
    super.debugPaintSize(context, offset);
    assert(() {
      Paint paint;
      if (child != null && !child.size.isEmpty) {
        Path path;
        paint = new Paint()
          ..color = debugPaintPaddingColor;
        path = new Path()
          ..moveTo(offset.dx, offset.dy)
          ..lineTo(offset.dx + size.width, offset.dy)
          ..lineTo(offset.dx + size.width, offset.dy + size.height)
          ..lineTo(offset.dx, offset.dy + size.height)
          ..close()
          ..moveTo(offset.dx + padding.left, offset.dy + padding.top)
          ..lineTo(offset.dx + padding.left, offset.dy + size.height - padding.bottom)
          ..lineTo(offset.dx + size.width - padding.right, offset.dy + size.height - padding.bottom)
          ..lineTo(offset.dx + size.width - padding.right, offset.dy + padding.top)
          ..close();
        context.canvas.drawPath(path, paint);
        paint = new Paint()
          ..color = debugPaintPaddingInnerEdgeColor;
        const kOutline = 2.0;
        path = new Path()
          ..moveTo(offset.dx + math.max(padding.left - kOutline, 0.0), offset.dy + math.max(padding.top - kOutline, 0.0))
          ..lineTo(offset.dx + math.min(size.width - padding.right + kOutline, size.width), offset.dy + math.max(padding.top - kOutline, 0.0))
          ..lineTo(offset.dx + math.min(size.width - padding.right + kOutline, size.width), offset.dy + math.min(size.height - padding.bottom + kOutline, size.height))
          ..lineTo(offset.dx + math.max(padding.left - kOutline, 0.0), offset.dy + math.min(size.height - padding.bottom + kOutline, size.height))
          ..close()
          ..moveTo(offset.dx + padding.left, offset.dy + padding.top)
          ..lineTo(offset.dx + padding.left, offset.dy + size.height - padding.bottom)
          ..lineTo(offset.dx + size.width - padding.right, offset.dy + size.height - padding.bottom)
          ..lineTo(offset.dx + size.width - padding.right, offset.dy + padding.top)
          ..close();
        context.canvas.drawPath(path, paint);
      } else {
        paint = new Paint()
          ..color = debugPaintSpacingColor;
        context.canvas.drawRect(offset & size, paint);
      }
      return true;
    });
  }

201 202 203
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('padding: $padding');
204
  }
205 206
}

207 208 209 210
/// Aligns its child box within itself.
///
/// For example, to align a box at the bottom right, you would pass this box a
/// tight constraint that is bigger than the child's natural size,
Adam Barth's avatar
Adam Barth committed
211
/// with an alignment of [const FractionalOffset(1.0, 1.0)].
212 213 214 215
///
/// By default, sizes to be as big as possible in both axes. If either axis is
/// unconstrained, then in that direction it will be sized to fit the child's
/// dimensions. Using widthFactor and heightFactor you can force this latter
216
/// behavior in all cases.
217 218 219
class RenderPositionedBox extends RenderShiftedBox {
  RenderPositionedBox({
    RenderBox child,
220
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5),
221 222
    double widthFactor,
    double heightFactor
223
  }) : _alignment = alignment,
224 225
       _widthFactor = widthFactor,
       _heightFactor = heightFactor,
226
       super(child) {
227
    assert(alignment != null && alignment.dx != null && alignment.dy != null);
228 229
    assert(widthFactor == null || widthFactor >= 0.0);
    assert(heightFactor == null || heightFactor >= 0.0);
230 231
  }

232 233 234 235 236 237 238 239 240
  /// How to align the child.
  ///
  /// The x and y values of the alignment control the horizontal and vertical
  /// alignment, respectively.  An x value of 0.0 means that the left edge of
  /// the child is aligned with the left edge of the parent whereas an x value
  /// of 1.0 means that the right edge of the child is aligned with the right
  /// edge of the parent. Other values interpolate (and extrapolate) linearly.
  /// For example, a value of 0.5 means that the center of the child is aligned
  /// with the center of the parent.
241 242 243
  FractionalOffset get alignment => _alignment;
  FractionalOffset _alignment;
  void set alignment (FractionalOffset newAlignment) {
244
    assert(newAlignment != null && newAlignment.dx != null && newAlignment.dy != null);
245
    if (_alignment == newAlignment)
246
      return;
247
    _alignment = newAlignment;
248 249 250
    markNeedsLayout();
  }

251 252 253
  /// If non-null, sets its width to the child's width multipled by this factor.
  ///
  /// Can be both greater and less than 1.0 but must be positive.
254
  double get widthFactor => _widthFactor;
255
  double _widthFactor;
256
  void set widthFactor (double value) {
257
    assert(value == null || value >= 0.0);
258
    if (_widthFactor == value)
259
      return;
260
    _widthFactor = value;
261 262 263
    markNeedsLayout();
  }

264 265 266
  /// If non-null, sets its height to the child's height multipled by this factor.
  ///
  /// Can be both greater and less than 1.0 but must be positive.
267
  double get heightFactor => _heightFactor;
268
  double _heightFactor;
269
  void set heightFactor (double value) {
270
    assert(value == null || value >= 0.0);
271 272 273 274 275
    if (_heightFactor == value)
      return;
    _heightFactor = value;
    markNeedsLayout();
  }
276

277
  void performLayout() {
278 279
    final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.INFINITY;
    final bool shrinkWrapHeight = _heightFactor != null || constraints.maxHeight == double.INFINITY;
280

281 282
    if (child != null) {
      child.layout(constraints.loosen(), parentUsesSize: true);
283 284
      size = constraints.constrain(new Size(shrinkWrapWidth ? child.size.width * (_widthFactor ?? 1.0) : double.INFINITY,
                                            shrinkWrapHeight ? child.size.height * (_heightFactor ?? 1.0) : double.INFINITY));
Hixie's avatar
Hixie committed
285
      final BoxParentData childParentData = child.parentData;
286
      childParentData.offset = _alignment.alongOffset(size - child.size);
287
    } else {
288 289
      size = constraints.constrain(new Size(shrinkWrapWidth ? 0.0 : double.INFINITY,
                                            shrinkWrapHeight ? 0.0 : double.INFINITY));
290 291 292
    }
  }

293 294 295 296 297 298 299
  void debugPaintSize(PaintingContext context, Offset offset) {
    super.debugPaintSize(context, offset);
    assert(() {
      Paint paint;
      if (child != null && !child.size.isEmpty) {
        Path path;
        paint = new Paint()
300
          ..style = PaintingStyle.stroke
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
          ..strokeWidth = 1.0
          ..color = debugPaintArrowColor;
        path = new Path();
        final BoxParentData childParentData = child.parentData;
        if (childParentData.offset.dy > 0.0) {
          // vertical alignment arrows
          double headSize = math.min(childParentData.offset.dy * 0.2, 10.0);
          path
            ..moveTo(offset.dx + size.width / 2.0, offset.dy)
            ..relativeLineTo(0.0, childParentData.offset.dy - headSize)
            ..relativeLineTo(headSize, 0.0)
            ..relativeLineTo(-headSize, headSize)
            ..relativeLineTo(-headSize, -headSize)
            ..relativeLineTo(headSize, 0.0)
            ..moveTo(offset.dx + size.width / 2.0, offset.dy + size.height)
            ..relativeLineTo(0.0, -childParentData.offset.dy + headSize)
            ..relativeLineTo(headSize, 0.0)
            ..relativeLineTo(-headSize, -headSize)
            ..relativeLineTo(-headSize, headSize)
            ..relativeLineTo(headSize, 0.0);
          context.canvas.drawPath(path, paint);
        }
        if (childParentData.offset.dx > 0.0) {
          // horizontal alignment arrows
          double headSize = math.min(childParentData.offset.dx * 0.2, 10.0);
          path
            ..moveTo(offset.dx, offset.dy + size.height / 2.0)
            ..relativeLineTo(childParentData.offset.dx - headSize, 0.0)
            ..relativeLineTo(0.0, headSize)
            ..relativeLineTo(headSize, -headSize)
            ..relativeLineTo(-headSize, -headSize)
            ..relativeLineTo(0.0, headSize)
            ..moveTo(offset.dx + size.width, offset.dy + size.height / 2.0)
            ..relativeLineTo(-childParentData.offset.dx + headSize, 0.0)
            ..relativeLineTo(0.0, headSize)
            ..relativeLineTo(-headSize, -headSize)
            ..relativeLineTo(headSize, -headSize)
            ..relativeLineTo(0.0, headSize);
          context.canvas.drawPath(path, paint);
        }
      } else {
        paint = new Paint()
          ..color = debugPaintSpacingColor;
        context.canvas.drawRect(offset & size, paint);
      }
      return true;
    });
  }

350 351 352
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('alignment: $alignment');
353
  }
354 355
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/// A render object that imposes different constraints on its child than it gets
/// from its parent, possibly allowing the child to overflow the parent.
///
/// A render overflow box proxies most functions in the render box protocol to
/// its child, except that when laying out its child, it passes constraints
/// based on the minWidth, maxWidth, minHeight, and maxHeight fields instead of
/// just passing the parent's constraints in. Specifically, it overrides any of
/// the equivalent fields on the constraints given by the parent with the
/// constraints given by these fields for each such field that is not null. It
/// then sizes itself based on the parent's constraints' maxWidth and maxHeight,
/// ignoring the child's dimensions.
///
/// For example, if you wanted a box to always render 50 pixels high, regardless
/// of where it was rendered, you would wrap it in a RenderOverflow with
/// minHeight and maxHeight set to 50.0. Generally speaking, to avoid confusing
371
/// behavior around hit testing, a RenderOverflowBox should usually be wrapped
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/// in a RenderClipRect.
///
/// The child is positioned at the top left of the box. To position a smaller
/// child inside a larger parent, use [RenderPositionedBox] and
/// [RenderConstrainedBox] rather than RenderOverflowBox.
class RenderOverflowBox extends RenderShiftedBox {
  RenderOverflowBox({
    RenderBox child,
    double minWidth,
    double maxWidth,
    double minHeight,
    double maxHeight,
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5)
  }) : _minWidth = minWidth,
       _maxWidth = maxWidth,
       _minHeight = minHeight,
       _maxHeight = maxHeight,
       _alignment = alignment,
       super(child);

  /// The minimum width constraint to give the child. Set this to null (the
  /// default) to use the constraint from the parent instead.
  double get minWidth => _minWidth;
  double _minWidth;
  void set minWidth (double value) {
    if (_minWidth == value)
      return;
    _minWidth = value;
    markNeedsLayout();
  }

  /// The maximum width constraint to give the child. Set this to null (the
  /// default) to use the constraint from the parent instead.
  double get maxWidth => _maxWidth;
  double _maxWidth;
  void set maxWidth (double value) {
    if (_maxWidth == value)
      return;
    _maxWidth = value;
    markNeedsLayout();
  }

  /// The minimum height constraint to give the child. Set this to null (the
  /// default) to use the constraint from the parent instead.
  double get minHeight => _minHeight;
  double _minHeight;
  void set minHeight (double value) {
    if (_minHeight == value)
      return;
    _minHeight = value;
    markNeedsLayout();
  }

  /// The maximum height constraint to give the child. Set this to null (the
  /// default) to use the constraint from the parent instead.
  double get maxHeight => _maxHeight;
  double _maxHeight;
  void set maxHeight (double value) {
    if (_maxHeight == value)
      return;
    _maxHeight = value;
    markNeedsLayout();
  }

  /// How to align the child.
  ///
  /// The x and y values of the alignment control the horizontal and vertical
  /// alignment, respectively.  An x value of 0.0 means that the left edge of
  /// the child is aligned with the left edge of the parent whereas an x value
  /// of 1.0 means that the right edge of the child is aligned with the right
  /// edge of the parent. Other values interpolate (and extrapolate) linearly.
  /// For example, a value of 0.5 means that the center of the child is aligned
  /// with the center of the parent.
  FractionalOffset get alignment => _alignment;
  FractionalOffset _alignment;
  void set alignment (FractionalOffset newAlignment) {
    assert(newAlignment != null && newAlignment.dx != null && newAlignment.dy != null);
    if (_alignment == newAlignment)
      return;
    _alignment = newAlignment;
    markNeedsLayout();
  }

  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
    return new BoxConstraints(
      minWidth: _minWidth ?? constraints.minWidth,
      maxWidth: _maxWidth ?? constraints.maxWidth,
      minHeight: _minHeight ?? constraints.minHeight,
      maxHeight: _maxHeight ?? constraints.maxHeight
    );
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
465
    assert(constraints.debugAssertIsNormalized);
466 467 468 469
    return constraints.minWidth;
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
470
    assert(constraints.debugAssertIsNormalized);
471 472 473 474
    return constraints.minWidth;
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
475
    assert(constraints.debugAssertIsNormalized);
476 477 478 479
    return constraints.minHeight;
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
480
    assert(constraints.debugAssertIsNormalized);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    return constraints.minHeight;
  }

  bool get sizedByParent => true;

  void performResize() {
    size = constraints.biggest;
  }

  void performLayout() {
    if (child != null) {
      child.layout(_getInnerConstraints(constraints), parentUsesSize: true);
      final BoxParentData childParentData = child.parentData;
      childParentData.offset = _alignment.alongOffset(size - child.size);
    }
  }

498 499 500 501 502 503 504
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('minWidth: ${minWidth ?? "use parent minWidth constraint"}');
    description.add('maxWidth: ${maxWidth ?? "use parent maxWidth constraint"}');
    description.add('minHeight: ${minHeight ?? "use parent minHeight constraint"}');
    description.add('maxHeight: ${maxHeight ?? "use parent maxHeight constraint"}');
    description.add('alignment: $alignment');
505 506 507
  }
}

Adam Barth's avatar
Adam Barth committed
508 509
/// A delegate for computing the layout of a render object with a single child.
class OneChildLayoutDelegate {
510
  /// Returns the size of this object given the incoming constraints.
Adam Barth's avatar
Adam Barth committed
511 512
  Size getSize(BoxConstraints constraints) => constraints.biggest;

513
  /// Returns the box constraints for the child given the incoming constraints.
Adam Barth's avatar
Adam Barth committed
514 515 516
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints;

  /// Returns the position where the child should be placed given the size of this object and the size of the child.
517
  Offset getPositionForChild(Size size, Size childSize) => Offset.zero;
518 519 520

  /// Override this method to return true when the child needs to be laid out.
  bool shouldRelayout(OneChildLayoutDelegate oldDelegate) => true;
Adam Barth's avatar
Adam Barth committed
521 522
}

523 524 525 526 527 528
/// Defers the layout of its single child to a delegate.
///
/// The delegate can determine the layout constraints for the child and can
/// decide where to position the child. The delegate can also determine the size
/// of the parent, but the size of the parent cannot depend on the size of the
/// child.
Adam Barth's avatar
Adam Barth committed
529 530 531 532 533 534 535 536
class RenderCustomOneChildLayoutBox extends RenderShiftedBox {
  RenderCustomOneChildLayoutBox({
    RenderBox child,
    OneChildLayoutDelegate delegate
  }) : _delegate = delegate, super(child) {
    assert(delegate != null);
  }

537
  /// A delegate that controls this object's layout.
Adam Barth's avatar
Adam Barth committed
538 539 540 541 542 543
  OneChildLayoutDelegate get delegate => _delegate;
  OneChildLayoutDelegate _delegate;
  void set delegate (OneChildLayoutDelegate newDelegate) {
    assert(newDelegate != null);
    if (_delegate == newDelegate)
      return;
544 545
    if (newDelegate.runtimeType != _delegate.runtimeType || newDelegate.shouldRelayout(_delegate))
      markNeedsLayout();
Adam Barth's avatar
Adam Barth committed
546 547 548 549 550 551 552 553
    _delegate = newDelegate;
  }

  Size _getSize(BoxConstraints constraints) {
    return constraints.constrain(_delegate.getSize(constraints));
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
554
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
555 556 557 558
    return _getSize(constraints).width;
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
559
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
560 561 562 563
    return _getSize(constraints).width;
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
564
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
565 566 567 568
    return _getSize(constraints).height;
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
569
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
570 571 572 573 574 575 576 577 578 579 580
    return _getSize(constraints).height;
  }

  bool get sizedByParent => true;

  void performResize() {
    size = _getSize(constraints);
  }

  void performLayout() {
    if (child != null) {
581
      BoxConstraints childConstraints = delegate.getConstraintsForChild(constraints);
Hixie's avatar
Hixie committed
582
      assert(childConstraints.debugAssertIsNormalized);
583
      child.layout(childConstraints, parentUsesSize: !childConstraints.isTight);
Adam Barth's avatar
Adam Barth committed
584
      final BoxParentData childParentData = child.parentData;
585
      childParentData.offset = delegate.getPositionForChild(size, childConstraints.isTight ? childConstraints.smallest : child.size);
Adam Barth's avatar
Adam Barth committed
586 587 588 589
    }
  }
}

590
/// Positions its child vertically according to the child's baseline.
591 592 593 594 595 596 597 598 599 600 601 602 603
class RenderBaseline extends RenderShiftedBox {

  RenderBaseline({
    RenderBox child,
    double baseline,
    TextBaseline baselineType
  }) : _baseline = baseline,
       _baselineType = baselineType,
       super(child) {
    assert(baseline != null);
    assert(baselineType != null);
  }

604 605
  /// The number of logical pixels from the top of this box at which to position
  /// the child's baseline.
606
  double get baseline => _baseline;
607
  double _baseline;
608 609 610 611 612 613 614 615
  void set baseline (double value) {
    assert(value != null);
    if (_baseline == value)
      return;
    _baseline = value;
    markNeedsLayout();
  }

616
  /// The type of baseline to use for positioning the child.
617
  TextBaseline get baselineType => _baselineType;
618
  TextBaseline _baselineType;
619 620 621 622 623 624 625 626 627 628 629 630 631
  void set baselineType (TextBaseline value) {
    assert(value != null);
    if (_baselineType == value)
      return;
    _baselineType = value;
    markNeedsLayout();
  }

  void performLayout() {
    if (child != null) {
      child.layout(constraints.loosen(), parentUsesSize: true);
      size = constraints.constrain(child.size);
      double delta = baseline - child.getDistanceToBaseline(baselineType);
Hixie's avatar
Hixie committed
632
      final BoxParentData childParentData = child.parentData;
633
      childParentData.offset = new Offset(0.0, delta);
634 635 636 637 638
    } else {
      performResize();
    }
  }

639 640 641 642
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('baseline: $baseline');
    description.add('baselineType: $baselineType');
643
  }
644
}