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

H
Hixie 已提交
5
import 'dart:math' as math;
6
import 'dart:sky' as sky;
H
Hixie 已提交
7
import 'object.dart';
8
import '../painting/box_painter.dart';
9
import 'package:vector_math/vector_math.dart';
10
import 'package:sky/framework/net/image_cache.dart' as image_cache;
11

12 13
export '../painting/box_painter.dart';

14 15 16 17 18 19
// 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 已提交
20 21
  const EdgeDims.all(double value)
      : top = value, right = value, bottom = value, left = value;
22 23 24 25 26 27 28
  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;
29

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

35 36 37 38
  operator ==(EdgeDims other) => (top == other.top) ||
                                 (right == other.right) ||
                                 (bottom == other.bottom) ||
                                 (left == other.left);
39 40

  int get hashCode {
41
    int value = 373;
42 43 44 45 46 47 48
    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)";
49 50 51 52 53 54 55 56 57
}

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

58
  BoxConstraints.tight(Size size)
59 60 61 62 63
    : minWidth = size.width,
      maxWidth = size.width,
      minHeight = size.height,
      maxHeight = size.height;

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

70 71
  BoxConstraints deflate(EdgeDims edges) {
    assert(edges != null);
H
Hixie 已提交
72 73
    double horizontal = edges.left + edges.right;
    double vertical = edges.top + edges.bottom;
74
    return new BoxConstraints(
75
      minWidth: math.max(0.0, minWidth - horizontal),
H
Hixie 已提交
76
      maxWidth: maxWidth - horizontal,
77
      minHeight: math.max(0.0, minHeight - vertical),
H
Hixie 已提交
78
      maxHeight: maxHeight - vertical
79 80 81
    );
  }

82 83 84 85 86 87 88 89
  BoxConstraints apply(BoxConstraints constraints) {
    return new BoxConstraints(
      minWidth: math.max(minWidth, constraints.minWidth),
      maxWidth: math.min(maxWidth, constraints.maxWidth),
      minHeight: math.max(minHeight, constraints.minHeight),
      maxHeight: math.min(maxHeight, constraints.maxHeight));
  }

A
Adam Barth 已提交
90
  BoxConstraints applyWidth(double width) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    return new BoxConstraints(minWidth: math.max(minWidth, width),
                              maxWidth: math.min(maxWidth, width),
                              minHeight: minHeight,
                              maxHeight: maxHeight);
  }

  BoxConstraints applyMinWidth(double width) {
    return new BoxConstraints(minWidth: math.max(minWidth, width),
                              maxWidth: maxWidth,
                              minHeight: minHeight,
                              maxHeight: maxHeight);
  }

  BoxConstraints applyMaxWidth(double width) {
    return new BoxConstraints(minWidth: minWidth,
                              maxWidth: math.min(maxWidth, width),
A
Adam Barth 已提交
107 108 109 110 111 112 113
                              minHeight: minHeight,
                              maxHeight: maxHeight);
  }

  BoxConstraints applyHeight(double height) {
    return new BoxConstraints(minWidth: minWidth,
                              maxWidth: maxWidth,
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                              minHeight: math.max(minHeight, height),
                              maxHeight: math.min(maxHeight, height));
  }

  BoxConstraints applyMinHeight(double height) {
    return new BoxConstraints(minWidth: minWidth,
                              maxWidth: maxWidth,
                              minHeight: math.max(minHeight, height),
                              maxHeight: maxHeight);
  }

  BoxConstraints applyMaxHeight(double height) {
    return new BoxConstraints(minWidth: minWidth,
                              maxWidth: maxWidth,
                              minHeight: minHeight,
                              maxHeight: math.min(maxHeight, height));
A
Adam Barth 已提交
130 131
  }

132 133 134 135 136
  final double minWidth;
  final double maxWidth;
  final double minHeight;
  final double maxHeight;

H
Hixie 已提交
137 138 139 140 141 142 143
  static double _clamp({double min: 0.0, double value: 0.0, double max: double.INFINITY}) {
    assert(min != null);
    assert(value != null);
    assert(max != null);
    return math.max(min, math.min(max, value));
  }

144
  double constrainWidth(double width) {
H
Hixie 已提交
145
    return _clamp(min: minWidth, max: maxWidth, value: width);
146 147 148
  }

  double constrainHeight(double height) {
H
Hixie 已提交
149
    return _clamp(min: minHeight, max: maxHeight, value: height);
150 151
  }

152 153
  Size constrain(Size size) {
    return new Size(constrainWidth(size.width), constrainHeight(size.height));
154 155 156
  }

  bool get isInfinite => maxWidth >= double.INFINITY || maxHeight >= double.INFINITY;
157 158

  int get hashCode {
159
    int value = 373;
160 161 162 163 164 165 166
    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)";
167 168
}

169 170 171 172 173 174 175
class BoxHitTestEntry extends HitTestEntry {
  const BoxHitTestEntry(RenderBox target, this.localPosition)
    : super(target);

  final Point localPosition;
}

176
class BoxParentData extends ParentData {
177 178 179 180 181 182
  Point _position = Point.origin;
  Point get position => _position;
  void set position(Point value) {
    assert(RenderObject.debugDoingLayout);
    _position = value;
  }
H
Hixie 已提交
183
  String toString() => 'position=$position';
184 185
}

H
Hixie 已提交
186
abstract class RenderBox extends RenderObject {
187

H
Hixie 已提交
188
  void setParentData(RenderObject child) {
189 190 191 192
    if (child.parentData is! BoxParentData)
      child.parentData = new BoxParentData();
  }

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  // getMinIntrinsicWidth() should return the minimum width that this box could 
  // be without failing to render its contents within itself.
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(0.0);
  }

  // getMaxIntrinsicWidth() should return the smallest width beyond which 
  // increasing the width never decreases the height.
  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(0.0);
  }

  // getMinIntrinsicHeight() should return the minimum height that this box could 
  // be without failing to render its contents within itself.
  double getMinIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(0.0);
  }

  // getMaxIntrinsicHeight should return the smallest height beyond which
  // increasing the height never decreases the width.
  // If the layout algorithm used is width-in-height-out, i.e. the height
  // depends on the width and not vice versa, then this will return the same
  // as getMinIntrinsicHeight().
  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(0.0);
218 219
  }

220
  BoxConstraints get constraints { BoxConstraints result = super.constraints; return result; }
221 222
  void performResize() {
    // default behaviour for subclasses that have sizedByParent = true
A
Adam Barth 已提交
223
    size = constraints.constrain(Size.zero);
224 225 226 227 228 229 230 231 232 233
    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);
  }

234
  bool hitTest(HitTestResult result, { Point position }) {
235
    hitTestChildren(result, position: position);
236
    result.add(new BoxHitTestEntry(this, position));
237 238
    return true;
  }
239
  void hitTestChildren(HitTestResult result, { Point position }) { }
240

241 242 243 244 245 246
  Size _size = Size.zero;
  Size get size => _size;
  void set size(Size value) {
    assert(RenderObject.debugDoingLayout);
    _size = value;
  }
247 248

  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}size: ${size}\n';
249 250
}

H
Hixie 已提交
251
abstract class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
252 253 254 255
  RenderProxyBox(RenderBox child) {
    this.child = child;
  }

256
  double getMinIntrinsicWidth(BoxConstraints constraints) {
257
    if (child != null)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
      return child.getMinIntrinsicWidth(constraints);
    return super.getMinIntrinsicWidth(constraints);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicWidth(constraints);
    return super.getMaxIntrinsicWidth(constraints);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMinIntrinsicHeight(constraints);
    return super.getMinIntrinsicHeight(constraints);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicHeight(constraints);
    return super.getMaxIntrinsicHeight(constraints);
278 279 280 281 282 283 284 285 286 287 288
  }

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

289
  void hitTestChildren(HitTestResult result, { Point position }) {
290 291 292 293 294 295
    if (child != null)
      child.hitTest(result, position: position);
    else
      super.hitTestChildren(result, position: position);
  }

H
Hixie 已提交
296
  void paint(RenderObjectDisplayList canvas) {
297 298 299 300 301 302 303 304
    if (child != null)
      child.paint(canvas);
  }
}

class RenderSizedBox extends RenderProxyBox {

  RenderSizedBox({
305
    RenderBox child,
306
    Size desiredSize: Size.infinite
307
  }) : super(child), _desiredSize = desiredSize {
A
Adam Barth 已提交
308 309 310
    assert(desiredSize != null);
  }

311 312 313
  Size _desiredSize;
  Size get desiredSize => _desiredSize;
  void set desiredSize (Size value) {
A
Adam Barth 已提交
314 315 316 317 318 319
    assert(value != null);
    if (_desiredSize == value)
      return;
    _desiredSize = value;
    markNeedsLayout();
  }
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(_desiredSize.width);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    return constraints.constrainWidth(_desiredSize.width);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(_desiredSize.height);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    return constraints.constrainHeight(_desiredSize.height);
335 336 337
  }

  void performLayout() {
A
Adam Barth 已提交
338
    size = constraints.constrain(_desiredSize);
H
Hixie 已提交
339 340
    if (child != null)
      child.layout(new BoxConstraints.tight(size));
341
  }
H
Hixie 已提交
342

343
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}desiredSize: ${desiredSize}\n';
344 345
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
class RenderConstrainedBox extends RenderProxyBox {
  RenderConstrainedBox({
    RenderBox child,
    BoxConstraints additionalConstraints
  }) : super(child), _additionalConstraints = additionalConstraints {
    assert(additionalConstraints != null);
  }

  BoxConstraints _additionalConstraints;
  BoxConstraints get additionalConstraints => _additionalConstraints;
  void set additionalConstraints (BoxConstraints value) {
    assert(value != null);
    if (_additionalConstraints == value)
      return;
    _additionalConstraints = value;
    markNeedsLayout();
  }

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    if (child != null)
      return child.getMinIntrinsicWidth(constraints.apply(_additionalConstraints));
    return constraints.constrainWidth(0.0);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicWidth(constraints.apply(_additionalConstraints));
    return constraints.constrainWidth(0.0);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMinIntrinsicHeight(constraints.apply(_additionalConstraints));
    return constraints.constrainHeight(0.0);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicHeight(constraints.apply(_additionalConstraints));
    return constraints.constrainHeight(0.0);
386 387 388 389 390 391 392 393 394 395 396 397 398 399
  }

  void performLayout() {
    if (child != null) {
      child.layout(constraints.apply(_additionalConstraints));
      size = child.size;
    } else {
      performResize();
    }
  }

  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}additionalConstraints: ${additionalConstraints}\n';
}

A
Adam Barth 已提交
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
class RenderShrinkWrapWidth extends RenderProxyBox {
  RenderShrinkWrapWidth({ RenderBox child }) : super(child);

  BoxConstraints _getInnerConstraints(BoxConstraints constraints) {
    double width = child.getMaxIntrinsicWidth(constraints);
    assert(width == constraints.constrainWidth(width));
    return constraints.applyWidth(width);
  }

  double getMinIntrinsicWidth(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicWidth(constraints);
    return constraints.constrainWidth(0.0);
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicWidth(constraints);
    return constraints.constrainWidth(0.0);
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMinIntrinsicHeight(_getInnerConstraints(constraints));
    return constraints.constrainWidth(0.0);
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    if (child != null)
      return child.getMaxIntrinsicHeight(_getInnerConstraints(constraints));
    return constraints.constrainWidth(0.0);
  }

  void performLayout() {
    if (child != null) {
      child.layout(_getInnerConstraints(constraints));
      size = child.size;
    } else {
      performResize();
    }
  }
}

A
Adam Barth 已提交
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
class RenderOpacity extends RenderProxyBox {
  RenderOpacity({ RenderBox child, double opacity })
    : this._opacity = opacity, super(child) {
    assert(opacity >= 0.0 && opacity <= 1.0);
  }

  double _opacity;
  double get opacity => _opacity;
  void set opacity (double value) {
    assert(value != null);
    assert(value >= 0.0 && value <= 1.0);
    if (_opacity == value)
      return;
    _opacity = value;
    markNeedsPaint();
  }

  void paint(RenderObjectDisplayList canvas) {
    if (child != null) {
      int a = (_opacity * 255).round();

      if (a == 0)
        return;

      if (a == 255) {
        child.paint(canvas);
        return;
      }

      Paint paint = new Paint()
        ..color = new Color.fromARGB(a, 0, 0, 0)
        ..setTransferMode(sky.TransferMode.srcOverMode);
      canvas.saveLayer(null, paint);
      child.paint(canvas);
      canvas.restore();
    }
  }
}

482 483
class RenderClipRect extends RenderProxyBox {
  RenderClipRect({ RenderBox child }) : super(child);
484 485 486 487

  void paint(RenderObjectDisplayList canvas) {
    if (child != null) {
      canvas.save();
488
      canvas.clipRect(new Rect.fromSize(size));
489 490 491 492 493 494
      child.paint(canvas);
      canvas.restore();
    }
  }
}

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
class RenderClipOval extends RenderProxyBox {
  RenderClipOval({ RenderBox child }) : super(child);

  void paint(RenderObjectDisplayList canvas) {
    if (child != null) {
      Rect rect = new Rect.fromSize(size);
      canvas.saveLayer(rect, new Paint());
      Path path = new Path();
      path.addOval(rect);
      canvas.clipPath(path);
      child.paint(canvas);
      canvas.restore();
    }
  }
}

H
Hixie 已提交
511
class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
512

A
Adam Barth 已提交
513
  RenderPadding({ EdgeDims padding, RenderBox child }) {
514 515 516 517 518 519 520 521 522
    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 已提交
523 524 525 526
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
527 528
  }

529
  double getMinIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
530
    double totalPadding = padding.left + padding.right;
531
    if (child != null)
A
Adam Barth 已提交
532 533
      return child.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
534 535 536
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
537
    double totalPadding = padding.left + padding.right;
538
    if (child != null)
A
Adam Barth 已提交
539 540
      return child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
541 542 543
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
544
    double totalPadding = padding.top + padding.bottom;
545
    if (child != null)
A
Adam Barth 已提交
546 547
      return child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
548 549 550
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
551
    double totalPadding = padding.top + padding.bottom;
552
    if (child != null)
A
Adam Barth 已提交
553 554
      return child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
555 556 557 558 559 560 561
  }

  void performLayout() {
    assert(padding != null);
    BoxConstraints innerConstraints = constraints.deflate(padding);
    if (child == null) {
      size = innerConstraints.constrain(
562
          new Size(padding.left + padding.right, padding.top + padding.bottom));
563 564 565 566
      return;
    }
    child.layout(innerConstraints, parentUsesSize: true);
    assert(child.parentData is BoxParentData);
567 568
    child.parentData.position = new Point(padding.left, padding.top);
    size = constraints.constrain(new Size(padding.left + child.size.width + padding.right,
569 570 571
                                              padding.top + child.size.height + padding.bottom));
  }

H
Hixie 已提交
572
  void paint(RenderObjectDisplayList canvas) {
573 574 575 576
    if (child != null)
      canvas.paintChild(child, child.parentData.position);
  }

577
  void hitTestChildren(HitTestResult result, { Point position }) {
578 579
    if (child != null) {
      assert(child.parentData is BoxParentData);
580
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
581
      if (childBounds.contains(position)) {
582
        child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
583 584 585 586 587
                                                      position.y - child.parentData.position.y));
      }
    }
  }

588
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}padding: ${padding}\n';
589 590
}

591 592
class RenderImage extends RenderBox {

593
  RenderImage(String url, Size dimensions) {
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    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();
    });
  }

613 614 615
  Size _requestedSize;
  Size get requestedSize => _requestedSize;
  void set requestedSize (Size value) {
616 617 618 619 620 621
    if (value == _requestedSize)
      return;
    _requestedSize = value;
    markNeedsLayout();
  }

622
  Size _sizeForConstraints(BoxConstraints innerConstraints) {
623 624 625 626
    // 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;
627
      return constraints.constrain(new Size(width, height));
628 629 630 631 632 633 634
    }

    // 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) {
635
        return constraints.constrain(new Size(_image.width.toDouble(), _image.height.toDouble()));
636 637
      } else {
        double width = requestedSize.height * _image.width / _image.height;
638
        return constraints.constrain(new Size(width, requestedSize.height));
639 640 641
      }
    } else if (requestedSize.height == null) {
      double height = requestedSize.width * _image.height / _image.width;
642
      return constraints.constrain(new Size(requestedSize.width, height));
643
    } else {
644
      return constraints.constrain(requestedSize);
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
  double getMinIntrinsicWidth(BoxConstraints constraints) {
    if (requestedSize.width == null && requestedSize.height == null)
      return constraints.constrainWidth(0.0);
    return _sizeForConstraints(constraints).width;
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
    return _sizeForConstraints(constraints).width;
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
    if (requestedSize.width == null && requestedSize.height == null)
      return constraints.constrainHeight(0.0);
    return _sizeForConstraints(constraints).height;
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
    return _sizeForConstraints(constraints).height;
  }

  void performLayout() {
    size = _sizeForConstraints(constraints);
  }

H
Hixie 已提交
672
  void paint(RenderObjectDisplayList canvas) {
673 674 675 676 677 678 679 680
    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);
    }
681
    Paint paint = new Paint();
682 683 684 685
    canvas.drawImage(_image, 0.0, 0.0, paint);
    if (needsScale)
      canvas.restore();
  }
H
Hixie 已提交
686

687
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}\n';
688 689
}

690 691 692 693 694
class RenderDecoratedBox extends RenderProxyBox {

  RenderDecoratedBox({
    BoxDecoration decoration,
    RenderBox child
695
  }) : _painter = new BoxPainter(decoration), super(child);
696

697 698
  BoxPainter _painter;
  BoxDecoration get decoration => _painter.decoration;
699
  void set decoration (BoxDecoration value) {
700
    assert(value != null);
701
    if (value == _painter.decoration)
702
      return;
703
    _painter.decoration = value;
704 705 706
    markNeedsPaint();
  }

H
Hixie 已提交
707
  void paint(RenderObjectDisplayList canvas) {
708 709
    assert(size.width != null);
    assert(size.height != null);
710
    _painter.paint(canvas, new Rect.fromSize(size));
711 712
    super.paint(canvas);
  }
H
Hixie 已提交
713

714
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + "  ")}\n';
715 716 717 718 719 720 721 722 723 724 725 726 727
}

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

  Matrix4 _transform;

728
  void set transform(Matrix4 value) {
729 730 731 732 733 734 735
    assert(value != null);
    if (_transform == value)
      return;
    _transform = new Matrix4.copy(value);
    markNeedsPaint();
  }

736 737 738 739 740
  void setIdentity() {
    _transform.setIdentity();
    markNeedsPaint();
  }

741 742 743 744 745 746 747 748 749 750 751 752 753 754
  void rotateX(double radians) {
    _transform.rotateX(radians);
    markNeedsPaint();
  }

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

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

756 757 758 759 760 761 762 763 764 765
  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();
  }

766
  void hitTestChildren(HitTestResult result, { Point position }) {
767 768 769 770 771 772
    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);
773
    Point transformed = new Point(transformed3.x, transformed3.y);
774 775 776
    super.hitTestChildren(result, position: transformed);
  }

H
Hixie 已提交
777
  void paint(RenderObjectDisplayList canvas) {
778
    canvas.save();
779
    canvas.concat(_transform.storage);
780 781 782
    super.paint(canvas);
    canvas.restore();
  }
783 784 785 786 787 788

  String debugDescribeSettings(String prefix) {
    List<String> result = _transform.toString().split('\n').map((s) => '$prefix  $s\n').toList();
    result.removeLast();
    return '${super.debugDescribeSettings(prefix)}${prefix}transform matrix:\n${result.join()}';
  }
789 790
}

791
typedef void SizeChangedCallback(Size newSize);
792 793 794 795 796 797 798 799 800 801 802 803

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

  SizeChangedCallback callback;

  void performLayout() {
804
    Size oldSize = size;
805 806 807 808 809 810 811 812

    super.performLayout();

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

813
typedef void CustomPaintCallback(sky.Canvas canvas, Size size);
814 815 816 817 818 819 820 821 822 823 824 825 826

class RenderCustomPaint extends RenderProxyBox {

  RenderCustomPaint({
    CustomPaintCallback callback,
    RenderBox child
  }) : super(child) {
    assert(callback != null);
    _callback = callback;
  }

  CustomPaintCallback _callback;
  void set callback (CustomPaintCallback value) {
827
    assert(value != null || !attached);
828 829 830 831 832 833
    if (_callback == value)
      return;
    _callback = value;
    markNeedsPaint();
  }

834 835 836 837 838
  void attach() {
    assert(_callback != null);
    super.attach();
  }

839
  void paint(RenderObjectDisplayList canvas) {
840
    assert(_callback != null);
841
    _callback(canvas, size);
842 843 844 845
    super.paint(canvas);
  }
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859
// 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 已提交
860
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
861 862 863 864 865 866 867 868

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

A
Adam Barth 已提交
869
  Size _size = Size.zero;
870 871 872 873 874 875 876
  double get width => _size.width;
  double get height => _size.height;

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

877 878 879 880 881 882 883 884 885 886 887
  ViewConstraints _rootConstraints;
  ViewConstraints get rootConstraints => _rootConstraints;
  void set rootConstraints(ViewConstraints value) {
    if (_rootConstraints == value)
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

  void performLayout() {
    if (_rootConstraints.orientation != _orientation) {
888
      if (_orientation != null && child != null)
889 890
        child.rotate(oldAngle: _orientation, newAngle: _rootConstraints.orientation, time: timeForRotation);
      _orientation = _rootConstraints.orientation;
891
    }
892
    _size = new Size(_rootConstraints.width, _rootConstraints.height);
893 894
    assert(_size.height < double.INFINITY);
    assert(_size.width < double.INFINITY);
895

896 897 898 899 900 901 902 903 904 905 906
    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()
  }

907
  bool hitTest(HitTestResult result, { Point position }) {
908
    if (child != null) {
909
      Rect childBounds = new Rect.fromSize(child.size);
910 911 912
      if (childBounds.contains(position))
        child.hitTest(result, position: position);
    }
913
    result.add(new HitTestEntry(this));
914 915 916
    return true;
  }

H
Hixie 已提交
917
  void paint(RenderObjectDisplayList canvas) {
918
    if (child != null)
919
      canvas.paintChild(child, Point.origin);
920 921 922
  }

  void paintFrame() {
H
Hixie 已提交
923 924
    RenderObject.debugDoingPaint = true;
    RenderObjectDisplayList canvas = new RenderObjectDisplayList(sky.view.width, sky.view.height);
925 926
    paint(canvas);
    sky.view.picture = canvas.endRecording();
H
Hixie 已提交
927
    RenderObject.debugDoingPaint = false;
928 929 930 931 932
  }

}

// DEFAULT BEHAVIORS FOR RENDERBOX CONTAINERS
H
Hixie 已提交
933
abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, ParentDataType extends ContainerParentDataMixin<ChildType>> implements ContainerRenderObjectMixin<ChildType, ParentDataType> {
934

935
  void defaultHitTestChildren(HitTestResult result, { Point position }) {
936 937 938 939
    // 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);
940
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
941
      if (childBounds.contains(position)) {
942
        if (child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
943 944 945 946 947 948 949
                                                          position.y - child.parentData.position.y)))
          break;
      }
      child = child.parentData.previousSibling;
    }
  }

H
Hixie 已提交
950
  void defaultPaint(RenderObjectDisplayList canvas) {
951 952 953 954 955 956 957 958
    RenderBox child = firstChild;
    while (child != null) {
      assert(child.parentData is ParentDataType);
      canvas.paintChild(child, child.parentData.position);
      child = child.parentData.nextSibling;
    }
  }
}