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

Unified Diff: sky/sdk/lib/widgets/toggleable.dart

Issue 1185173002: Really basic Switch implementation in Sky. Could definitely use some polish (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/sdk/lib/widgets/switch.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/sdk/lib/widgets/toggleable.dart
diff --git a/sky/sdk/lib/widgets/toggleable.dart b/sky/sdk/lib/widgets/toggleable.dart
new file mode 100644
index 0000000000000000000000000000000000000000..621804c29d6cac778bad1cfb3f249e87536c11a1
--- /dev/null
+++ b/sky/sdk/lib/widgets/toggleable.dart
@@ -0,0 +1,74 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:sky' as sky;
+
+import 'animated_component.dart';
+import 'basic.dart';
+import '../framework/animation/animated_value.dart';
+import '../framework/animation/curves.dart';
+
+typedef void ValueChanged(value);
+
+const double _kCheckDuration = 200.0;
+
+abstract class Toggleable extends AnimatedComponent {
+
+ Toggleable({
+ Object key,
+ this.value,
+ this.onChanged
+ }) : super(key: key) {
+ toggleAnimation = new AnimatedValue(value ? 1.0 : 0.0);
+ }
+
+ bool value;
+ AnimatedValue toggleAnimation;
+ ValueChanged onChanged;
+
+ void syncFields(Toggleable source) {
+ onChanged = source.onChanged;
+ if (value != source.value) {
+ value = source.value;
+ double targetValue = value ? 1.0 : 0.0;
+ double difference = (toggleAnimation.value - targetValue).abs();
+ if (difference > 0) {
+ toggleAnimation.stop();
+ double t = difference * duration;
+ Curve curve = targetValue > toggleAnimation.value ? curveUp : curveDown;
+ toggleAnimation.animateTo(targetValue, t, curve: curve);
+ }
+ }
+ super.syncFields(source);
+ }
+
+ void _handleClick(sky.Event e) {
+ onChanged(!value);
+ }
+
+ // Override these methods to draw yourself
+ void customPaintCallback(sky.Canvas canvas, Size size) {
+ assert(false);
+ }
+ Size get size => const Size.zero;
+ EdgeDims get margin => const EdgeDims.symmetric(horizontal: 5.0);
+ double get duration => 200.0;
+ Curve get curveUp => easeIn;
+ Curve get curveDown => easeOut;
+
+ UINode build() {
+ return new EventListenerNode(
+ new Container(
+ margin: margin,
+ width: size.width,
+ height: size.height,
+ child: new CustomPaint(
+ token: toggleAnimation.value,
+ callback: customPaintCallback
+ )
+ ),
+ onGestureTap: _handleClick
+ );
+ }
+}
« no previous file with comments | « sky/sdk/lib/widgets/switch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698