box.dart 20.3 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.

H
Hixie 已提交
5
import 'dart:math' as math;
6
import 'dart:sky' as sky;
7
import 'dart:typed_data';
H
Hixie 已提交
8
import 'object.dart';
9
import 'package:vector_math/vector_math.dart';
10
import 'package:sky/framework/net/image_cache.dart' as image_cache;
11 12 13 14 15 16 17

// GENERIC BOX RENDERING
// Anything that has a concept of x, y, width, height is going to derive from this

class EdgeDims {
  // used for e.g. padding
  const EdgeDims(this.top, this.right, this.bottom, this.left);
A
Adam Barth 已提交
18 19
  const EdgeDims.all(double value)
      : top = value, right = value, bottom = value, left = value;
20 21 22 23 24 25 26
  const EdgeDims.only({ this.top: 0.0,
                        this.right: 0.0,
                        this.bottom: 0.0,
                        this.left: 0.0 });
  const EdgeDims.symmetric({ double vertical: 0.0,
                             double horizontal: 0.0 })
    : top = vertical, left = horizontal, bottom = vertical, right = horizontal;
27

28 29 30 31
  final double top;
  final double right;
  final double bottom;
  final double left;
A
Adam Barth 已提交
32

33 34 35 36
  operator ==(EdgeDims other) => (top == other.top) ||
                                 (right == other.right) ||
                                 (bottom == other.bottom) ||
                                 (left == other.left);
37 38 39 40 41 42 43 44 45 46

  int get hashCode {
    value = 373;
    value = 37 * value + top.hashCode;
    value = 37 * value + left.hashCode;
    value = 37 * value + bottom.hashCode;
    value = 37 * value + right.hashCode;
    return value;
  }
  String toString() => "EdgeDims($top, $right, $bottom, $left)";
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

class BoxConstraints {
  const BoxConstraints({
    this.minWidth: 0.0,
    this.maxWidth: double.INFINITY,
    this.minHeight: 0.0,
    this.maxHeight: double.INFINITY});

  BoxConstraints.tight(sky.Size size)
    : minWidth = size.width,
      maxWidth = size.width,
      minHeight = size.height,
      maxHeight = size.height;

A
Adam Barth 已提交
62 63 64 65 66 67
  BoxConstraints.loose(sky.Size size)
    : minWidth = 0.0,
      maxWidth = size.width,
      minHeight = 0.0,
      maxHeight = size.height;

68 69
  BoxConstraints deflate(EdgeDims edges) {
    assert(edges != null);
H
Hixie 已提交
70 71
    double horizontal = edges.left + edges.right;
    double vertical = edges.top + edges.bottom;
72
    return new BoxConstraints(
73
      minWidth: math.max(0.0, minWidth - horizontal),
H
Hixie 已提交
74
      maxWidth: maxWidth - horizontal,
75
      minHeight: math.max(0.0, minHeight - vertical),
H
Hixie 已提交
76
      maxHeight: maxHeight - vertical
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    );
  }

  final double minWidth;
  final double maxWidth;
  final double minHeight;
  final double maxHeight;

  double constrainWidth(double width) {
    return clamp(min: minWidth, max: maxWidth, value: width);
  }

  double constrainHeight(double height) {
    return clamp(min: minHeight, max: maxHeight, value: height);
  }

  sky.Size constrain(sky.Size size) {
    return new sky.Size(constrainWidth(size.width), constrainHeight(size.height));
  }

  bool get isInfinite => maxWidth >= double.INFINITY || maxHeight >= double.INFINITY;
98 99 100 101 102 103 104 105 106 107

  int get hashCode {
    value = 373;
    value = 37 * value + minWidth.hashCode;
    value = 37 * value + maxWidth.hashCode;
    value = 37 * value + minHeight.hashCode;
    value = 37 * value + maxHeight.hashCode;
    return value;
  }
  String toString() => "BoxConstraints($minWidth<=w<$maxWidth, $minHeight<=h<$maxHeight)";
108 109 110 111 112 113
}

class BoxParentData extends ParentData {
  sky.Point position = new sky.Point(0.0, 0.0);
}

H
Hixie 已提交
114
abstract class RenderBox extends RenderObject {
115

H
Hixie 已提交
116
  void setParentData(RenderObject child) {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if (child.parentData is! BoxParentData)
      child.parentData = new BoxParentData();
  }

  // override this to report what dimensions you would have if you
  // were laid out with the given constraints this can walk the tree
  // if it must, but it should be as cheap as possible; just get the
  // dimensions and nothing else (e.g. don't calculate hypothetical
  // child positions if they're not needed to determine dimensions)
  sky.Size getIntrinsicDimensions(BoxConstraints constraints) {
    return constraints.constrain(new sky.Size(0.0, 0.0));
  }

  BoxConstraints get constraints => super.constraints as BoxConstraints;
  void performResize() {
    // default behaviour for subclasses that have sizedByParent = true
    size = constraints.constrain(new sky.Size(0.0, 0.0));
    assert(size.height < double.INFINITY);
    assert(size.width < double.INFINITY);
  }
  void performLayout() {
    // descendants have to either override performLayout() to set both
    // width and height and lay out children, or, set sizedByParent to
    // true so that performResize()'s logic above does its thing.
    assert(sizedByParent);
  }

  bool hitTest(HitTestResult result, { sky.Point position }) {
    hitTestChildren(result, position: position);
    result.add(this);
    return true;
  }
  void hitTestChildren(HitTestResult result, { sky.Point position }) { }

  sky.Size size = new sky.Size(0.0, 0.0);
}

H
Hixie 已提交
154
abstract class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  RenderProxyBox(RenderBox child) {
    this.child = child;
  }

  sky.Size getIntrinsicDimensions(BoxConstraints constraints) {
    if (child != null)
      return child.getIntrinsicDimensions(constraints);
    return super.getIntrinsicDimensions(constraints);
  }

  void performLayout() {
    if (child != null) {
      child.layout(constraints, parentUsesSize: true);
      size = child.size;
    } else {
      performResize();
    }
  }

  void hitTestChildren(HitTestResult result, { sky.Point position }) {
    if (child != null)
      child.hitTest(result, position: position);
    else
      super.hitTestChildren(result, position: position);
  }

H
Hixie 已提交
181
  void paint(RenderObjectDisplayList canvas) {
182 183 184 185 186 187 188 189
    if (child != null)
      child.paint(canvas);
  }
}

class RenderSizedBox extends RenderProxyBox {

  RenderSizedBox({
190
    RenderBox child,
A
Adam Barth 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    sky.Size desiredSize: const sky.Size.infinite()
  }) : super(child) {
    assert(desiredSize != null);
    this.desiredSize = desiredSize;
  }

  sky.Size _desiredSize;
  sky.Size get desiredSize => _desiredSize;
  void set desiredSize (sky.Size value) {
    assert(value != null);
    if (_desiredSize == value)
      return;
    _desiredSize = value;
    markNeedsLayout();
  }
206 207

  sky.Size getIntrinsicDimensions(BoxConstraints constraints) {
A
Adam Barth 已提交
208
    return constraints.constrain(_desiredSize);
209 210 211
  }

  void performLayout() {
A
Adam Barth 已提交
212
    size = constraints.constrain(_desiredSize);
H
Hixie 已提交
213 214
    if (child != null)
      child.layout(new BoxConstraints.tight(size));
215 216 217
  }
}

218 219 220 221 222 223 224 225 226 227 228 229 230
class RenderClip extends RenderProxyBox {
  RenderClip({ RenderBox child }) : super(child);

  void paint(RenderObjectDisplayList canvas) {
    if (child != null) {
      canvas.save();
      canvas.clipRect(new sky.Rect.fromSize(size));
      child.paint(canvas);
      canvas.restore();
    }
  }
}

H
Hixie 已提交
231
class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
232

A
Adam Barth 已提交
233
  RenderPadding({ EdgeDims padding, RenderBox child }) {
234 235 236 237 238 239 240 241 242
    assert(padding != null);
    this.padding = padding;
    this.child = child;
  }

  EdgeDims _padding;
  EdgeDims get padding => _padding;
  void set padding (EdgeDims value) {
    assert(value != null);
A
Adam Barth 已提交
243 244 245 246
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  }

  sky.Size getIntrinsicDimensions(BoxConstraints constraints) {
    assert(padding != null);
    constraints = constraints.deflate(padding);
    if (child == null)
      return super.getIntrinsicDimensions(constraints);
    return child.getIntrinsicDimensions(constraints);
  }

  void performLayout() {
    assert(padding != null);
    BoxConstraints innerConstraints = constraints.deflate(padding);
    if (child == null) {
      size = innerConstraints.constrain(
          new sky.Size(padding.left + padding.right, padding.top + padding.bottom));
      return;
    }
    child.layout(innerConstraints, parentUsesSize: true);
    assert(child.parentData is BoxParentData);
    child.parentData.position = new sky.Point(padding.left, padding.top);
    size = constraints.constrain(new sky.Size(padding.left + child.size.width + padding.right,
                                              padding.top + child.size.height + padding.bottom));
  }

H
Hixie 已提交
272
  void paint(RenderObjectDisplayList canvas) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    if (child != null)
      canvas.paintChild(child, child.parentData.position);
  }

  void hitTestChildren(HitTestResult result, { sky.Point position }) {
    if (child != null) {
      assert(child.parentData is BoxParentData);
      sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.position, child.size);
      if (childBounds.contains(position)) {
        child.hitTest(result, position: new sky.Point(position.x - child.parentData.position.x,
                                                      position.y - child.parentData.position.y));
      }
    }
  }

}

290 291 292 293 294 295 296 297 298 299 300 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
class RenderImage extends RenderBox {

  RenderImage(String url, sky.Size dimensions) {
    requestedSize = dimensions;
    src = url;
  }

  sky.Image _image;
  String _src;
  String get src => _src;
  void set src (String value) {
    if (value == _src)
      return;
    _src = value;
    image_cache.load(_src, (result) {
      _image = result;
      if (requestedSize.width == null || requestedSize.height == null)
        markNeedsLayout();
      markNeedsPaint();
    });
  }

  sky.Size _requestedSize;
  sky.Size get requestedSize => _requestedSize;
  void set requestedSize (sky.Size value) {
    if (value == _requestedSize)
      return;
    _requestedSize = value;
    markNeedsLayout();
  }

  void performLayout() {
    // If there's no image, we can't size ourselves automatically
    if (_image == null) {
      double width = requestedSize.width == null ? 0.0 : requestedSize.width;
      double height = requestedSize.height == null ? 0.0 : requestedSize.height;
      size = constraints.constrain(new sky.Size(width, height));
      return;
    }

    // If neither height nor width are specified, use inherent image dimensions
    // If only one dimension is specified, adjust the other dimension to
    // maintain the aspect ratio
    if (requestedSize.width == null) {
      if (requestedSize.height == null) {
        size = constraints.constrain(new sky.Size(_image.width, _image.height));
      } else {
        double width = requestedSize.height * _image.width / _image.height;
        size = constraints.constrain(new sky.Size(width, requestedSize.height));
      }
    } else if (requestedSize.height == null) {
      double height = requestedSize.width * _image.height / _image.width;
      size = constraints.constrain(new sky.Size(requestedSize.width, height));
    } else {
      size = constraints.constrain(requestedSize);
    }
  }

H
Hixie 已提交
348
  void paint(RenderObjectDisplayList canvas) {
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    if (_image == null) return;
    bool needsScale = size.width != _image.width || size.height != _image.height;
    if (needsScale) {
      double widthScale = size.width / _image.width;
      double heightScale = size.height / _image.height;
      canvas.save();
      canvas.scale(widthScale, heightScale);
    }
    sky.Paint paint = new sky.Paint();
    canvas.drawImage(_image, 0.0, 0.0, paint);
    if (needsScale)
      canvas.restore();
  }
}

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 399 400 401 402 403 404 405 406 407 408 409 410
class BorderSide {
  const BorderSide({
    this.color: const sky.Color(0xFF000000),
    this.width: 1.0
  });
  final sky.Color color;
  final double width;

  static const None = const BorderSide(width: 0.0);

  int get hashCode {
    int value = 373;
    value = 37 * value * color.hashCode;
    value = 37 * value * width.hashCode;
    return value;
  }
  String toString() => 'BorderSide($color, $width)';
}

class Border {
  const Border({
    this.top: BorderSide.None,
    this.right: BorderSide.None,
    this.bottom: BorderSide.None,
    this.left: BorderSide.None
  });
  const Border.all(BorderSide side) :
    top = side,
    right = side,
    bottom = side,
    left = side;
  final BorderSide top;
  final BorderSide right;
  final BorderSide bottom;
  final BorderSide left;

  int get hashCode {
    int value = 373;
    value = 37 * value * top.hashCode;
    value = 37 * value * right.hashCode;
    value = 37 * value * bottom.hashCode;
    value = 37 * value * left.hashCode;
    return value;
  }
  String toString() => 'Border($top, $right, $bottom, $left)';
}

411 412
// This must be immutable, because we won't notice when it changes
class BoxDecoration {
413 414 415 416
  const BoxDecoration({
    this.backgroundColor,
    this.border
  });
417

418
  final sky.Color backgroundColor;
419
  final Border border;
420 421 422 423 424 425 426
}

class RenderDecoratedBox extends RenderProxyBox {

  RenderDecoratedBox({
    BoxDecoration decoration,
    RenderBox child
427 428 429
  }) : _decoration = decoration, super(child) {
    assert(_decoration != null);
  }
430 431 432 433

  BoxDecoration _decoration;
  BoxDecoration get decoration => _decoration;
  void set decoration (BoxDecoration value) {
434
    assert(value != null);
435 436 437 438 439 440
    if (value == _decoration)
      return;
    _decoration = value;
    markNeedsPaint();
  }

H
Hixie 已提交
441
  void paint(RenderObjectDisplayList canvas) {
442 443 444 445 446 447 448
    assert(size.width != null);
    assert(size.height != null);

    if (_decoration.backgroundColor != null) {
      sky.Paint paint = new sky.Paint()..color = _decoration.backgroundColor;
      canvas.drawRect(new sky.Rect.fromLTRB(0.0, 0.0, size.width, size.height), paint);
    }
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 495

    if (_decoration.border != null) {
      assert(_decoration.border.top != null);
      assert(_decoration.border.right != null);
      assert(_decoration.border.bottom != null);
      assert(_decoration.border.left != null);

      sky.Paint paint = new sky.Paint();
      sky.Path path;

      paint.color = _decoration.border.top.color;
      path = new sky.Path();
      path.moveTo(0.0,0.0);
      path.lineTo(_decoration.border.left.width, _decoration.border.top.width);
      path.lineTo(size.width - _decoration.border.right.width, _decoration.border.top.width);
      path.lineTo(size.width, 0.0);
      path.close();
      canvas.drawPath(path, paint);

      paint.color = _decoration.border.right.color;
      path = new sky.Path();
      path.moveTo(size.width, 0.0);
      path.lineTo(size.width - _decoration.border.right.width, _decoration.border.top.width);
      path.lineTo(size.width - _decoration.border.right.width, size.height - _decoration.border.bottom.width);
      path.lineTo(size.width, size.height);
      path.close();
      canvas.drawPath(path, paint);

      paint.color = _decoration.border.bottom.color;
      path = new sky.Path();
      path.moveTo(size.width, size.height);
      path.lineTo(size.width - _decoration.border.right.width, size.height - _decoration.border.bottom.width);
      path.lineTo(_decoration.border.left.width, size.height - _decoration.border.bottom.width);
      path.lineTo(0.0, size.height);
      path.close();
      canvas.drawPath(path, paint);

      paint.color = _decoration.border.left.color;
      path = new sky.Path();
      path.moveTo(0.0, size.height);
      path.lineTo(_decoration.border.left.width, size.height - _decoration.border.bottom.width);
      path.lineTo(_decoration.border.left.width, _decoration.border.top.width);
      path.lineTo(0.0,0.0);
      path.close();
      canvas.drawPath(path, paint);
    }

496 497
    super.paint(canvas);
  }
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
}

class RenderTransform extends RenderProxyBox {
  RenderTransform({
    Matrix4 transform,
    RenderBox child
  }) : super(child) {
    assert(transform != null);
    this.transform = transform;
  }

  Matrix4 _transform;

  void set transform (Matrix4 value) {
    assert(value != null);
    if (_transform == value)
      return;
    _transform = new Matrix4.copy(value);
    markNeedsPaint();
  }

  void rotateX(double radians) {
    _transform.rotateX(radians);
    markNeedsPaint();
  }

  void rotateY(double radians) {
    _transform.rotateY(radians);
    markNeedsPaint();
  }

  void rotateZ(double radians) {
    _transform.rotateZ(radians);
    markNeedsPaint();
  }
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  void translate(x, [double y = 0.0, double z = 0.0]) {
    _transform.translate(x, y, z);
    markNeedsPaint();
  }

  void scale(x, [double y, double z]) {
    _transform.scale(x, y, z);
    markNeedsPaint();
  }

  void hitTestChildren(HitTestResult result, { sky.Point position }) {
    Matrix4 inverse = new Matrix4.zero();
    double det = inverse.copyInverse(_transform);
    // TODO(abarth): Check the determinant for degeneracy.

    Vector3 position3 = new Vector3(position.x, position.y, 0.0);
    Vector3 transformed3 = inverse.transform3(position3);
    sky.Point transformed = new sky.Point(transformed3.x, transformed3.y);
    super.hitTestChildren(result, position: transformed);
  }

H
Hixie 已提交
555
  void paint(RenderObjectDisplayList canvas) {
556
    canvas.save();
557
    canvas.concat(_transform.storage);
558 559 560
    super.paint(canvas);
    canvas.restore();
  }
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
typedef void SizeChangedCallback(sky.Size newSize);

class RenderSizeObserver extends RenderProxyBox {
  RenderSizeObserver({
    this.callback,
    RenderBox child
  }) : super(child) {
    assert(callback != null);
  }

  SizeChangedCallback callback;

  void performLayout() {
    sky.Size oldSize = size;

    super.performLayout();

    if (oldSize != size)
      callback(size);
  }
}

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

// RENDER VIEW LAYOUT MANAGER

class ViewConstraints {

  const ViewConstraints({
    this.width: 0.0, this.height: 0.0, this.orientation: null
  });

  final double width;
  final double height;
  final int orientation;

}

H
Hixie 已提交
600
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
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

  RenderView({
    RenderBox child,
    this.timeForRotation: const Duration(microseconds: 83333)
  }) {
    this.child = child;
  }

  sky.Size _size = new sky.Size(0.0, 0.0);
  double get width => _size.width;
  double get height => _size.height;

  int _orientation; // 0..3
  int get orientation => _orientation;
  Duration timeForRotation;

  ViewConstraints get constraints => super.constraints as ViewConstraints;
  bool get sizedByParent => true;
  void performResize() {
    if (constraints.orientation != _orientation) {
      if (_orientation != null && child != null)
        child.rotate(oldAngle: _orientation, newAngle: constraints.orientation, time: timeForRotation);
      _orientation = constraints.orientation;
    }
    _size = new sky.Size(constraints.width, constraints.height);
    assert(_size.height < double.INFINITY);
    assert(_size.width < double.INFINITY);
  }
  void performLayout() {
    if (child != null) {
      child.layout(new BoxConstraints.tight(_size));
      assert(child.size.width == width);
      assert(child.size.height == height);
    }
  }

  void rotate({ int oldAngle, int newAngle, Duration time }) {
    assert(false); // nobody tells the screen to rotate, the whole rotate() dance is started from our performResize()
  }

  bool hitTest(HitTestResult result, { sky.Point position }) {
    if (child != null) {
      sky.Rect childBounds = new sky.Rect.fromSize(child.size);
      if (childBounds.contains(position))
        child.hitTest(result, position: position);
    }
    result.add(this);
    return true;
  }

H
Hixie 已提交
651
  void paint(RenderObjectDisplayList canvas) {
652 653 654 655 656
    if (child != null)
      canvas.paintChild(child, new sky.Point(0.0, 0.0));
  }

  void paintFrame() {
H
Hixie 已提交
657 658
    RenderObject.debugDoingPaint = true;
    RenderObjectDisplayList canvas = new RenderObjectDisplayList(sky.view.width, sky.view.height);
659 660
    paint(canvas);
    sky.view.picture = canvas.endRecording();
H
Hixie 已提交
661
    RenderObject.debugDoingPaint = false;
662 663 664 665 666
  }

}

// DEFAULT BEHAVIORS FOR RENDERBOX CONTAINERS
H
Hixie 已提交
667
abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, ParentDataType extends ContainerParentDataMixin<ChildType>> implements ContainerRenderObjectMixin<ChildType, ParentDataType> {
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

  void defaultHitTestChildren(HitTestResult result, { sky.Point position }) {
    // the x, y parameters have the top left of the node's box as the origin
    ChildType child = lastChild;
    while (child != null) {
      assert(child.parentData is ParentDataType);
      sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.position, child.size);
      if (childBounds.contains(position)) {
        if (child.hitTest(result, position: new sky.Point(position.x - child.parentData.position.x,
                                                          position.y - child.parentData.position.y)))
          break;
      }
      child = child.parentData.previousSibling;
    }
  }

H
Hixie 已提交
684
  void defaultPaint(RenderObjectDisplayList canvas) {
685 686 687 688 689 690 691 692
    RenderBox child = firstChild;
    while (child != null) {
      assert(child.parentData is ParentDataType);
      canvas.paintChild(child, child.parentData.position);
      child = child.parentData.nextSibling;
    }
  }
}