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

Side by Side 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, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 import 'dart:sky' as sky;
6 import 'package:sky/framework/net/image_cache.dart' as image_cache;
7 import 'render_node.dart';
8 import 'render_box.dart';
9
10 class RenderImage extends RenderBox {
11
12 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.
13 configure(src, width, height);
14 }
15
16 sky.Image _image;
17 sky.Size requestedSize;
18 final sky.Paint _paint = new sky.Paint();
19
20 void configure(String src, num width, num height) {
eseidel 2015/06/02 23:46:01 Should this be private?
21 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
22 if (requestedSize != newSize) {
23 requestedSize = newSize;
24 markNeedsLayout();
25 markNeedsPaint();
abarth-chromium 2015/06/03 00:07:54 No need to markNeedsPaint here. markNeedsLayout w
26 }
27 image_cache.load(src, (result) {
28 if (_image != result) {
29 _image = result;
30 updateSizeFromImage();
31 markNeedsLayout();
abarth-chromium 2015/06/03 00:07:54 Shouldn't updateSizeFromImage do the work of markN
32 markNeedsPaint();
33 }
34 });
35 }
36
37 void updateSizeFromImage() {
eseidel 2015/06/02 23:46:01 Should this be private?
38 // If both height and width are specified, we don't care about image dimensi ons
39 if (requestedSize.width != null && requestedSize.height != null)
40 size = constraints.constrain(requestedSize);
abarth-chromium 2015/06/03 00:07:53 How do you know |constraints| is populated at this
41
42 // If there's no image, we can't size ourselves
43 if (_image == null)
44 return;
45
46 // If neither height nor width are specified, use inherent image dimensions
47 // If only one dimension is specified, adjust the other dimension to
48 // maintain the aspect ratio
49 if (requestedSize.width == null) {
50 if (requestedSize.height == null) {
51 size = constraints.constrain(new sky.Size(_image.width, _image.height));
52 } else {
53 num width = requestedSize.height * _image.width / _image.height;
54 size = constraints.constrain(new sky.Size(width, requestedSize.height));
55 }
56 } else if (requestedSize.height == null) {
57 num height = requestedSize.width * _image.height / _image.width;
58 size = constraints.constrain(new sky.Size(requestedSize.width, height));
59 } else {
60 size = constraints.constrain(requestedSize);
61 }
62 }
63
64 void performLayout() {
65 updateSizeFromImage();
66 }
67
68 void paint(RenderNodeDisplayList canvas) {
69 if (_image == null) return;
70 double widthScale = size.width / _image.width;
71 double heightScale = size.height / _image.height;
72 canvas.save();
73 canvas.scale(widthScale, heightScale);
abarth-chromium 2015/06/03 00:07:54 We should skip the save/scale/restore if size.widt
74 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
75 canvas.restore();
76 }
77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698