box.dart 34.4 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
  BoxConstraints get constraints { BoxConstraints result = super.constraints; return result; }
220 221
  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
class RenderClipRect extends RenderProxyBox {
  RenderClipRect({ RenderBox child }) : super(child);
444 445 446 447

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
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 已提交
471
class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
472

A
Adam Barth 已提交
473
  RenderPadding({ EdgeDims padding, RenderBox child }) {
474 475 476 477 478 479 480 481 482
    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 已提交
483 484 485 486
    if (_padding == value)
      return;
    _padding = value;
    markNeedsLayout();
487 488
  }

489
  double getMinIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
490
    double totalPadding = padding.left + padding.right;
491
    if (child != null)
A
Adam Barth 已提交
492 493
      return child.getMinIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
494 495 496
  }

  double getMaxIntrinsicWidth(BoxConstraints constraints) {
A
Adam Barth 已提交
497
    double totalPadding = padding.left + padding.right;
498
    if (child != null)
A
Adam Barth 已提交
499 500
      return child.getMaxIntrinsicWidth(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainWidth(totalPadding);
501 502 503
  }

  double getMinIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
504
    double totalPadding = padding.top + padding.bottom;
505
    if (child != null)
A
Adam Barth 已提交
506 507
      return child.getMinIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
508 509 510
  }

  double getMaxIntrinsicHeight(BoxConstraints constraints) {
A
Adam Barth 已提交
511
    double totalPadding = padding.top + padding.bottom;
512
    if (child != null)
A
Adam Barth 已提交
513 514
      return child.getMaxIntrinsicHeight(constraints.deflate(padding)) + totalPadding;
    return constraints.constrainHeight(totalPadding);
515 516 517 518 519 520 521
  }

  void performLayout() {
    assert(padding != null);
    BoxConstraints innerConstraints = constraints.deflate(padding);
    if (child == null) {
      size = innerConstraints.constrain(
522
          new Size(padding.left + padding.right, padding.top + padding.bottom));
523 524 525 526
      return;
    }
    child.layout(innerConstraints, parentUsesSize: true);
    assert(child.parentData is BoxParentData);
527 528
    child.parentData.position = new Point(padding.left, padding.top);
    size = constraints.constrain(new Size(padding.left + child.size.width + padding.right,
529 530 531
                                              padding.top + child.size.height + padding.bottom));
  }

H
Hixie 已提交
532
  void paint(RenderObjectDisplayList canvas) {
533 534 535 536
    if (child != null)
      canvas.paintChild(child, child.parentData.position);
  }

537
  void hitTestChildren(HitTestResult result, { Point position }) {
538 539
    if (child != null) {
      assert(child.parentData is BoxParentData);
540
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
541
      if (childBounds.contains(position)) {
542
        child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
543 544 545 546 547
                                                      position.y - child.parentData.position.y));
      }
    }
  }

548
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}padding: ${padding}\n';
549 550
}

551 552
class RenderImage extends RenderBox {

553
  RenderImage(String url, Size dimensions) {
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    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();
    });
  }

573 574 575
  Size _requestedSize;
  Size get requestedSize => _requestedSize;
  void set requestedSize (Size value) {
576 577 578 579 580 581
    if (value == _requestedSize)
      return;
    _requestedSize = value;
    markNeedsLayout();
  }

582
  Size _sizeForConstraints(BoxConstraints innerConstraints) {
583 584 585 586
    // 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;
587
      return constraints.constrain(new Size(width, height));
588 589 590 591 592 593 594
    }

    // 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) {
595
        return constraints.constrain(new Size(_image.width.toDouble(), _image.height.toDouble()));
596 597
      } else {
        double width = requestedSize.height * _image.width / _image.height;
598
        return constraints.constrain(new Size(width, requestedSize.height));
599 600 601
      }
    } else if (requestedSize.height == null) {
      double height = requestedSize.width * _image.height / _image.width;
602
      return constraints.constrain(new Size(requestedSize.width, height));
603
    } else {
604
      return constraints.constrain(requestedSize);
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
  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 已提交
632
  void paint(RenderObjectDisplayList canvas) {
633 634 635 636 637 638 639 640
    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);
    }
641
    Paint paint = new Paint();
642 643 644 645
    canvas.drawImage(_image, 0.0, 0.0, paint);
    if (needsScale)
      canvas.restore();
  }
H
Hixie 已提交
646

647
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}\n';
648 649
}

650 651
class BorderSide {
  const BorderSide({
652
    this.color: const Color(0xFF000000),
653 654
    this.width: 1.0
  });
655
  final Color color;
656 657
  final double width;

658
  static const none = const BorderSide(width: 0.0);
659 660 661 662 663 664 665 666 667 668 669 670

  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({
671 672 673 674
    this.top: BorderSide.none,
    this.right: BorderSide.none,
    this.bottom: BorderSide.none,
    this.left: BorderSide.none
675
  });
H
Hixie 已提交
676

677 678 679 680 681
  const Border.all(BorderSide side) :
    top = side,
    right = side,
    bottom = side,
    left = side;
H
Hixie 已提交
682

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  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)';
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712
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)';
}

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 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
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;
}

763 764
// This must be immutable, because we won't notice when it changes
class BoxDecoration {
765 766
  const BoxDecoration({
    this.backgroundColor,
767
    this.border,
768
    this.borderRadius,
769 770
    this.boxShadow,
    this.gradient
771
  });
772

773
  final Color backgroundColor;
774
  final double borderRadius;
775
  final Border border;
776
  final List<BoxShadow> boxShadow;
777
  final Gradient gradient;
H
Hixie 已提交
778 779 780 781 782 783 784

  String toString([String prefix = '']) {
    List<String> result = [];
    if (backgroundColor != null)
      result.add('${prefix}backgroundColor: $backgroundColor');
    if (border != null)
      result.add('${prefix}border: $border');
785 786 787 788
    if (borderRadius != null)
      result.add('${prefix}borderRadius: $borderRadius');
    if (boxShadow != null)
      result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toString())}');
789 790
    if (gradient != null)
      result.add('${prefix}gradient: $gradient');
H
Hixie 已提交
791 792 793 794
    if (result.isEmpty)
      return '${prefix}<no decorations specified>';
    return result.join('\n');
  }
795 796 797 798 799 800 801
}

class RenderDecoratedBox extends RenderProxyBox {

  RenderDecoratedBox({
    BoxDecoration decoration,
    RenderBox child
802 803 804
  }) : _decoration = decoration, super(child) {
    assert(_decoration != null);
  }
805 806 807 808

  BoxDecoration _decoration;
  BoxDecoration get decoration => _decoration;
  void set decoration (BoxDecoration value) {
809
    assert(value != null);
810 811 812
    if (value == _decoration)
      return;
    _decoration = value;
813
    _cachedBackgroundPaint = null;
814 815 816
    markNeedsPaint();
  }

817 818 819 820 821 822 823 824 825
  Paint _cachedBackgroundPaint;
  Paint get _backgroundPaint {
    if (_cachedBackgroundPaint == null) {
      Paint paint = new Paint();

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

      if (_decoration.boxShadow != null) {
826 827 828
        var builder = new ShadowDrawLooperBuilder();
        for (BoxShadow boxShadow in _decoration.boxShadow)
          builder.addShadow(boxShadow.offset, boxShadow.color, boxShadow.blur);
829 830 831
        paint.setDrawLooper(builder.build());
      }

832 833 834
      if (_decoration.gradient != null)
        paint.setShader(_decoration.gradient.createShader());

835 836 837 838 839 840
      _cachedBackgroundPaint = paint;
    }

    return _cachedBackgroundPaint;
  }

H
Hixie 已提交
841
  void paint(RenderObjectDisplayList canvas) {
842 843 844
    assert(size.width != null);
    assert(size.height != null);

845
    if (_decoration.backgroundColor != null || _decoration.boxShadow != null ||
846
        _decoration.gradient != null) {
847 848 849 850 851 852
      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);
    }
853 854

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

857 858 859 860 861
      assert(_decoration.border.top != null);
      assert(_decoration.border.right != null);
      assert(_decoration.border.bottom != null);
      assert(_decoration.border.left != null);

862 863
      Paint paint = new Paint();
      Path path;
864 865

      paint.color = _decoration.border.top.color;
866
      path = new Path();
867 868 869 870 871 872 873 874
      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;
875
      path = new Path();
876 877 878 879 880 881 882 883
      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;
884
      path = new Path();
885 886 887 888 889 890 891 892
      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;
893
      path = new Path();
894 895 896 897 898 899 900 901
      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);
    }

902 903
    super.paint(canvas);
  }
H
Hixie 已提交
904

905
  String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${decoration.toString(prefix + "  ")}\n';
906 907 908 909 910 911 912 913 914 915 916 917 918
}

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

  Matrix4 _transform;

919
  void set transform(Matrix4 value) {
920 921 922 923 924 925 926
    assert(value != null);
    if (_transform == value)
      return;
    _transform = new Matrix4.copy(value);
    markNeedsPaint();
  }

927 928 929 930 931
  void setIdentity() {
    _transform.setIdentity();
    markNeedsPaint();
  }

932 933 934 935 936 937 938 939 940 941 942 943 944 945
  void rotateX(double radians) {
    _transform.rotateX(radians);
    markNeedsPaint();
  }

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

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

947 948 949 950 951 952 953 954 955 956
  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();
  }

957
  void hitTestChildren(HitTestResult result, { Point position }) {
958 959 960 961 962 963
    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);
964
    Point transformed = new Point(transformed3.x, transformed3.y);
965 966 967
    super.hitTestChildren(result, position: transformed);
  }

H
Hixie 已提交
968
  void paint(RenderObjectDisplayList canvas) {
969
    canvas.save();
970
    canvas.concat(_transform.storage);
971 972 973
    super.paint(canvas);
    canvas.restore();
  }
974 975 976 977 978 979

  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()}';
  }
980 981
}

982
typedef void SizeChangedCallback(Size newSize);
983 984 985 986 987 988 989 990 991 992 993 994

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

  SizeChangedCallback callback;

  void performLayout() {
995
    Size oldSize = size;
996 997 998 999 1000 1001 1002 1003

    super.performLayout();

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

1004
typedef void CustomPaintCallback(sky.Canvas canvas);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

class RenderCustomPaint extends RenderProxyBox {

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

  CustomPaintCallback _callback;
  void set callback (CustomPaintCallback value) {
1018
    assert(value != null || !attached);
1019 1020 1021 1022 1023 1024
    if (_callback == value)
      return;
    _callback = value;
    markNeedsPaint();
  }

1025 1026 1027 1028 1029
  void attach() {
    assert(_callback != null);
    super.attach();
  }

1030
  void paint(RenderObjectDisplayList canvas) {
1031
    assert(_callback != null);
1032 1033 1034 1035 1036
    _callback(canvas);
    super.paint(canvas);
  }
}

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
// 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 已提交
1051
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
1052 1053 1054 1055 1056 1057 1058 1059

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

A
Adam Barth 已提交
1060
  Size _size = Size.zero;
1061 1062 1063 1064 1065 1066 1067
  double get width => _size.width;
  double get height => _size.height;

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

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  ViewConstraints _rootConstraints;
  ViewConstraints get rootConstraints => _rootConstraints;
  void set rootConstraints(ViewConstraints value) {
    if (_rootConstraints == value)
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

  void performLayout() {
    if (_rootConstraints.orientation != _orientation) {
1079
      if (_orientation != null && child != null)
1080 1081
        child.rotate(oldAngle: _orientation, newAngle: _rootConstraints.orientation, time: timeForRotation);
      _orientation = _rootConstraints.orientation;
1082
    }
1083
    _size = new Size(_rootConstraints.width, _rootConstraints.height);
1084 1085
    assert(_size.height < double.INFINITY);
    assert(_size.width < double.INFINITY);
1086

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    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()
  }

1098
  bool hitTest(HitTestResult result, { Point position }) {
1099
    if (child != null) {
1100
      Rect childBounds = new Rect.fromSize(child.size);
1101 1102 1103
      if (childBounds.contains(position))
        child.hitTest(result, position: position);
    }
1104
    result.add(new HitTestEntry(this));
1105 1106 1107
    return true;
  }

H
Hixie 已提交
1108
  void paint(RenderObjectDisplayList canvas) {
1109
    if (child != null)
1110
      canvas.paintChild(child, Point.origin);
1111 1112 1113
  }

  void paintFrame() {
H
Hixie 已提交
1114 1115
    RenderObject.debugDoingPaint = true;
    RenderObjectDisplayList canvas = new RenderObjectDisplayList(sky.view.width, sky.view.height);
1116 1117
    paint(canvas);
    sky.view.picture = canvas.endRecording();
H
Hixie 已提交
1118
    RenderObject.debugDoingPaint = false;
1119 1120 1121 1122 1123
  }

}

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

1126
  void defaultHitTestChildren(HitTestResult result, { Point position }) {
1127 1128 1129 1130
    // 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);
1131
      Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, child.size);
1132
      if (childBounds.contains(position)) {
1133
        if (child.hitTest(result, position: new Point(position.x - child.parentData.position.x,
1134 1135 1136 1137 1138 1139 1140
                                                          position.y - child.parentData.position.y)))
          break;
      }
      child = child.parentData.previousSibling;
    }
  }

H
Hixie 已提交
1141
  void defaultPaint(RenderObjectDisplayList canvas) {
1142 1143 1144 1145 1146 1147 1148 1149
    RenderBox child = firstChild;
    while (child != null) {
      assert(child.parentData is ParentDataType);
      canvas.paintChild(child, child.parentData.position);
      child = child.parentData.nextSibling;
    }
  }
}