shifted_box.dart 30.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
abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
  RenderShiftedBox(RenderBox child) {
    this.child = child;
  }

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

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

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

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

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

65
  @override
66
  void paint(PaintingContext context, Offset offset) {
Hixie's avatar
Hixie committed
67 68
    if (child != null) {
      final BoxParentData childParentData = child.parentData;
Adam Barth's avatar
Adam Barth committed
69
      context.paintChild(child, childParentData.offset + offset);
Hixie's avatar
Hixie committed
70
    }
71 72
  }

73
  @override
Adam Barth's avatar
Adam Barth committed
74
  bool hitTestChildren(HitTestResult result, { Point position }) {
75
    if (child != null) {
Hixie's avatar
Hixie committed
76
      final BoxParentData childParentData = child.parentData;
77 78
      final Point childPosition = new Point(position.x - childParentData.offset.dx,
                                            position.y - childParentData.offset.dy);
Adam Barth's avatar
Adam Barth committed
79
      return child.hitTest(result, position: childPosition);
80
    }
Adam Barth's avatar
Adam Barth committed
81
    return false;
82 83 84 85
  }

}

86 87 88 89 90 91
/// 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.
92
class RenderPadding extends RenderShiftedBox {
93
  RenderPadding({
94
    EdgeInsets padding,
95 96
    RenderBox child
  }) : _padding = padding, super(child) {
97
    assert(padding != null);
98
    assert(padding.isNonNegative);
99 100
  }

101
  /// The amount to pad the child in each dimension.
102 103 104
  EdgeInsets get padding => _padding;
  EdgeInsets _padding;
  void set padding (EdgeInsets value) {
105
    assert(value != null);
106
    assert(value.isNonNegative);
107 108 109 110 111 112
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
  }

113
  @override
114
  double getMinIntrinsicWidth(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.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding);
119 120 121
    return constraints.constrainWidth(totalPadding);
  }

122
  @override
123
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
124
    assert(constraints.debugAssertIsNormalized);
125 126
    double totalPadding = padding.left + padding.right;
    if (child != null)
127
      return constraints.constrainWidth(child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding);
128 129 130
    return constraints.constrainWidth(totalPadding);
  }

131
  @override
132
  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
133
    assert(constraints.debugAssertIsNormalized);
134 135
    double totalPadding = padding.top + padding.bottom;
    if (child != null)
136
      return constraints.constrainHeight(child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding);
137 138 139
    return constraints.constrainHeight(totalPadding);
  }

140
  @override
141
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
142
    assert(constraints.debugAssertIsNormalized);
143 144
    double totalPadding = padding.top + padding.bottom;
    if (child != null)
145
      return constraints.constrainHeight(child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding);
146 147 148
    return constraints.constrainHeight(totalPadding);
  }

149
  @override
150 151 152 153 154 155 156 157 158 159 160
  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
161
    final BoxParentData childParentData = child.parentData;
162
    childParentData.offset = new Offset(padding.left, padding.top);
163 164 165 166 167 168
    size = constraints.constrain(new Size(
      padding.left + child.size.width + padding.right,
      padding.top + child.size.height + padding.bottom
    ));
  }

169
  @override
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  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;
192
        const double kOutline = 2.0;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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;
    });
  }

214
  @override
215 216 217
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('padding: $padding');
218
  }
219 220
}

221 222
abstract class RenderAligningShiftedBox extends RenderShiftedBox {
  RenderAligningShiftedBox({
223
    RenderBox child,
224
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5)
225
  }) : _alignment = alignment,
226
       super(child) {
227
    assert(alignment != null && alignment.dx != null && alignment.dy != null);
228 229
  }

230 231 232 233 234 235 236 237 238
  /// 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.
239 240
  FractionalOffset get alignment => _alignment;
  FractionalOffset _alignment;
241 242 243
  /// Sets the alignment to a new value, and triggers a layout update.
  ///
  /// The new alignment must not be null or have any null properties.
244
  void set alignment (FractionalOffset newAlignment) {
245
    assert(newAlignment != null && newAlignment.dx != null && newAlignment.dy != null);
246
    if (_alignment == newAlignment)
247
      return;
248
    _alignment = newAlignment;
249 250 251
    markNeedsLayout();
  }

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  /// Apply the current [alignment] to the [child].
  ///
  /// Subclasses should call this method if they have a child, to have
  /// this class perform the actual alignment. If there is no child,
  /// do not call this method.
  ///
  /// This method must be called after the child has been laid out and
  /// this object's own size has been set.
  void alignChild() {
    assert(child != null);
    assert(!child.needsLayout);
    assert(child.hasSize);
    assert(hasSize);
    final BoxParentData childParentData = child.parentData;
    childParentData.offset = alignment.alongOffset(size - child.size);
  }

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('alignment: $alignment');
  }
}

/// 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,
/// with an alignment of [const FractionalOffset(1.0, 1.0)].
///
/// 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
/// behavior in all cases.
class RenderPositionedBox extends RenderAligningShiftedBox {
  RenderPositionedBox({
    RenderBox child,
    double widthFactor,
    double heightFactor,
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5)
  }) : _widthFactor = widthFactor,
       _heightFactor = heightFactor,
       super(child: child, alignment: alignment) {
    assert(widthFactor == null || widthFactor >= 0.0);
    assert(heightFactor == null || heightFactor >= 0.0);
  }

299 300 301
  /// 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.
302
  double get widthFactor => _widthFactor;
303
  double _widthFactor;
304
  void set widthFactor (double value) {
305
    assert(value == null || value >= 0.0);
306
    if (_widthFactor == value)
307
      return;
308
    _widthFactor = value;
309 310 311
    markNeedsLayout();
  }

312 313 314
  /// 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.
315
  double get heightFactor => _heightFactor;
316
  double _heightFactor;
317
  void set heightFactor (double value) {
318
    assert(value == null || value >= 0.0);
319 320 321 322 323
    if (_heightFactor == value)
      return;
    _heightFactor = value;
    markNeedsLayout();
  }
324

325
  @override
326
  void performLayout() {
327 328
    final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.INFINITY;
    final bool shrinkWrapHeight = _heightFactor != null || constraints.maxHeight == double.INFINITY;
329

330 331
    if (child != null) {
      child.layout(constraints.loosen(), parentUsesSize: true);
332 333
      size = constraints.constrain(new Size(shrinkWrapWidth ? child.size.width * (_widthFactor ?? 1.0) : double.INFINITY,
                                            shrinkWrapHeight ? child.size.height * (_heightFactor ?? 1.0) : double.INFINITY));
334
      alignChild();
335
    } else {
336 337
      size = constraints.constrain(new Size(shrinkWrapWidth ? 0.0 : double.INFINITY,
                                            shrinkWrapHeight ? 0.0 : double.INFINITY));
338 339 340
    }
  }

341
  @override
342 343 344 345 346 347 348
  void debugPaintSize(PaintingContext context, Offset offset) {
    super.debugPaintSize(context, offset);
    assert(() {
      Paint paint;
      if (child != null && !child.size.isEmpty) {
        Path path;
        paint = new Paint()
349
          ..style = PaintingStyle.stroke
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 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
          ..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;
    });
  }

399
  @override
400 401
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
402 403
    description.add('widthFactor: ${_widthFactor ?? "expand"}');
    description.add('heightFactor: ${_heightFactor ?? "expand"}');
404
  }
405 406
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/// 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
422
/// behavior around hit testing, a RenderOverflowBox should usually be wrapped
423 424 425 426 427
/// 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.
428 429
class RenderConstrainedOverflowBox extends RenderAligningShiftedBox {
  RenderConstrainedOverflowBox({
430 431 432 433 434 435 436 437 438 439
    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,
440
       super(child: child, alignment: alignment);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

  /// 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();
  }

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

495
  @override
496
  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
497
    assert(constraints.debugAssertIsNormalized);
498 499 500
    return constraints.minWidth;
  }

501
  @override
502
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
503
    assert(constraints.debugAssertIsNormalized);
504 505 506
    return constraints.minWidth;
  }

507
  @override
508
  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
509
    assert(constraints.debugAssertIsNormalized);
510 511 512
    return constraints.minHeight;
  }

513
  @override
514
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
515
    assert(constraints.debugAssertIsNormalized);
516 517 518
    return constraints.minHeight;
  }

519
  @override
520 521
  bool get sizedByParent => true;

522
  @override
523 524 525 526
  void performResize() {
    size = constraints.biggest;
  }

527
  @override
528 529 530
  void performLayout() {
    if (child != null) {
      child.layout(_getInnerConstraints(constraints), parentUsesSize: true);
531
      alignChild();
532 533 534
    }
  }

535
  @override
536 537 538 539 540 541
  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"}');
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
  }
}

/// A render box that's a specific size but passes its original constraints
/// through to its child, which will probably overflow.
class RenderSizedOverflowBox extends RenderAligningShiftedBox {
  RenderSizedOverflowBox({
    RenderBox child,
    Size requestedSize,
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5)
  }) : _requestedSize = requestedSize,
       super(child: child, alignment: alignment) {
    assert(requestedSize != null);
  }

  /// The size this render box should attempt to be.
  Size get requestedSize => _requestedSize;
  Size _requestedSize;
  void set requestedSize (Size value) {
    assert(value != null);
    if (_requestedSize == value)
      return;
    _requestedSize = value;
    markNeedsLayout();
  }

  @override
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    return constraints.constrainWidth(_requestedSize.width);
  }

  @override
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    return constraints.constrainWidth(_requestedSize.width);
  }

  @override
  double getMinIntrinsicHeight(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    return constraints.constrainHeight(_requestedSize.height);
  }

  @override
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    return constraints.constrainHeight(_requestedSize.height);
  }

  @override
  double computeDistanceToActualBaseline(TextBaseline baseline) {
    if (child != null)
      return child.getDistanceToActualBaseline(baseline);
    return super.computeDistanceToActualBaseline(baseline);
  }

  @override
  void performLayout() {
    size = constraints.constrain(_requestedSize);
    if (child != null) {
      child.layout(constraints);
      alignChild();
    }
  }
}

/// Sizes its child to a fraction of the total available space.
///
/// For both its width and height, this render object imposes a tight
/// constraint on its child that is a multiple (typically less than 1.0) of the
/// maximum constraint it received from its parent on that axis. If the factor
/// for a given axis is null, then the constraints from the parent are just
/// passed through instead.
///
/// It then tries to size itself to the size of its child.
class RenderFractionallySizedOverflowBox extends RenderAligningShiftedBox {
  RenderFractionallySizedOverflowBox({
    RenderBox child,
    double widthFactor,
    double heightFactor,
    FractionalOffset alignment: const FractionalOffset(0.5, 0.5)
  }) : _widthFactor = widthFactor,
       _heightFactor = heightFactor,
       super(child: child, alignment: alignment) {
    assert(_widthFactor == null || _widthFactor >= 0.0);
    assert(_heightFactor == null || _heightFactor >= 0.0);
  }

  /// If non-null, the factor of the incoming width to use.
  ///
  /// If non-null, the child is given a tight width constraint that is the max
  /// incoming width constraint multipled by this factor.  If null, the child is
  /// given the incoming width constraings.
  double get widthFactor => _widthFactor;
  double _widthFactor;
  void set widthFactor (double value) {
    assert(value == null || value >= 0.0);
    if (_widthFactor == value)
      return;
    _widthFactor = value;
    markNeedsLayout();
  }

  /// If non-null, the factor of the incoming height to use.
  ///
  /// If non-null, the child is given a tight height constraint that is the max
  /// incoming width constraint multipled by this factor.  If null, the child is
  /// given the incoming width constraings.
  double get heightFactor => _heightFactor;
  double _heightFactor;
  void set heightFactor (double value) {
    assert(value == null || value >= 0.0);
    if (_heightFactor == value)
      return;
    _heightFactor = value;
    markNeedsLayout();
  }

  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
    double minWidth = constraints.minWidth;
    double maxWidth = constraints.maxWidth;
    if (_widthFactor != null) {
      double width = maxWidth * _widthFactor;
      minWidth = width;
      maxWidth = width;
    }
    double minHeight = constraints.minHeight;
    double maxHeight = constraints.maxHeight;
    if (_heightFactor != null) {
      double height = maxHeight * _heightFactor;
      minHeight = height;
      maxHeight = height;
    }
    return new BoxConstraints(
      minWidth: minWidth,
      maxWidth: maxWidth,
      minHeight: minHeight,
      maxHeight: maxHeight
    );
  }

  @override
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    if (child != null)
      return constraints.constrainWidth(child.getMinIntrinsicWidth(_getInnerConstraints(constraints)));
    return constraints.constrainWidth(_getInnerConstraints(constraints).constrainWidth(0.0));
  }

  @override
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    if (child != null)
      return constraints.constrainWidth(child.getMaxIntrinsicWidth(_getInnerConstraints(constraints)));
    return constraints.constrainWidth(_getInnerConstraints(constraints).constrainWidth(0.0));
  }

  @override
  double getMinIntrinsicHeight(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    if (child != null)
      return constraints.constrainHeight(child.getMinIntrinsicHeight(_getInnerConstraints(constraints)));
    return constraints.constrainHeight(_getInnerConstraints(constraints).constrainHeight(0.0));
  }

  @override
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    assert(constraints.debugAssertIsNormalized);
    if (child != null)
      return constraints.constrainHeight(child.getMaxIntrinsicHeight(_getInnerConstraints(constraints)));
    return constraints.constrainHeight(_getInnerConstraints(constraints).constrainHeight(0.0));
  }

  @override
  void performLayout() {
    if (child != null) {
      child.layout(_getInnerConstraints(constraints), parentUsesSize: true);
      size = constraints.constrain(child.size);
      alignChild();
    } else {
      size = constraints.constrain(_getInnerConstraints(constraints).constrain(Size.zero));
    }
  }

  @override
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('widthFactor: ${_widthFactor ?? "pass-through"}');
    description.add('heightFactor: ${_heightFactor ?? "pass-through"}');
732 733 734
  }
}

Adam Barth's avatar
Adam Barth committed
735
/// A delegate for computing the layout of a render object with a single child.
736
class SingleChildLayoutDelegate {
737
  /// Returns the size of this object given the incoming constraints.
Adam Barth's avatar
Adam Barth committed
738 739
  Size getSize(BoxConstraints constraints) => constraints.biggest;

740
  /// Returns the box constraints for the child given the incoming constraints.
Adam Barth's avatar
Adam Barth committed
741 742 743
  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.
744
  Offset getPositionForChild(Size size, Size childSize) => Offset.zero;
745 746

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

750 751 752 753 754 755
/// 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.
756 757
class RenderCustomSingleChildLayoutBox extends RenderShiftedBox {
  RenderCustomSingleChildLayoutBox({
Adam Barth's avatar
Adam Barth committed
758
    RenderBox child,
759
    SingleChildLayoutDelegate delegate
Adam Barth's avatar
Adam Barth committed
760 761 762 763
  }) : _delegate = delegate, super(child) {
    assert(delegate != null);
  }

764
  /// A delegate that controls this object's layout.
765 766 767
  SingleChildLayoutDelegate get delegate => _delegate;
  SingleChildLayoutDelegate _delegate;
  void set delegate (SingleChildLayoutDelegate newDelegate) {
Adam Barth's avatar
Adam Barth committed
768 769 770
    assert(newDelegate != null);
    if (_delegate == newDelegate)
      return;
771 772
    if (newDelegate.runtimeType != _delegate.runtimeType || newDelegate.shouldRelayout(_delegate))
      markNeedsLayout();
Adam Barth's avatar
Adam Barth committed
773 774 775 776 777 778 779
    _delegate = newDelegate;
  }

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

780
  @override
Adam Barth's avatar
Adam Barth committed
781
  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
782
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
783 784 785
    return _getSize(constraints).width;
  }

786
  @override
Adam Barth's avatar
Adam Barth committed
787
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
788
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
789 790 791
    return _getSize(constraints).width;
  }

792
  @override
Adam Barth's avatar
Adam Barth committed
793
  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
794
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
795 796 797
    return _getSize(constraints).height;
  }

798
  @override
Adam Barth's avatar
Adam Barth committed
799
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
800
    assert(constraints.debugAssertIsNormalized);
Adam Barth's avatar
Adam Barth committed
801 802 803
    return _getSize(constraints).height;
  }

804
  @override
Adam Barth's avatar
Adam Barth committed
805 806
  bool get sizedByParent => true;

807
  @override
Adam Barth's avatar
Adam Barth committed
808 809 810 811
  void performResize() {
    size = _getSize(constraints);
  }

812
  @override
Adam Barth's avatar
Adam Barth committed
813 814
  void performLayout() {
    if (child != null) {
815
      BoxConstraints childConstraints = delegate.getConstraintsForChild(constraints);
Hixie's avatar
Hixie committed
816
      assert(childConstraints.debugAssertIsNormalized);
817
      child.layout(childConstraints, parentUsesSize: !childConstraints.isTight);
Adam Barth's avatar
Adam Barth committed
818
      final BoxParentData childParentData = child.parentData;
819
      childParentData.offset = delegate.getPositionForChild(size, childConstraints.isTight ? childConstraints.smallest : child.size);
Adam Barth's avatar
Adam Barth committed
820 821 822 823
    }
  }
}

824
/// Positions its child vertically according to the child's baseline.
825 826 827 828 829 830 831 832 833 834 835 836 837
class RenderBaseline extends RenderShiftedBox {

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

838 839
  /// The number of logical pixels from the top of this box at which to position
  /// the child's baseline.
840
  double get baseline => _baseline;
841
  double _baseline;
842 843 844 845 846 847 848 849
  void set baseline (double value) {
    assert(value != null);
    if (_baseline == value)
      return;
    _baseline = value;
    markNeedsLayout();
  }

850
  /// The type of baseline to use for positioning the child.
851
  TextBaseline get baselineType => _baselineType;
852
  TextBaseline _baselineType;
853 854 855 856 857 858 859 860
  void set baselineType (TextBaseline value) {
    assert(value != null);
    if (_baselineType == value)
      return;
    _baselineType = value;
    markNeedsLayout();
  }

861
  @override
862 863 864 865 866
  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
867
      final BoxParentData childParentData = child.parentData;
868
      childParentData.offset = new Offset(0.0, delta);
869 870 871 872 873
    } else {
      performResize();
    }
  }

874
  @override
875 876 877 878
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('baseline: $baseline');
    description.add('baselineType: $baselineType');
879
  }
880
}