Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: sky/sdk/lib/framework/widgets/checkbox.dart

Issue 1182743002: Animate checkboxes (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/sdk/lib/framework/widgets/wrappers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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'; 12 import 'animated_component.dart';
11 import 'wrappers.dart'; 13 import 'wrappers.dart';
12 14
13 typedef void ValueChanged(value); 15 typedef void ValueChanged(value);
14 16
15 class Checkbox extends ButtonBase { 17 const double _kMidpoint = 0.5;
18 const double _kCheckDuration = 200.0;
19 const sky.Color _kUncheckedColor = const sky.Color(0x8A000000);
20 // TODO(jackson): This should change colors with the theme
21 sky.Color _kCheckedColor = colors.Purple[500];
22 const double _kEdgeSize = 20.0;
23 const double _kEdgeRadius = 1.0;
16 24
17 Checkbox({ Object key, this.onChanged, this.checked }) : super(key: key); 25 class Checkbox extends AnimatedComponent {
26
27 Checkbox({
28 Object key,
29 this.checked,
30 this.onChanged
31 }) : super(key: key) {
32 _checkedAnimation = new AnimatedValue(checked ? 1.0 : 0.0);
33 }
18 34
19 bool checked; 35 bool checked;
36 AnimatedValue _checkedAnimation;
20 ValueChanged onChanged; 37 ValueChanged onChanged;
21 38
22 void syncFields(Checkbox source) { 39 void syncFields(Checkbox source) {
23 checked = source.checked;
24 onChanged = source.onChanged; 40 onChanged = source.onChanged;
41 if (checked != source.checked) {
42 checked = source.checked;
43 double targetValue = checked ? 1.0 : 0.0;
44 double difference = (_checkedAnimation.value - targetValue).abs();
45 if (difference > 0) {
46 double duration = difference * _kCheckDuration;
47 _checkedAnimation.stop();
48 Curve curve;
49 if (targetValue > _checkedAnimation.value) {
50 curve = easeIn;
51 } else {
52 curve = easeOut;
53 }
54 _checkedAnimation.animateTo(targetValue, duration, curve: curve);
55 }
56 }
25 super.syncFields(source); 57 super.syncFields(source);
26 } 58 }
27 59
28 void _handleClick(sky.Event e) { 60 void _handleClick(sky.Event e) {
29 onChanged(!checked); 61 onChanged(!checked);
30 } 62 }
31 63
32 UINode buildContent() { 64 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( 65 return new EventListenerNode(
38 new Container( 66 new Container(
39 margin: const EdgeDims.symmetric(horizontal: 5.0), 67 margin: const EdgeDims.symmetric(horizontal: 5.0),
40 width: kEdgeSize + 2.0, 68 width: _kEdgeSize + 2.0,
41 height: kEdgeSize + 2.0, 69 height: _kEdgeSize + 2.0,
42 child: new CustomPaint( 70 child: new CustomPaint(
71 token: _checkedAnimation.value,
43 callback: (sky.Canvas canvas, Size size) { 72 callback: (sky.Canvas canvas, Size size) {
73 // Choose a color between grey and the theme color
74 sky.Paint paint = new sky.Paint()..strokeWidth = 2.0
75 ..color = _kUncheckedColor;
44 76
45 sky.Paint paint = new sky.Paint()..color = color 77 // The rrect contracts slightly during the animation
46 ..strokeWidth = 2.0; 78 double inset = 2.0 - (_checkedAnimation.value - _kMidpoint).abs() * 2.0;
79 sky.Rect rect = new sky.Rect.fromLTRB(inset, inset, _kEdgeSize - ins et, _kEdgeSize - inset);
80 sky.RRect rrect = new sky.RRect()..setRectXY(rect, _kEdgeRadius, _kE dgeRadius);
47 81
48 // Draw the outer rrect 82
49 paint.setStyle(checked ? sky.PaintingStyle.strokeAndFill : sky.Paint ingStyle.stroke); 83 // Outline of the empty rrect
50 sky.Rect rect = new sky.Rect.fromLTRB(0.0, 0.0, kEdgeSize, kEdgeSize ); 84 paint.setStyle(sky.PaintingStyle.stroke);
51 sky.RRect rrect = new sky.RRect()..setRectXY(rect, kEdgeRadius, kEdg eRadius);
52 canvas.drawRRect(rrect, paint); 85 canvas.drawRRect(rrect, paint);
53 86
54 // Draw the inner check 87 // Radial gradient that changes size
55 if (checked) { 88 if (_checkedAnimation.value > 0) {
56 // TODO(jackson): Use the theme color 89 paint.setStyle(sky.PaintingStyle.fill);
90 paint.setShader(
91 new sky.Gradient.radial(
92 new Point(_kEdgeSize / 2.0, _kEdgeSize / 2.0),
93 _kEdgeSize * (_kMidpoint - _checkedAnimation.value) * 8.0,
94 [const sky.Color(0x00000000), _kUncheckedColor],
95 [0.0, 1.0]
96 )
97 );
98 canvas.drawRRect(rrect, paint);
99 }
100
101 if (_checkedAnimation.value > _kMidpoint) {
102 double t = (_checkedAnimation.value - _kMidpoint) / (1.0 - _kMidpo int);
103
104 // Solid filled rrect
105 paint.setStyle(sky.PaintingStyle.strokeAndFill);
106 paint.color = new Color.fromARGB((t * 255).floor(),
107 _kCheckedColor.red,
108 _kCheckedColor.green,
109 _kCheckedColor.blue);
110 canvas.drawRRect(rrect, paint);
111
112 // White inner check
57 paint.color = const sky.Color(0xFFFFFFFF); 113 paint.color = const sky.Color(0xFFFFFFFF);
58 paint.setStyle(sky.PaintingStyle.stroke); 114 paint.setStyle(sky.PaintingStyle.stroke);
59 sky.Path path = new sky.Path(); 115 sky.Path path = new sky.Path();
60 path.moveTo(kEdgeSize * 0.2, kEdgeSize * 0.5); 116 sky.Point start = new sky.Point(_kEdgeSize * 0.2, _kEdgeSize * 0.5 );
61 path.lineTo(kEdgeSize * 0.4, kEdgeSize * 0.7); 117 sky.Point mid = new sky.Point(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
62 path.lineTo(kEdgeSize * 0.8, kEdgeSize * 0.3); 118 sky.Point end = new sky.Point(_kEdgeSize * 0.8, _kEdgeSize * 0.3);
119 Point lerp(Point p1, Point p2, double t)
120 => new Point(p1.x * (1.0 - t) + p2.x * t, p1.y * (1.0 - t) + p2. y * t);
121 sky.Point drawStart = lerp(start, mid, 1.0 - t);
122 sky.Point drawEnd = lerp(mid, end, t);
123 path.moveTo(drawStart.x, drawStart.y);
124 path.lineTo(mid.x, mid.y);
125 path.lineTo(drawEnd.x, drawEnd.y);
63 canvas.drawPath(path, paint); 126 canvas.drawPath(path, paint);
64 } 127 }
65 } 128 }
66 ) 129 )
67 ), 130 ),
68 onGestureTap: _handleClick 131 onGestureTap: _handleClick
69 ); 132 );
70 } 133 }
71 134
72 } 135 }
OLDNEW
« no previous file with comments | « no previous file | sky/sdk/lib/framework/widgets/wrappers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698