1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.animation.tooling
18 
19 import kotlin.jvm.JvmDefaultWithCompatibility
20 
21 /**
22  * Type of the animation. Different types might have different properties, for example a
23  * `TransitionAnimation` (represented by [TRANSITION_ANIMATION]) has a set of states associated with
24  * it.
25  */
26 public enum class ComposeAnimationType {
27     TRANSITION_ANIMATION,
28     ANIMATED_VALUE,
29     ANIMATED_VISIBILITY,
30     /** `Animatable` compose animation. */
31     ANIMATABLE,
32     /** `animateContentSize` compose animation. */
33     ANIMATE_CONTENT_SIZE,
34     /**
35      * `animateXAsState` animations, for example `animateDpAsState`, `animateIntAsState`. Includes
36      * `animateValueAsState`.
37      */
38     ANIMATE_X_AS_STATE,
39     /** `AnimatedContent` animation. */
40     ANIMATED_CONTENT,
41     /** `DecayAnimation` animation. */
42     DECAY_ANIMATION,
43     /** `rememberInfiniteTransition` animation. */
44     INFINITE_TRANSITION,
45     /** `TargetBasedAnimation` animation. */
46     TARGET_BASED_ANIMATION,
47     /**
48      * Detected animation without a support - only [ComposeAnimation.label] and
49      * [ComposeAnimation.type] are provided.
50      */
51     UNSUPPORTED
52 }
53 
54 /**
55  * Simple interface to make it easier to share Compose animation objects between `ui-tooling` and
56  * Android Studio. Since both ends communicate mostly using bytecode manipulation and reflection,
57  * being able to parse these objects into a common type makes
58  */
59 @JvmDefaultWithCompatibility
60 public interface ComposeAnimation {
61 
62     /**
63      * The animation type. Ideally, the type should be checked before accessing properties specific
64      * to a certain type, e.g. [states].
65      */
66     public val type: ComposeAnimationType
67 
68     /** The actual animation object. */
69     public val animationObject: Any
70 
71     /**
72      * All the available states of a `TransitionAnimation`.
73      *
74      * @throws UnsupportedOperationException if [type] does not return `TRANSITION_ANIMATION`.
75      */
76     public val states: Set<Any>
77         @Suppress("DocumentExceptions")
78         get() =
79             throw UnsupportedOperationException(
80                 "Only available when getType() is TRANSITION_ANIMATION"
81             )
82 
83     /**
84      * A label which can be used to represent the animation as text in Android Studio. Null if the
85      * label is not set or if it can't be inferred from the animation states.
86      */
87     public val label: String?
88         get() = null
89 }
90