box.dart 33.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;
7
import 'dart:typed_data';
H
Hixie 已提交
8
import 'object.dart';
9
import '../painting/shadows.dart';
10
import 'package:vector_math/vector_math.dart';
11
import 'package:sky/framework/net/image_cache.dart' as image_cache;
12 13 14 15 16 17 18

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

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

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

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

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

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

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

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

81 82 83 84 85 86 87 88
  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 已提交
89
  BoxConstraints applyWidth(double width) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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 已提交
106 107 108 109 110 111 112
                              minHeight: minHeight,
                              maxHeight: maxHeight);
  }

  BoxConstraints applyHeight(double height) {
    return new BoxConstraints(minWidth: minWidth,
                              maxWidth: maxWidth,
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                              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 已提交
129 130
  }

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

H
Hixie 已提交
136 137 138 139 140 141 142
  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));
  }

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

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

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

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

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

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

  final Point localPosition;
}

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

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

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

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  // 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);
217 218 219 220 221
  }

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

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

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

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

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

255
  double getMinIntrinsicWidth(BoxConstraints constraints) {
256
    if (child != null)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
      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);
277 278 279 280 281 282 283 284 285 286 287
  }

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

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

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

class RenderSizedBox extends RenderProxyBox {

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

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

320 321 322 323 324 325 326 327 328 329 330 331 332 333
  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);
334 335 336
  }

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

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

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
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();
  }

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  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);
385 386 387 388 389 390 391 392 393 394 395 396 397 398
  }

  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 已提交
399 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
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();
    }
  }
}

442 443 444 445 446 447
class RenderClip extends RenderProxyBox {
  RenderClip({ RenderBox child }) : super(child);

  void paint(RenderObjectDisplayList canvas) {
    if (child != null) {
      canvas.save();
448
      canvas.clipRect(new Rect.fromSize(size));
449 450 451 452 453 454
      child.paint(canvas);
      canvas.restore();
    }
  }
}

H
Hixie 已提交
455
class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
456

A
Adam Barth 已提交
457
  RenderPadding({ EdgeDims padding, RenderBox child }) {
458 459 460 461 462 463 464 465 466
    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 已提交
467 468 469 470
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
471 472
  }

473
  double getMinIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
474
    double totalPadding = padding.left + padding.right;
475
    if (child != null)
A
Adam Barth 已提交
476 477
      return child.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
478 479 480
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
481
    double totalPadding = padding.left + padding.right;
482
    if (child != null)
A
Adam Barth 已提交
483 484
      return child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
485 486 487
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
488
    double totalPadding = padding.top + padding.bottom;
489
    if (child != null)
A
Adam Barth 已提交
490 491
      return child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
492 493 494
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
495
    double totalPadding = padding.top + padding.bottom;
496
    if (child != null)
A
Adam Barth 已提交
497 498
      return child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
499 500 501 502 503 504 505
  }

  void performLayout() {
    assert(padding != null);
    BoxConstraints innerConstraints = constraints.deflate(padding);
    if (child == null) {
      size = innerConstraints.constrain(
506
          new Size(padding.left + padding.right, padding.top + padding.bottom));
507 508 509 510
      return;
    }
    child.layout(innerConstraints, parentUsesSize: true);
    assert(child.parentData is BoxParentData);
511 512
    child.parentData.position = new Point(padding.left, padding.top);
    size = constraints.constrain(new Size(padding.left + child.size.width + padding.right,
513 514 515
                                              padding.top + child.size.height + padding.bottom));
  }

H
Hixie 已提交
516
  void paint(RenderObjectDisplayList canvas) {
517 518 519 520
    if (child != null)
      canvas.paintChild(child, child.parentData.position);
  }

521
  void hitTestChildren(HitTestResult result, { Point position }) {
522 523
    if (child != null) {
      assert(child.parentData is BoxParentData);
524
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
525
      if (childBounds.contains(position)) {
526
        child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
527 528 529 530 531
                                                      position.y - child.parentData.position.y));
      }
    }
  }

532
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}padding: ${padding}\n';
533 534
}

535 536
class RenderImage extends RenderBox {

537
  RenderImage(String url, Size dimensions) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    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();
    });
  }

557 558 559
  Size _requestedSize;
  Size get requestedSize => _requestedSize;
  void set requestedSize (Size value) {
560 561 562 563 564 565
    if (value == _requestedSize)
      return;
    _requestedSize = value;
    markNeedsLayout();
  }

566
  Size _sizeForConstraints(BoxConstraints innerConstraints) {
567 568 569 570
    // 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;
571
      return constraints.constrain(new Size(width, height));
572 573 574 575 576 577 578
    }

    // 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) {
579
        return constraints.constrain(new Size(_image.width.toDouble(), _image.height.toDouble()));
580 581
      } else {
        double width = requestedSize.height * _image.width / _image.height;
582
        return constraints.constrain(new Size(width, requestedSize.height));
583 584 585
      }
    } else if (requestedSize.height == null) {
      double height = requestedSize.width * _image.height / _image.width;
586
      return constraints.constrain(new Size(requestedSize.width, height));
587
    } else {
588
      return constraints.constrain(requestedSize);
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
  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 已提交
616
  void paint(RenderObjectDisplayList canvas) {
617 618 619 620 621 622 623 624
    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);
    }
625
    Paint paint = new Paint();
626 627 628 629
    canvas.drawImage(_image, 0.0, 0.0, paint);
    if (needsScale)
      canvas.restore();
  }
H
Hixie 已提交
630

631
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}\n';
632 633
}

634 635
class BorderSide {
  const BorderSide({
636
    this.color: const Color(0xFF000000),
637 638
    this.width: 1.0
  });
639
  final Color color;
640 641
  final double width;

642
  static const none = const BorderSide(width: 0.0);
643 644 645 646 647 648 649 650 651 652 653 654

  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({
655 656 657 658
    this.top: BorderSide.none,
    this.right: BorderSide.none,
    this.bottom: BorderSide.none,
    this.left: BorderSide.none
659
  });
H
Hixie 已提交
660

661 662 663 664 665
  const Border.all(BorderSide side) :
    top = side,
    right = side,
    bottom = side,
    left = side;
H
Hixie 已提交
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  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)';
}

683 684 685 686 687 688 689 690 691 692 693 694 695 696
class BoxShadow {
  const BoxShadow({
    this.color,
    this.offset,
    this.blur
  });

  final Color color;
  final Size offset;
  final double blur;

  String toString() => 'BoxShadow($color, $offset, $blur)';
}

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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
abstract class Gradient {
  sky.Shader createShader();
}

class LinearGradient extends Gradient {
  LinearGradient({
    this.endPoints,
    this.colors,
    this.colorStops,
    this.tileMode: sky.TileMode.clamp
  });

  String toString() =>
      'LinearGradient($endPoints, $colors, $colorStops, $tileMode)';

  sky.Shader createShader() {
    return new sky.Gradient.Linear(this.endPoints, this.colors, this.colorStops,
                                   this.tileMode);
  }

  final List<Point> endPoints;
  final List<Color> colors;
  final List<double> colorStops;
  final sky.TileMode tileMode;
}

class RadialGradient extends Gradient {
  RadialGradient({
    this.center,
    this.radius,
    this.colors,
    this.colorStops,
    this.tileMode: sky.TileMode.clamp
  });

  String toString() =>
      'RadialGradient($center, $radius, $colors, $colorStops, $tileMode)';

  sky.Shader createShader() {
    return new sky.Gradient.Radial(this.center, this.radius, this.colors,
                                   this.colorStops, this.tileMode);
  }

  final Point center;
  final double radius;
  final List<Color> colors;
  final List<double> colorStops;
  final sky.TileMode tileMode;
}

747 748
// This must be immutable, because we won't notice when it changes
class BoxDecoration {
749 750
  const BoxDecoration({
    this.backgroundColor,
751
    this.border,
752
    this.borderRadius,
753 754
    this.boxShadow,
    this.gradient
755
  });
756

757
  final Color backgroundColor;
758
  final double borderRadius;
759
  final Border border;
760
  final List<BoxShadow> boxShadow;
761
  final Gradient gradient;
H
Hixie 已提交
762 763 764 765 766 767 768

  String toString([String prefix = '']) {
    List<String> result = [];
    if (backgroundColor != null)
      result.add('${prefix}backgroundColor: $backgroundColor');
    if (border != null)
      result.add('${prefix}border: $border');
769 770 771 772
    if (borderRadius != null)
      result.add('${prefix}borderRadius: $borderRadius');
    if (boxShadow != null)
      result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toString())}');
773 774
    if (gradient != null)
      result.add('${prefix}gradient: $gradient');
H
Hixie 已提交
775 776 777 778
    if (result.isEmpty)
      return '${prefix}<no decorations specified>';
    return result.join('\n');
  }
779 780 781 782 783 784 785
}

class RenderDecoratedBox extends RenderProxyBox {

  RenderDecoratedBox({
    BoxDecoration decoration,
    RenderBox child
786 787 788
  }) : _decoration = decoration, super(child) {
    assert(_decoration != null);
  }
789 790 791 792

  BoxDecoration _decoration;
  BoxDecoration get decoration => _decoration;
  void set decoration (BoxDecoration value) {
793
    assert(value != null);
794 795 796
    if (value == _decoration)
      return;
    _decoration = value;
797
    _cachedBackgroundPaint = null;
798 799 800
    markNeedsPaint();
  }

801 802 803 804 805 806 807 808 809
  Paint _cachedBackgroundPaint;
  Paint get _backgroundPaint {
    if (_cachedBackgroundPaint == null) {
      Paint paint = new Paint();

      if (_decoration.backgroundColor != null)
        paint.color = _decoration.backgroundColor;

      if (_decoration.boxShadow != null) {
810 811 812
        var builder = new ShadowDrawLooperBuilder();
        for (BoxShadow boxShadow in _decoration.boxShadow)
          builder.addShadow(boxShadow.offset, boxShadow.color, boxShadow.blur);
813 814 815
        paint.setDrawLooper(builder.build());
      }

816 817 818
      if (_decoration.gradient != null)
        paint.setShader(_decoration.gradient.createShader());

819 820 821 822 823 824
      _cachedBackgroundPaint = paint;
    }

    return _cachedBackgroundPaint;
  }

H
Hixie 已提交
825
  void paint(RenderObjectDisplayList canvas) {
826 827 828
    assert(size.width != null);
    assert(size.height != null);

829
    if (_decoration.backgroundColor != null || _decoration.boxShadow != null ||
830
        _decoration.gradient != null) {
831 832 833 834 835 836
      Rect rect = new Rect.fromLTRB(0.0, 0.0, size.width, size.height);
      if (_decoration.borderRadius == null)
        canvas.drawRect(rect, _backgroundPaint);
      else
        canvas.drawRRect(new sky.RRect()..setRectXY(rect, _decoration.borderRadius, _decoration.borderRadius), _backgroundPaint);
    }
837 838

    if (_decoration.border != null) {
839 840
      assert(_decoration.borderRadius == null); // TODO(abarth): Implement borders with border radius.

841 842 843 844 845
      assert(_decoration.border.top != null);
      assert(_decoration.border.right != null);
      assert(_decoration.border.bottom != null);
      assert(_decoration.border.left != null);

846 847
      Paint paint = new Paint();
      Path path;
848 849

      paint.color = _decoration.border.top.color;
850
      path = new Path();
851 852 853 854 855 856 857 858
      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;
859
      path = new Path();
860 861 862 863 864 865 866 867
      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;
868
      path = new Path();
869 870 871 872 873 874 875 876
      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;
877
      path = new Path();
878 879 880 881 882 883 884 885
      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);
    }

886 887
    super.paint(canvas);
  }
H
Hixie 已提交
888

889
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${decoration.toString(prefix + "  ")}\n';
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
}

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

926 927 928 929 930 931 932 933 934 935
  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();
  }

936
  void hitTestChildren(HitTestResult result, { Point position }) {
937 938 939 940 941 942
    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);
943
    Point transformed = new Point(transformed3.x, transformed3.y);
944 945 946
    super.hitTestChildren(result, position: transformed);
  }

H
Hixie 已提交
947
  void paint(RenderObjectDisplayList canvas) {
948
    canvas.save();
949
    canvas.concat(_transform.storage);
950 951 952
    super.paint(canvas);
    canvas.restore();
  }
953 954 955 956 957 958

  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()}';
  }
959 960
}

961
typedef void SizeChangedCallback(Size newSize);
962 963 964 965 966 967 968 969 970 971 972 973

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

  SizeChangedCallback callback;

  void performLayout() {
974
    Size oldSize = size;
975 976 977 978 979 980 981 982

    super.performLayout();

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

983
typedef void CustomPaintCallback(sky.Canvas canvas);
984 985 986 987 988 989 990 991 992 993 994 995 996

class RenderCustomPaint extends RenderProxyBox {

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

  CustomPaintCallback _callback;
  void set callback (CustomPaintCallback value) {
997
    assert(value != null || !attached);
998 999 1000 1001 1002 1003
    if (_callback == value)
      return;
    _callback = value;
    markNeedsPaint();
  }

1004 1005 1006 1007 1008
  void attach() {
    assert(_callback != null);
    super.attach();
  }

1009
  void paint(RenderObjectDisplayList canvas) {
1010
    assert(_callback != null);
1011 1012 1013 1014 1015
    _callback(canvas);
    super.paint(canvas);
  }
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
// 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 已提交
1030
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
1031 1032 1033 1034 1035 1036 1037 1038

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

A
Adam Barth 已提交
1039
  Size _size = Size.zero;
1040 1041 1042 1043 1044 1045 1046
  double get width => _size.width;
  double get height => _size.height;

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

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  ViewConstraints _rootConstraints;
  ViewConstraints get rootConstraints => _rootConstraints;
  void set rootConstraints(ViewConstraints value) {
    if (_rootConstraints == value)
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

  void performLayout() {
    if (_rootConstraints.orientation != _orientation) {
1058
      if (_orientation != null && child != null)
1059 1060
        child.rotate(oldAngle: _orientation, newAngle: _rootConstraints.orientation, time: timeForRotation);
      _orientation = _rootConstraints.orientation;
1061
    }
1062
    _size = new Size(_rootConstraints.width, _rootConstraints.height);
1063 1064
    assert(_size.height < double.INFINITY);
    assert(_size.width < double.INFINITY);
1065

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    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()
  }

1077
  bool hitTest(HitTestResult result, { Point position }) {
1078
    if (child != null) {
1079
      Rect childBounds = new Rect.fromSize(child.size);
1080 1081 1082
      if (childBounds.contains(position))
        child.hitTest(result, position: position);
    }
1083
    result.add(new HitTestEntry(this));
1084 1085 1086
    return true;
  }

H
Hixie 已提交
1087
  void paint(RenderObjectDisplayList canvas) {
1088
    if (child != null)
1089
      canvas.paintChild(child, Point.origin);
1090 1091 1092
  }

  void paintFrame() {
H
Hixie 已提交
1093 1094
    RenderObject.debugDoingPaint = true;
    RenderObjectDisplayList canvas = new RenderObjectDisplayList(sky.view.width, sky.view.height);
1095 1096
    paint(canvas);
    sky.view.picture = canvas.endRecording();
H
Hixie 已提交
1097
    RenderObject.debugDoingPaint = false;
1098 1099 1100 1101 1102
  }

}

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

1105
  void defaultHitTestChildren(HitTestResult result, { Point position }) {
1106 1107 1108 1109
    // 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);
1110
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
1111
      if (childBounds.contains(position)) {
1112
        if (child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
1113 1114 1115 1116 1117 1118 1119
                                                          position.y - child.parentData.position.y)))
          break;
      }
      child = child.parentData.previousSibling;
    }
  }

H
Hixie 已提交
1120
  void defaultPaint(RenderObjectDisplayList canvas) {
1121 1122 1123 1124 1125 1126 1127 1128
    RenderBox child = firstChild;
    while (child != null) {
      assert(child.parentData is ParentDataType);
      canvas.paintChild(child, child.parentData.position);
      child = child.parentData.nextSibling;
    }
  }
}