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:sky' as sky; | 5 import 'dart:sky' as sky; |
| 6 | 6 |
| 7 import 'package:sky/framework/theme2/colors.dart' as colors; | 7 import 'package:sky/framework/theme2/colors.dart' as colors; |
| 8 | 8 |
| 9 import '../animation/animated_value.dart'; | |
| 10 import '../animation/curves.dart'; | |
| 9 import '../rendering/box.dart'; | 11 import '../rendering/box.dart'; |
| 10 import 'button_base.dart'; | |
| 11 import 'wrappers.dart'; | 12 import 'wrappers.dart'; |
| 13 import 'animated_component.dart'; | |
|
abarth-chromium
2015/06/12 13:35:14
Please sort these alphabetically.
| |
| 12 | 14 |
| 13 typedef void ValueChanged(value); | 15 typedef void ValueChanged(value); |
| 14 | 16 |
| 15 class Checkbox extends ButtonBase { | 17 const Curve _kAnimationCurve = parabolicRise; |
| 18 const double _kMidpoint = 0.5; | |
| 19 const double _kCheckDuration = 200.0; | |
| 20 const sky.Color _kUncheckedColor = const sky.Color(0x8A000000); | |
| 21 // TODO(jackson): This should change colors with the theme | |
| 22 sky.Color _kCheckedColor = colors.Purple[500]; | |
| 23 const double _kEdgeSize = 20.0; | |
| 24 const double _kEdgeRadius = 1.0; | |
| 16 | 25 |
| 17 Checkbox({ Object key, this.onChanged, this.checked }) : super(key: key); | 26 class Checkbox extends AnimatedComponent { |
| 27 | |
| 28 Checkbox({ | |
| 29 Object key, | |
| 30 this.checked, | |
| 31 this.onChanged | |
| 32 }) : super(key: key) { | |
| 33 checkedAnimation = new AnimatedValue(checked ? 1.0 : 0.0); | |
|
abarth-chromium
2015/06/12 13:35:13
checkedAnimation -> _checkedAnimation ?
| |
| 34 } | |
| 18 | 35 |
| 19 bool checked; | 36 bool checked; |
| 37 AnimatedValue checkedAnimation; | |
| 20 ValueChanged onChanged; | 38 ValueChanged onChanged; |
| 21 | 39 |
| 22 void syncFields(Checkbox source) { | 40 void syncFields(Checkbox source) { |
| 23 checked = source.checked; | |
| 24 onChanged = source.onChanged; | 41 onChanged = source.onChanged; |
| 42 if (checked != source.checked) { | |
| 43 checked = source.checked; | |
| 44 double targetValue = checked ? 1.0 : 0.0; | |
| 45 double difference = (checkedAnimation.value - targetValue).abs(); | |
| 46 if (difference > 0) { | |
| 47 double duration = difference * _kCheckDuration; | |
| 48 checkedAnimation.stop(); | |
| 49 checkedAnimation.animateTo(targetValue, duration, curve: _kAnimationCurv e); | |
| 50 } else { | |
|
abarth-chromium
2015/06/12 13:35:13
remove?
| |
| 51 } | |
| 52 } | |
| 25 super.syncFields(source); | 53 super.syncFields(source); |
| 26 } | 54 } |
| 27 | 55 |
| 28 void _handleClick(sky.Event e) { | 56 void _handleClick(sky.Event e) { |
| 29 onChanged(!checked); | 57 onChanged(!checked); |
| 30 } | 58 } |
| 31 | 59 |
| 32 UINode buildContent() { | 60 UINode build() { |
| 33 // TODO(jackson): This should change colors with the theme | |
| 34 sky.Color color = highlight ? colors.Purple[500] : const sky.Color(0x8A00000 0); | |
| 35 const double kEdgeSize = 20.0; | |
| 36 const double kEdgeRadius = 1.0; | |
| 37 return new EventListenerNode( | 61 return new EventListenerNode( |
| 38 new Container( | 62 new Container( |
| 39 margin: const EdgeDims.symmetric(horizontal: 5.0), | 63 margin: const EdgeDims.symmetric(horizontal: 5.0), |
| 40 width: kEdgeSize + 2.0, | 64 width: _kEdgeSize + 2.0, |
| 41 height: kEdgeSize + 2.0, | 65 height: _kEdgeSize + 2.0, |
| 42 child: new CustomPaint( | 66 child: new CustomPaint( |
| 67 token: checkedAnimation.value, | |
| 43 callback: (sky.Canvas canvas, Size size) { | 68 callback: (sky.Canvas canvas, Size size) { |
| 69 // Choose a color between grey and the theme color | |
| 70 sky.Paint paint = new sky.Paint()..strokeWidth = 2.0 | |
| 71 ..color = _kUncheckedColor; | |
| 44 | 72 |
| 45 sky.Paint paint = new sky.Paint()..color = color | 73 // The rrect contracts slightly during the animation |
| 46 ..strokeWidth = 2.0; | 74 double inset = 2.0 - (checkedAnimation.value - _kMidpoint).abs() * 2 .0; |
| 75 sky.Rect rect = new sky.Rect.fromLTRB(inset, inset, _kEdgeSize - ins et, _kEdgeSize - inset); | |
| 76 sky.RRect rrect = new sky.RRect()..setRectXY(rect, _kEdgeRadius, _kE dgeRadius); | |
| 47 | 77 |
| 48 // Draw the outer rrect | 78 |
| 49 paint.setStyle(checked ? sky.PaintingStyle.strokeAndFill : sky.Paint ingStyle.stroke); | 79 // Outline of the empty rrect |
| 50 sky.Rect rect = new sky.Rect.fromLTRB(0.0, 0.0, kEdgeSize, kEdgeSize ); | 80 paint.setStyle(sky.PaintingStyle.stroke); |
| 51 sky.RRect rrect = new sky.RRect()..setRectXY(rect, kEdgeRadius, kEdg eRadius); | |
| 52 canvas.drawRRect(rrect, paint); | 81 canvas.drawRRect(rrect, paint); |
| 53 | 82 |
| 54 // Draw the inner check | 83 // Radial gradient that changes size |
| 55 if (checked) { | 84 if (checkedAnimation.value > 0) { |
| 56 // TODO(jackson): Use the theme color | 85 paint.setStyle(sky.PaintingStyle.fill); |
| 86 paint.setShader( | |
| 87 new sky.Gradient.radial( | |
| 88 new Point(_kEdgeSize / 2.0, _kEdgeSize / 2.0), | |
| 89 _kEdgeSize * (_kMidpoint - checkedAnimation.value) * 8.0, | |
| 90 [const sky.Color(0x00000000), _kUncheckedColor], | |
| 91 [0.0, 1.0] | |
| 92 ) | |
| 93 ); | |
| 94 canvas.drawRRect(rrect, paint); | |
| 95 } | |
| 96 | |
| 97 if (checkedAnimation.value > _kMidpoint) { | |
| 98 double t = (checkedAnimation.value - _kMidpoint) / (1.0 - _kMidpoi nt); | |
| 99 | |
| 100 // Solid filled rrect | |
| 101 paint.setStyle(sky.PaintingStyle.strokeAndFill); | |
| 102 paint.color = new Color.fromARGB((t * 255).floor(), | |
| 103 _kCheckedColor.red, | |
| 104 _kCheckedColor.green, | |
| 105 _kCheckedColor.blue); | |
| 106 canvas.drawRRect(rrect, paint); | |
| 107 | |
| 108 // White inner check | |
| 57 paint.color = const sky.Color(0xFFFFFFFF); | 109 paint.color = const sky.Color(0xFFFFFFFF); |
| 58 paint.setStyle(sky.PaintingStyle.stroke); | 110 paint.setStyle(sky.PaintingStyle.stroke); |
| 59 sky.Path path = new sky.Path(); | 111 sky.Path path = new sky.Path(); |
| 60 path.moveTo(kEdgeSize * 0.2, kEdgeSize * 0.5); | 112 sky.Point start = new sky.Point(_kEdgeSize * 0.2, _kEdgeSize * 0.5 ); |
| 61 path.lineTo(kEdgeSize * 0.4, kEdgeSize * 0.7); | 113 sky.Point mid = new sky.Point(_kEdgeSize * 0.4, _kEdgeSize * 0.7); |
| 62 path.lineTo(kEdgeSize * 0.8, kEdgeSize * 0.3); | 114 sky.Point end = new sky.Point(_kEdgeSize * 0.8, _kEdgeSize * 0.3); |
| 115 Point lerp(Point p1, Point p2, double t) | |
| 116 => new Point(p1.x * (1.0 - t) + p2.x * t, p1.y * (1.0 - t) + p2. y * t); | |
|
abarth-chromium
2015/06/12 13:35:14
Should this function be in the animations folder s
jackson
2015/06/12 19:45:03
Let's wait to see if we use it again somewhere
| |
| 117 sky.Point drawStart = lerp(start, mid, 1.0 - t); | |
| 118 sky.Point drawEnd = lerp(mid, end, t); | |
| 119 path.moveTo(drawStart.x, drawStart.y); | |
| 120 path.lineTo(mid.x, mid.y); | |
| 121 path.lineTo(drawEnd.x, drawEnd.y); | |
| 63 canvas.drawPath(path, paint); | 122 canvas.drawPath(path, paint); |
| 64 } | 123 } |
| 65 } | 124 } |
| 66 ) | 125 ) |
| 67 ), | 126 ), |
| 68 onGestureTap: _handleClick | 127 onGestureTap: _handleClick |
| 69 ); | 128 ); |
| 70 } | 129 } |
| 71 | 130 |
| 72 } | 131 } |
| OLD | NEW |