1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::{ffi, AnimationState, AnimationTarget};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// A base class for animations.
    ///
    /// `BisAnimation` represents an animation on a widget. It has a target that
    /// provides a value to animate, and a state indicating whether the
    /// animation hasn't been started yet, is playing, paused or finished.
    ///
    /// Currently there are two concrete animation types:
    /// [`TimedAnimation`][crate::TimedAnimation] and [`SpringAnimation`][crate::SpringAnimation].
    ///
    /// `BisAnimation` will automatically skip the animation if
    /// [`widget`][struct@crate::Animation#widget] is unmapped, or if
    /// [`gtk-enable-animations`][struct@crate::Gtk::Settings#gtk-enable-animations] is `FALSE`.
    ///
    /// The [`done`][struct@crate::Animation#done] signal can be used to perform an action after
    /// the animation ends, for example hiding a widget after animating its
    /// [`opacity`][struct@crate::gtk::Widget#opacity] to 0.
    ///
    /// `BisAnimation` will be kept alive while the animation is playing. As such,
    /// it's safe to create an animation, start it and immediately unref it:
    /// A fire-and-forget animation:
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// static void
    /// animation_cb (double    value,
    ///               MyObject *self)
    /// {
    ///   // Do something with @value
    /// }
    ///
    /// static void
    /// my_object_animate (MyObject *self)
    /// {
    ///   BisAnimationTarget *target =
    ///     bis_callback_animation_target_new ((BisAnimationTargetFunc) animation_cb,
    ///                                        self, NULL);
    ///   g_autoptr (BisAnimation) animation =
    ///     bis_timed_animation_new (widget, 0, 1, 250, target);
    ///
    ///   bis_animation_play (animation);
    /// }
    /// ```
    ///
    /// If there's a chance the previous animation for the same target hasn't yet
    /// finished, the previous animation should be stopped first, or the existing
    /// `BisAnimation` object can be reused.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// ## Properties
    ///
    ///
    /// #### `state`
    ///  The animation state.
    ///
    /// The state indicates whether the animation is currently playing, paused,
    /// finished or hasn't been started yet.
    ///
    /// Readable
    ///
    ///
    /// #### `target`
    ///  The target to animate.
    ///
    /// Readable | Writeable | Construct
    ///
    ///
    /// #### `value`
    ///  The current value of the animation.
    ///
    /// Readable
    ///
    ///
    /// #### `widget`
    ///  The animation widget.
    ///
    /// It provides the frame clock for the animation. It's not strictly necessary
    /// for this widget to be same as the one being animated.
    ///
    /// The widget must be mapped in order for the animation to work. If it's not
    /// mapped, or if it gets unmapped during an ongoing animation, the animation
    /// will be automatically skipped.
    ///
    /// Readable | Writeable | Construct Only
    ///
    /// ## Signals
    ///
    ///
    /// #### `done`
    ///  This signal is emitted when the animation has been completed, either on its
    /// own or via calling [`AnimationExt::skip()`][crate::prelude::AnimationExt::skip()].
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`AnimationExt`][trait@crate::prelude::AnimationExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "BisAnimation")]
    pub struct Animation(Object<ffi::BisAnimation, ffi::BisAnimationClass>);

    match fn {
        type_ => || ffi::bis_animation_get_type(),
    }
}

impl Animation {
    pub const NONE: Option<&'static Animation> = None;
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Animation>> Sealed for T {}
}

/// Trait containing all [`struct@Animation`] methods.
///
/// # Implementors
///
/// [`Animation`][struct@crate::Animation], [`SpringAnimation`][struct@crate::SpringAnimation], [`TimedAnimation`][struct@crate::TimedAnimation]
pub trait AnimationExt: IsA<Animation> + sealed::Sealed + 'static {
    /// Gets the current value of @self.
    ///
    /// The state indicates whether @self is currently playing, paused, finished or
    /// hasn't been started yet.
    ///
    /// # Returns
    ///
    /// the animation value
    #[doc(alias = "bis_animation_get_state")]
    #[doc(alias = "get_state")]
    fn state(&self) -> AnimationState {
        unsafe { from_glib(ffi::bis_animation_get_state(self.as_ref().to_glib_none().0)) }
    }

    /// Gets the target @self animates.
    ///
    /// # Returns
    ///
    /// the animation target
    #[doc(alias = "bis_animation_get_target")]
    #[doc(alias = "get_target")]
    fn target(&self) -> AnimationTarget {
        unsafe {
            from_glib_none(ffi::bis_animation_get_target(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Gets the current value of @self.
    ///
    /// # Returns
    ///
    /// the current value
    #[doc(alias = "bis_animation_get_value")]
    #[doc(alias = "get_value")]
    fn value(&self) -> f64 {
        unsafe { ffi::bis_animation_get_value(self.as_ref().to_glib_none().0) }
    }

    /// Gets the widget @self was created for.
    ///
    /// It provides the frame clock for the animation. It's not strictly necessary
    /// for this widget to be same as the one being animated.
    ///
    /// The widget must be mapped in order for the animation to work. If it's not
    /// mapped, or if it gets unmapped during an ongoing animation, the animation
    /// will be automatically skipped.
    ///
    /// # Returns
    ///
    /// the animation widget
    #[doc(alias = "bis_animation_get_widget")]
    #[doc(alias = "get_widget")]
    fn widget(&self) -> gtk::Widget {
        unsafe {
            from_glib_none(ffi::bis_animation_get_widget(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Pauses a playing animation for @self.
    ///
    /// Does nothing if the current state of @self isn't `BIS_ANIMATION_PLAYING`.
    ///
    /// Sets [`state`][struct@crate::Animation#state] to `BIS_ANIMATION_PAUSED`.
    #[doc(alias = "bis_animation_pause")]
    fn pause(&self) {
        unsafe {
            ffi::bis_animation_pause(self.as_ref().to_glib_none().0);
        }
    }

    /// Starts the animation for @self.
    ///
    /// If the animation is playing, paused or has been completed, restarts it from
    /// the beginning. This allows to easily play an animation regardless of whether
    /// it's already playing or not.
    ///
    /// Sets [`state`][struct@crate::Animation#state] to `BIS_ANIMATION_PLAYING`.
    ///
    /// The animation will be automatically skipped if [`widget`][struct@crate::Animation#widget] is
    /// unmapped, or if [`gtk-enable-animations`][struct@crate::Gtk::Settings#gtk-enable-animations] is `FALSE`.
    ///
    /// As such, it's not guaranteed that the animation will actually run. For
    /// example, when using `idle_add()` and starting an animation
    /// immediately afterwards, it's entirely possible that the idle callback will
    /// run after the animation has already finished, and not while it's playing.
    #[doc(alias = "bis_animation_play")]
    fn play(&self) {
        unsafe {
            ffi::bis_animation_play(self.as_ref().to_glib_none().0);
        }
    }

    /// Resets the animation for @self.
    ///
    /// Sets [`state`][struct@crate::Animation#state] to `BIS_ANIMATION_IDLE`.
    #[doc(alias = "bis_animation_reset")]
    fn reset(&self) {
        unsafe {
            ffi::bis_animation_reset(self.as_ref().to_glib_none().0);
        }
    }

    /// Resumes a paused animation for @self.
    ///
    /// This function must only be used if the animation has been paused with
    /// [`pause()`][Self::pause()].
    ///
    /// Sets [`state`][struct@crate::Animation#state] to `BIS_ANIMATION_PLAYING`.
    #[doc(alias = "bis_animation_resume")]
    fn resume(&self) {
        unsafe {
            ffi::bis_animation_resume(self.as_ref().to_glib_none().0);
        }
    }

    /// Sets the target @self animates to @target.
    /// ## `target`
    /// an animation target
    #[doc(alias = "bis_animation_set_target")]
    #[doc(alias = "target")]
    fn set_target(&self, target: &impl IsA<AnimationTarget>) {
        unsafe {
            ffi::bis_animation_set_target(
                self.as_ref().to_glib_none().0,
                target.as_ref().to_glib_none().0,
            );
        }
    }

    /// Skips the animation for @self.
    ///
    /// If the animation hasn't been started yet, is playing, or is paused, instantly
    /// skips the animation to the end and causes [`done`][struct@crate::Animation#done] to be
    /// emitted.
    ///
    /// Sets [`state`][struct@crate::Animation#state] to `BIS_ANIMATION_FINISHED`.
    #[doc(alias = "bis_animation_skip")]
    fn skip(&self) {
        unsafe {
            ffi::bis_animation_skip(self.as_ref().to_glib_none().0);
        }
    }

    /// This signal is emitted when the animation has been completed, either on its
    /// own or via calling [`skip()`][Self::skip()].
    #[doc(alias = "done")]
    fn connect_done<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn done_trampoline<P: IsA<Animation>, F: Fn(&P) + 'static>(
            this: *mut ffi::BisAnimation,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Animation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"done\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    done_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "state")]
    fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_state_trampoline<P: IsA<Animation>, F: Fn(&P) + 'static>(
            this: *mut ffi::BisAnimation,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Animation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::state\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_state_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "target")]
    fn connect_target_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_target_trampoline<P: IsA<Animation>, F: Fn(&P) + 'static>(
            this: *mut ffi::BisAnimation,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Animation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::target\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_target_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "value")]
    fn connect_value_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_value_trampoline<P: IsA<Animation>, F: Fn(&P) + 'static>(
            this: *mut ffi::BisAnimation,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Animation::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::value\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_value_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<Animation>> AnimationExt for O {}