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

Side by Side Diff: sky/sdk/lib/framework/rendering/image.dart

Issue 1160013004: Implement RenderImage and Image for Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: CR feedback from Eric 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 {
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.
11
12 RenderImage(String src, num width, num height) {
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) {
21 sky.Size newSize = new sky.Size(width, height);
22 if (requestedSize != newSize) {
23 requestedSize = newSize;
24 markNeedsLayout();
25 markNeedsPaint();
26 }
27 image_cache.load(src, (result) {
28 if (_image != result) {
29 _image = result;
30 _updateSizeFromImage();
31 markNeedsLayout();
32 markNeedsPaint();
33 }
34 });
35 }
36
37 void _updateSizeFromImage() {
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);
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);
74 canvas.drawImage(_image, 0.0, 0.0, _paint);
75 canvas.restore();
76 }
77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698