Skip to main content

vizia_core/animation/
animation_builder.rs

1use crate::prelude::*;
2
3use vizia_style::{BorderWidth, Property};
4
5/// A builder for constructing animations.
6pub struct AnimationBuilder<'a> {
7    pub(crate) keyframes: Vec<KeyframeBuilder<'a>>,
8}
9
10impl Default for AnimationBuilder<'_> {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl AnimationBuilder<'_> {
17    /// Creates a new [AnimationBuilder].
18    pub fn new() -> Self {
19        Self { keyframes: Vec::new() }
20    }
21
22    /// Adds a new keyframe to the animation.
23    pub fn keyframe(
24        mut self,
25        time: f32,
26        keyframe: impl FnOnce(KeyframeBuilder) -> KeyframeBuilder,
27    ) -> Self {
28        let keyframe = (keyframe)(KeyframeBuilder::new(time));
29        self.keyframes.push(keyframe);
30
31        self
32    }
33}
34
35/// A builder for constructing keyframes.
36pub struct KeyframeBuilder<'a> {
37    pub(crate) time: f32,
38    pub(crate) properties: Vec<Property<'a>>,
39}
40
41impl<'a> KeyframeBuilder<'a> {
42    /// Creates a new [KeyframeBuilder].
43    pub(crate) fn new(time: f32) -> Self {
44        Self { time, properties: Vec::new() }
45    }
46
47    // DISPLAY
48
49    /// Set the display value for the keyframe.
50    pub fn display(mut self, val: impl Into<Display>) -> Self {
51        self.properties.push(Property::Display(val.into()));
52
53        self
54    }
55
56    /// Set the opacity value for the keyframe.
57    pub fn opacity(mut self, val: impl Into<Opacity>) -> Self {
58        self.properties.push(Property::Opacity(val.into()));
59
60        self
61    }
62
63    /// Set the clip-path value for the keyframe.
64    pub fn clip_path(mut self, val: impl Into<ClipPath>) -> Self {
65        self.properties.push(Property::ClipPath(val.into()));
66
67        self
68    }
69
70    // TRANSFORM
71
72    /// Set the transform value for the keyframe.
73    pub fn transform(mut self, val: impl Into<Vec<Transform>>) -> Self {
74        self.properties.push(Property::Transform(val.into()));
75
76        self
77    }
78
79    /// Set the transform origin value for the keyframe.
80    pub fn transform_origin(mut self, val: impl Into<Position>) -> Self {
81        self.properties.push(Property::TransformOrigin(val.into()));
82
83        self
84    }
85
86    /// Set the translate value for the keyframe.
87    pub fn translate(mut self, val: impl Into<Translate>) -> Self {
88        self.properties.push(Property::Translate(val.into()));
89
90        self
91    }
92
93    /// Set the rotate value for the keyframe.
94    pub fn rotate(mut self, val: impl Into<Angle>) -> Self {
95        self.properties.push(Property::Rotate(val.into()));
96
97        self
98    }
99
100    /// Set the scale value for the keyframe.
101    pub fn scale(mut self, val: impl Into<Scale>) -> Self {
102        self.properties.push(Property::Scale(val.into()));
103
104        self
105    }
106
107    // BORDER
108
109    /// Set the border width value for the keyframe.
110    pub fn border_width(mut self, val: impl Into<BorderWidth>) -> Self {
111        self.properties.push(Property::BorderWidth(val.into()));
112
113        self
114    }
115
116    /// Set the border color value for the keyframe.
117    pub fn border_color(mut self, val: impl Into<Color>) -> Self {
118        self.properties.push(Property::BorderColor(val.into()));
119
120        self
121    }
122
123    // CORNERS
124
125    pub fn corner_top_left_radius(mut self, val: impl Into<LengthOrPercentage>) -> Self {
126        self.properties.push(Property::CornerTopLeftRadius(val.into()));
127
128        self
129    }
130
131    pub fn corner_top_right_radius(mut self, val: impl Into<LengthOrPercentage>) -> Self {
132        self.properties.push(Property::CornerTopRightRadius(val.into()));
133
134        self
135    }
136
137    pub fn corner_bottom_left_radius(mut self, val: impl Into<LengthOrPercentage>) -> Self {
138        self.properties.push(Property::CornerBottomLeftRadius(val.into()));
139
140        self
141    }
142
143    pub fn corner_bottom_right_radius(mut self, val: impl Into<LengthOrPercentage>) -> Self {
144        self.properties.push(Property::CornerBottomRightRadius(val.into()));
145
146        self
147    }
148
149    // OUTLINE
150
151    pub fn outline_width(mut self, val: impl Into<BorderWidth>) -> Self {
152        self.properties.push(Property::OutlineWidth(val.into()));
153
154        self
155    }
156
157    pub fn outline_color(mut self, val: impl Into<Color>) -> Self {
158        self.properties.push(Property::OutlineColor(val.into()));
159
160        self
161    }
162
163    pub fn outline_offset(mut self, val: impl Into<LengthOrPercentage>) -> Self {
164        self.properties.push(Property::OutlineOffset(val.into()));
165
166        self
167    }
168
169    // BACKGROUND
170
171    pub fn background_color(mut self, val: impl Into<Color>) -> Self {
172        self.properties.push(Property::BackgroundColor(val.into()));
173
174        self
175    }
176
177    pub fn background_image(mut self, val: impl Into<Vec<BackgroundImage<'a>>>) -> Self {
178        self.properties.push(Property::BackgroundImage(val.into()));
179
180        self
181    }
182
183    pub fn background_position(mut self, val: impl Into<Vec<Position>>) -> Self {
184        self.properties.push(Property::BackgroundPosition(val.into()));
185
186        self
187    }
188
189    pub fn background_size(mut self, val: impl Into<Vec<BackgroundSize>>) -> Self {
190        self.properties.push(Property::BackgroundSize(val.into()));
191
192        self
193    }
194
195    pub fn background_repeat(mut self, val: impl Into<Vec<BackgroundRepeat>>) -> Self {
196        self.properties.push(Property::BackgroundRepeat(val.into()));
197
198        self
199    }
200
201    // BOX SHADOW
202
203    pub fn shadow(mut self, val: impl Into<Vec<Shadow>>) -> Self {
204        self.properties.push(Property::Shadow(val.into()));
205
206        self
207    }
208
209    // TEXT
210
211    pub fn color(mut self, val: impl Into<Color>) -> Self {
212        self.properties.push(Property::FontColor(val.into()));
213
214        self
215    }
216
217    pub fn font_size(mut self, val: impl Into<FontSize>) -> Self {
218        self.properties.push(Property::FontSize(val.into()));
219
220        self
221    }
222
223    pub fn letter_spacing(mut self, val: impl Into<LetterSpacing>) -> Self {
224        self.properties.push(Property::LetterSpacing(val.into()));
225
226        self
227    }
228
229    pub fn caret_color(mut self, val: impl Into<Color>) -> Self {
230        self.properties.push(Property::CaretColor(val.into()));
231
232        self
233    }
234
235    pub fn selection_color(mut self, val: impl Into<Color>) -> Self {
236        self.properties.push(Property::SelectionColor(val.into()));
237
238        self
239    }
240
241    // SPACE
242
243    pub fn left(mut self, val: impl Into<Units>) -> Self {
244        self.properties.push(Property::Left(val.into()));
245
246        self
247    }
248
249    pub fn right(mut self, val: impl Into<Units>) -> Self {
250        self.properties.push(Property::Right(val.into()));
251
252        self
253    }
254
255    pub fn top(mut self, val: impl Into<Units>) -> Self {
256        self.properties.push(Property::Top(val.into()));
257
258        self
259    }
260
261    pub fn bottom(mut self, val: impl Into<Units>) -> Self {
262        self.properties.push(Property::Bottom(val.into()));
263
264        self
265    }
266
267    // PADDING
268
269    pub fn padding_left(mut self, val: impl Into<Units>) -> Self {
270        self.properties.push(Property::PaddingLeft(val.into()));
271
272        self
273    }
274
275    pub fn padding_right(mut self, val: impl Into<Units>) -> Self {
276        self.properties.push(Property::PaddingRight(val.into()));
277
278        self
279    }
280
281    pub fn padding_top(mut self, val: impl Into<Units>) -> Self {
282        self.properties.push(Property::PaddingTop(val.into()));
283
284        self
285    }
286
287    pub fn padding_bottom(mut self, val: impl Into<Units>) -> Self {
288        self.properties.push(Property::PaddingBottom(val.into()));
289
290        self
291    }
292
293    pub fn horizontal_gap(mut self, val: impl Into<Units>) -> Self {
294        self.properties.push(Property::HorizontalGap(val.into()));
295
296        self
297    }
298
299    pub fn vertical_gap(mut self, val: impl Into<Units>) -> Self {
300        self.properties.push(Property::VerticalGap(val.into()));
301
302        self
303    }
304
305    // SIZE
306
307    pub fn width(mut self, val: impl Into<Units>) -> Self {
308        self.properties.push(Property::Width(val.into()));
309
310        self
311    }
312
313    pub fn height(mut self, val: impl Into<Units>) -> Self {
314        self.properties.push(Property::Height(val.into()));
315
316        self
317    }
318
319    // SIZE CONSTRAINTS
320    pub fn min_width(mut self, val: impl Into<Units>) -> Self {
321        self.properties.push(Property::MinWidth(val.into()));
322
323        self
324    }
325
326    pub fn max_width(mut self, val: impl Into<Units>) -> Self {
327        self.properties.push(Property::MaxWidth(val.into()));
328
329        self
330    }
331
332    pub fn min_height(mut self, val: impl Into<Units>) -> Self {
333        self.properties.push(Property::MinHeight(val.into()));
334
335        self
336    }
337
338    pub fn max_height(mut self, val: impl Into<Units>) -> Self {
339        self.properties.push(Property::MaxHeight(val.into()));
340
341        self
342    }
343}