block.dart 7.83 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'dart:math' as math;

7 8
import 'box.dart';
import 'object.dart';
9

Florian Loitsch's avatar
Florian Loitsch committed
10
/// Parent data for use with [RenderBlockBase].
Hixie's avatar
Hixie committed
11
class BlockParentData extends ContainerBoxParentDataMixin<RenderBox> { }
12

13
typedef double _ChildSizingFunction(RenderBox child);
14

Florian Loitsch's avatar
Florian Loitsch committed
15
/// Implements the block layout algorithm.
16 17 18 19 20 21 22 23
///
/// In block layout, children are arranged linearly along the main axis (either
/// horizontally or vertically). In the cross axis, children are stretched to
/// match the block's cross-axis extent. In the main axis, children are given
/// unlimited space and the block expands its main axis to contain all its
/// children. Because blocks expand in the main axis, blocks must be given
/// unlimited space in the main axis, typically by being contained in a
/// viewport with a scrolling direction that matches the block's main axis.
24
class RenderBlock extends RenderBox
25
    with ContainerRenderObjectMixin<RenderBox, BlockParentData>,
26
         RenderBoxContainerDefaultsMixin<RenderBox, BlockParentData> {
27 28 29
  /// Creates a block render object.
  ///
  /// By default, the block positions children along the vertical axis.
30
  RenderBlock({
31
    List<RenderBox> children,
32 33
    Axis mainAxis: Axis.vertical
  }) : _mainAxis = mainAxis {
34 35 36
    addAll(children);
  }

37
  @override
38 39 40 41 42
  void setupParentData(RenderBox child) {
    if (child.parentData is! BlockParentData)
      child.parentData = new BlockParentData();
  }

Florian Loitsch's avatar
Florian Loitsch committed
43
  /// The direction to use as the main axis.
44 45
  Axis get mainAxis => _mainAxis;
  Axis _mainAxis;
46
  set mainAxis (Axis value) {
47 48
    if (_mainAxis != value) {
      _mainAxis = value;
49 50 51 52 53
      markNeedsLayout();
    }
  }

  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
54 55
    switch (_mainAxis) {
      case Axis.horizontal:
56
        return new BoxConstraints.tightFor(height: constraints.maxHeight);
57
      case Axis.vertical:
58
        return new BoxConstraints.tightFor(width: constraints.maxWidth);
59
    }
60 61
  }

62 63 64
  double get _mainAxisExtent {
    RenderBox child = lastChild;
    if (child == null)
65
      return 0.0;
66
    BoxParentData parentData = child.parentData;
67 68 69 70 71 72
    switch (mainAxis) {
      case Axis.horizontal:
        return parentData.offset.dx + child.size.width;
      case Axis.vertical:
        return parentData.offset.dy + child.size.height;
    }
73 74
  }

75
  @override
76
  void performLayout() {
77 78 79
    assert(() {
      switch (mainAxis) {
        case Axis.horizontal:
80
          if (!constraints.hasBoundedWidth)
81 82 83
            return true;
          break;
        case Axis.vertical:
84
          if (!constraints.hasBoundedHeight)
85 86 87 88 89 90 91 92 93 94 95 96
            return true;
          break;
      }
      throw new FlutterError(
        'RenderBlock must have unlimited space along its main axis.\n'
        'RenderBlock does not clip or resize its children, so it must be '
        'placed in a parent that does not constrain the block\'s main '
        'axis. You probably want to put the RenderBlock inside a '
        'RenderViewport with a matching main axis.'
      );
      return false;
    });
97 98 99
    assert(() {
      switch (mainAxis) {
        case Axis.horizontal:
100
          if (constraints.hasBoundedHeight)
101 102 103
            return true;
          break;
        case Axis.vertical:
104
          if (constraints.hasBoundedWidth)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            return true;
          break;
      }
      // TODO(ianh): Detect if we're actually nested blocks and say something
      // more specific to the exact situation in that case, and don't mention
      // nesting blocks in the negative case.
      throw new FlutterError(
        'RenderBlock must have a bounded constraint for its cross axis.\n'
        'RenderBlock forces its children to expand to fit the block\'s container, '
        'so it must be placed in a parent that does constrain the block\'s cross '
        'axis to a finite dimension. If you are attempting to nest a block with '
        'one direction inside a block of another direction, you will want to '
        'wrap the inner one inside a box that fixes the dimension in that direction, '
        'for example, a RenderIntrinsicWidth or RenderIntrinsicHeight object. '
        'This is relatively expensive, however.' // (that's why we don't do it automatically)
      );
      return false;
    });
123 124
    BoxConstraints innerConstraints = _getInnerConstraints(constraints);
    double position = 0.0;
125 126 127
    RenderBox child = firstChild;
    while (child != null) {
      child.layout(innerConstraints, parentUsesSize: true);
Hixie's avatar
Hixie committed
128
      final BlockParentData childParentData = child.parentData;
129 130 131 132 133 134 135 136 137 138
      switch (mainAxis) {
        case Axis.horizontal:
          childParentData.offset = new Offset(position, 0.0);
          position += child.size.width;
          break;
        case Axis.vertical:
          childParentData.offset = new Offset(0.0, position);
          position += child.size.height;
          break;
      }
Hixie's avatar
Hixie committed
139 140
      assert(child.parentData == childParentData);
      child = childParentData.nextSibling;
141
    }
142 143 144 145 146 147 148 149 150
    switch (mainAxis) {
      case Axis.horizontal:
        size = constraints.constrain(new Size(_mainAxisExtent, constraints.maxHeight));
        break;
      case Axis.vertical:
        size = constraints.constrain(new Size(constraints.maxWidth, _mainAxisExtent));
        break;
    }

151
    assert(!size.isInfinite);
152 153
  }

154
  @override
155 156
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
157
    description.add('mainAxis: $mainAxis');
158
  }
159

160
  double _getIntrinsicCrossAxis(_ChildSizingFunction childSize) {
161
    double extent = 0.0;
162 163
    RenderBox child = firstChild;
    while (child != null) {
164
      extent = math.max(extent, childSize(child));
Hixie's avatar
Hixie committed
165 166
      final BlockParentData childParentData = child.parentData;
      child = childParentData.nextSibling;
167
    }
168
    return extent;
169 170
  }

171
  double _getIntrinsicMainAxis(_ChildSizingFunction childSize) {
172
    double extent = 0.0;
173 174
    RenderBox child = firstChild;
    while (child != null) {
175
      extent += childSize(child);
Hixie's avatar
Hixie committed
176 177
      final BlockParentData childParentData = child.parentData;
      child = childParentData.nextSibling;
178
    }
179
    return extent;
180 181
  }

182
  @override
183
  double getMinIntrinsicWidth(double height) {
184 185
    switch (mainAxis) {
      case Axis.horizontal:
186
        return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicWidth(height));
187
      case Axis.vertical:
188
        return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicWidth(height));
189 190 191
    }
  }

192
  @override
193
  double getMaxIntrinsicWidth(double height) {
194 195
    switch (mainAxis) {
      case Axis.horizontal:
196
        return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicWidth(height));
197
      case Axis.vertical:
198
        return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicWidth(height));
199 200 201
    }
  }

202
  @override
203
  double getMinIntrinsicHeight(double width) {
204 205
    switch (mainAxis) {
      case Axis.horizontal:
206
        return _getIntrinsicMainAxis((RenderBox child) => child.getMinIntrinsicHeight(width));
207
      case Axis.vertical:
208
        return _getIntrinsicCrossAxis((RenderBox child) => child.getMinIntrinsicHeight(width));
209
    }
210 211
  }

212
  @override
213
  double getMaxIntrinsicHeight(double width) {
214 215
    switch (mainAxis) {
      case Axis.horizontal:
216
        return _getIntrinsicMainAxis((RenderBox child) => child.getMaxIntrinsicHeight(width));
217
      case Axis.vertical:
218
        return _getIntrinsicCrossAxis((RenderBox child) => child.getMaxIntrinsicHeight(width));
219
    }
220 221
  }

222
  @override
223 224 225 226
  double computeDistanceToActualBaseline(TextBaseline baseline) {
    return defaultComputeDistanceToFirstActualBaseline(baseline);
  }

227
  @override
228 229
  void paint(PaintingContext context, Offset offset) {
    defaultPaint(context, offset);
230 231
  }

232
  @override
Adam Barth's avatar
Adam Barth committed
233 234
  bool hitTestChildren(HitTestResult result, { Point position }) {
    return defaultHitTestChildren(result, position: position);
235 236 237
  }

}