Index: sky/sdk/lib/framework/rendering/box.dart |
diff --git a/sky/sdk/lib/framework/rendering/box.dart b/sky/sdk/lib/framework/rendering/box.dart |
index 803c85b2b884b7e1d962cfa6940fd2f753dd3fc5..a79686b54bab58f0b9cba252da6870af67ddfbbf 100644 |
--- a/sky/sdk/lib/framework/rendering/box.dart |
+++ b/sky/sdk/lib/framework/rendering/box.dart |
@@ -4,6 +4,7 @@ |
import 'node.dart'; |
import 'dart:sky' as sky; |
+import 'package:sky/framework/net/image_cache.dart' as image_cache; |
// GENERIC BOX RENDERING |
// Anything that has a concept of x, y, width, height is going to derive from this |
@@ -243,6 +244,71 @@ class RenderPadding extends RenderBox with RenderNodeWithChildMixin<RenderBox> { |
} |
+class RenderImage extends RenderBox { |
+ |
+ RenderImage(String src, sky.Size dimensions) { |
+ requestedSize = dimensions; |
+ image_cache.load(src, (result) { |
+ _image = result; |
+ markNeedsLayout(); |
+ markNeedsPaint(); |
+ }); |
+ } |
+ |
+ sky.Image _image; |
+ sky.Size _requestedSize; |
+ sky.Size get requestedSize => _requestedSize; |
+ void set requestedSize (sky.Size value) { |
+ if (value == _requestedSize) |
+ return; |
+ _requestedSize = value; |
+ markNeedsLayout(); |
+ markNeedsPaint(); |
abarth-chromium
2015/06/03 17:53:21
No need to call markNeedsPaint here. markNeedsLay
jackson
2015/06/03 22:06:26
Done.
|
+ } |
+ |
+ void _updateSizeFromImage() { |
abarth-chromium
2015/06/03 17:53:21
This function has one caller, performLayout. We s
jackson
2015/06/03 22:06:26
Done.
|
+ // If both height and width are specified, we don't care about image dimensions |
+ if (requestedSize.width != null && requestedSize.height != null) |
+ size = constraints.constrain(requestedSize); |
abarth-chromium
2015/06/03 17:53:21
Should we return in this case?
jackson
2015/06/03 22:06:26
Done.
|
+ |
+ // If there's no image, we can't size ourselves |
+ if (_image == null) |
+ return; |
+ |
+ // If neither height nor width are specified, use inherent image dimensions |
+ // If only one dimension is specified, adjust the other dimension to |
+ // maintain the aspect ratio |
+ if (requestedSize.width == null) { |
+ if (requestedSize.height == null) { |
+ size = constraints.constrain(new sky.Size(_image.width, _image.height)); |
+ } else { |
+ num width = requestedSize.height * _image.width / _image.height; |
abarth-chromium
2015/06/03 17:53:21
s/num/double/ It's always a double
jackson
2015/06/03 22:06:26
Done.
|
+ size = constraints.constrain(new sky.Size(width, requestedSize.height)); |
+ } |
+ } else if (requestedSize.height == null) { |
+ num height = requestedSize.width * _image.height / _image.width; |
+ size = constraints.constrain(new sky.Size(requestedSize.width, height)); |
+ } else { |
+ size = constraints.constrain(requestedSize); |
+ } |
+ } |
+ |
+ void performLayout() { |
+ _updateSizeFromImage(); |
+ } |
+ |
+ void paint(RenderNodeDisplayList canvas) { |
+ if (_image == null) return; |
+ double widthScale = size.width / _image.width; |
+ double heightScale = size.height / _image.height; |
+ canvas.save(); |
+ canvas.scale(widthScale, heightScale); |
abarth-chromium
2015/06/03 17:53:21
We should skip the save/scale/restore dance if siz
jackson
2015/06/03 22:06:26
Done.
|
+ sky.Paint paint = new sky.Paint(); |
+ canvas.drawImage(_image, 0.0, 0.0, paint); |
+ canvas.restore(); |
+ } |
+} |
+ |
// This must be immutable, because we won't notice when it changes |
class BoxDecoration { |
// TODO(mpcomplete): go through and change the users of this class to pass |