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'; | 5 import 'dart:math'; |
6 import 'dart:sky'; | 6 import 'dart:sky'; |
7 import 'package:sky/framework/layout2.dart'; | 7 import 'package:sky/framework/layout2.dart'; |
8 | 8 |
9 class RenderSolidColor extends RenderDecoratedBox { | 9 class RenderSolidColor extends RenderDecoratedBox { |
10 final int backgroundColor; | 10 final int backgroundColor; |
11 | 11 |
12 RenderSolidColor(int backgroundColor) | 12 RenderSolidColor(int backgroundColor) |
13 : super(new BoxDecoration(backgroundColor: backgroundColor)), | 13 : super(new BoxDecoration(backgroundColor: backgroundColor)), |
14 backgroundColor = backgroundColor; | 14 backgroundColor = backgroundColor; |
15 | 15 |
16 BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) { | |
17 return new BoxDimensions.withConstraints(constraints, height: 200.0); | |
18 } | |
19 | |
20 void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { | |
21 width = constraints.constrainWidth(constraints.maxWidth); | |
22 height = constraints.constrainHeight(200.0); | |
23 layoutDone(); | |
24 } | |
25 | |
26 bool handlePointer(PointerEvent event, { double x: 0.0, double y: 0.0 }) { | 16 bool handlePointer(PointerEvent event, { double x: 0.0, double y: 0.0 }) { |
27 if (event.type == 'pointerdown') { | 17 if (event.type == 'pointerdown') { |
28 setBoxDecoration(new BoxDecoration(backgroundColor: 0xFFFF0000)); | 18 setBoxDecoration(new BoxDecoration(backgroundColor: 0xFFFF0000)); |
29 return true; | 19 return true; |
30 } | 20 } |
31 | 21 |
32 if (event.type == 'pointerup') { | 22 if (event.type == 'pointerup') { |
33 setBoxDecoration(new BoxDecoration(backgroundColor: backgroundColor)); | 23 setBoxDecoration(new BoxDecoration(backgroundColor: backgroundColor)); |
34 return true; | 24 return true; |
35 } | 25 } |
36 | 26 |
37 return false; | 27 return false; |
38 } | 28 } |
39 } | 29 } |
40 | 30 |
| 31 class RenderSolidColorBlock extends RenderSolidColor { |
| 32 final double desiredHeight; |
| 33 |
| 34 RenderSolidColorBlock(int backgroundColor, { double desiredHeight : 100.0 }) |
| 35 : super(backgroundColor), desiredHeight = desiredHeight; |
| 36 |
| 37 BoxDimensions getIntrinsicDimensions(BoxConstraints constraints) { |
| 38 return new BoxDimensions.withConstraints(constraints, height: requestedHeigh
t); |
| 39 } |
| 40 |
| 41 void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { |
| 42 width = constraints.constrainWidth(constraints.maxWidth); |
| 43 height = constraints.constrainHeight(desiredHeight); |
| 44 layoutDone(); |
| 45 } |
| 46 } |
| 47 |
| 48 class RenderSolidColorFlex extends RenderSolidColor { |
| 49 RenderSolidColorFlex(int backgroundColor, flex) |
| 50 : super(backgroundColor) { |
| 51 parentData = new FlexBoxParentData(); |
| 52 parentData.flex = flex; |
| 53 } |
| 54 |
| 55 void layout(BoxConstraints constraints, { RenderNode relayoutSubtreeRoot }) { |
| 56 width = constraints.constrainWidth(constraints.maxWidth); |
| 57 height = constraints.constrainHeight(constraints.maxHeight); |
| 58 layoutDone(); |
| 59 } |
| 60 } |
| 61 |
41 RenderView renderView; | 62 RenderView renderView; |
42 | 63 |
43 void beginFrame(double timeStamp) { | 64 void beginFrame(double timeStamp) { |
44 RenderNode.flushLayout(); | 65 RenderNode.flushLayout(); |
45 | 66 |
46 renderView.paintFrame(); | 67 renderView.paintFrame(); |
47 } | 68 } |
48 | 69 |
49 bool handleEvent(Event event) { | 70 bool handleEvent(Event event) { |
50 if (event is! PointerEvent) | 71 if (event is! PointerEvent) |
51 return false; | 72 return false; |
52 return renderView.handlePointer(event, x: event.x, y: event.y); | 73 return renderView.handlePointer(event, x: event.x, y: event.y); |
53 } | 74 } |
54 | 75 |
55 void main() { | 76 void main() { |
56 view.setEventCallback(handleEvent); | 77 view.setEventCallback(handleEvent); |
57 view.setBeginFrameCallback(beginFrame); | 78 view.setBeginFrameCallback(beginFrame); |
58 | 79 |
59 var root = new RenderBlock( | 80 var root = new RenderFlex( |
60 decoration: new BoxDecoration(backgroundColor: 0xFF00FFFF), | 81 direction: FlexDirection.Column, |
| 82 decoration: new BoxDecoration(backgroundColor: 0xFFFFFFFF)); |
| 83 |
| 84 var block = new RenderBlock( |
| 85 decoration: new BoxDecoration(backgroundColor: 0x77000000), |
61 padding: const EdgeDims(10.0, 10.0, 10.0, 10.0)); | 86 padding: const EdgeDims(10.0, 10.0, 10.0, 10.0)); |
| 87 block.add(new RenderSolidColorBlock(0xFF00FF00)); |
| 88 block.add(new RenderSolidColorBlock(0x3300FFFF)); |
62 | 89 |
63 root.add(new RenderSolidColor(0xFF00FF00)); | 90 root.add(new RenderSolidColorFlex(0xFFFFFF00, 1)); |
64 root.add(new RenderSolidColor(0xFF0000FF)); | 91 root.add(block); |
| 92 root.add(new RenderSolidColorFlex(0xFF0000FF, 1)); |
| 93 root.add(new RenderSolidColorFlex(0x77FF00FF, 2)); |
65 | 94 |
66 renderView = new RenderView(root: root); | 95 renderView = new RenderView(root: root); |
67 renderView.layout(newWidth: view.width, newHeight: view.height); | 96 renderView.layout(newWidth: view.width, newHeight: view.height); |
68 | 97 |
69 view.scheduleFrame(); | 98 view.scheduleFrame(); |
70 } | 99 } |
OLD | NEW |