| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
| 7 import 'object.dart'; | 7 import 'object.dart'; |
| 8 import '../painting/box_painter.dart'; | 8 import '../painting/box_painter.dart'; |
| 9 import 'package:vector_math/vector_math.dart'; | 9 import 'package:vector_math/vector_math.dart'; |
| 10 import 'package:sky/framework/net/image_cache.dart' as image_cache; | 10 import 'package:sky/framework/net/image_cache.dart' as image_cache; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 return value; | 46 return value; |
| 47 } | 47 } |
| 48 String toString() => "EdgeDims($top, $right, $bottom, $left)"; | 48 String toString() => "EdgeDims($top, $right, $bottom, $left)"; |
| 49 } | 49 } |
| 50 | 50 |
| 51 class BoxConstraints { | 51 class BoxConstraints { |
| 52 const BoxConstraints({ | 52 const BoxConstraints({ |
| 53 this.minWidth: 0.0, | 53 this.minWidth: 0.0, |
| 54 this.maxWidth: double.INFINITY, | 54 this.maxWidth: double.INFINITY, |
| 55 this.minHeight: 0.0, | 55 this.minHeight: 0.0, |
| 56 this.maxHeight: double.INFINITY}); | 56 this.maxHeight: double.INFINITY |
| 57 }); |
| 57 | 58 |
| 58 BoxConstraints.tight(Size size) | 59 BoxConstraints.tight(Size size) |
| 59 : minWidth = size.width, | 60 : minWidth = size.width, |
| 60 maxWidth = size.width, | 61 maxWidth = size.width, |
| 61 minHeight = size.height, | 62 minHeight = size.height, |
| 62 maxHeight = size.height; | 63 maxHeight = size.height; |
| 63 | 64 |
| 64 BoxConstraints.loose(Size size) | 65 BoxConstraints.loose(Size size) |
| 65 : minWidth = 0.0, | 66 : minWidth = 0.0, |
| 66 maxWidth = size.width, | 67 maxWidth = size.width, |
| 67 minHeight = 0.0, | 68 minHeight = 0.0, |
| 68 maxHeight = size.height; | 69 maxHeight = size.height; |
| 69 | 70 |
| 70 BoxConstraints deflate(EdgeDims edges) { | 71 BoxConstraints deflate(EdgeDims edges) { |
| 71 assert(edges != null); | 72 assert(edges != null); |
| 72 double horizontal = edges.left + edges.right; | 73 double horizontal = edges.left + edges.right; |
| 73 double vertical = edges.top + edges.bottom; | 74 double vertical = edges.top + edges.bottom; |
| 74 return new BoxConstraints( | 75 return new BoxConstraints( |
| 75 minWidth: math.max(0.0, minWidth - horizontal), | 76 minWidth: math.max(0.0, minWidth - horizontal), |
| 76 maxWidth: maxWidth - horizontal, | 77 maxWidth: maxWidth - horizontal, |
| 77 minHeight: math.max(0.0, minHeight - vertical), | 78 minHeight: math.max(0.0, minHeight - vertical), |
| 78 maxHeight: maxHeight - vertical | 79 maxHeight: maxHeight - vertical |
| 79 ); | 80 ); |
| 80 } | 81 } |
| 81 | 82 |
| 83 BoxConstraints loosen() { |
| 84 return new BoxConstraints( |
| 85 minWidth: 0.0, |
| 86 maxWidth: maxWidth, |
| 87 minHeight: 0.0, |
| 88 maxHeight: maxHeight |
| 89 ); |
| 90 } |
| 91 |
| 82 BoxConstraints apply(BoxConstraints constraints) { | 92 BoxConstraints apply(BoxConstraints constraints) { |
| 83 return new BoxConstraints( | 93 return new BoxConstraints( |
| 84 minWidth: math.max(minWidth, constraints.minWidth), | 94 minWidth: math.max(minWidth, constraints.minWidth), |
| 85 maxWidth: math.min(maxWidth, constraints.maxWidth), | 95 maxWidth: math.min(maxWidth, constraints.maxWidth), |
| 86 minHeight: math.max(minHeight, constraints.minHeight), | 96 minHeight: math.max(minHeight, constraints.minHeight), |
| 87 maxHeight: math.min(maxHeight, constraints.maxHeight)); | 97 maxHeight: math.min(maxHeight, constraints.maxHeight)); |
| 88 } | 98 } |
| 89 | 99 |
| 90 BoxConstraints applyWidth(double width) { | 100 BoxConstraints applyWidth(double width) { |
| 91 return new BoxConstraints(minWidth: math.max(minWidth, width), | 101 return new BoxConstraints(minWidth: math.max(minWidth, width), |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 void set size(Size value) { | 246 void set size(Size value) { |
| 237 assert(RenderObject.debugDoingLayout); | 247 assert(RenderObject.debugDoingLayout); |
| 238 _size = value; | 248 _size = value; |
| 239 } | 249 } |
| 240 | 250 |
| 241 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}size: ${size}\n'; | 251 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}size: ${size}\n'; |
| 242 } | 252 } |
| 243 | 253 |
| 244 class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox
> { | 254 class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox
> { |
| 245 | 255 |
| 256 // ProxyBox assumes the child will be at 0,0 and will have the same size |
| 257 |
| 246 RenderProxyBox([RenderBox child = null]) { | 258 RenderProxyBox([RenderBox child = null]) { |
| 247 this.child = child; | 259 this.child = child; |
| 248 } | 260 } |
| 249 | 261 |
| 250 double getMinIntrinsicWidth(BoxConstraints constraints) { | 262 double getMinIntrinsicWidth(BoxConstraints constraints) { |
| 251 if (child != null) | 263 if (child != null) |
| 252 return child.getMinIntrinsicWidth(constraints); | 264 return child.getMinIntrinsicWidth(constraints); |
| 253 return super.getMinIntrinsicWidth(constraints); | 265 return super.getMinIntrinsicWidth(constraints); |
| 254 } | 266 } |
| 255 | 267 |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 canvas.saveLayer(rect, new Paint()); | 508 canvas.saveLayer(rect, new Paint()); |
| 497 Path path = new Path(); | 509 Path path = new Path(); |
| 498 path.addOval(rect); | 510 path.addOval(rect); |
| 499 canvas.clipPath(path); | 511 canvas.clipPath(path); |
| 500 child.paint(canvas); | 512 child.paint(canvas); |
| 501 canvas.restore(); | 513 canvas.restore(); |
| 502 } | 514 } |
| 503 } | 515 } |
| 504 } | 516 } |
| 505 | 517 |
| 506 class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox>
{ | 518 abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi
n<RenderBox> { |
| 507 | 519 |
| 508 RenderPadding({ EdgeDims padding, RenderBox child }) { | 520 // Abstract class for one-child-layout render boxes |
| 509 assert(padding != null); | 521 |
| 510 this.padding = padding; | 522 RenderShiftedBox(RenderBox child) { |
| 511 this.child = child; | 523 this.child = child; |
| 512 } | 524 } |
| 513 | 525 |
| 526 void paint(RenderObjectDisplayList canvas) { |
| 527 if (child != null) |
| 528 canvas.paintChild(child, child.parentData.position); |
| 529 } |
| 530 |
| 531 void hitTestChildren(HitTestResult result, { Point position }) { |
| 532 if (child != null) { |
| 533 assert(child.parentData is BoxParentData); |
| 534 Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, ch
ild.size); |
| 535 if (childBounds.contains(position)) { |
| 536 child.hitTest(result, position: new Point(position.x - child.parentData.
position.x, |
| 537 position.y - child.parentD
ata.position.y)); |
| 538 } |
| 539 } |
| 540 } |
| 541 |
| 542 } |
| 543 |
| 544 class RenderPadding extends RenderShiftedBox { |
| 545 |
| 546 RenderPadding({ EdgeDims padding, RenderBox child }) : super(child) { |
| 547 assert(padding != null); |
| 548 this.padding = padding; |
| 549 } |
| 550 |
| 514 EdgeDims _padding; | 551 EdgeDims _padding; |
| 515 EdgeDims get padding => _padding; | 552 EdgeDims get padding => _padding; |
| 516 void set padding (EdgeDims value) { | 553 void set padding (EdgeDims value) { |
| 517 assert(value != null); | 554 assert(value != null); |
| 518 if (_padding == value) | 555 if (_padding == value) |
| 519 return; | 556 return; |
| 520 _padding = value; | 557 _padding = value; |
| 521 markNeedsLayout(); | 558 markNeedsLayout(); |
| 522 } | 559 } |
| 523 | 560 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 new Size(padding.left + padding.right, padding.top + padding.bottom)); | 594 new Size(padding.left + padding.right, padding.top + padding.bottom)); |
| 558 return; | 595 return; |
| 559 } | 596 } |
| 560 child.layout(innerConstraints, parentUsesSize: true); | 597 child.layout(innerConstraints, parentUsesSize: true); |
| 561 assert(child.parentData is BoxParentData); | 598 assert(child.parentData is BoxParentData); |
| 562 child.parentData.position = new Point(padding.left, padding.top); | 599 child.parentData.position = new Point(padding.left, padding.top); |
| 563 size = constraints.constrain(new Size(padding.left + child.size.width + padd
ing.right, | 600 size = constraints.constrain(new Size(padding.left + child.size.width + padd
ing.right, |
| 564 padding.top + child.size.height +
padding.bottom)); | 601 padding.top + child.size.height +
padding.bottom)); |
| 565 } | 602 } |
| 566 | 603 |
| 567 void paint(RenderObjectDisplayList canvas) { | |
| 568 if (child != null) | |
| 569 canvas.paintChild(child, child.parentData.position); | |
| 570 } | |
| 571 | |
| 572 void hitTestChildren(HitTestResult result, { Point position }) { | |
| 573 if (child != null) { | |
| 574 assert(child.parentData is BoxParentData); | |
| 575 Rect childBounds = new Rect.fromPointAndSize(child.parentData.position, ch
ild.size); | |
| 576 if (childBounds.contains(position)) { | |
| 577 child.hitTest(result, position: new Point(position.x - child.parentData.
position.x, | |
| 578 position.y - child.parentD
ata.position.y)); | |
| 579 } | |
| 580 } | |
| 581 } | |
| 582 | |
| 583 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}padding: ${padding}\n'; | 604 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}padding: ${padding}\n'; |
| 584 } | 605 } |
| 585 | 606 |
| 607 class RenderPositionedBox extends RenderShiftedBox { |
| 608 |
| 609 RenderPositionedBox({ |
| 610 RenderBox child, |
| 611 double horizontal: 0.5, |
| 612 double vertical: 0.5 |
| 613 }) : _horizontal = horizontal, |
| 614 _vertical = vertical, |
| 615 super(child) { |
| 616 assert(horizontal != null); |
| 617 assert(vertical != null); |
| 618 } |
| 619 |
| 620 double _horizontal; |
| 621 double get horizontal => _horizontal; |
| 622 void set horizontal (double value) { |
| 623 assert(value != null); |
| 624 if (_horizontal == value) |
| 625 return; |
| 626 _horizontal = value; |
| 627 markNeedsLayout(); |
| 628 } |
| 629 |
| 630 double _vertical; |
| 631 double get vertical => _vertical; |
| 632 void set vertical (double value) { |
| 633 assert(value != null); |
| 634 if (_vertical == value) |
| 635 return; |
| 636 _vertical = value; |
| 637 markNeedsLayout(); |
| 638 } |
| 639 |
| 640 double getMinIntrinsicWidth(BoxConstraints constraints) { |
| 641 if (child != null) |
| 642 return child.getMinIntrinsicWidth(constraints); |
| 643 return super.getMinIntrinsicWidth(constraints); |
| 644 } |
| 645 |
| 646 double getMaxIntrinsicWidth(BoxConstraints constraints) { |
| 647 if (child != null) |
| 648 return child.getMaxIntrinsicWidth(constraints); |
| 649 return super.getMaxIntrinsicWidth(constraints); |
| 650 } |
| 651 |
| 652 double getMinIntrinsicHeight(BoxConstraints constraints) { |
| 653 if (child != null) |
| 654 return child.getMinIntrinsicHeight(constraints); |
| 655 return super.getMinIntrinsicHeight(constraints); |
| 656 } |
| 657 |
| 658 double getMaxIntrinsicHeight(BoxConstraints constraints) { |
| 659 if (child != null) |
| 660 return child.getMaxIntrinsicHeight(constraints); |
| 661 return super.getMaxIntrinsicHeight(constraints); |
| 662 } |
| 663 |
| 664 void performLayout() { |
| 665 if (child != null) { |
| 666 child.layout(constraints.loosen()); |
| 667 size = constraints.constrain(child.size); |
| 668 assert(child.parentData is BoxParentData); |
| 669 Size delta = size - child.size; |
| 670 child.parentData.position = new Point(delta.width * horizontal, delta.heig
ht * vertical); |
| 671 } else { |
| 672 performResize(); |
| 673 } |
| 674 } |
| 675 |
| 676 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(
prefix)}${prefix}horizontal: ${horizontal}\n${prefix}vertical: ${vertical}\n'; |
| 677 } |
| 678 |
| 586 class RenderImage extends RenderBox { | 679 class RenderImage extends RenderBox { |
| 587 | 680 |
| 588 RenderImage(String url, Size dimensions) { | 681 RenderImage(String url, Size dimensions) { |
| 589 requestedSize = dimensions; | 682 requestedSize = dimensions; |
| 590 src = url; | 683 src = url; |
| 591 } | 684 } |
| 592 | 685 |
| 593 sky.Image _image; | 686 sky.Image _image; |
| 594 String _src; | 687 String _src; |
| 595 String get src => _src; | 688 String get src => _src; |
| 596 void set src (String value) { | 689 void set src (String value) { |
| 597 if (value == _src) | 690 if (value == _src) |
| 598 return; | 691 return; |
| 599 _src = value; | 692 _src = value; |
| 600 image_cache.load(_src, (result) { | 693 image_cache.load(_src, (result) { |
| 601 _image = result; | 694 _image = result; |
| 602 if (requestedSize.width == null || requestedSize.height == null) | 695 if (requestedSize.width == null || requestedSize.height == null) |
| 603 markNeedsLayout(); | 696 markNeedsLayout(); |
| 604 markNeedsPaint(); | 697 markNeedsPaint(); |
| 605 }); | 698 }); |
| 606 } | 699 } |
| 607 | 700 |
| 608 Size _requestedSize; | 701 Size _requestedSize; |
| 609 Size get requestedSize => _requestedSize; | 702 Size get requestedSize => _requestedSize; |
| 610 void set requestedSize (Size value) { | 703 void set requestedSize (Size value) { |
| 704 if (value == null) |
| 705 value = const Size(null, null); |
| 611 if (value == _requestedSize) | 706 if (value == _requestedSize) |
| 612 return; | 707 return; |
| 613 _requestedSize = value; | 708 _requestedSize = value; |
| 614 markNeedsLayout(); | 709 markNeedsLayout(); |
| 615 } | 710 } |
| 616 | 711 |
| 617 Size _sizeForConstraints(BoxConstraints innerConstraints) { | 712 Size _sizeForConstraints(BoxConstraints innerConstraints) { |
| 618 // If there's no image, we can't size ourselves automatically | 713 // If there's no image, we can't size ourselves automatically |
| 619 if (_image == null) { | 714 if (_image == null) { |
| 620 double width = requestedSize.width == null ? 0.0 : requestedSize.width; | 715 double width = requestedSize.width == null ? 0.0 : requestedSize.width; |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 944 | 1039 |
| 945 void defaultPaint(RenderObjectDisplayList canvas) { | 1040 void defaultPaint(RenderObjectDisplayList canvas) { |
| 946 RenderBox child = firstChild; | 1041 RenderBox child = firstChild; |
| 947 while (child != null) { | 1042 while (child != null) { |
| 948 assert(child.parentData is ParentDataType); | 1043 assert(child.parentData is ParentDataType); |
| 949 canvas.paintChild(child, child.parentData.position); | 1044 canvas.paintChild(child, child.parentData.position); |
| 950 child = child.parentData.nextSibling; | 1045 child = child.parentData.nextSibling; |
| 951 } | 1046 } |
| 952 } | 1047 } |
| 953 } | 1048 } |
| OLD | NEW |