| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:sky' as sky; |
| 6 |
| 7 import 'animated_component.dart'; |
| 8 import 'basic.dart'; |
| 9 import '../framework/animation/animated_value.dart'; |
| 10 import '../framework/animation/curves.dart'; |
| 11 |
| 12 typedef void ValueChanged(value); |
| 13 |
| 14 const double _kCheckDuration = 200.0; |
| 15 |
| 16 abstract class Toggleable extends AnimatedComponent { |
| 17 |
| 18 Toggleable({ |
| 19 Object key, |
| 20 this.value, |
| 21 this.onChanged |
| 22 }) : super(key: key) { |
| 23 toggleAnimation = new AnimatedValue(value ? 1.0 : 0.0); |
| 24 } |
| 25 |
| 26 bool value; |
| 27 AnimatedValue toggleAnimation; |
| 28 ValueChanged onChanged; |
| 29 |
| 30 void syncFields(Toggleable source) { |
| 31 onChanged = source.onChanged; |
| 32 if (value != source.value) { |
| 33 value = source.value; |
| 34 double targetValue = value ? 1.0 : 0.0; |
| 35 double difference = (toggleAnimation.value - targetValue).abs(); |
| 36 if (difference > 0) { |
| 37 toggleAnimation.stop(); |
| 38 double t = difference * duration; |
| 39 Curve curve = targetValue > toggleAnimation.value ? curveUp : curveDown; |
| 40 toggleAnimation.animateTo(targetValue, t, curve: curve); |
| 41 } |
| 42 } |
| 43 super.syncFields(source); |
| 44 } |
| 45 |
| 46 void _handleClick(sky.Event e) { |
| 47 onChanged(!value); |
| 48 } |
| 49 |
| 50 // Override these methods to draw yourself |
| 51 void customPaintCallback(sky.Canvas canvas, Size size) { |
| 52 assert(false); |
| 53 } |
| 54 Size get size => const Size.zero; |
| 55 EdgeDims get margin => const EdgeDims.symmetric(horizontal: 5.0); |
| 56 double get duration => 200.0; |
| 57 Curve get curveUp => easeIn; |
| 58 Curve get curveDown => easeOut; |
| 59 |
| 60 UINode build() { |
| 61 return new EventListenerNode( |
| 62 new Container( |
| 63 margin: margin, |
| 64 width: size.width, |
| 65 height: size.height, |
| 66 child: new CustomPaint( |
| 67 token: toggleAnimation.value, |
| 68 callback: customPaintCallback |
| 69 ) |
| 70 ), |
| 71 onGestureTap: _handleClick |
| 72 ); |
| 73 } |
| 74 } |
| OLD | NEW |