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:sky' as sky; | 6 import 'dart:sky' as sky; |
6 import 'dart:sky' show Point, Size, Rect, Color, Paint, Path; | 7 import 'dart:sky' show Point, Size, Rect, Color, Paint, Path; |
7 import 'shadows.dart'; | 8 import 'shadows.dart'; |
8 | 9 |
9 class BorderSide { | 10 class BorderSide { |
10 const BorderSide({ | 11 const BorderSide({ |
11 this.color: const Color(0xFF000000), | 12 this.color: const Color(0xFF000000), |
12 this.width: 1.0 | 13 this.width: 1.0 |
13 }); | 14 }); |
14 final Color color; | 15 final Color color; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 this.colorStops, this.tileMode); | 113 this.colorStops, this.tileMode); |
113 } | 114 } |
114 | 115 |
115 final Point center; | 116 final Point center; |
116 final double radius; | 117 final double radius; |
117 final List<Color> colors; | 118 final List<Color> colors; |
118 final List<double> colorStops; | 119 final List<double> colorStops; |
119 final sky.TileMode tileMode; | 120 final sky.TileMode tileMode; |
120 } | 121 } |
121 | 122 |
| 123 enum Shape { rectangle, circle } |
| 124 |
122 // This must be immutable, because we won't notice when it changes | 125 // This must be immutable, because we won't notice when it changes |
123 class BoxDecoration { | 126 class BoxDecoration { |
124 const BoxDecoration({ | 127 const BoxDecoration({ |
125 this.backgroundColor, | 128 this.backgroundColor, // null = don't draw background |
126 this.border, | 129 this.border, // null = don't draw border |
127 this.borderRadius, | 130 this.borderRadius, // null = use more efficient border drawing; note that th
is must be null for circles |
128 this.boxShadow, | 131 this.boxShadow, // null = don't draw shadows |
129 this.gradient | 132 this.gradient, // null = don't allocate gradient objects |
| 133 this.shape: Shape.rectangle |
130 }); | 134 }); |
131 | 135 |
132 final Color backgroundColor; | 136 final Color backgroundColor; |
133 final double borderRadius; | 137 final double borderRadius; |
134 final Border border; | 138 final Border border; |
135 final List<BoxShadow> boxShadow; | 139 final List<BoxShadow> boxShadow; |
136 final Gradient gradient; | 140 final Gradient gradient; |
| 141 final Shape shape; |
137 | 142 |
138 String toString([String prefix = '']) { | 143 String toString([String prefix = '']) { |
139 List<String> result = []; | 144 List<String> result = []; |
140 if (backgroundColor != null) | 145 if (backgroundColor != null) |
141 result.add('${prefix}backgroundColor: $backgroundColor'); | 146 result.add('${prefix}backgroundColor: $backgroundColor'); |
142 if (border != null) | 147 if (border != null) |
143 result.add('${prefix}border: $border'); | 148 result.add('${prefix}border: $border'); |
144 if (borderRadius != null) | 149 if (borderRadius != null) |
145 result.add('${prefix}borderRadius: $borderRadius'); | 150 result.add('${prefix}borderRadius: $borderRadius'); |
146 if (boxShadow != null) | 151 if (boxShadow != null) |
147 result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toStrin
g())}'); | 152 result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toStrin
g())}'); |
148 if (gradient != null) | 153 if (gradient != null) |
149 result.add('${prefix}gradient: $gradient'); | 154 result.add('${prefix}gradient: $gradient'); |
| 155 if (shape != Shape.rectangle) |
| 156 result.add('${prefix}shape: $shape'); |
150 if (result.isEmpty) | 157 if (result.isEmpty) |
151 return '${prefix}<no decorations specified>'; | 158 return '${prefix}<no decorations specified>'; |
152 return result.join('\n'); | 159 return result.join('\n'); |
153 } | 160 } |
154 } | 161 } |
155 | 162 |
156 class BoxPainter { | 163 class BoxPainter { |
157 BoxPainter(BoxDecoration decoration) : _decoration = decoration { | 164 BoxPainter(BoxDecoration decoration) : _decoration = decoration { |
158 assert(decoration != null); | 165 assert(decoration != null); |
159 } | 166 } |
(...skipping 28 matching lines...) Expand all Loading... |
188 | 195 |
189 _cachedBackgroundPaint = paint; | 196 _cachedBackgroundPaint = paint; |
190 } | 197 } |
191 | 198 |
192 return _cachedBackgroundPaint; | 199 return _cachedBackgroundPaint; |
193 } | 200 } |
194 | 201 |
195 void paint(sky.Canvas canvas, Rect rect) { | 202 void paint(sky.Canvas canvas, Rect rect) { |
196 if (_decoration.backgroundColor != null || _decoration.boxShadow != null || | 203 if (_decoration.backgroundColor != null || _decoration.boxShadow != null || |
197 _decoration.gradient != null) { | 204 _decoration.gradient != null) { |
198 if (_decoration.borderRadius == null) | 205 switch (_decoration.shape) { |
199 canvas.drawRect(rect, _backgroundPaint); | 206 case Shape.circle: |
200 else | 207 assert(_decoration.borderRadius == null); |
201 canvas.drawRRect(new sky.RRect()..setRectXY(rect, _decoration.borderRadi
us, _decoration.borderRadius), _backgroundPaint); | 208 Point center = rect.center; |
| 209 Size size = rect.size; |
| 210 double radius = math.min(size.width, size.height) / 2.0; |
| 211 canvas.drawCircle(center.x, center.y, radius, _backgroundPaint); |
| 212 break; |
| 213 case Shape.rectangle: |
| 214 if (_decoration.borderRadius == null) |
| 215 canvas.drawRect(rect, _backgroundPaint); |
| 216 else |
| 217 canvas.drawRRect(new sky.RRect()..setRectXY(rect, _decoration.border
Radius, _decoration.borderRadius), _backgroundPaint); |
| 218 break; |
| 219 } |
202 } | 220 } |
203 | 221 |
204 if (_decoration.border != null) { | 222 if (_decoration.border != null) { |
205 assert(_decoration.borderRadius == null); // TODO(abarth): Implement borde
rs with border radius. | 223 assert(_decoration.borderRadius == null); // TODO(abarth): Implement borde
rs with border radius. |
| 224 assert(_decoration.shape == Shape.rectangle); // TODO(ianh): Implement bor
ders on circles. |
206 | 225 |
207 assert(_decoration.border.top != null); | 226 assert(_decoration.border.top != null); |
208 assert(_decoration.border.right != null); | 227 assert(_decoration.border.right != null); |
209 assert(_decoration.border.bottom != null); | 228 assert(_decoration.border.bottom != null); |
210 assert(_decoration.border.left != null); | 229 assert(_decoration.border.left != null); |
211 | 230 |
212 Paint paint = new Paint(); | 231 Paint paint = new Paint(); |
213 Path path; | 232 Path path; |
214 | 233 |
215 paint.color = _decoration.border.top.color; | 234 paint.color = _decoration.border.top.color; |
(...skipping 27 matching lines...) Expand all Loading... |
243 path = new Path(); | 262 path = new Path(); |
244 path.moveTo(rect.left, rect.bottom); | 263 path.moveTo(rect.left, rect.bottom); |
245 path.lineTo(rect.left + _decoration.border.left.width, rect.bottom - _deco
ration.border.bottom.width); | 264 path.lineTo(rect.left + _decoration.border.left.width, rect.bottom - _deco
ration.border.bottom.width); |
246 path.lineTo(rect.left + _decoration.border.left.width, rect.top + _decorat
ion.border.top.width); | 265 path.lineTo(rect.left + _decoration.border.left.width, rect.top + _decorat
ion.border.top.width); |
247 path.lineTo(rect.left, rect.top); | 266 path.lineTo(rect.left, rect.top); |
248 path.close(); | 267 path.close(); |
249 canvas.drawPath(path, paint); | 268 canvas.drawPath(path, paint); |
250 } | 269 } |
251 } | 270 } |
252 } | 271 } |
OLD | NEW |