Chromium Code Reviews| Index: sky/sdk/lib/framework/rendering/render_image.dart |
| diff --git a/sky/sdk/lib/framework/rendering/render_image.dart b/sky/sdk/lib/framework/rendering/render_image.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..22daea768fddda4bc173463a35b5a3d73e2b6fe8 |
| --- /dev/null |
| +++ b/sky/sdk/lib/framework/rendering/render_image.dart |
| @@ -0,0 +1,77 @@ |
| +// 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. |
| + |
| +import 'dart:sky' as sky; |
| +import 'package:sky/framework/net/image_cache.dart' as image_cache; |
| +import 'render_node.dart'; |
| +import 'render_box.dart'; |
| + |
| +class RenderImage extends RenderBox { |
| + |
| + RenderImage(String src, num width, num height) { |
|
abarth-chromium
2015/06/03 00:07:54
s/num/double/
We're pretty committed to render tr
jackson
2015/06/03 17:41:42
Acknowledged.
|
| + configure(src, width, height); |
| + } |
| + |
| + sky.Image _image; |
| + sky.Size requestedSize; |
| + final sky.Paint _paint = new sky.Paint(); |
| + |
| + void configure(String src, num width, num height) { |
|
eseidel
2015/06/02 23:46:01
Should this be private?
|
| + sky.Size newSize = new sky.Size(width, height); |
|
abarth-chromium
2015/06/03 00:07:54
Probably the caller should pass in the sky.Size in
|
| + if (requestedSize != newSize) { |
| + requestedSize = newSize; |
| + markNeedsLayout(); |
| + markNeedsPaint(); |
|
abarth-chromium
2015/06/03 00:07:54
No need to markNeedsPaint here. markNeedsLayout w
|
| + } |
| + image_cache.load(src, (result) { |
| + if (_image != result) { |
| + _image = result; |
| + updateSizeFromImage(); |
| + markNeedsLayout(); |
|
abarth-chromium
2015/06/03 00:07:54
Shouldn't updateSizeFromImage do the work of markN
|
| + markNeedsPaint(); |
| + } |
| + }); |
| + } |
| + |
| + void updateSizeFromImage() { |
|
eseidel
2015/06/02 23:46:01
Should this be private?
|
| + // 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 00:07:53
How do you know |constraints| is populated at this
|
| + |
| + // 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; |
| + 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 00:07:54
We should skip the save/scale/restore if size.widt
|
| + canvas.drawImage(_image, 0.0, 0.0, _paint); |
|
abarth-chromium
2015/06/03 00:07:54
I don't think it's worth caching the paint on this
|
| + canvas.restore(); |
| + } |
| +} |