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

Side by Side 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 and merge conflicts 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
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 import 'dart:math' as math; 5 import 'dart:math' as math;
6 import 'dart:sky' as sky; 6 import 'dart:sky' as sky;
7 import 'dart:typed_data'; 7 import 'dart:typed_data';
8 import 'node.dart'; 8 import 'node.dart';
9 import 'package:vector_math/vector_math.dart'; 9 import 'package:vector_math/vector_math.dart';
10 import 'package:sky/framework/net/image_cache.dart' as image_cache;
10 11
11 // GENERIC BOX RENDERING 12 // GENERIC BOX RENDERING
12 // Anything that has a concept of x, y, width, height is going to derive from th is 13 // Anything that has a concept of x, y, width, height is going to derive from th is
13 14
14 class EdgeDims { 15 class EdgeDims {
15 // used for e.g. padding 16 // used for e.g. padding
16 const EdgeDims(this.top, this.right, this.bottom, this.left); 17 const EdgeDims(this.top, this.right, this.bottom, this.left);
17 const EdgeDims.all(double value) 18 const EdgeDims.all(double value)
18 : top = value, right = value, bottom = value, left = value; 19 : top = value, right = value, bottom = value, left = value;
19 20
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi tion, child.size); 249 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi tion, child.size);
249 if (childBounds.contains(position)) { 250 if (childBounds.contains(position)) {
250 child.hitTest(result, position: new sky.Point(position.x - child.parentD ata.position.x, 251 child.hitTest(result, position: new sky.Point(position.x - child.parentD ata.position.x,
251 position.y - child.parentD ata.position.y)); 252 position.y - child.parentD ata.position.y));
252 } 253 }
253 } 254 }
254 } 255 }
255 256
256 } 257 }
257 258
259 class RenderImage extends RenderBox {
260
261 RenderImage(String url, sky.Size dimensions) {
262 requestedSize = dimensions;
263 src = url;
264 }
265
266 sky.Image _image;
267 String _src;
268 String get src => _src;
269 void set src (String value) {
270 if (value == _src)
271 return;
272 _src = value;
273 image_cache.load(_src, (result) {
274 _image = result;
275 if (requestedSize.width == null || requestedSize.height == null)
276 markNeedsLayout();
277 markNeedsPaint();
278 });
279 }
280
281 sky.Size _requestedSize;
282 sky.Size get requestedSize => _requestedSize;
283 void set requestedSize (sky.Size value) {
284 if (value == _requestedSize)
285 return;
286 _requestedSize = value;
287 markNeedsLayout();
288 }
289
290 void performLayout() {
291 // If there's no image, we can't size ourselves automatically
292 if (_image == null) {
293 size = new sky.Size(requestedSize.width == null ? 0.0 : requestedSize.widt h,
294 requestedSize.height == null ? 0.0 : requestedSize.hei ght);
abarth-chromium 2015/06/03 22:10:30 We need to constaint the size using the box constr
295 return;
296 }
297
298 // If neither height nor width are specified, use inherent image dimensions
299 // If only one dimension is specified, adjust the other dimension to
300 // maintain the aspect ratio
301 if (requestedSize.width == null) {
302 if (requestedSize.height == null) {
303 size = constraints.constrain(new sky.Size(_image.width, _image.height));
304 } else {
305 double width = requestedSize.height * _image.width / _image.height;
306 size = constraints.constrain(new sky.Size(width, requestedSize.height));
307 }
308 } else if (requestedSize.height == null) {
309 double height = requestedSize.width * _image.height / _image.width;
310 size = constraints.constrain(new sky.Size(requestedSize.width, height));
311 } else {
312 size = constraints.constrain(requestedSize);
313 }
314 }
315
316 void paint(RenderNodeDisplayList canvas) {
317 if (_image == null) return;
318 bool needsScale = size.width != _image.width || size.height != _image.height ;
319 if (needsScale) {
320 double widthScale = size.width / _image.width;
321 double heightScale = size.height / _image.height;
322 canvas.save();
323 canvas.scale(widthScale, heightScale);
324 }
325 sky.Paint paint = new sky.Paint();
326 canvas.drawImage(_image, 0.0, 0.0, paint);
327 if (needsScale)
328 canvas.restore();
329 }
330 }
331
258 // This must be immutable, because we won't notice when it changes 332 // This must be immutable, because we won't notice when it changes
259 class BoxDecoration { 333 class BoxDecoration {
260 const BoxDecoration({this.backgroundColor}); 334 const BoxDecoration({this.backgroundColor});
261 335
262 final sky.Color backgroundColor; 336 final sky.Color backgroundColor;
263 } 337 }
264 338
265 class RenderDecoratedBox extends RenderProxyBox { 339 class RenderDecoratedBox extends RenderProxyBox {
266 340
267 RenderDecoratedBox({ 341 RenderDecoratedBox({
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 537
464 void defaultPaint(RenderNodeDisplayList canvas) { 538 void defaultPaint(RenderNodeDisplayList canvas) {
465 RenderBox child = firstChild; 539 RenderBox child = firstChild;
466 while (child != null) { 540 while (child != null) {
467 assert(child.parentData is ParentDataType); 541 assert(child.parentData is ParentDataType);
468 canvas.paintChild(child, child.parentData.position); 542 canvas.paintChild(child, child.parentData.position);
469 child = child.parentData.nextSibling; 543 child = child.parentData.nextSibling;
470 } 544 }
471 } 545 }
472 } 546 }
OLDNEW
« 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