OLD | NEW |
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 'package:sky/framework/app.dart'; | 7 import 'package:sky/framework/app.dart'; |
8 import 'package:sky/framework/layout2.dart'; | 8 import 'package:sky/framework/layout2.dart'; |
9 | 9 |
10 const double kTwoPi = 2 * math.PI; | 10 const double kTwoPi = 2 * math.PI; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 void setParentData(RenderNode child) { | 66 void setParentData(RenderNode child) { |
67 if (child.parentData is! SectorParentData) | 67 if (child.parentData is! SectorParentData) |
68 child.parentData = new SectorParentData(); | 68 child.parentData = new SectorParentData(); |
69 } | 69 } |
70 | 70 |
71 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double
radius) { | 71 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double
radius) { |
72 return new SectorDimensions.withConstraints(constraints); | 72 return new SectorDimensions.withConstraints(constraints); |
73 } | 73 } |
74 | 74 |
75 void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot })
{ | 75 SectorConstraints get constraints => super.constraints as SectorConstraints; |
| 76 void performResize() { |
| 77 // default behaviour for subclasses that have sizedByParent = true |
76 deltaRadius = constraints.constrainDeltaRadius(0.0); | 78 deltaRadius = constraints.constrainDeltaRadius(0.0); |
77 deltaTheta = constraints.constrainDeltaTheta(0.0); | 79 deltaTheta = constraints.constrainDeltaTheta(0.0); |
78 layoutDone(); | 80 } |
| 81 void performLayout() { |
| 82 // descendants have to either override performLayout() to set both |
| 83 // the dimensions and lay out children, or, set sizedByParent to |
| 84 // true so that performResize()'s logic above does its thing. |
| 85 assert(sizedByParent); |
79 } | 86 } |
80 | 87 |
81 bool hitTest(HitTestResult result, { double radius, double theta }) { | 88 bool hitTest(HitTestResult result, { double radius, double theta }) { |
82 assert(parentData is SectorParentData); | 89 assert(parentData is SectorParentData); |
83 if (radius < parentData.radius || radius >= parentData.radius + deltaRadius
|| | 90 if (radius < parentData.radius || radius >= parentData.radius + deltaRadius
|| |
84 theta < parentData.theta || theta >= parentData.theta + deltaTheta) | 91 theta < parentData.theta || theta >= parentData.theta + deltaTheta) |
85 return false; | 92 return false; |
86 hitTestChildren(result, radius: radius, theta: theta); | 93 hitTestChildren(result, radius: radius, theta: theta); |
87 result.add(this); | 94 result.add(this); |
88 return true; | 95 return true; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 if (child != null) { | 210 if (child != null) { |
204 innerTheta += paddingTheta; | 211 innerTheta += paddingTheta; |
205 remainingDeltaTheta -= paddingTheta; | 212 remainingDeltaTheta -= paddingTheta; |
206 } | 213 } |
207 } | 214 } |
208 return new SectorDimensions.withConstraints(constraints, | 215 return new SectorDimensions.withConstraints(constraints, |
209 deltaRadius: outerDeltaRadius, | 216 deltaRadius: outerDeltaRadius, |
210 deltaTheta: innerTheta); | 217 deltaTheta: innerTheta); |
211 } | 218 } |
212 | 219 |
213 SectorConstraints _constraints; | 220 void performLayout() { |
214 void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot })
{ | 221 assert(this.parentData is SectorParentData); |
215 if (relayoutSubtreeRoot != null) | |
216 saveRelayoutSubtreeRoot(relayoutSubtreeRoot); | |
217 relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRo
ot; | |
218 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); | 222 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); |
219 assert(deltaRadius < double.INFINITY); | 223 assert(deltaRadius < double.INFINITY); |
220 _constraints = constraints; | |
221 internalLayout(relayoutSubtreeRoot); | |
222 } | |
223 | |
224 void relayout() { | |
225 // TODO(ianh): avoid code duplication | |
226 assert(parentData is SectorParentData); | |
227 internalLayout(this); | |
228 } | |
229 | |
230 void internalLayout(RenderNode relayoutSubtreeRoot) { | |
231 assert(this.parentData is SectorParentData); | |
232 double innerDeltaRadius = deltaRadius - padding * 2.0; | 224 double innerDeltaRadius = deltaRadius - padding * 2.0; |
233 double childRadius = this.parentData.radius + padding; | 225 double childRadius = this.parentData.radius + padding; |
234 double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRad
ius)); | 226 double paddingTheta = math.atan(padding / (this.parentData.radius + deltaRad
ius)); |
235 double innerTheta = paddingTheta; // increments with each child | 227 double innerTheta = paddingTheta; // increments with each child |
236 double remainingDeltaTheta = _constraints.maxDeltaTheta - (innerTheta + padd
ingTheta); | 228 double remainingDeltaTheta = constraints.maxDeltaTheta - (innerTheta + paddi
ngTheta); |
237 RenderSector child = firstChild; | 229 RenderSector child = firstChild; |
238 while (child != null) { | 230 while (child != null) { |
239 SectorConstraints innerConstraints = new SectorConstraints( | 231 SectorConstraints innerConstraints = new SectorConstraints( |
240 maxDeltaRadius: innerDeltaRadius, | 232 maxDeltaRadius: innerDeltaRadius, |
241 maxDeltaTheta: remainingDeltaTheta | 233 maxDeltaTheta: remainingDeltaTheta |
242 ); | 234 ); |
243 assert(child.parentData is SectorParentData); | 235 assert(child.parentData is SectorParentData); |
244 child.parentData.theta = innerTheta; | 236 child.parentData.theta = innerTheta; |
245 child.parentData.radius = childRadius; | 237 child.parentData.radius = childRadius; |
246 child.layout(innerConstraints, relayoutSubtreeRoot: relayoutSubtreeRoot); | 238 child.layout(innerConstraints, parentUsesSize: true); |
247 innerTheta += child.deltaTheta; | 239 innerTheta += child.deltaTheta; |
248 remainingDeltaTheta -= child.deltaTheta; | 240 remainingDeltaTheta -= child.deltaTheta; |
249 assert(child.parentData is SectorChildListParentData); | 241 assert(child.parentData is SectorChildListParentData); |
250 child = child.parentData.nextSibling; | 242 child = child.parentData.nextSibling; |
251 if (child != null) { | 243 if (child != null) { |
252 innerTheta += paddingTheta; | 244 innerTheta += paddingTheta; |
253 remainingDeltaTheta -= paddingTheta; | 245 remainingDeltaTheta -= paddingTheta; |
254 } | 246 } |
255 } | 247 } |
256 deltaTheta = innerTheta; | 248 deltaTheta = innerTheta; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 assert(child.parentData is SectorChildListParentData); | 318 assert(child.parentData is SectorChildListParentData); |
327 child = child.parentData.nextSibling; | 319 child = child.parentData.nextSibling; |
328 childRadius += padding; | 320 childRadius += padding; |
329 remainingDeltaRadius -= padding; | 321 remainingDeltaRadius -= padding; |
330 } | 322 } |
331 return new SectorDimensions.withConstraints(constraints, | 323 return new SectorDimensions.withConstraints(constraints, |
332 deltaRadius: childRadius - this.
parentData.radius, | 324 deltaRadius: childRadius - this.
parentData.radius, |
333 deltaTheta: outerDeltaTheta); | 325 deltaTheta: outerDeltaTheta); |
334 } | 326 } |
335 | 327 |
336 SectorConstraints _constraints; | 328 void performLayout() { |
337 void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot })
{ | 329 assert(this.parentData is SectorParentData); |
338 if (relayoutSubtreeRoot != null) | |
339 saveRelayoutSubtreeRoot(relayoutSubtreeRoot); | |
340 relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRo
ot; | |
341 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); | 330 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); |
342 assert(deltaTheta <= kTwoPi); | 331 assert(deltaTheta <= kTwoPi); |
343 _constraints = constraints; | |
344 internalLayout(relayoutSubtreeRoot); | |
345 } | |
346 | |
347 void relayout() { | |
348 // TODO(ianh): avoid code duplication | |
349 assert(parentData is SectorParentData); | |
350 internalLayout(this); | |
351 } | |
352 | |
353 void internalLayout(RenderNode relayoutSubtreeRoot) { | |
354 assert(this.parentData is SectorParentData); | |
355 double paddingTheta = math.atan(padding / this.parentData.radius); | 332 double paddingTheta = math.atan(padding / this.parentData.radius); |
356 double innerTheta = this.parentData.theta + paddingTheta; | 333 double innerTheta = this.parentData.theta + paddingTheta; |
357 double innerDeltaTheta = deltaTheta - paddingTheta * 2.0; | 334 double innerDeltaTheta = deltaTheta - paddingTheta * 2.0; |
358 double childRadius = this.parentData.radius + padding; | 335 double childRadius = this.parentData.radius + padding; |
359 double remainingDeltaRadius = _constraints.maxDeltaRadius - (padding * 2.0); | 336 double remainingDeltaRadius = constraints.maxDeltaRadius - (padding * 2.0); |
360 RenderSector child = firstChild; | 337 RenderSector child = firstChild; |
361 while (child != null) { | 338 while (child != null) { |
362 SectorConstraints innerConstraints = new SectorConstraints( | 339 SectorConstraints innerConstraints = new SectorConstraints( |
363 maxDeltaRadius: remainingDeltaRadius, | 340 maxDeltaRadius: remainingDeltaRadius, |
364 maxDeltaTheta: innerDeltaTheta | 341 maxDeltaTheta: innerDeltaTheta |
365 ); | 342 ); |
366 child.parentData.theta = innerTheta; | 343 child.parentData.theta = innerTheta; |
367 child.parentData.radius = childRadius; | 344 child.parentData.radius = childRadius; |
368 child.layout(innerConstraints); | 345 child.layout(innerConstraints, parentUsesSize: true); |
369 childRadius += child.deltaRadius; | 346 childRadius += child.deltaRadius; |
370 remainingDeltaRadius -= child.deltaRadius; | 347 remainingDeltaRadius -= child.deltaRadius; |
371 assert(child.parentData is SectorChildListParentData); | 348 assert(child.parentData is SectorChildListParentData); |
372 child = child.parentData.nextSibling; | 349 child = child.parentData.nextSibling; |
373 childRadius += padding; | 350 childRadius += padding; |
374 remainingDeltaRadius -= padding; | 351 remainingDeltaRadius -= padding; |
375 } | 352 } |
376 deltaRadius = childRadius - this.parentData.radius; | 353 deltaRadius = childRadius - this.parentData.radius; |
377 } | 354 } |
378 | 355 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 return new BoxDimensions.withConstraints(constraints, width: 0.0, height:
0.0); | 403 return new BoxDimensions.withConstraints(constraints, width: 0.0, height:
0.0); |
427 assert(child is RenderSector); | 404 assert(child is RenderSector); |
428 assert(child.parentData is SectorParentData); | 405 assert(child.parentData is SectorParentData); |
429 assert(!constraints.isInfinite); | 406 assert(!constraints.isInfinite); |
430 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxH
eight) / 2.0 - innerRadius; | 407 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxH
eight) / 2.0 - innerRadius; |
431 SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorCo
nstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius); | 408 SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorCo
nstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius); |
432 double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0; | 409 double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0; |
433 return new BoxDimensions.withConstraints(constraints, width: dimension, heig
ht: dimension); | 410 return new BoxDimensions.withConstraints(constraints, width: dimension, heig
ht: dimension); |
434 } | 411 } |
435 | 412 |
436 void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { | 413 void performLayout() { |
437 if (relayoutSubtreeRoot != null) | |
438 saveRelayoutSubtreeRoot(relayoutSubtreeRoot); | |
439 relayoutSubtreeRoot = relayoutSubtreeRoot == null ? this : relayoutSubtreeRo
ot; | |
440 BoxDimensions ourDimensions; | 414 BoxDimensions ourDimensions; |
441 if (child == null) { | 415 if (child == null) { |
442 ourDimensions = new BoxDimensions.withConstraints(constraints, width: 0.0,
height: 0.0); | 416 ourDimensions = new BoxDimensions.withConstraints(constraints, width: 0.0,
height: 0.0); |
443 } else { | 417 } else { |
444 assert(child is RenderSector); | 418 assert(child is RenderSector); |
445 assert(!constraints.isInfinite); | 419 assert(!constraints.isInfinite); |
446 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.ma
xHeight) / 2.0 - innerRadius; | 420 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.ma
xHeight) / 2.0 - innerRadius; |
447 assert(child.parentData is SectorParentData); | 421 assert(child.parentData is SectorParentData); |
448 child.parentData.radius = innerRadius; | 422 child.parentData.radius = innerRadius; |
449 child.parentData.theta = 0.0; | 423 child.parentData.theta = 0.0; |
450 child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), r
elayoutSubtreeRoot: relayoutSubtreeRoot); | 424 child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), p
arentUsesSize: true); |
451 double dimension = (innerRadius + child.deltaRadius) * 2.0; | 425 double dimension = (innerRadius + child.deltaRadius) * 2.0; |
452 ourDimensions = new BoxDimensions.withConstraints(constraints, width: dime
nsion, height: dimension); | 426 ourDimensions = new BoxDimensions.withConstraints(constraints, width: dime
nsion, height: dimension); |
453 } | 427 } |
454 width = ourDimensions.width; | 428 width = ourDimensions.width; |
455 height = ourDimensions.height; | 429 height = ourDimensions.height; |
456 layoutDone(); | |
457 } | 430 } |
458 | 431 |
459 double width; | 432 double width; |
460 double height; | 433 double height; |
461 | 434 |
462 // paint origin is 0,0 of our circle | 435 // paint origin is 0,0 of our circle |
463 void paint(RenderNodeDisplayList canvas) { | 436 void paint(RenderNodeDisplayList canvas) { |
464 super.paint(canvas); | 437 super.paint(canvas); |
465 if (child != null) | 438 if (child != null) |
466 canvas.paintChild(child, width/2.0, height/2.0); | 439 canvas.paintChild(child, width/2.0, height/2.0); |
(...skipping 29 matching lines...) Expand all Loading... |
496 super(new BoxDecoration(backgroundColor: backgroundColor)); | 469 super(new BoxDecoration(backgroundColor: backgroundColor)); |
497 | 470 |
498 double desiredDeltaRadius; | 471 double desiredDeltaRadius; |
499 double desiredDeltaTheta; | 472 double desiredDeltaTheta; |
500 final int backgroundColor; | 473 final int backgroundColor; |
501 | 474 |
502 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double
radius) { | 475 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double
radius) { |
503 return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); /
/ 1.0 radians | 476 return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); /
/ 1.0 radians |
504 } | 477 } |
505 | 478 |
506 void layout(SectorConstraints constraints, { RenderNode relayoutSubtreeRoot })
{ | 479 void performLayout() { |
507 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); | 480 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); |
508 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); | 481 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); |
509 layoutDone(); | |
510 } | 482 } |
511 | 483 |
512 void handlePointer(sky.PointerEvent event) { | 484 void handlePointer(sky.PointerEvent event) { |
513 if (event.type == 'pointerdown') | 485 if (event.type == 'pointerdown') |
514 decoration = new BoxDecoration(backgroundColor: 0xFFFF0000); | 486 decoration = new BoxDecoration(backgroundColor: 0xFFFF0000); |
515 else if (event.type == 'pointerup') | 487 else if (event.type == 'pointerup') |
516 decoration = new BoxDecoration(backgroundColor: backgroundColor); | 488 decoration = new BoxDecoration(backgroundColor: backgroundColor); |
517 } | 489 } |
518 } | 490 } |
519 | 491 |
520 AppView app; | 492 AppView app; |
521 | 493 |
522 void main() { | 494 void main() { |
523 | 495 |
524 var rootCircle = new RenderSectorRing(padding: 20.0); | 496 var rootCircle = new RenderSectorRing(padding: 20.0); |
525 rootCircle.add(new RenderSolidColor(0xFF00FFFF, desiredDeltaTheta: kTwoPi * 0.
15)); | 497 rootCircle.add(new RenderSolidColor(0xFF00FFFF, desiredDeltaTheta: kTwoPi * 0.
15)); |
526 rootCircle.add(new RenderSolidColor(0xFF0000FF, desiredDeltaTheta: kTwoPi * 0.
4)); | 498 rootCircle.add(new RenderSolidColor(0xFF0000FF, desiredDeltaTheta: kTwoPi * 0.
4)); |
527 var stack = new RenderSectorSlice(padding: 2.0); | 499 var stack = new RenderSectorSlice(padding: 2.0); |
528 stack.add(new RenderSolidColor(0xFFFFFF00, desiredDeltaRadius: 20.0)); | 500 stack.add(new RenderSolidColor(0xFFFFFF00, desiredDeltaRadius: 20.0)); |
529 stack.add(new RenderSolidColor(0xFFFF9000, desiredDeltaRadius: 20.0)); | 501 stack.add(new RenderSolidColor(0xFFFF9000, desiredDeltaRadius: 20.0)); |
530 stack.add(new RenderSolidColor(0xFF00FF00)); | 502 stack.add(new RenderSolidColor(0xFF00FF00)); |
531 rootCircle.add(stack); | 503 rootCircle.add(stack); |
532 | 504 |
533 var root = new RenderBoxToRenderSectorAdapter(innerRadius: 50.0, child: rootCi
rcle); | 505 var root = new RenderBoxToRenderSectorAdapter(innerRadius: 50.0, child: rootCi
rcle); |
534 app = new AppView(root); | 506 app = new AppView(root); |
535 } | 507 } |
OLD | NEW |