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 '../fn2.dart'; | 5 import '../fn2.dart'; |
6 import '../theme/typography.dart' as typography; | |
7 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
8 import '../rendering/box.dart'; | 7 import '../rendering/box.dart'; |
9 import '../rendering/node.dart'; | 8 import '../rendering/node.dart'; |
10 | 9 |
11 // RenderNode | 10 |
| 11 enum ScaffoldSlots { |
| 12 Toolbar, |
| 13 Body, |
| 14 StatusBar, |
| 15 Drawer, |
| 16 FloatingActionButton |
| 17 } |
| 18 |
12 class RenderScaffold extends RenderBox { | 19 class RenderScaffold extends RenderBox { |
13 | 20 |
14 RenderScaffold({ | 21 RenderScaffold({ |
15 RenderBox toolbar, | 22 RenderBox toolbar, |
16 RenderBox body, | 23 RenderBox body, |
17 RenderBox statusbar, | 24 RenderBox statusbar, |
18 RenderBox drawer, | 25 RenderBox drawer, |
19 RenderBox floatingActionButton | 26 RenderBox floatingActionButton |
20 }) { | 27 }) { |
21 this.toolbar = toolbar; | 28 this[ScaffoldSlots.Toolbar] = toolbar; |
22 this.body = body; | 29 this[ScaffoldSlots.Body] = body; |
23 this.statusbar = statusbar; | 30 this[ScaffoldSlots.StatusBar] = statusbar; |
24 this.drawer = drawer; | 31 this[ScaffoldSlots.Drawer] = drawer; |
25 this.floatingActionButton = floatingActionButton; | 32 this[ScaffoldSlots.FloatingActionButton] = floatingActionButton; |
26 } | 33 } |
27 | 34 |
28 RenderBox _toolbar; | 35 Map<ScaffoldSlots, RenderBox> _slots = new Map<ScaffoldSlots, RenderBox>(); |
29 RenderBox get toolbar => _toolbar; | 36 RenderBox operator[] (ScaffoldSlots slot) => _slots[slot]; |
30 void set toolbar (RenderBox value) { | 37 void operator[]= (ScaffoldSlots slot, RenderBox value) { |
31 if (_toolbar != null) | 38 RenderBox old = _slots[slot]; |
32 dropChild(_toolbar); | 39 if (old == value) |
33 _toolbar = value; | 40 return; |
34 if (_toolbar != null) | 41 if (old != null) |
35 adoptChild(_toolbar); | 42 dropChild(old); |
| 43 _slots[slot] = value; |
| 44 if (value != null) |
| 45 adoptChild(value); |
36 markNeedsLayout(); | 46 markNeedsLayout(); |
37 } | 47 } |
38 | 48 |
39 RenderBox _body; | 49 ScaffoldSlots remove(RenderBox child) { |
40 RenderBox get body => _body; | 50 assert(child != null); |
41 void set body (RenderBox value) { | 51 for (ScaffoldSlots slot in ScaffoldSlots) { |
42 if (_body != null) | 52 if (_slots[slot] == child) { |
43 dropChild(_body); | 53 this[slot] = null; |
44 _body = value; | 54 return slot; |
45 if (_body != null) | 55 } |
46 adoptChild(_body); | 56 } |
47 markNeedsLayout(); | 57 return null; |
48 } | |
49 | |
50 RenderBox _statusbar; | |
51 RenderBox get statusbar => _statusbar; | |
52 void set statusbar (RenderBox value) { | |
53 if (_statusbar != null) | |
54 dropChild(_statusbar); | |
55 _statusbar = value; | |
56 if (_statusbar != null) | |
57 adoptChild(_statusbar); | |
58 markNeedsLayout(); | |
59 } | |
60 | |
61 RenderBox _drawer; | |
62 RenderBox get drawer => _drawer; | |
63 void set drawer (RenderBox value) { | |
64 if (_drawer != null) | |
65 dropChild(_drawer); | |
66 _drawer = value; | |
67 if (_drawer != null) | |
68 adoptChild(_drawer); | |
69 markNeedsLayout(); | |
70 } | |
71 | |
72 RenderBox _floatingActionButton; | |
73 RenderBox get floatingActionButton => _floatingActionButton; | |
74 void set floatingActionButton (RenderBox value) { | |
75 if (_floatingActionButton != null) | |
76 dropChild(_floatingActionButton); | |
77 _floatingActionButton = value; | |
78 if (_floatingActionButton != null) | |
79 adoptChild(_floatingActionButton); | |
80 markNeedsLayout(); | |
81 } | 58 } |
82 | 59 |
83 bool get sizedByParent => true; | 60 bool get sizedByParent => true; |
84 void performResize() { | 61 void performResize() { |
85 size = constraints.constrain(new sky.Size.infinite()); | 62 size = constraints.constrain(new sky.Size.infinite()); |
86 assert(size.width < double.INFINITY); | 63 assert(size.width < double.INFINITY); |
87 assert(size.height < double.INFINITY); | 64 assert(size.height < double.INFINITY); |
88 } | 65 } |
89 | 66 |
90 static const kToolbarHeight = 100.0; | 67 static const kToolbarHeight = 100.0; |
91 static const kStatusbarHeight = 50.0; | 68 static const kStatusbarHeight = 50.0; |
92 static const kButtonX = -16.0; // from right edge of body | 69 static const kButtonX = -16.0; // from right edge of body |
93 static const kButtonY = -16.0; // from bottom edge of body | 70 static const kButtonY = -16.0; // from bottom edge of body |
94 | 71 |
95 void performLayout() { | 72 void performLayout() { |
96 double bodyHeight = size.height; | 73 double bodyHeight = size.height; |
97 double bodyPosition = 0.0; | 74 double bodyPosition = 0.0; |
98 if (toolbar != null) { | 75 if (_slots[ScaffoldSlots.Toolbar] != null) { |
| 76 RenderBox toolbar = _slots[ScaffoldSlots.Toolbar]; |
99 toolbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kToolbarH
eight))); | 77 toolbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kToolbarH
eight))); |
100 assert(toolbar.parentData is BoxParentData); | 78 assert(toolbar.parentData is BoxParentData); |
101 toolbar.parentData.position = new sky.Point(0.0, 0.0); | 79 toolbar.parentData.position = new sky.Point(0.0, 0.0); |
102 bodyPosition = kToolbarHeight; | 80 bodyPosition = kToolbarHeight; |
103 bodyHeight -= kToolbarHeight; | 81 bodyHeight -= kToolbarHeight; |
104 } | 82 } |
105 if (statusbar != null) { | 83 if (_slots[ScaffoldSlots.StatusBar] != null) { |
| 84 RenderBox statusbar = _slots[ScaffoldSlots.StatusBar]; |
106 statusbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kStatus
barHeight))); | 85 statusbar.layout(new BoxConstraints.tight(new sky.Size(size.width, kStatus
barHeight))); |
107 assert(statusbar.parentData is BoxParentData); | 86 assert(statusbar.parentData is BoxParentData); |
108 statusbar.parentData.position = new sky.Point(0.0, size.height - kStatusba
rHeight); | 87 statusbar.parentData.position = new sky.Point(0.0, size.height - kStatusba
rHeight); |
109 bodyHeight -= kStatusbarHeight; | 88 bodyHeight -= kStatusbarHeight; |
110 } | 89 } |
111 if (body != null) { | 90 if (_slots[ScaffoldSlots.Body] != null) { |
| 91 RenderBox body = _slots[ScaffoldSlots.Body]; |
112 body.layout(new BoxConstraints.tight(new sky.Size(size.width, bodyHeight))
); | 92 body.layout(new BoxConstraints.tight(new sky.Size(size.width, bodyHeight))
); |
113 assert(body.parentData is BoxParentData); | 93 assert(body.parentData is BoxParentData); |
114 body.parentData.position = new sky.Point(0.0, bodyPosition); | 94 body.parentData.position = new sky.Point(0.0, bodyPosition); |
115 } | 95 } |
116 if (drawer != null) { | 96 if (_slots[ScaffoldSlots.Drawer] != null) { |
| 97 RenderBox drawer = _slots[ScaffoldSlots.Drawer]; |
117 drawer.layout(new BoxConstraints(minWidth: 0.0, maxWidth: size.width, minH
eight: size.height, maxHeight: size.height)); | 98 drawer.layout(new BoxConstraints(minWidth: 0.0, maxWidth: size.width, minH
eight: size.height, maxHeight: size.height)); |
118 assert(drawer.parentData is BoxParentData); | 99 assert(drawer.parentData is BoxParentData); |
119 drawer.parentData.position = new sky.Point(0.0, 0.0); | 100 drawer.parentData.position = new sky.Point(0.0, 0.0); |
120 } | 101 } |
121 if (floatingActionButton != null) { | 102 if (_slots[ScaffoldSlots.FloatingActionButton] != null) { |
| 103 RenderBox floatingActionButton = _slots[ScaffoldSlots.FloatingActionButton
]; |
122 floatingActionButton.layout(new BoxConstraints(minWidth: 0.0, maxWidth: si
ze.width, minHeight: size.height, maxHeight: size.height)); | 104 floatingActionButton.layout(new BoxConstraints(minWidth: 0.0, maxWidth: si
ze.width, minHeight: size.height, maxHeight: size.height)); |
123 assert(floatingActionButton.parentData is BoxParentData); | 105 assert(floatingActionButton.parentData is BoxParentData); |
124 floatingActionButton.parentData.position = new sky.Point(size.width - kBut
tonX, bodyPosition + bodyHeight - kButtonY); | 106 floatingActionButton.parentData.position = new sky.Point(size.width - kBut
tonX, bodyPosition + bodyHeight - kButtonY); |
125 } | 107 } |
126 } | 108 } |
127 | 109 |
128 void paint(RenderNodeDisplayList canvas) { | 110 void paint(RenderNodeDisplayList canvas) { |
129 if (body != null) | 111 for (ScaffoldSlots slot in [ScaffoldSlots.Body, ScaffoldSlots.StatusBar, Sca
ffoldSlots.Toolbar, ScaffoldSlots.FloatingActionButton, ScaffoldSlots.Drawer]) { |
130 canvas.paintChild(body, (body.parentData as BoxParentData).position); | 112 RenderBox box = _slots[slot]; |
131 if (statusbar != null) | 113 if (box != null) { |
132 canvas.paintChild(statusbar, (statusbar.parentData as BoxParentData).posit
ion); | 114 assert(box.parentData is BoxParentData); |
133 if (toolbar != null) | 115 canvas.paintChild(box, box.parentData.position); |
134 canvas.paintChild(toolbar, (toolbar.parentData as BoxParentData).position)
; | 116 } |
135 if (floatingActionButton != null) | 117 } |
136 canvas.paintChild(floatingActionButton, (floatingActionButton.parentData a
s BoxParentData).position); | |
137 if (drawer != null) | |
138 canvas.paintChild(drawer, (drawer.parentData as BoxParentData).position); | |
139 } | 118 } |
140 | 119 |
141 void hitTestChildren(HitTestResult result, { sky.Point position }) { | 120 void hitTestChildren(HitTestResult result, { sky.Point position }) { |
142 assert(floatingActionButton == null || floatingActionButton.parentData is Bo
xParentData); | 121 for (ScaffoldSlots slot in [ScaffoldSlots.Drawer, ScaffoldSlots.FloatingActi
onButton, ScaffoldSlots.Toolbar, ScaffoldSlots.StatusBar, ScaffoldSlots.Body]) { |
143 assert(statusbar == null || statusbar.parentData is BoxParentData); | 122 RenderBox box = _slots[slot]; |
144 if ((drawer != null) && (position.x < drawer.size.width)) { | 123 if (box != null) { |
145 drawer.hitTest(result, position: position); | 124 assert(box.parentData is BoxParentData); |
146 } else if ((floatingActionButton != null) && (position.x >= floatingActionBu
tton.parentData.position.x) && (position.x < floatingActionButton.parentData.pos
ition.x + floatingActionButton.size.width) | 125 if (new sky.Rect.fromPointAndSize(box.parentData.position, box.size).con
tains(position)) { |
147 && (position.y >= floatingActionBu
tton.parentData.position.y) && (position.y < floatingActionButton.parentData.pos
ition.y + floatingActionButton.size.height)) { | 126 if (box.hitTest(result, position: (position - box.parentData.position)
.toPoint())) |
148 floatingActionButton.hitTest(result, position: new sky.Point(position.x -
floatingActionButton.parentData.position.x, position.y - floatingActionButton.pa
rentData.position.y)); | 127 return; |
149 } else if ((toolbar != null) && (position.y < toolbar.size.height)) { | 128 } |
150 toolbar.hitTest(result, position: position); | 129 } |
151 } else if ((statusbar != null) && (position.y > statusbar.parentData.positio
n.y)) { | |
152 statusbar.hitTest(result, position: new sky.Point(position.x, position.y -
statusbar.parentData.position.y)); | |
153 } else if (body != null) { | |
154 body.hitTest(result, position: new sky.Point(position.x, position.y - body
.parentData.position.y)); | |
155 } | 130 } |
156 } | 131 } |
157 | 132 |
158 } | 133 } |
159 | 134 |
160 class Scaffold extends RenderNodeWrapper { | 135 class Scaffold extends RenderNodeWrapper { |
161 | 136 |
162 // static final Style _style = new Style(''' | 137 // static final Style _style = new Style(''' |
163 // ${typography.typeface}; | 138 // ${typography.typeface}; |
164 // ${typography.black.body1};'''); | 139 // ${typography.black.body1};'''); |
(...skipping 11 matching lines...) Expand all Loading... |
176 | 151 |
177 final UINode toolbar; | 152 final UINode toolbar; |
178 final UINode body; | 153 final UINode body; |
179 final UINode statusbar; | 154 final UINode statusbar; |
180 final UINode drawer; | 155 final UINode drawer; |
181 final UINode floatingActionButton; | 156 final UINode floatingActionButton; |
182 | 157 |
183 RenderScaffold root; | 158 RenderScaffold root; |
184 RenderScaffold createNode() => new RenderScaffold(); | 159 RenderScaffold createNode() => new RenderScaffold(); |
185 | 160 |
186 void insert(RenderNodeWrapper child, dynamic slot) { | 161 void insert(RenderNodeWrapper child, ScaffoldSlots slot) { |
187 switch (slot) { | 162 root[slot] = child != null ? child.root : null; |
188 case #toolbar: root.toolbar = toolbar == null ? null : toolbar.root; break
; | |
189 case #body: root.body = body == null ? null : body.root; break; | |
190 case #statusbar: root.statusbar = statusbar == null ? null : statusbar.roo
t; break; | |
191 case #drawer: root.drawer = drawer == null ? null : drawer.root; break; | |
192 case #floatingActionButton: root.floatingActionButton = floatingActionButt
on == null ? null : floatingActionButton.root; break; | |
193 default: assert(false); | |
194 } | |
195 } | 163 } |
196 | 164 |
197 void removeChild(UINode node) { | 165 void removeChild(UINode node) { |
198 if (node == root.toolbar) | 166 assert(node != null); |
199 root.toolbar = null; | 167 root.remove(node.root); |
200 if (node == root.body) | |
201 root.body = null; | |
202 if (node == root.statusbar) | |
203 root.statusbar = null; | |
204 if (node == root.drawer) | |
205 root.drawer = null; | |
206 if (node == root.floatingActionButton) | |
207 root.floatingActionButton = null; | |
208 super.removeChild(node); | 168 super.removeChild(node); |
209 } | 169 } |
210 | 170 |
211 void syncRenderNode(UINode old) { | 171 void syncRenderNode(UINode old) { |
212 super.syncRenderNode(old); | 172 super.syncRenderNode(old); |
213 syncChild(toolbar, old is Scaffold ? old.toolbar : null, #toolbar); | 173 syncChild(toolbar, old is Scaffold ? old.toolbar : null, ScaffoldSlots.Toolb
ar); |
214 syncChild(body, old is Scaffold ? old.body : null, #body); | 174 syncChild(body, old is Scaffold ? old.body : null, ScaffoldSlots.Body); |
215 syncChild(statusbar, old is Scaffold ? old.statusbar : null, #statusbar); | 175 syncChild(statusbar, old is Scaffold ? old.statusbar : null, ScaffoldSlots.S
tatusBar); |
216 syncChild(drawer, old is Scaffold ? old.drawer : null, #drawer); | 176 syncChild(drawer, old is Scaffold ? old.drawer : null, ScaffoldSlots.Drawer)
; |
217 syncChild(floatingActionButton, old is Scaffold ? old.floatingActionButton :
null, #floatingActionButton); | 177 syncChild(floatingActionButton, old is Scaffold ? old.floatingActionButton :
null, ScaffoldSlots.FloatingActionButton); |
218 } | 178 } |
219 | 179 |
220 } | 180 } |
OLD | NEW |