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    /// ## `start_time`
45    /// time when the animation starts playing
46    ///
47    /// # Returns
48    ///
49    /// an iterator to move over the animation
50    #[doc(alias = "gdk_pixbuf_animation_get_iter")]
51    #[doc(alias = "get_iter")]
52    fn iter(&self, start_time: Option<SystemTime>) -> PixbufAnimationIter {
53        let start_time = start_time.map(|s| {
54            let diff = s
55                .duration_since(SystemTime::UNIX_EPOCH)
56                .expect("failed to convert time");
57            glib::ffi::GTimeVal {
58                tv_sec: diff.as_secs() as _,
59                tv_usec: diff.subsec_micros() as _,
60            }
61        });
62
63        unsafe {
64            from_glib_full(ffi::gdk_pixbuf_animation_get_iter(
65                self.as_ref().to_glib_none().0,
66                start_time
67                    .as_ref()
68                    .map(|t| t as *const glib::ffi::GTimeVal)
69                    .unwrap_or(ptr::null()),
70            ))
71        }
72    }
73}
74
75impl<O: IsA<PixbufAnimation>> PixbufAnimationExtManual for O {}