viewport.dart 13.2 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:ui' as ui show window;
6

7
import 'package:vector_math/vector_math_64.dart';
8

9 10 11
import 'box.dart';
import 'object.dart';

12
/// The end of the viewport from which the paint offset is computed.
13
enum ViewportAnchor {
14 15 16
  /// The start (e.g., top or left, depending on the axis) of the first item
  /// should be aligned with the start (e.g., top or left, depending on the
  /// axis) of the viewport.
17
  start,
18 19 20 21

  /// The end (e.g., bottom or right, depending on the axis) of the last item
  /// should be aligned with the end (e.g., bottom or right, depending on the
  /// axis) of the viewport.
22 23 24
  end,
}

25
/// The interior and exterior dimensions of a viewport.
26 27 28 29 30 31
class ViewportDimensions {
  const ViewportDimensions({
    this.contentSize: Size.zero,
    this.containerSize: Size.zero
  });

32
  /// A viewport that has zero size, both inside and outside.
33 34
  static const ViewportDimensions zero = const ViewportDimensions();

35
  /// The size of the content inside the viewport.
36
  final Size contentSize;
37 38

  /// The size of the outside of the viewport.
39 40 41 42 43 44 45
  final Size containerSize;

  bool get _debugHasAtLeastOneCommonDimension {
    return contentSize.width == containerSize.width
        || contentSize.height == containerSize.height;
  }

46 47
  /// Returns the offset at which to paint the content, accounting for the given
  /// anchor and the dimensions of the viewport.
48 49 50 51 52 53 54 55 56
  Offset getAbsolutePaintOffset({ Offset paintOffset, ViewportAnchor anchor }) {
    assert(_debugHasAtLeastOneCommonDimension);
    switch (anchor) {
      case ViewportAnchor.start:
        return paintOffset;
      case ViewportAnchor.end:
        return paintOffset + (containerSize - contentSize);
    }
  }
57 58 59 60 61 62 63 64 65 66 67 68 69 70

  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! ViewportDimensions)
      return false;
    final ViewportDimensions typedOther = other;
    return contentSize == typedOther.contentSize &&
           containerSize == typedOther.containerSize;
  }

  int get hashCode => hashValues(contentSize, containerSize);

  String toString() => 'ViewportDimensions(container: $containerSize, content: $contentSize)';
71 72
}

73
/// An interface that indicates that an object has a scroll direction.
74
abstract class HasScrollDirection {
75
  /// Whether this object scrolls horizontally or vertically.
76
  Axis get scrollDirection;
77 78
}

79
/// A base class for render objects that are bigger on the inside.
80
///
81 82 83 84 85 86 87
/// This class holds the common fields for viewport render objects but does not
/// have a child model. See [RenderViewport] for a viewport with a single child
/// and [RenderVirtualViewport] for a viewport with multiple children.
class RenderViewportBase extends RenderBox implements HasScrollDirection {
  RenderViewportBase(
    Offset paintOffset,
    Axis scrollDirection,
88
    ViewportAnchor scrollAnchor,
89 90 91
    Painter overlayPainter
  ) : _paintOffset = paintOffset,
      _scrollDirection = scrollDirection,
92
      _scrollAnchor = scrollAnchor,
93 94 95 96
      _overlayPainter = overlayPainter {
    assert(paintOffset != null);
    assert(scrollDirection != null);
    assert(_offsetIsSane(_paintOffset, scrollDirection));
97 98
  }

99
  bool _offsetIsSane(Offset offset, Axis direction) {
100
    switch (direction) {
101
      case Axis.horizontal:
102
        return offset.dy == 0.0;
103
      case Axis.vertical:
104 105 106 107
        return offset.dx == 0.0;
    }
  }

108
  /// The offset at which to paint the child.
109 110
  ///
  /// The offset can be non-zero only in the [scrollDirection].
111 112 113 114 115
  Offset get paintOffset => _paintOffset;
  Offset _paintOffset;
  void set paintOffset(Offset value) {
    assert(value != null);
    if (value == _paintOffset)
116 117
      return;
    assert(_offsetIsSane(value, scrollDirection));
118
    _paintOffset = value;
119
    markNeedsPaint();
Hixie's avatar
Hixie committed
120
    markNeedsSemanticsUpdate();
121 122
  }

123
  /// The direction in which the child is permitted to be larger than the viewport
124 125 126 127
  ///
  /// If the viewport is scrollable in a particular direction (e.g., vertically),
  /// the child is given layout constraints that are fully unconstrainted in
  /// that direction (e.g., the child can be as tall as it wants).
128 129 130
  Axis get scrollDirection => _scrollDirection;
  Axis _scrollDirection;
  void set scrollDirection(Axis value) {
131
    assert(value != null);
132 133
    if (value == _scrollDirection)
      return;
134
    assert(_offsetIsSane(_paintOffset, value));
135 136 137 138
    _scrollDirection = value;
    markNeedsLayout();
  }

139 140 141
  /// The end of the viewport from which the paint offset is computed.
  ///
  /// See [ViewportAnchor] for more detail.
142 143 144 145 146 147 148 149 150 151 152
  ViewportAnchor get scrollAnchor => _scrollAnchor;
  ViewportAnchor _scrollAnchor;
  void set scrollAnchor(ViewportAnchor value) {
    assert(value != null);
    if (value == _scrollAnchor)
      return;
    _scrollAnchor = value;
    markNeedsPaint();
    markNeedsSemanticsUpdate();
  }

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  Painter get overlayPainter => _overlayPainter;
  Painter _overlayPainter;
  void set overlayPainter(Painter value) {
    if (_overlayPainter == value)
      return;
    if (attached)
      _overlayPainter?.detach();
    _overlayPainter = value;
    if (attached)
      _overlayPainter?.attach(this);
    markNeedsPaint();
  }

  void attach() {
    super.attach();
    _overlayPainter?.attach(this);
  }

  void detach() {
    super.detach();
    _overlayPainter?.detach();
  }

176 177 178 179 180 181 182 183
  ViewportDimensions get dimensions => _dimensions;
  ViewportDimensions _dimensions = ViewportDimensions.zero;
  void set dimensions(ViewportDimensions value) {
    assert(debugDoingThisLayout);
    _dimensions = value;
  }

  Offset get _effectivePaintOffset {
184 185 186
    final double devicePixelRatio = ui.window.devicePixelRatio;
    int dxInDevicePixels = (_paintOffset.dx * devicePixelRatio).round();
    int dyInDevicePixels = (_paintOffset.dy * devicePixelRatio).round();
187 188 189 190
    return _dimensions.getAbsolutePaintOffset(
      paintOffset: new Offset(dxInDevicePixels / devicePixelRatio, dyInDevicePixels / devicePixelRatio),
      anchor: _scrollAnchor
    );
191 192 193
  }

  void applyPaintTransform(RenderBox child, Matrix4 transform) {
194
    final Offset effectivePaintOffset = _effectivePaintOffset;
195 196 197
    super.applyPaintTransform(child, transform.translate(effectivePaintOffset.dx, effectivePaintOffset.dy));
  }

Hixie's avatar
Hixie committed
198 199 200 201 202 203 204 205
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('paintOffset: $paintOffset');
    description.add('scrollDirection: $scrollDirection');
    description.add('scrollAnchor: $scrollAnchor');
    if (overlayPainter != null)
      description.add('overlay painter: $overlayPainter');
  }
206 207
}

208 209
typedef Offset ViewportDimensionsChangeCallback(ViewportDimensions dimensions);

210 211 212 213 214 215 216 217 218 219 220 221
/// A render object that's bigger on the inside.
///
/// The child of a viewport can layout to a larger size than the viewport
/// itself. If that happens, only a portion of the child will be visible through
/// the viewport. The portion of the child that is visible is controlled by the
/// paint offset.
class RenderViewport extends RenderViewportBase with RenderObjectWithChildMixin<RenderBox> {

  RenderViewport({
    RenderBox child,
    Offset paintOffset: Offset.zero,
    Axis scrollDirection: Axis.vertical,
222
    ViewportAnchor scrollAnchor: ViewportAnchor.start,
223 224
    Painter overlayPainter,
    this.onPaintOffsetUpdateNeeded
225
  }) : super(paintOffset, scrollDirection, scrollAnchor, overlayPainter) {
226 227 228
    this.child = child;
  }

229 230 231 232
  /// Called during [layout] to report the dimensions of the viewport
  /// and its child.
  ViewportDimensionsChangeCallback onPaintOffsetUpdateNeeded;

233 234 235
  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
    BoxConstraints innerConstraints;
    switch (scrollDirection) {
236
      case Axis.horizontal:
237 238
        innerConstraints = constraints.heightConstraints();
        break;
239
      case Axis.vertical:
240 241 242 243 244 245 246
        innerConstraints = constraints.widthConstraints();
        break;
    }
    return innerConstraints;
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
247
    assert(constraints.debugAssertIsNormalized);
248
    if (child != null)
249
      return constraints.constrainWidth(child.getMinIntrinsicWidth(_getInnerConstraints(constraints)));
250 251 252 253
    return super.getMinIntrinsicWidth(constraints);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
254
    assert(constraints.debugAssertIsNormalized);
255
    if (child != null)
256
      return constraints.constrainWidth(child.getMaxIntrinsicWidth(_getInnerConstraints(constraints)));
257 258 259 260
    return super.getMaxIntrinsicWidth(constraints);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
261
    assert(constraints.debugAssertIsNormalized);
262
    if (child != null)
263
      return constraints.constrainHeight(child.getMinIntrinsicHeight(_getInnerConstraints(constraints)));
264 265 266 267
    return super.getMinIntrinsicHeight(constraints);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
Hixie's avatar
Hixie committed
268
    assert(constraints.debugAssertIsNormalized);
269
    if (child != null)
270
      return constraints.constrainHeight(child.getMaxIntrinsicHeight(_getInnerConstraints(constraints)));
271 272 273 274
    return super.getMaxIntrinsicHeight(constraints);
  }

  // We don't override computeDistanceToActualBaseline(), because we
275
  // want the default behavior (returning null). Otherwise, as you
276 277 278 279
  // scroll the RenderViewport, it would shift in its parent if the
  // parent was baseline-aligned, which makes no sense.

  void performLayout() {
280
    ViewportDimensions oldDimensions = dimensions;
281 282 283
    if (child != null) {
      child.layout(_getInnerConstraints(constraints), parentUsesSize: true);
      size = constraints.constrain(child.size);
Hixie's avatar
Hixie committed
284
      final BoxParentData childParentData = child.parentData;
285
      childParentData.offset = Offset.zero;
286
      dimensions = new ViewportDimensions(containerSize: size, contentSize: child.size);
287 288
    } else {
      performResize();
289
      dimensions = new ViewportDimensions(containerSize: size);
290
    }
291 292 293
    if (onPaintOffsetUpdateNeeded != null && dimensions != oldDimensions)
      paintOffset = onPaintOffsetUpdateNeeded(dimensions);
    assert(paintOffset != null);
294 295
  }

296
  bool _shouldClipAtPaintOffset(Offset paintOffset) {
Hixie's avatar
Hixie committed
297
    assert(child != null);
298
    return paintOffset < Offset.zero || !(Offset.zero & size).contains((paintOffset & child.size).bottomRight);
Hixie's avatar
Hixie committed
299 300
  }

301 302
  void paint(PaintingContext context, Offset offset) {
    if (child != null) {
303
      final Offset effectivePaintOffset = _effectivePaintOffset;
304 305 306 307 308 309 310 311

      void paintContents(PaintingContext context, Offset offset) {
        context.paintChild(child, offset + effectivePaintOffset);
        _overlayPainter?.paint(context, offset);
      }

      if (_shouldClipAtPaintOffset(effectivePaintOffset)) {
        context.pushClipRect(needsCompositing, offset, Point.origin & size, paintContents);
Adam Barth's avatar
Adam Barth committed
312
      } else {
313
        paintContents(context, offset);
Adam Barth's avatar
Adam Barth committed
314
      }
315 316 317
    }
  }

Hixie's avatar
Hixie committed
318
  Rect describeApproximatePaintClip(RenderObject child) {
319
    if (child != null && _shouldClipAtPaintOffset(_effectivePaintOffset))
Hixie's avatar
Hixie committed
320 321 322 323
      return Point.origin & size;
    return null;
  }

Adam Barth's avatar
Adam Barth committed
324
  bool hitTestChildren(HitTestResult result, { Point position }) {
325 326
    if (child != null) {
      assert(child.parentData is BoxParentData);
327
      Point transformed = position + -_effectivePaintOffset;
Adam Barth's avatar
Adam Barth committed
328
      return child.hitTest(result, position: transformed);
329
    }
Adam Barth's avatar
Adam Barth committed
330
    return false;
331 332
  }
}
Adam Barth's avatar
Adam Barth committed
333 334

abstract class RenderVirtualViewport<T extends ContainerBoxParentDataMixin<RenderBox>>
335 336
    extends RenderViewportBase with ContainerRenderObjectMixin<RenderBox, T>,
                                    RenderBoxContainerDefaultsMixin<RenderBox, T> {
Adam Barth's avatar
Adam Barth committed
337 338
  RenderVirtualViewport({
    int virtualChildCount,
339
    LayoutCallback callback,
340 341
    Offset paintOffset: Offset.zero,
    Axis scrollDirection: Axis.vertical,
342
    ViewportAnchor scrollAnchor: ViewportAnchor.start,
343
    Painter overlayPainter
Adam Barth's avatar
Adam Barth committed
344
  }) : _virtualChildCount = virtualChildCount,
345
       _callback = callback,
346
       super(paintOffset, scrollDirection, scrollAnchor, overlayPainter);
Adam Barth's avatar
Adam Barth committed
347

348
  int get virtualChildCount => _virtualChildCount;
Adam Barth's avatar
Adam Barth committed
349 350 351 352 353 354 355 356
  int _virtualChildCount;
  void set virtualChildCount(int value) {
    if (_virtualChildCount == value)
      return;
    _virtualChildCount = value;
    markNeedsLayout();
  }

357
  /// Called during [layout] to determine the render object's children.
Adam Barth's avatar
Adam Barth committed
358 359 360 361 362 363 364 365 366 367 368 369 370
  ///
  /// Typically the callback will mutate the child list appropriately, for
  /// example so the child list contains only visible children.
  LayoutCallback get callback => _callback;
  LayoutCallback _callback;
  void set callback(LayoutCallback value) {
    if (value == _callback)
      return;
    _callback = value;
    markNeedsLayout();
  }

  bool hitTestChildren(HitTestResult result, { Point position }) {
371
    return defaultHitTestChildren(result, position: position + -_effectivePaintOffset);
Adam Barth's avatar
Adam Barth committed
372 373 374
  }

  void _paintContents(PaintingContext context, Offset offset) {
375
    defaultPaint(context, offset + _effectivePaintOffset);
376
    _overlayPainter?.paint(context, offset);
Adam Barth's avatar
Adam Barth committed
377 378 379 380 381
  }

  void paint(PaintingContext context, Offset offset) {
    context.pushClipRect(needsCompositing, offset, Point.origin & size, _paintContents);
  }
Hixie's avatar
Hixie committed
382 383

  Rect describeApproximatePaintClip(RenderObject child) => Point.origin & size;
Hixie's avatar
Hixie committed
384 385 386 387 388

  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('virtual child count: $virtualChildCount');
  }
Adam Barth's avatar
Adam Barth committed
389
}