Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Unified Diff: sky/sdk/lib/framework/rendering/box.dart

Issue 1160013004: Implement RenderImage and Image for Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: rebase again Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/sdk/lib/framework/fn2.dart ('k') | sky/sdk/lib/framework/rendering/node.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 ed9762e19f21a171201b8a21b72c3ec2ec6f6d90..cfa83a7cac4506c6546e6a02c09d0b6db3e455bf 100644
--- a/sky/sdk/lib/framework/rendering/box.dart
+++ b/sky/sdk/lib/framework/rendering/box.dart
@@ -7,6 +7,7 @@ import 'dart:sky' as sky;
import 'dart:typed_data';
import 'node.dart';
import 'package:vector_math/vector_math.dart';
+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
@@ -255,6 +256,80 @@ class RenderPadding extends RenderBox with RenderNodeWithChildMixin<RenderBox> {
}
+class RenderImage extends RenderBox {
+
+ RenderImage(String url, sky.Size dimensions) {
+ 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();
+ });
+ }
+
+ sky.Size _requestedSize;
+ sky.Size get requestedSize => _requestedSize;
+ void set requestedSize (sky.Size value) {
+ if (value == _requestedSize)
+ return;
+ _requestedSize = value;
+ markNeedsLayout();
+ }
+
+ void performLayout() {
+ // 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;
+ size = constraints.constrain(new sky.Size(width, height));
+ 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 {
+ double width = requestedSize.height * _image.width / _image.height;
+ size = constraints.constrain(new sky.Size(width, requestedSize.height));
+ }
+ } else if (requestedSize.height == null) {
+ double height = requestedSize.width * _image.height / _image.width;
+ size = constraints.constrain(new sky.Size(requestedSize.width, height));
+ } else {
+ size = constraints.constrain(requestedSize);
+ }
+ }
+
+ void paint(RenderNodeDisplayList canvas) {
+ 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);
+ }
+ sky.Paint paint = new sky.Paint();
+ canvas.drawImage(_image, 0.0, 0.0, paint);
+ if (needsScale)
+ canvas.restore();
+ }
+}
+
// This must be immutable, because we won't notice when it changes
class BoxDecoration {
const BoxDecoration({this.backgroundColor});
« no previous file with comments | « sky/sdk/lib/framework/fn2.dart ('k') | sky/sdk/lib/framework/rendering/node.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698