Chromium Code Reviews| Index: sky/sdk/lib/framework/rendering/image.dart |
| diff --git a/sky/sdk/lib/framework/rendering/image.dart b/sky/sdk/lib/framework/rendering/image.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e5e9f6ce272f8be68e5133b238a82c0847c657a |
| --- /dev/null |
| +++ b/sky/sdk/lib/framework/rendering/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 { |
|
abarth-chromium
2015/06/03 00:16:31
We probably should just put the class in box.dart.
jackson
2015/06/03 17:41:43
Done.
|
| + |
| + RenderImage(String src, num width, num height) { |
| + 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) { |
| + sky.Size newSize = new sky.Size(width, height); |
| + if (requestedSize != newSize) { |
| + requestedSize = newSize; |
| + markNeedsLayout(); |
| + markNeedsPaint(); |
| + } |
| + image_cache.load(src, (result) { |
| + if (_image != result) { |
| + _image = result; |
| + _updateSizeFromImage(); |
| + markNeedsLayout(); |
| + markNeedsPaint(); |
| + } |
| + }); |
| + } |
| + |
| + void _updateSizeFromImage() { |
| + // 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); |
| + |
| + // 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); |
| + canvas.drawImage(_image, 0.0, 0.0, _paint); |
| + canvas.restore(); |
| + } |
| +} |