Chromium Code Reviews| 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 'dart:sky' show Point, Offset, Size, Rect, Color, Paint, Path; | 7 import 'dart:sky' show Point, Offset, Size, Rect, Color, Paint, Path; |
| 8 | 8 |
| 9 import 'shadows.dart'; | 9 import 'shadows.dart'; |
| 10 import 'package:sky/mojo/net/image_cache.dart' as image_cache; | |
| 10 | 11 |
| 11 class BorderSide { | 12 class BorderSide { |
| 12 const BorderSide({ | 13 const BorderSide({ |
| 13 this.color: const Color(0xFF000000), | 14 this.color: const Color(0xFF000000), |
| 14 this.width: 1.0 | 15 this.width: 1.0 |
| 15 }); | 16 }); |
| 16 final Color color; | 17 final Color color; |
| 17 final double width; | 18 final double width; |
| 18 | 19 |
| 19 static const none = const BorderSide(width: 0.0); | 20 static const none = const BorderSide(width: 0.0); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 this.colorStops, this.tileMode); | 115 this.colorStops, this.tileMode); |
| 115 } | 116 } |
| 116 | 117 |
| 117 final Point center; | 118 final Point center; |
| 118 final double radius; | 119 final double radius; |
| 119 final List<Color> colors; | 120 final List<Color> colors; |
| 120 final List<double> colorStops; | 121 final List<double> colorStops; |
| 121 final sky.TileMode tileMode; | 122 final sky.TileMode tileMode; |
| 122 } | 123 } |
| 123 | 124 |
| 125 enum BackgroundFit { fill, contain, cover, none, scaleDown } | |
| 126 | |
| 127 enum BackgroundRepeat { repeat, repeatX, repeatY, noRepeat } | |
| 128 | |
| 129 class BackgroundImage { | |
| 130 final String src; | |
|
Hixie
2015/06/26 23:13:26
I don't think we should have the painter code work
jackson
2015/06/29 16:53:25
I'll do this, added a TODO for now.
| |
| 131 final BackgroundFit fit; | |
| 132 final BackgroundRepeat repeat; | |
| 133 BackgroundImage({ | |
| 134 this.src, | |
| 135 this.fit: BackgroundFit.scaleDown, | |
| 136 this.repeat: BackgroundRepeat.noRepeat | |
| 137 }) { | |
| 138 image_cache.load(src, (image) { | |
| 139 _image = image; | |
| 140 _size = new Size(image.width.toDouble(), image.height.toDouble()); | |
| 141 for (Function listener in _listeners) { | |
| 142 listener(); | |
| 143 } | |
| 144 }); | |
| 145 } | |
| 146 | |
| 147 sky.Image _image; | |
| 148 sky.Image get image => _image; | |
| 149 | |
| 150 Size _size; | |
| 151 | |
| 152 final List<Function> _listeners = new List<Function>(); | |
| 153 | |
| 154 void addChangeListener(Function listener) { | |
| 155 _listeners.add(listener); | |
| 156 } | |
| 157 | |
| 158 void removeChangeListener(Function listener) { | |
| 159 _listeners.remove(listener); | |
| 160 } | |
| 161 | |
| 162 String toString() => 'BackgroundImage($image, $fit, $repeat)'; | |
|
Hixie
2015/06/26 23:13:27
what does Image.toString() do?
jackson
2015/06/29 16:53:24
I changed it to $src, I think that's fine for now,
| |
| 163 } | |
| 164 | |
| 124 enum Shape { rectangle, circle } | 165 enum Shape { rectangle, circle } |
| 125 | 166 |
| 126 // This must be immutable, because we won't notice when it changes | 167 // This must be immutable, because we won't notice when it changes |
| 127 class BoxDecoration { | 168 class BoxDecoration { |
| 128 const BoxDecoration({ | 169 const BoxDecoration({ |
| 129 this.backgroundColor, // null = don't draw background | 170 this.backgroundColor, // null = don't draw background color |
| 171 this.backgroundImage, // null = don't draw background image | |
| 130 this.border, // null = don't draw border | 172 this.border, // null = don't draw border |
| 131 this.borderRadius, // null = use more efficient background drawing; note tha t this must be null for circles | 173 this.borderRadius, // null = use more efficient background drawing; note tha t this must be null for circles |
| 132 this.boxShadow, // null = don't draw shadows | 174 this.boxShadow, // null = don't draw shadows |
| 133 this.gradient, // null = don't allocate gradient objects | 175 this.gradient, // null = don't allocate gradient objects |
| 134 this.shape: Shape.rectangle | 176 this.shape: Shape.rectangle |
| 135 }); | 177 }); |
| 136 | 178 |
| 137 final Color backgroundColor; | 179 final Color backgroundColor; |
| 180 final BackgroundImage backgroundImage; | |
| 138 final double borderRadius; | 181 final double borderRadius; |
| 139 final Border border; | 182 final Border border; |
| 140 final List<BoxShadow> boxShadow; | 183 final List<BoxShadow> boxShadow; |
| 141 final Gradient gradient; | 184 final Gradient gradient; |
| 142 final Shape shape; | 185 final Shape shape; |
| 143 | 186 |
| 144 String toString([String prefix = '']) { | 187 String toString([String prefix = '']) { |
| 145 List<String> result = []; | 188 List<String> result = []; |
| 146 if (backgroundColor != null) | 189 if (backgroundColor != null) |
| 147 result.add('${prefix}backgroundColor: $backgroundColor'); | 190 result.add('${prefix}backgroundColor: $backgroundColor'); |
| 191 if (backgroundImage != null) | |
| 192 result.add('${prefix}backgroundImage: $backgroundImage'); | |
| 148 if (border != null) | 193 if (border != null) |
| 149 result.add('${prefix}border: $border'); | 194 result.add('${prefix}border: $border'); |
| 150 if (borderRadius != null) | 195 if (borderRadius != null) |
| 151 result.add('${prefix}borderRadius: $borderRadius'); | 196 result.add('${prefix}borderRadius: $borderRadius'); |
| 152 if (boxShadow != null) | 197 if (boxShadow != null) |
| 153 result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toStrin g())}'); | 198 result.add('${prefix}boxShadow: ${boxShadow.map((shadow) => shadow.toStrin g())}'); |
| 154 if (gradient != null) | 199 if (gradient != null) |
| 155 result.add('${prefix}gradient: $gradient'); | 200 result.add('${prefix}gradient: $gradient'); |
| 156 if (shape != Shape.rectangle) | 201 if (shape != Shape.rectangle) |
| 157 result.add('${prefix}shape: $shape'); | 202 result.add('${prefix}shape: $shape'); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 canvas.drawCircle(center, radius, _backgroundPaint); | 257 canvas.drawCircle(center, radius, _backgroundPaint); |
| 213 break; | 258 break; |
| 214 case Shape.rectangle: | 259 case Shape.rectangle: |
| 215 if (_decoration.borderRadius == null) | 260 if (_decoration.borderRadius == null) |
| 216 canvas.drawRect(rect, _backgroundPaint); | 261 canvas.drawRect(rect, _backgroundPaint); |
| 217 else | 262 else |
| 218 canvas.drawRRect(new sky.RRect()..setRectXY(rect, _decoration.border Radius, _decoration.borderRadius), _backgroundPaint); | 263 canvas.drawRRect(new sky.RRect()..setRectXY(rect, _decoration.border Radius, _decoration.borderRadius), _backgroundPaint); |
| 219 break; | 264 break; |
| 220 } | 265 } |
| 221 } | 266 } |
| 222 | 267 |
|
Hixie
2015/06/26 23:13:27
we're going to want to split this function into su
jackson
2015/06/29 16:53:25
Done.
| |
| 268 if (_decoration.backgroundImage != null) { | |
| 269 sky.Image image = _decoration.backgroundImage.image; | |
| 270 if (image != null) { | |
| 271 Rect src; | |
| 272 Rect dst; | |
| 273 switch(_decoration.backgroundImage.fit) { | |
| 274 case BackgroundFit.fill: | |
| 275 src = new Rect.fromSize(_decoration.backgroundImage._size); | |
| 276 dst = rect; | |
| 277 break; | |
| 278 case BackgroundFit.contain: | |
| 279 src = new Rect.fromSize(_decoration.backgroundImage._size); | |
| 280 if (rect.size.width / rect.size.height > src.size.width / rect.size. height) { | |
| 281 dst = new Rect.fromLTRB(0.0, 0.0, rect.size.width, src.size.height * rect.size.width / src.size.width); | |
| 282 } else { | |
| 283 dst = new Rect.fromLTRB(0.0, 0.0, src.size.width * rect.size.heigh t / src.size.height, rect.size.height); | |
| 284 } | |
| 285 break; | |
| 286 case BackgroundFit.cover: | |
| 287 src = new Rect.fromSize(_decoration.backgroundImage._size); | |
| 288 if (rect.size.width / rect.size.height > src.size.width / src.size.h eight) { | |
| 289 src = new Rect.fromLTRB(0.0, 0.0, src.size.width, src.size.width * rect.size.height / rect.size.width); | |
| 290 } else { | |
| 291 src = new Rect.fromLTRB(0.0, 0.0, src.size.height * rect.size.widt h / rect.size.height, src.size.height); | |
| 292 } | |
| 293 dst = rect; | |
| 294 break; | |
| 295 case BackgroundFit.none: | |
| 296 assert(false); | |
| 297 // src = // new Rect.fromSize(; | |
| 298 // dst = // rect; | |
|
Hixie
2015/06/26 23:13:27
remove comments here
jackson
2015/06/29 16:53:24
I think for this mode we can just say src = rect a
| |
| 299 break; | |
| 300 case BackgroundFit.scaleDown: | |
| 301 src = new Rect.fromSize(_decoration.backgroundImage._size); | |
| 302 dst = rect; | |
| 303 if (src.size.height > dst.size.height) { | |
| 304 dst = new Rect.fromLTRB(0.0, 0.0, src.size.width * dst.size.height / src.size.height, src.size.height); | |
| 305 } | |
| 306 if (src.size.width > dst.size.width) { | |
| 307 dst = new Rect.fromLTRB(0.0, 0.0, dst.size.width, src.size.height * dst.size.width / src.size.width); | |
| 308 } | |
| 309 break; | |
| 310 } | |
| 311 canvas.drawImageRect(image, src, dst, new Paint()); | |
| 312 } | |
| 313 } | |
| 314 | |
| 223 if (_decoration.border != null) { | 315 if (_decoration.border != null) { |
| 224 assert(_decoration.borderRadius == null); // TODO(abarth): Implement borde rs with border radius. | 316 assert(_decoration.borderRadius == null); // TODO(abarth): Implement borde rs with border radius. |
| 225 assert(_decoration.shape == Shape.rectangle); // TODO(ianh): Implement bor ders on circles. | 317 assert(_decoration.shape == Shape.rectangle); // TODO(ianh): Implement bor ders on circles. |
| 226 | 318 |
| 227 assert(_decoration.border.top != null); | 319 assert(_decoration.border.top != null); |
| 228 assert(_decoration.border.right != null); | 320 assert(_decoration.border.right != null); |
| 229 assert(_decoration.border.bottom != null); | 321 assert(_decoration.border.bottom != null); |
| 230 assert(_decoration.border.left != null); | 322 assert(_decoration.border.left != null); |
| 231 | 323 |
| 232 Paint paint = new Paint(); | 324 Paint paint = new Paint(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 263 path = new Path(); | 355 path = new Path(); |
| 264 path.moveTo(rect.left, rect.bottom); | 356 path.moveTo(rect.left, rect.bottom); |
| 265 path.lineTo(rect.left + _decoration.border.left.width, rect.bottom - _deco ration.border.bottom.width); | 357 path.lineTo(rect.left + _decoration.border.left.width, rect.bottom - _deco ration.border.bottom.width); |
| 266 path.lineTo(rect.left + _decoration.border.left.width, rect.top + _decorat ion.border.top.width); | 358 path.lineTo(rect.left + _decoration.border.left.width, rect.top + _decorat ion.border.top.width); |
| 267 path.lineTo(rect.left, rect.top); | 359 path.lineTo(rect.left, rect.top); |
| 268 path.close(); | 360 path.close(); |
| 269 canvas.drawPath(path, paint); | 361 canvas.drawPath(path, paint); |
| 270 } | 362 } |
| 271 } | 363 } |
| 272 } | 364 } |
| OLD | NEW |