gdk_pixbuf/
pixbuf_animation.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{ptr, time::SystemTime};
4
5use glib::{prelude::*, translate::*};
6
7use crate::{ffi, PixbufAnimation, PixbufAnimationIter};
8
9pub trait PixbufAnimationExtManual: IsA<PixbufAnimation> + 'static {
10    /// Get an iterator for displaying an animation.
11    ///
12    /// The iterator provides the frames that should be displayed at a
13    /// given time.
14    ///
15    /// @start_time would normally come from g_get_current_time(), and marks
16    /// the beginning of animation playback. After creating an iterator, you
17    /// should immediately display the pixbuf returned by
18    /// gdk_pixbuf_animation_iter_get_pixbuf(). Then, you should install
19    /// a timeout (with g_timeout_add()) or by some other mechanism ensure
20    /// that you'll update the image after
21    /// gdk_pixbuf_animation_iter_get_delay_time() milliseconds. Each time
22    /// the image is updated, you should reinstall the timeout with the new,
23    /// possibly-changed delay time.
24    ///
25    /// As a shortcut, if @start_time is `NULL`, the result of
26    /// g_get_current_time() will be used automatically.
27    ///
28    /// To update the image (i.e. possibly change the result of
29    /// gdk_pixbuf_animation_iter_get_pixbuf() to a new frame of the animation),
30    /// call gdk_pixbuf_animation_iter_advance().
31    ///
32    /// If you're using #GdkPixbufLoader, in addition to updating the image
33    /// after the delay time, you should also update it whenever you
34    /// receive the area_updated signal and
35    /// gdk_pixbuf_animation_iter_on_currently_loading_frame() returns
36    /// `TRUE`. In this case, the frame currently being fed into the loader
37    /// has received new data, so needs to be refreshed. The delay time for
38    /// a frame may also be modified after an area_updated signal, for
39    /// example if the delay time for a frame is encoded in the data after
40    /// the frame itself. So your timeout should be reinstalled after any
41    /// area_updated signal.
42    ///
43    /// A delay time of -1 is possible, indicating "infinite".
44    ///
45    /// # Deprecated since 2.44
46    ///
47    /// Use a different image loading library for animatable assets
48    /// ## `start_time`
49    /// time when the animation starts playing
50    ///
51    /// # Returns
52    ///
53    /// an iterator to move over the animation
54    #[doc(alias = "gdk_pixbuf_animation_get_iter")]
55    #[doc(alias = "get_iter")]
56    fn iter(&self, start_time: Option<SystemTime>) -> PixbufAnimationIter {
57        let start_time = start_time.map(|s| {
58            let diff = s
59                .duration_since(SystemTime::UNIX_EPOCH)
60                .expect("failed to convert time");
61            glib::ffi::GTimeVal {
62                tv_sec: diff.as_secs() as _,
63                tv_usec: diff.subsec_micros() as _,
64            }
65        });
66
67        unsafe {
68            from_glib_full(ffi::gdk_pixbuf_animation_get_iter(
69                self.as_ref().to_glib_none().0,
70                start_time
71                    .as_ref()
72                    .map(|t| t as *const glib::ffi::GTimeVal)
73                    .unwrap_or(ptr::null()),
74            ))
75        }
76    }
77}
78
79impl<O: IsA<PixbufAnimation>> PixbufAnimationExtManual for O {}