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

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: Implement code review feedback from Adam and improve demo 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
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 'node.dart'; 5 import 'node.dart';
6 import 'dart:sky' as sky; 6 import 'dart:sky' as sky;
7 import 'package:sky/framework/net/image_cache.dart' as image_cache;
7 8
8 // GENERIC BOX RENDERING 9 // GENERIC BOX RENDERING
9 // Anything that has a concept of x, y, width, height is going to derive from th is 10 // Anything that has a concept of x, y, width, height is going to derive from th is
10 11
11 class EdgeDims { 12 class EdgeDims {
12 // used for e.g. padding 13 // used for e.g. padding
13 const EdgeDims(this.top, this.right, this.bottom, this.left); 14 const EdgeDims(this.top, this.right, this.bottom, this.left);
14 const EdgeDims.all(double value) 15 const EdgeDims.all(double value)
15 : top = value, right = value, bottom = value, left = value; 16 : top = value, right = value, bottom = value, left = value;
16 17
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi tion, child.size); 237 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi tion, child.size);
237 if (childBounds.contains(position)) { 238 if (childBounds.contains(position)) {
238 child.hitTest(result, position: new sky.Point(position.x - child.parentD ata.position.x, 239 child.hitTest(result, position: new sky.Point(position.x - child.parentD ata.position.x,
239 position.y - child.parentD ata.position.y)); 240 position.y - child.parentD ata.position.y));
240 } 241 }
241 } 242 }
242 } 243 }
243 244
244 } 245 }
245 246
247 class RenderImage extends RenderBox {
248
249 RenderImage(String src, sky.Size dimensions) {
250 requestedSize = dimensions;
251 image_cache.load(src, (result) {
252 _image = result;
253 markNeedsLayout();
254 markNeedsPaint();
255 });
256 }
257
258 sky.Image _image;
259 sky.Size _requestedSize;
260 sky.Size get requestedSize => _requestedSize;
261 void set requestedSize (sky.Size value) {
262 if (value == _requestedSize)
263 return;
264 _requestedSize = value;
265 markNeedsLayout();
266 markNeedsPaint();
abarth-chromium 2015/06/03 17:53:21 No need to call markNeedsPaint here. markNeedsLay
jackson 2015/06/03 22:06:26 Done.
267 }
268
269 void _updateSizeFromImage() {
abarth-chromium 2015/06/03 17:53:21 This function has one caller, performLayout. We s
jackson 2015/06/03 22:06:26 Done.
270 // If both height and width are specified, we don't care about image dimensi ons
271 if (requestedSize.width != null && requestedSize.height != null)
272 size = constraints.constrain(requestedSize);
abarth-chromium 2015/06/03 17:53:21 Should we return in this case?
jackson 2015/06/03 22:06:26 Done.
273
274 // If there's no image, we can't size ourselves
275 if (_image == null)
276 return;
277
278 // If neither height nor width are specified, use inherent image dimensions
279 // If only one dimension is specified, adjust the other dimension to
280 // maintain the aspect ratio
281 if (requestedSize.width == null) {
282 if (requestedSize.height == null) {
283 size = constraints.constrain(new sky.Size(_image.width, _image.height));
284 } else {
285 num width = requestedSize.height * _image.width / _image.height;
abarth-chromium 2015/06/03 17:53:21 s/num/double/ It's always a double
jackson 2015/06/03 22:06:26 Done.
286 size = constraints.constrain(new sky.Size(width, requestedSize.height));
287 }
288 } else if (requestedSize.height == null) {
289 num height = requestedSize.width * _image.height / _image.width;
290 size = constraints.constrain(new sky.Size(requestedSize.width, height));
291 } else {
292 size = constraints.constrain(requestedSize);
293 }
294 }
295
296 void performLayout() {
297 _updateSizeFromImage();
298 }
299
300 void paint(RenderNodeDisplayList canvas) {
301 if (_image == null) return;
302 double widthScale = size.width / _image.width;
303 double heightScale = size.height / _image.height;
304 canvas.save();
305 canvas.scale(widthScale, heightScale);
abarth-chromium 2015/06/03 17:53:21 We should skip the save/scale/restore dance if siz
jackson 2015/06/03 22:06:26 Done.
306 sky.Paint paint = new sky.Paint();
307 canvas.drawImage(_image, 0.0, 0.0, paint);
308 canvas.restore();
309 }
310 }
311
246 // This must be immutable, because we won't notice when it changes 312 // This must be immutable, because we won't notice when it changes
247 class BoxDecoration { 313 class BoxDecoration {
248 // TODO(mpcomplete): go through and change the users of this class to pass 314 // TODO(mpcomplete): go through and change the users of this class to pass
249 // a Color object. 315 // a Color object.
250 BoxDecoration({ 316 BoxDecoration({
251 backgroundColor 317 backgroundColor
252 }) : backgroundColor = new sky.Color(backgroundColor); 318 }) : backgroundColor = new sky.Color(backgroundColor);
253 319
254 final sky.Color backgroundColor; 320 final sky.Color backgroundColor;
255 } 321 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 453
388 void defaultPaint(RenderNodeDisplayList canvas) { 454 void defaultPaint(RenderNodeDisplayList canvas) {
389 RenderBox child = firstChild; 455 RenderBox child = firstChild;
390 while (child != null) { 456 while (child != null) {
391 assert(child.parentData is ParentDataType); 457 assert(child.parentData is ParentDataType);
392 canvas.paintChild(child, child.parentData.position); 458 canvas.paintChild(child, child.parentData.position);
393 child = child.parentData.nextSibling; 459 child = child.parentData.nextSibling;
394 } 460 }
395 } 461 }
396 } 462 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698