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 '../animation/animated_value.dart'; | |
| 5 import '../painting/box_painter.dart'; | 6 import '../painting/box_painter.dart'; |
| 6 import '../theme/colors.dart' as colors; | 7 import '../theme/colors.dart' as colors; |
| 7 import '../theme/edges.dart'; | 8 import '../theme/edges.dart'; |
| 8 import '../theme/shadows.dart'; | 9 import '../theme/shadows.dart'; |
| 10 import 'animated_component.dart'; | |
| 9 import 'basic.dart'; | 11 import 'basic.dart'; |
| 10 import 'default_text_style.dart'; | 12 import 'default_text_style.dart'; |
| 11 import 'theme.dart'; | 13 import 'theme.dart'; |
| 12 | 14 |
| 13 class Material extends Component { | 15 // Ugly code to linearly interpolate 2 BoxShadows. |
|
Hixie
2015/06/25 21:00:59
This logic should go in ../painting/ somewhere.
| |
| 16 double lerpDouble(double p1, double p2, double t) => p1 * (1.0 - t) + p2 * t; | |
| 17 | |
| 18 Color lerpColor(Color p1, Color p2, double t) => | |
| 19 new Color.fromARGB((p1.alpha * (1.0 - t) + p2.alpha * t).toInt(), | |
| 20 (p1.red * (1.0 - t) + p2.red * t).toInt(), | |
| 21 (p1.green * (1.0 - t) + p2.green * t).toInt(), | |
| 22 (p1.blue * (1.0 - t) + p2.blue * t).toInt()); | |
| 23 | |
| 24 Size lerpSize(Size p1, Size p2, double t) => | |
| 25 new Size(p1.width * (1.0 - t) + p2.width * t, | |
| 26 p1.height * (1.0 - t) + p2.height * t); | |
| 27 | |
| 28 BoxShadow lerpShadow(BoxShadow p1, BoxShadow p2, double t) { | |
| 29 return new BoxShadow( | |
| 30 color: lerpColor(p1.color, p2.color, t), | |
| 31 offset: lerpSize(p1.offset, p2.offset, t), | |
| 32 blur: lerpDouble(p1.blur, p2.blur, t)); | |
| 33 } | |
| 34 | |
| 35 List<BoxShadow> getShadow(double t) { | |
|
Hixie
2015/06/25 21:00:59
computeShadow(), maybe? getShadow() sounds cheaper
Matt Perry
2015/06/25 22:31:46
Done.
| |
| 36 int level1 = t.floor(); | |
| 37 int level2 = t.ceil(); | |
| 38 t -= level1.toDouble(); | |
| 39 if (level1 == 0) | |
| 40 return shadows[level2]; | |
|
Hixie
2015/06/25 21:00:59
this line confuses me
Matt Perry
2015/06/25 22:31:46
clarified and added a comment.
| |
| 41 List<BoxShadow> shadow = new List<BoxShadow>(); | |
| 42 for (int i = 0; i < shadows[level1].length; ++i) | |
| 43 shadow.add(lerpShadow(shadows[level1][i], shadows[level2][i], t)); | |
| 44 return shadow; | |
| 45 } | |
| 46 | |
| 47 class Material extends AnimatedComponent { | |
| 14 | 48 |
| 15 Material({ | 49 Material({ |
| 16 String key, | 50 String key, |
| 17 this.child, | 51 this.child, |
| 18 this.edge: MaterialEdge.card, | 52 this.edge: MaterialEdge.card, |
| 19 this.level: 0, | 53 int level: 0, |
| 20 this.color | 54 this.color |
| 21 }) : super(key: key); | 55 }) : super(key: key) { |
| 56 this.level = new AnimatedValue(level.toDouble()); | |
|
Hixie
2015/06/25 21:00:59
Doing this every time Material() is constructed (m
Matt Perry
2015/06/25 22:31:46
I'm doing this here because animate() requires tha
| |
| 57 animate(this.level, (_) {}); | |
| 58 } | |
| 22 | 59 |
| 23 final Widget child; | 60 AnimatedValue level; |
| 24 final int level; | 61 |
| 25 final MaterialEdge edge; | 62 Widget child; |
|
Hixie
2015/06/25 21:00:58
maintain the same order here as in the constructor
Matt Perry
2015/06/25 22:31:46
Done.
| |
| 26 final Color color; | 63 MaterialEdge edge; |
| 64 Color color; | |
| 27 | 65 |
| 28 Color get backgroundColor { | 66 Color get backgroundColor { |
| 29 if (color != null) | 67 if (color != null) |
| 30 return color; | 68 return color; |
| 31 switch (Theme.of(this).brightness) { | 69 switch (Theme.of(this).brightness) { |
| 32 case ThemeBrightness.light: | 70 case ThemeBrightness.light: |
| 33 return colors.Grey[50]; | 71 return colors.Grey[50]; |
| 34 case ThemeBrightness.dark: | 72 case ThemeBrightness.dark: |
| 35 return colors.Grey[850]; | 73 return colors.Grey[850]; |
| 36 } | 74 } |
| 37 } | 75 } |
| 38 | 76 |
| 77 void syncFields(Material source) { | |
| 78 if (level.value != source.level.value) { | |
|
Hixie
2015/06/25 21:00:59
(no {} around one-line blocks)
Matt Perry
2015/06/25 22:31:46
Done.
| |
| 79 level.animateTo(source.level.value.toDouble(), 200.0); | |
|
Hixie
2015/06/25 21:00:59
the 200.0 should use a constant from theme/ or som
| |
| 80 } | |
| 81 child = source.child; | |
| 82 edge = source.edge; | |
| 83 color = source.color; | |
| 84 super.syncFields(source); | |
| 85 } | |
| 86 | |
| 39 // TODO(ianh): we should make this animate level changes and color changes | 87 // TODO(ianh): we should make this animate level changes and color changes |
|
Hixie
2015/06/25 21:00:59
remove the TODO if you're doing it :-D
Matt Perry
2015/06/25 22:31:46
Done.
| |
| 40 | 88 |
| 41 Widget build() { | 89 Widget build() { |
| 42 return new Container( | 90 return new Container( |
| 43 decoration: new BoxDecoration( | 91 decoration: new BoxDecoration( |
| 44 boxShadow: shadows[level], | 92 boxShadow: getShadow(level.value), |
| 45 borderRadius: edges[edge], | 93 borderRadius: edges[edge], |
| 46 backgroundColor: backgroundColor, | 94 backgroundColor: backgroundColor, |
| 47 shape: edge == MaterialEdge.circle ? Shape.circle : Shape.rectangle | 95 shape: edge == MaterialEdge.circle ? Shape.circle : Shape.rectangle |
| 48 ), | 96 ), |
| 49 child: new DefaultTextStyle(style: Theme.of(this).text.body1, child: child ) | 97 child: new DefaultTextStyle(style: Theme.of(this).text.body1, child: child ) |
| 50 ); | 98 ); |
| 51 } | 99 } |
| 52 | 100 |
| 53 } | 101 } |
| OLD | NEW |