提交 1eb13d01 编写于 作者: A Adam Barth

Organize sky/framework/animation

This CL cleans up the sky/framework/animation as follows:

1) I've moved code that's used only by the custom elements framework into
   sky/framework/elements/animation. This code is based on AnimationDelegates
   rather than Streams.
2) Rename ScrollCurve to ScrollBehavior because it encapsulates more behavior
   than just a curve.
3) Make the Generator interface explicit and mark subclasses as actual
   subclasses.
4) Move Simulation into generators.dart because it implements the Generator
   interface.
5) Move Animation out of generators.dart because it does not implement the
   Generator interface.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/1001373002
上级 06bfddff
......@@ -8,7 +8,7 @@ class Stocklist extends FixedHeightScrollable {
Object key,
this.stocks,
this.query
}) : super(key: key, scrollCurve: new OverscrollCurve());
}) : super(key: key, scrollBehavior: new OverscrollBehavior());
List<Node> buildItems(int start, int count) {
return stocks
......
library stocksapp;
import '../../framework/fn.dart';
import '../../framework/animation/scroll_curve.dart';
import '../../framework/animation/scroll_behavior.dart';
import '../../framework/components/drawer.dart';
import '../../framework/components/drawer_header.dart';
import '../../framework/components/fixed_height_scrollable.dart';
......
// 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 'curves.dart';
import 'dart:async';
import 'generators.dart';
class Animation {
Stream<double> get onValueChanged => _controller.stream;
double get value => _value;
void set value(double value) {
stop();
_setValue(value);
}
bool get isAnimating => _animation != null;
StreamController _controller = new StreamController(sync: true);
AnimationGenerator _animation;
double _value;
void _setValue(double value) {
_value = value;
_controller.add(_value);
}
void stop() {
if (_animation != null) {
_animation.cancel();
_animation = null;
}
}
void animateTo(double newValue, double duration,
{ Curve curve: linear, double initialDelay: 0.0 }) {
stop();
_animation = new AnimationGenerator(
duration: duration,
begin: _value,
end: newValue,
curve: curve,
initialDelay: initialDelay);
_animation.onTick.listen(_setValue, onDone: () {
_animation = null;
});
}
}
......@@ -7,7 +7,12 @@ import 'dart:async';
import 'dart:math' as math;
import 'dart:sky' as sky;
class FrameGenerator {
abstract class Generator {
Stream<double> get onTick;
void cancel();
}
class FrameGenerator extends Generator {
Function onDone;
StreamController _controller;
......@@ -51,7 +56,7 @@ class FrameGenerator {
}
}
class AnimationGenerator {
class AnimationGenerator extends Generator {
Stream<double> get onTick => _stream;
final double initialDelay;
final double duration;
......@@ -107,49 +112,40 @@ class AnimationGenerator {
}
}
class Animation {
Stream<double> get onValueChanged => _controller.stream;
double get value => _value;
void set value(double value) {
stop();
_setValue(value);
}
bool get isAnimating => _animation != null;
StreamController _controller = new StreamController(sync: true);
AnimationGenerator _animation;
double _value;
class Simulation extends Generator {
Stream<double> get onTick => _stream;
final System system;
void _setValue(double value) {
_value = value;
_controller.add(_value);
}
FrameGenerator _generator;
Stream<double> _stream;
double _previousTime = 0.0;
void stop() {
if (_animation != null) {
_animation.cancel();
_animation = null;
Simulation(this.system, {Function terminationCondition, Function onDone}) {
_generator = new FrameGenerator(onDone: onDone);
_stream = _generator.onTick.map(_update);
if (terminationCondition != null) {
bool done = false;
_stream = _stream.takeWhile((_) {
if (done)
return false;
done = terminationCondition();
return true;
});
}
}
void animateTo(double newValue, double duration,
{ Curve curve: linear, double initialDelay: 0.0 }) {
stop();
_animation = new AnimationGenerator(
duration: duration,
begin: _value,
end: newValue,
curve: curve,
initialDelay: initialDelay);
void cancel() {
_generator.cancel();
}
_animation.onTick.listen(_setValue, onDone: () {
_animation = null;
});
double _update(double timeStamp) {
double previousTime = _previousTime;
_previousTime = timeStamp;
if (previousTime == 0.0)
return timeStamp;
double deltaT = timeStamp - previousTime;
system.update(deltaT);
return timeStamp;
}
}
......@@ -4,24 +4,24 @@
import 'dart:math' as math;
import 'mechanics.dart';
import 'simulation.dart';
import 'generators.dart';
const double _kSlope = 0.01;
abstract class ScrollCurve {
abstract class ScrollBehavior {
Simulation release(Particle particle) => null;
// Returns the new scroll offset.
double apply(double scrollOffset, double scrollDelta);
double applyCurve(double scrollOffset, double scrollDelta);
}
class BoundedScrollCurve extends ScrollCurve {
class BoundedScrollBehavior extends ScrollBehavior {
double minOffset;
double maxOffset;
BoundedScrollCurve({this.minOffset: 0.0, this.maxOffset});
BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset});
double apply(double scrollOffset, double scrollDelta) {
double applyCurve(double scrollOffset, double scrollDelta) {
double newScrollOffset = scrollOffset + scrollDelta;
if (minOffset != null)
newScrollOffset = math.max(minOffset, newScrollOffset);
......@@ -31,7 +31,7 @@ class BoundedScrollCurve extends ScrollCurve {
}
}
class OverscrollCurve extends ScrollCurve {
class OverscrollBehavior extends ScrollBehavior {
Simulation release(Particle particle) {
if (particle.position >= 0.0)
return null;
......@@ -44,7 +44,7 @@ class OverscrollCurve extends ScrollCurve {
terminationCondition: () => particle.position == 0.0);
}
double apply(double scrollOffset, double scrollDelta) {
double applyCurve(double scrollOffset, double scrollDelta) {
double newScrollOffset = scrollOffset + scrollDelta;
if (newScrollOffset < 0.0) {
// If we're overscrolling, we want move the scroll offset 2x slower than
......
// 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:async';
import 'generator.dart';
import 'mechanics.dart';
class Simulation {
Stream<double> get onTick => _stream;
final System system;
FrameGenerator _generator;
Stream<double> _stream;
double _previousTime = 0.0;
Simulation(this.system, {Function terminationCondition, Function onDone}) {
_generator = new FrameGenerator(onDone: onDone);
_stream = _generator.onTick.map(_update);
if (terminationCondition != null) {
bool done = false;
_stream = _stream.takeWhile((_) {
if (done)
return false;
done = terminationCondition();
return true;
});
}
}
void cancel() {
_generator.cancel();
}
double _update(double timeStamp) {
double previousTime = _previousTime;
_previousTime = timeStamp;
if (previousTime == 0.0)
return timeStamp;
double deltaT = timeStamp - previousTime;
system.update(deltaT);
return timeStamp;
}
}
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../animation/animation.dart';
import '../animation/curves.dart';
import '../animation/generator.dart';
import '../fn.dart';
import '../theme/colors.dart';
import '../theme/shadows.dart';
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../animation/scroll_curve.dart';
import '../animation/scroll_behavior.dart';
import '../fn.dart';
import 'dart:math' as math;
import 'dart:sky' as sky;
......@@ -29,8 +29,8 @@ abstract class FixedHeightScrollable extends Scrollable {
FixedHeightScrollable({
Object key,
ScrollCurve scrollCurve
}) : super(key: key, scrollCurve: scrollCurve);
ScrollBehavior scrollBehavior
}) : super(key: key, scrollBehavior: scrollBehavior);
void didMount() {
super.didMount();
......
......@@ -3,7 +3,7 @@
// found in the LICENSE file.
import '../animation/curves.dart';
import '../animation/generator.dart';
import '../animation/generators.dart';
import '../fn.dart';
import 'dart:async';
import 'dart:sky' as sky;
......
......@@ -4,15 +4,14 @@
import '../animation/curves.dart';
import '../animation/fling_curve.dart';
import '../animation/generator.dart';
import '../animation/scroll_curve.dart';
import '../animation/generators.dart';
import '../animation/scroll_behavior.dart';
import '../animation/mechanics.dart';
import '../animation/simulation.dart';
import '../fn.dart';
import 'dart:sky' as sky;
abstract class Scrollable extends Component {
ScrollCurve scrollCurve;
ScrollBehavior scrollBehavior;
double get scrollOffset => _scrollOffset;
double _scrollOffset = 0.0;
......@@ -20,7 +19,7 @@ abstract class Scrollable extends Component {
int _flingAnimationId;
Simulation _simulation;
Scrollable({Object key, this.scrollCurve}) : super(key: key) {
Scrollable({Object key, this.scrollBehavior}) : super(key: key) {
events.listen('pointerdown', _handlePointerDown);
events.listen('pointerup', _handlePointerUpOrCancel);
events.listen('pointercancel', _handlePointerUpOrCancel);
......@@ -37,7 +36,7 @@ abstract class Scrollable extends Component {
}
bool scrollBy(double scrollDelta) {
var newScrollOffset = scrollCurve.apply(_scrollOffset, scrollDelta);
var newScrollOffset = scrollBehavior.applyCurve(_scrollOffset, scrollDelta);
if (newScrollOffset == _scrollOffset)
return false;
setState(() {
......@@ -75,7 +74,7 @@ abstract class Scrollable extends Component {
void _settle() {
_stopFling();
Particle particle = new Particle(position: scrollOffset);
_simulation = scrollCurve.release(particle);
_simulation = scrollBehavior.release(particle);
if (_simulation == null)
return;
_simulation.onTick.listen((_) {
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import "curves.dart";
import "../../animation/curves.dart";
import "timer.dart";
class AnimationController extends AnimationDelegate {
......
......@@ -36,9 +36,9 @@
</div>
</template>
<script>
import "../animation/controller.dart";
import "../animation/curves.dart";
import "../animation/timer.dart";
import "animation/controller.dart";
import "dart:math" as math;
import "dart:sky";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册