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

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

Issue 1160013004: Implement RenderImage and Image for Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Clean up the example 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
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();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698