| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8141737 commit fa80355
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,6 +54,31 @@ export function test_AnimatingProperties(done) { | |||
| 54 | 54 | // << animation-properties | |
| 55 | 55 | } | |
| 56 | 56 | ||
| 57 | + export function test_PlayRejectsWhenAlreadyPlayingAnimation(done) { | ||
| 58 | + let label = prepareTest(); | ||
| 59 | + | ||
| 60 | + var animation = label.createAnimation({ translate: { x: 100, y: 100 }, duration: 5 }); | ||
| 61 | + | ||
| 62 | + animation.play(); | ||
| 63 | + animation.play().then(() => { | ||
| 64 | + // should never get here | ||
| 65 | + throw new Error("Already playing."); | ||
| 66 | + }, (e) => { | ||
| 67 | + TKUnit.assert(animation.isPlaying === true, "animation.isPlaying should be true since it's currently playing."); | ||
| 68 | + if (e === "Animation is already playing.") { | ||
| 69 | + done(); | ||
| 70 | + } | ||
| 71 | + }); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + export function test_CancelIgnoredWhenNotPlayingAnimation() { | ||
| 75 | + let label = prepareTest(); | ||
| 76 | + | ||
| 77 | + var animation = label.createAnimation({ translate: { x: 100, y: 100 }, duration: 5 }); | ||
| 78 | + animation.cancel(); // should not throw | ||
| 79 | + TKUnit.assert(!animation.isPlaying, "animation.isPlaying should be falsey since it was never played."); | ||
| 80 | + } | ||
| 81 | + | ||
| 57 | 82 | export function test_CancellingAnimation(done) { | |
| 58 | 83 | let label = prepareTest(); | |
| 59 | 84 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,9 +10,9 @@ import { View } from "../core/view"; | |||
| 10 | 10 | ||
| 11 | 11 | // Types. | |
| 12 | 12 | import { Color } from "../../color"; | |
| 13 | - import { isEnabled as traceEnabled, write as traceWrite, categories as traceCategories } from "../../trace"; | ||
| 13 | + import { isEnabled as traceEnabled, write as traceWrite, categories as traceCategories, messageType as traceType } from "../../trace"; | ||
| 14 | 14 | ||
| 15 | - export { Color, traceEnabled, traceWrite, traceCategories }; | ||
| 15 | + export { Color, traceEnabled, traceWrite, traceCategories, traceType }; | ||
| 16 | 16 | export { AnimationPromise } from "."; | |
| 17 | 17 | ||
| 18 | 18 | export module Properties { | |
@@ -84,11 +84,16 @@ export abstract class AnimationBase implements AnimationBaseDefinition { | |||
| 84 | 84 | ||
| 85 | 85 | abstract _resolveAnimationCurve(curve: any): any; | |
| 86 | 86 | ||
| 87 | - public play(): AnimationPromiseDefinition { | ||
| 88 | - if (this.isPlaying) { | ||
| 89 | - throw new Error("Animation is already playing."); | ||
| 90 | - } | ||
| 87 | + protected _rejectAlreadyPlaying(): AnimationPromiseDefinition{ | ||
| 88 | + const reason = "Animation is already playing."; | ||
| 89 | + traceWrite(reason, traceCategories.Animation, traceType.warn); | ||
| 90 | + | ||
| 91 | + return <AnimationPromiseDefinition>new Promise<void>((resolve, reject) => { | ||
| 92 | + reject(reason); | ||
| 93 | + }); | ||
| 94 | + } | ||
| 91 | 95 | ||
| 96 | + public play(): AnimationPromiseDefinition { | ||
| 92 | 97 | // We have to actually create a "Promise" due to a bug in the v8 engine and decedent promises | |
| 93 | 98 | // We just cast it to a animationPromise so that all the rest of the code works fine | |
| 94 | 99 | var animationFinishedPromise = <AnimationPromiseDefinition>new Promise<void>((resolve, reject) => { | |
@@ -123,9 +128,7 @@ export abstract class AnimationBase implements AnimationBaseDefinition { | |||
| 123 | 128 | } | |
| 124 | 129 | ||
| 125 | 130 | public cancel(): void { | |
| 126 | - if (!this.isPlaying) { | ||
| 127 | - throw new Error("Animation is not currently playing."); | ||
| 128 | - } | ||
| 131 | + // Implemented in platform specific files | ||
| 129 | 132 | } | |
| 130 | 133 | ||
| 131 | 134 | public get isPlaying(): boolean { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | import { AnimationDefinition } from "."; | |
| 3 | 3 | import { View } from "../core/view"; | |
| 4 | 4 | ||
| 5 | - import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, Color, traceWrite, traceEnabled, traceCategories } from "./animation-common"; | ||
| 5 | + import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, Color, traceWrite, traceEnabled, traceCategories, traceType } from "./animation-common"; | ||
| 6 | 6 | import { | |
| 7 | 7 | opacityProperty, backgroundColorProperty, rotateProperty, | |
| 8 | 8 | translateXProperty, translateYProperty, scaleXProperty, scaleYProperty | |
@@ -135,6 +135,10 @@ export class Animation extends AnimationBase { | |||
| 135 | 135 | } | |
| 136 | 136 | ||
| 137 | 137 | public play(): AnimationPromise { | |
| 138 | + if (this.isPlaying) { | ||
| 139 | + return this._rejectAlreadyPlaying(); | ||
| 140 | + } | ||
| 141 | + | ||
| 138 | 142 | let animationFinishedPromise = super.play(); | |
| 139 | 143 | ||
| 140 | 144 | this._animators = new Array<android.animation.Animator>(); | |
@@ -170,10 +174,13 @@ export class Animation extends AnimationBase { | |||
| 170 | 174 | } | |
| 171 | 175 | ||
| 172 | 176 | public cancel(): void { | |
| 173 | - super.cancel(); | ||
| 174 | - if (traceEnabled()) { | ||
| 175 | - traceWrite("Cancelling AnimatorSet.", traceCategories.Animation); | ||
| 177 | + if (!this.isPlaying) { | ||
| 178 | + traceWrite("Animation is not currently playing.", traceCategories.Animation, traceType.warn); | ||
| 179 | + return; | ||
| 176 | 180 | } | |
| 181 | + | ||
| 182 | + traceWrite("Cancelling AnimatorSet.", traceCategories.Animation); | ||
| 183 | + | ||
| 177 | 184 | this._animatorSet.cancel(); | |
| 178 | 185 | } | |
| 179 | 186 | ||
@@ -301,7 +308,7 @@ export class Animation extends AnimationBase { | |||
| 301 | 308 | } else { | |
| 302 | 309 | propertyAnimation.target.style[backgroundColorProperty.keyframe] = originalValue1; | |
| 303 | 310 | } | |
| 304 | - | ||
| 311 | + | ||
| 305 | 312 | if (propertyAnimation.target.nativeViewProtected && propertyAnimation.target[backgroundColorProperty.setNative]) { | |
| 306 | 313 | propertyAnimation.target[backgroundColorProperty.setNative](propertyAnimation.target.style.backgroundColor); | |
| 307 | 314 | } | |
@@ -414,7 +421,7 @@ export class Animation extends AnimationBase { | |||
| 414 | 421 | } else { | |
| 415 | 422 | propertyAnimation.target.style[rotateProperty.keyframe] = originalValue1; | |
| 416 | 423 | } | |
| 417 | - | ||
| 424 | + | ||
| 418 | 425 | if (propertyAnimation.target.nativeViewProtected) { | |
| 419 | 426 | propertyAnimation.target[rotateProperty.setNative](propertyAnimation.target.style.rotate); | |
| 420 | 427 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | import { AnimationDefinition } from "."; | |
| 2 | 2 | import { View } from "../core/view"; | |
| 3 | 3 | ||
| 4 | - import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, traceWrite, traceEnabled, traceCategories } from "./animation-common"; | ||
| 4 | + import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, traceWrite, traceEnabled, traceCategories, traceType } from "./animation-common"; | ||
| 5 | 5 | import { | |
| 6 | 6 | opacityProperty, backgroundColorProperty, rotateProperty, | |
| 7 | 7 | translateXProperty, translateYProperty, scaleXProperty, scaleYProperty | |
@@ -210,6 +210,10 @@ export class Animation extends AnimationBase { | |||
| 210 | 210 | } | |
| 211 | 211 | ||
| 212 | 212 | public play(): AnimationPromise { | |
| 213 | + if (this.isPlaying) { | ||
| 214 | + return this._rejectAlreadyPlaying(); | ||
| 215 | + } | ||
| 216 | + | ||
| 213 | 217 | let animationFinishedPromise = super.play(); | |
| 214 | 218 | this._finishedAnimations = 0; | |
| 215 | 219 | this._cancelledAnimations = 0; | |
@@ -218,7 +222,10 @@ export class Animation extends AnimationBase { | |||
| 218 | 222 | } | |
| 219 | 223 | ||
| 220 | 224 | public cancel(): void { | |
| 221 | - super.cancel(); | ||
| 225 | + if (!this.isPlaying) { | ||
| 226 | + traceWrite("Animation is not currently playing.", traceCategories.Animation, traceType.warn); | ||
| 227 | + return; | ||
| 228 | + } | ||
| 222 | 229 | ||
| 223 | 230 | let i = 0; | |
| 224 | 231 | let length = this._mergedPropertyAnimations.length; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,8 @@ import { View, Color } from "../core/view"; | |||
| 12 | 12 | ||
| 13 | 13 | import { AnimationCurve } from "../enums"; | |
| 14 | 14 | ||
| 15 | + import { isEnabled as traceEnabled, write as traceWrite, categories as traceCategories, messageType as traceType } from "../../trace"; | ||
| 16 | + | ||
| 15 | 17 | // Types. | |
| 16 | 18 | import { unsetValue } from "../core/properties"; | |
| 17 | 19 | import { Animation } from "./animation"; | |
@@ -143,25 +145,32 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition { | |||
| 143 | 145 | } | |
| 144 | 146 | ||
| 145 | 147 | public cancel() { | |
| 146 | - if (this._isPlaying) { | ||
| 147 | - this._isPlaying = false; | ||
| 148 | - for (let i = this._nativeAnimations.length - 1; i >= 0; i--) { | ||
| 149 | - let animation = this._nativeAnimations[i]; | ||
| 150 | - if (animation.isPlaying) { | ||
| 151 | - animation.cancel(); | ||
| 152 | - } | ||
| 153 | - } | ||
| 154 | - if (this._nativeAnimations.length > 0) { | ||
| 155 | - let animation = this._nativeAnimations[0]; | ||
| 156 | - this._resetAnimationValues(this._target, animation); | ||
| 148 | + if (!this.isPlaying) { | ||
| 149 | + traceWrite("Keyframe animation is already playing.", traceCategories.Animation, traceType.warn); | ||
| 150 | + return; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + this._isPlaying = false; | ||
| 154 | + for (let i = this._nativeAnimations.length - 1; i >= 0; i--) { | ||
| 155 | + let animation = this._nativeAnimations[i]; | ||
| 156 | + if (animation.isPlaying) { | ||
| 157 | + animation.cancel(); | ||
| 157 | 158 | } | |
| 158 | - this._rejectAnimationFinishedPromise(); | ||
| 159 | 159 | } | |
| 160 | + if (this._nativeAnimations.length > 0) { | ||
| 161 | + let animation = this._nativeAnimations[0]; | ||
| 162 | + this._resetAnimationValues(this._target, animation); | ||
| 163 | + } | ||
| 164 | + this._rejectAnimationFinishedPromise(); | ||
| 160 | 165 | } | |
| 161 | 166 | ||
| 162 | 167 | public play(view: View): Promise<void> { | |
| 163 | 168 | if (this._isPlaying) { | |
| 164 | - throw new Error("Animation is already playing."); | ||
| 169 | + const reason = "Keyframe animation is already playing."; | ||
| 170 | + traceWrite(reason, traceCategories.Animation, traceType.warn); | ||
| 171 | + return new Promise<void>((resolve, reject) => { | ||
| 172 | + reject(reason); | ||
| 173 | + }); | ||
| 165 | 174 | } | |
| 166 | 175 | ||
| 167 | 176 | let animationFinishedPromise = new Promise<void>((resolve, reject) => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments