From 692095b56cb7327b623fe73b4292898767e3752a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 13 Jul 2015 17:17:48 -0700 Subject: [PATCH] Rename watchPerformance to watch Now that we don't have the old animation system, we can use the shorter name again. Also, move state initialization code into initState(). R=ianh@google.com Review URL: https://codereview.chromium.org/1231103005 . --- sdk/example/stocks/lib/stock_home.dart | 2 +- sdk/lib/widgets/animated_component.dart | 2 +- sdk/lib/widgets/animated_container.dart | 121 ------------------------ sdk/lib/widgets/dismissable.dart | 2 +- sdk/lib/widgets/drawer.dart | 8 +- sdk/lib/widgets/material.dart | 4 +- sdk/lib/widgets/popup_menu.dart | 14 +-- sdk/lib/widgets/scrollable.dart | 8 +- sdk/lib/widgets/toggleable.dart | 18 ++-- 9 files changed, 33 insertions(+), 146 deletions(-) delete mode 100644 sdk/lib/widgets/animated_container.dart diff --git a/sdk/example/stocks/lib/stock_home.dart b/sdk/example/stocks/lib/stock_home.dart index 9dbf26f4f..9115736cc 100644 --- a/sdk/example/stocks/lib/stock_home.dart +++ b/sdk/example/stocks/lib/stock_home.dart @@ -286,7 +286,7 @@ class StockHome extends AnimatedComponent { ..position = new AnimatedType(const Point(0.0, 45.0), end: Point.origin); var performance = _snackbarTransform.createPerformance( [_snackbarTransform.position], duration: _kSnackbarSlideDuration); - watchPerformance(performance); + watch(performance); performance.play(); }); } diff --git a/sdk/lib/widgets/animated_component.dart b/sdk/lib/widgets/animated_component.dart index c49168e03..37e077f41 100644 --- a/sdk/lib/widgets/animated_component.dart +++ b/sdk/lib/widgets/animated_component.dart @@ -13,7 +13,7 @@ abstract class AnimatedComponent extends StatefulComponent { final List _watchedPerformances = new List(); - void watchPerformance(AnimationPerformance performance) { + void watch(AnimationPerformance performance) { assert(!_watchedPerformances.contains(performance)); _watchedPerformances.add(performance); if (mounted) diff --git a/sdk/lib/widgets/animated_container.dart b/sdk/lib/widgets/animated_container.dart deleted file mode 100644 index 9c6f8aad9..000000000 --- a/sdk/lib/widgets/animated_container.dart +++ /dev/null @@ -1,121 +0,0 @@ -// 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 'package:vector_math/vector_math.dart'; - -import 'package:sky/animation/animation_performance.dart'; -import 'package:sky/animation/curves.dart'; -import 'package:sky/base/lerp.dart'; -import 'package:sky/painting/box_painter.dart'; -import 'package:sky/theme/shadows.dart'; -import 'package:sky/widgets/basic.dart'; - -// This class builds a Builder object from a collection of optionally- -// animated properties. Use syncFields to update the Builder's properties, -// which will optionally animate them using an AnimationPerformance. -class AnimationBuilder { - AnimatedType opacity; - AnimatedType position; - AnimatedType shadow; - AnimatedColor backgroundColor; - - // These don't animate, but are used to build the AnimationBuilder anyway. - double borderRadius; - Shape shape; - - Map _variableToPerformance = - new Map(); - - AnimationBuilder(); - - AnimationPerformance createPerformance(List variables, - {Duration duration}) { - AnimationPerformance performance = new AnimationPerformance() - ..duration = duration - ..variable = new AnimatedList(variables); - for (AnimatedVariable variable in variables) - _variableToPerformance[variable] = performance; - return performance; - } - - Widget build(Widget child) { - Widget current = child; - if (shadow != null || backgroundColor != null || - borderRadius != null || shape != null) { - current = new DecoratedBox( - decoration: new BoxDecoration( - borderRadius: borderRadius, - shape: shape, - boxShadow: shadow != null ? _computeShadow(shadow.value) : null, - backgroundColor: backgroundColor != null ? backgroundColor.value : null), - child: current); - } - - if (position != null) { - Matrix4 transform = new Matrix4.identity(); - transform.translate(position.value.x, position.value.y); - current = new Transform(transform: transform, child: current); - } - - if (opacity != null) { - current = new Opacity(opacity: opacity.value, child: current); - } - - return current; - } - - void syncFields(AnimationBuilder source) { - _syncField(position, source.position); - _syncField(shadow, source.shadow); - _syncField(backgroundColor, source.backgroundColor); - - borderRadius = source.borderRadius; - shape = source.shape; - } - - void _syncField(AnimatedType variable, AnimatedType sourceVariable) { - if (variable == null) - return; // TODO(mpcomplete): Should we handle transition from null? - - AnimationPerformance performance = _variableToPerformance[variable]; - if (performance == null) { - // If there's no performance, no need to animate. - if (sourceVariable != null) - variable.value = sourceVariable.value; - return; - } - - if (variable.value != sourceVariable.value) { - variable - ..begin = variable.value - ..end = sourceVariable.value; - performance - ..progress = 0.0 - ..play(); - } - } -} - -class AnimatedColor extends AnimatedType { - AnimatedColor(Color begin, {Color end, Curve curve: linear}) - : super(begin, end: end, curve: curve); - - void setFraction(double t) { - value = lerpColor(begin, end, t); - } -} - -List _computeShadow(double level) { - if (level < 1.0) // shadows[1] is the first shadow - return null; - - int level1 = level.floor(); - int level2 = level.ceil(); - double t = level - level1.toDouble(); - - List shadow = new List(); - for (int i = 0; i < shadows[level1].length; ++i) - shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); - return shadow; -} diff --git a/sdk/lib/widgets/dismissable.dart b/sdk/lib/widgets/dismissable.dart index e248a475d..d093d964e 100644 --- a/sdk/lib/widgets/dismissable.dart +++ b/sdk/lib/widgets/dismissable.dart @@ -45,7 +45,7 @@ class Dismissable extends AnimatedComponent { [_transform.position, _transform.opacity], duration: new Duration(milliseconds: _kCardDismissFadeoutMS)); _performance.addListener(_handleAnimationProgressChanged); - watchPerformance(_performance); + watch(_performance); } void syncFields(Dismissable source) { diff --git a/sdk/lib/widgets/drawer.dart b/sdk/lib/widgets/drawer.dart index 13d00f1de..150ca3f17 100644 --- a/sdk/lib/widgets/drawer.dart +++ b/sdk/lib/widgets/drawer.dart @@ -105,14 +105,16 @@ class Drawer extends AnimatedComponent { this.controller, this.children, this.level: 0 - }) : super(key: key) { - watchPerformance(controller.performance); - } + }) : super(key: key); List children; int level; DrawerController controller; + void initState() { + watch(controller.performance); + } + void syncFields(Drawer source) { children = source.children; level = source.level; diff --git a/sdk/lib/widgets/material.dart b/sdk/lib/widgets/material.dart index ff2a9bf32..4efcae343 100644 --- a/sdk/lib/widgets/material.dart +++ b/sdk/lib/widgets/material.dart @@ -47,11 +47,11 @@ class Material extends AnimatedComponent { ..backgroundColor = _getBackgroundColor(type, color) ..borderRadius = edges[type] ..shape = type == MaterialType.circle ? Shape.circle : Shape.rectangle; - watchPerformance(_builder.createPerformance( + watch(_builder.createPerformance( [_builder.shadow], duration: _kAnimateShadowDuration )); - watchPerformance(_builder.createPerformance( + watch(_builder.createPerformance( [_builder.backgroundColor], duration: _kAnimateColorDuration )); diff --git a/sdk/lib/widgets/popup_menu.dart b/sdk/lib/widgets/popup_menu.dart index f5101e989..79bb6f685 100644 --- a/sdk/lib/widgets/popup_menu.dart +++ b/sdk/lib/widgets/popup_menu.dart @@ -91,18 +91,20 @@ class PopupMenuController { class PopupMenu extends AnimatedComponent { PopupMenu({ String key, this.controller, this.items, this.level }) - : super(key: key) { + : super(key: key); + + PopupMenuController controller; + List items; + int level; + + void initState() { _painter = new BoxPainter(new BoxDecoration( backgroundColor: Grey[50], borderRadius: 2.0, boxShadow: shadows[level])); - watchPerformance(controller.performance); + watch(controller.performance); } - PopupMenuController controller; - List items; - int level; - void syncFields(PopupMenu source) { controller = source.controller; items = source.items; diff --git a/sdk/lib/widgets/scrollable.dart b/sdk/lib/widgets/scrollable.dart index 87dcbe1cd..807bd26a9 100644 --- a/sdk/lib/widgets/scrollable.dart +++ b/sdk/lib/widgets/scrollable.dart @@ -30,13 +30,15 @@ abstract class Scrollable extends StatefulComponent { String key, this.backgroundColor, this.direction: ScrollDirection.vertical - }) : super(key: key) { - _animation = new AnimatedSimulation(_tickScrollOffset); - } + }) : super(key: key); Color backgroundColor; ScrollDirection direction; + void initState() { + _animation = new AnimatedSimulation(_tickScrollOffset); + } + void syncFields(Scrollable source) { backgroundColor = source.backgroundColor; direction == source.direction; diff --git a/sdk/lib/widgets/toggleable.dart b/sdk/lib/widgets/toggleable.dart index 5d5a945e2..dd06917ae 100644 --- a/sdk/lib/widgets/toggleable.dart +++ b/sdk/lib/widgets/toggleable.dart @@ -19,14 +19,7 @@ abstract class Toggleable extends AnimatedComponent { String key, this.value, this.onChanged - }) : super(key: key) { - _position = new AnimatedType(0.0, end: 1.0); - _performance = new AnimationPerformance() - ..variable = position - ..duration = _kCheckDuration - ..progress = value ? 1.0 : 0.0; - watchPerformance(performance); - } + }) : super(key: key); bool value; ValueChanged onChanged; @@ -37,6 +30,15 @@ abstract class Toggleable extends AnimatedComponent { AnimationPerformance _performance; AnimationPerformance get performance => _performance; + void initState() { + _position = new AnimatedType(0.0, end: 1.0); + _performance = new AnimationPerformance() + ..variable = position + ..duration = _kCheckDuration + ..progress = value ? 1.0 : 0.0; + watch(performance); + } + void syncFields(Toggleable source) { onChanged = source.onChanged; if (value != source.value) { -- GitLab