提交 692095b5 编写于 作者: A Adam Barth

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 .
上级 6501df75
......@@ -286,7 +286,7 @@ class StockHome extends AnimatedComponent {
..position = new AnimatedType<Point>(const Point(0.0, 45.0), end: Point.origin);
var performance = _snackbarTransform.createPerformance(
[_snackbarTransform.position], duration: _kSnackbarSlideDuration);
watchPerformance(performance);
watch(performance);
performance.play();
});
}
......
......@@ -13,7 +13,7 @@ abstract class AnimatedComponent extends StatefulComponent {
final List<AnimationPerformance> _watchedPerformances = new List<AnimationPerformance>();
void watchPerformance(AnimationPerformance performance) {
void watch(AnimationPerformance performance) {
assert(!_watchedPerformances.contains(performance));
_watchedPerformances.add(performance);
if (mounted)
......
// 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<double> opacity;
AnimatedType<Point> position;
AnimatedType<double> shadow;
AnimatedColor backgroundColor;
// These don't animate, but are used to build the AnimationBuilder anyway.
double borderRadius;
Shape shape;
Map<AnimatedVariable, AnimationPerformance> _variableToPerformance =
new Map<AnimatedVariable, AnimationPerformance>();
AnimationBuilder();
AnimationPerformance createPerformance(List<AnimatedType> 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<Color> {
AnimatedColor(Color begin, {Color end, Curve curve: linear})
: super(begin, end: end, curve: curve);
void setFraction(double t) {
value = lerpColor(begin, end, t);
}
}
List<BoxShadow> _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<BoxShadow> shadow = new List<BoxShadow>();
for (int i = 0; i < shadows[level1].length; ++i)
shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t));
return shadow;
}
......@@ -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) {
......
......@@ -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<Widget> children;
int level;
DrawerController controller;
void initState() {
watch(controller.performance);
}
void syncFields(Drawer source) {
children = source.children;
level = source.level;
......
......@@ -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
));
......
......@@ -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<PopupMenuItem> 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<PopupMenuItem> items;
int level;
void syncFields(PopupMenu source) {
controller = source.controller;
items = source.items;
......
......@@ -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;
......
......@@ -19,14 +19,7 @@ abstract class Toggleable extends AnimatedComponent {
String key,
this.value,
this.onChanged
}) : super(key: key) {
_position = new AnimatedType<double>(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<double>(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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册