mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
* feat(animation): support animating width/height properties - width/height can be specified in any valid PercentLength form that can be parsed. - make width/height properties be based on animatable CSS property. TODO: affectsLayout???? - add a few basic tests. Could probably use a few more? - fix a few null pointer exceptions in PercentLength helpers * test(ui): add animation examples to ui-tests-app - basic height animation - height animation in StackLayout - fix an issue where strings were not automatically converted to PercentLength when calling directly into `View.animate` * test(ui): cleanup and add summary/details layout example - use height transition to cover textview content. - when clicking on the summary view, animate the summary height up to a small header and show the text view. - fake animating the height on the textview by very subtly animating its translateY value while shrinking the header height. This tricks your mind into think that the text view is also vertically growing, even thought it's just slightly moving up along the Y axis. * test(ui): add animation curves test page - verify all built-in animation curve types work as expected. * test(ui): update animation curve example for multiple properties - add a segmented bar that allows choosing which properties to animate using the various curves. - interestingly, a whole bunch of properties fail with spring on iOS. - refactor width/height animations handlers to remove duplication on iOS. - implement proper spring animation for width/height on iOS. * test(ui): add stress example with 100 labels animating and fps meter - same curve/property selector as the curves example, but with 10x10 grid of items that stagger animate, and an FPS meter. - sadly it looks like width/height animations are considerably slower than the others when you have a bunch of them. I'm not sure that's entirely surprising since they interact with the layout system. - the better news is that even with the army example, my really old android 4 tablet manages ~30fps. On height/width animations from the curves example, the old tablet does fine with no noticeable FPS hit. * refactor: deduplicate existing droid width/height animations - stash to prep for replacing with LayoutTransition. * test(animation): unit tests for extent animation and PercentLength parse - update animation scaffold to allow specifying the parent stack layout height/width - test basic supported units, px, % - test basic percent length parser behaviors * chore: cleanup cruft and remove noise from diff - undo the import mangling that WebStorm helpfully applied - remove .editorconfig file - clean up in tests, remove cruft * chore: cleanup from review - more import changes * chore: remove .editorconfig
288 lines
10 KiB
TypeScript
288 lines
10 KiB
TypeScript
// Definitions.
|
|
import {
|
|
CubicBezierAnimationCurve as CubicBezierAnimationCurveDefinition,
|
|
AnimationPromise as AnimationPromiseDefinition,
|
|
Animation as AnimationBaseDefinition,
|
|
AnimationDefinition,
|
|
Pair
|
|
} from ".";
|
|
import { View } from "../core/view";
|
|
|
|
// Types.
|
|
import { Color } from "../../color";
|
|
import { isEnabled as traceEnabled, write as traceWrite, categories as traceCategories } from "../../trace";
|
|
import { PercentLength } from "../styling/style-properties";
|
|
|
|
export { Color, traceEnabled, traceWrite, traceCategories };
|
|
export { AnimationPromise } from ".";
|
|
|
|
export module Properties {
|
|
export const opacity = "opacity";
|
|
export const backgroundColor = "backgroundColor";
|
|
export const translate = "translate";
|
|
export const rotate = "rotate";
|
|
export const scale = "scale";
|
|
export const height = "height";
|
|
export const width = "width";
|
|
}
|
|
|
|
export interface PropertyAnimation {
|
|
target: View;
|
|
property: string;
|
|
value: any;
|
|
duration?: number;
|
|
delay?: number;
|
|
iterations?: number;
|
|
curve?: any;
|
|
}
|
|
|
|
export class CubicBezierAnimationCurve implements CubicBezierAnimationCurveDefinition {
|
|
|
|
public x1: number;
|
|
public y1: number;
|
|
public x2: number;
|
|
public y2: number;
|
|
|
|
constructor(x1: number, y1: number, x2: number, y2: number) {
|
|
this.x1 = x1;
|
|
this.y1 = y1;
|
|
this.x2 = x2;
|
|
this.y2 = y2;
|
|
}
|
|
}
|
|
|
|
export abstract class AnimationBase implements AnimationBaseDefinition {
|
|
public _propertyAnimations: Array<PropertyAnimation>;
|
|
public _playSequentially: boolean;
|
|
private _isPlaying: boolean;
|
|
private _resolve;
|
|
private _reject;
|
|
|
|
constructor(animationDefinitions: Array<AnimationDefinition>, playSequentially?: boolean) {
|
|
if (!animationDefinitions || animationDefinitions.length === 0) {
|
|
throw new Error("No animation definitions specified");
|
|
}
|
|
|
|
if (traceEnabled()) {
|
|
traceWrite("Analyzing " + animationDefinitions.length + " animation definitions...", traceCategories.Animation);
|
|
}
|
|
|
|
this._propertyAnimations = new Array<PropertyAnimation>();
|
|
for (let i = 0, length = animationDefinitions.length; i < length; i++) {
|
|
if (animationDefinitions[i].curve){
|
|
animationDefinitions[i].curve = this._resolveAnimationCurve(animationDefinitions[i].curve);
|
|
}
|
|
this._propertyAnimations = this._propertyAnimations.concat(AnimationBase._createPropertyAnimations(animationDefinitions[i]));
|
|
}
|
|
|
|
if (this._propertyAnimations.length === 0) {
|
|
throw new Error("Nothing to animate.");
|
|
}
|
|
if (traceEnabled()) {
|
|
traceWrite("Created " + this._propertyAnimations.length + " individual property animations.", traceCategories.Animation);
|
|
}
|
|
|
|
this._playSequentially = playSequentially;
|
|
}
|
|
|
|
abstract _resolveAnimationCurve(curve: any): any;
|
|
|
|
public play(): AnimationPromiseDefinition {
|
|
if (this.isPlaying) {
|
|
throw new Error("Animation is already playing.");
|
|
}
|
|
|
|
// We have to actually create a "Promise" due to a bug in the v8 engine and decedent promises
|
|
// We just cast it to a animationPromise so that all the rest of the code works fine
|
|
var animationFinishedPromise = <AnimationPromiseDefinition>new Promise<void>((resolve, reject) => {
|
|
this._resolve = resolve;
|
|
this._reject = reject;
|
|
});
|
|
|
|
this.fixupAnimationPromise(animationFinishedPromise);
|
|
|
|
this._isPlaying = true;
|
|
return animationFinishedPromise;
|
|
}
|
|
|
|
private fixupAnimationPromise(promise: AnimationPromiseDefinition): void {
|
|
// Since we are using function() below because of arguments, TS won't automatically do a _this for those functions.
|
|
var _this = this;
|
|
promise.cancel = () => {
|
|
_this.cancel();
|
|
};
|
|
var _then = promise.then;
|
|
promise.then = function () {
|
|
var r = _then.apply(promise, arguments);
|
|
_this.fixupAnimationPromise(r);
|
|
return r;
|
|
};
|
|
var _catch = promise.catch;
|
|
promise.catch = function () {
|
|
var r = _catch.apply(promise, arguments);
|
|
_this.fixupAnimationPromise(r);
|
|
return r;
|
|
};
|
|
}
|
|
|
|
public cancel(): void {
|
|
if (!this.isPlaying) {
|
|
throw new Error("Animation is not currently playing.");
|
|
}
|
|
}
|
|
|
|
public get isPlaying(): boolean {
|
|
return this._isPlaying;
|
|
}
|
|
|
|
public _resolveAnimationFinishedPromise() {
|
|
this._isPlaying = false;
|
|
this._resolve();
|
|
}
|
|
|
|
public _rejectAnimationFinishedPromise() {
|
|
this._isPlaying = false;
|
|
this._reject(new Error("Animation cancelled."));
|
|
}
|
|
|
|
private static _createPropertyAnimations(animationDefinition: AnimationDefinition): Array<PropertyAnimation> {
|
|
if (!animationDefinition.target) {
|
|
throw new Error("No animation target specified.");
|
|
}
|
|
|
|
for (let item in animationDefinition) {
|
|
if (animationDefinition[item] === undefined) {
|
|
continue;
|
|
}
|
|
|
|
if ((item === Properties.opacity ||
|
|
item === Properties.rotate ||
|
|
item === "duration" ||
|
|
item === "delay" ||
|
|
item === "iterations") && typeof animationDefinition[item] !== "number") {
|
|
throw new Error(`Property ${item} must be valid number. Value: ${animationDefinition[item]}`);
|
|
} else if ((item === Properties.scale || item === Properties.translate) &&
|
|
(typeof (<Pair>animationDefinition[item]).x !== "number" || typeof (<Pair>animationDefinition[item]).y !== "number")) {
|
|
throw new Error(`Property ${item} must be valid Pair. Value: ${animationDefinition[item]}`);
|
|
} else if (item === Properties.backgroundColor && !Color.isValid(animationDefinition.backgroundColor)) {
|
|
throw new Error(`Property ${item} must be valid color. Value: ${animationDefinition[item]}`);
|
|
} else if (item === Properties.width || item === Properties.height) {
|
|
// Coerce input into a PercentLength object in case it's a string.
|
|
animationDefinition[item] = PercentLength.parse(<any>animationDefinition[item]);
|
|
}
|
|
}
|
|
|
|
const propertyAnimations = new Array<PropertyAnimation>();
|
|
|
|
// opacity
|
|
if (animationDefinition.opacity !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.opacity,
|
|
value: animationDefinition.opacity,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// backgroundColor
|
|
if (animationDefinition.backgroundColor !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.backgroundColor,
|
|
value: typeof animationDefinition.backgroundColor === "string" ?
|
|
new Color(<any>animationDefinition.backgroundColor) : animationDefinition.backgroundColor,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// translate
|
|
if (animationDefinition.translate !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.translate,
|
|
value: animationDefinition.translate,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// scale
|
|
if (animationDefinition.scale !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.scale,
|
|
value: animationDefinition.scale,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// rotate
|
|
if (animationDefinition.rotate !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.rotate,
|
|
value: animationDefinition.rotate,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// height
|
|
if (animationDefinition.height !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.height,
|
|
value: animationDefinition.height,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
// width
|
|
if (animationDefinition.width !== undefined) {
|
|
propertyAnimations.push({
|
|
target: animationDefinition.target,
|
|
property: Properties.width,
|
|
value: animationDefinition.width,
|
|
duration: animationDefinition.duration,
|
|
delay: animationDefinition.delay,
|
|
iterations: animationDefinition.iterations,
|
|
curve: animationDefinition.curve
|
|
});
|
|
}
|
|
|
|
if (propertyAnimations.length === 0) {
|
|
throw new Error('No known animation properties specified');
|
|
}
|
|
|
|
return propertyAnimations;
|
|
}
|
|
|
|
public static _getAnimationInfo(animation: PropertyAnimation): string {
|
|
return JSON.stringify({
|
|
target: animation.target.id,
|
|
property: animation.property,
|
|
value: animation.value,
|
|
duration: animation.duration,
|
|
delay: animation.delay,
|
|
iterations: animation.iterations,
|
|
curve: animation.curve
|
|
});
|
|
}
|
|
}
|