gdk_pixbuf/auto/
pixbuf_animation.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4#![allow(deprecated)]
5
6use crate::{Pixbuf, ffi};
7use glib::{prelude::*, translate::*};
8use std::{boxed::Box as Box_, pin::Pin};
9
10glib::wrapper! {
11    /// Use a different image loading library for animatable assets
12    /// An opaque object representing an animation.
13    ///
14    /// The GdkPixBuf library provides a simple mechanism to load and
15    /// represent animations. An animation is conceptually a series of
16    /// frames to be displayed over time.
17    ///
18    /// The animation may not be represented as a series of frames
19    /// internally; for example, it may be stored as a sprite and
20    /// instructions for moving the sprite around a background.
21    ///
22    /// To display an animation you don't need to understand its
23    /// representation, however; you just ask [`Pixbuf`][crate::Pixbuf] what should
24    /// be displayed at a given point in time.
25    ///
26    /// # Implements
27    ///
28    /// [`PixbufAnimationExt`][trait@crate::prelude::PixbufAnimationExt], [`PixbufAnimationExtManual`][trait@crate::prelude::PixbufAnimationExtManual]
29    #[doc(alias = "GdkPixbufAnimation")]
30    pub struct PixbufAnimation(Object<ffi::GdkPixbufAnimation, ffi::GdkPixbufAnimationClass>);
31
32    match fn {
33        type_ => || ffi::gdk_pixbuf_animation_get_type(),
34    }
35}
36
37impl PixbufAnimation {
38    pub const NONE: Option<&'static PixbufAnimation> = None;
39
40    /// Creates a new animation by loading it from a file.
41    ///
42    /// The file format is detected automatically.
43    ///
44    /// If the file's format does not support multi-frame images, then an animation
45    /// with a single frame will be created.
46    ///
47    /// Possible errors are in the `GDK_PIXBUF_ERROR` and `G_FILE_ERROR` domains.
48    ///
49    /// # Deprecated since 2.44
50    ///
51    /// Use a different image loading library for animatable assets
52    /// ## `filename`
53    /// Name of file to load, in the GLib file
54    ///   name encoding
55    ///
56    /// # Returns
57    ///
58    /// A newly-created animation
59    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
60    #[allow(deprecated)]
61    #[doc(alias = "gdk_pixbuf_animation_new_from_file")]
62    #[doc(alias = "new_from_file")]
63    pub fn from_file(
64        filename: impl AsRef<std::path::Path>,
65    ) -> Result<PixbufAnimation, glib::Error> {
66        unsafe {
67            let mut error = std::ptr::null_mut();
68            let ret = ffi::gdk_pixbuf_animation_new_from_file(
69                filename.as_ref().to_glib_none().0,
70                &mut error,
71            );
72            if error.is_null() {
73                Ok(from_glib_full(ret))
74            } else {
75                Err(from_glib_full(error))
76            }
77        }
78    }
79
80    /// Creates a new pixbuf animation by loading an image from an resource.
81    ///
82    /// The file format is detected automatically. If `NULL` is returned, then
83    /// @error will be set.
84    ///
85    /// # Deprecated since 2.44
86    ///
87    /// Use a different image loading library for animatable assets
88    /// ## `resource_path`
89    /// the path of the resource file
90    ///
91    /// # Returns
92    ///
93    /// A newly-created animation
94    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
95    #[allow(deprecated)]
96    #[doc(alias = "gdk_pixbuf_animation_new_from_resource")]
97    #[doc(alias = "new_from_resource")]
98    pub fn from_resource(resource_path: &str) -> Result<PixbufAnimation, glib::Error> {
99        unsafe {
100            let mut error = std::ptr::null_mut();
101            let ret = ffi::gdk_pixbuf_animation_new_from_resource(
102                resource_path.to_glib_none().0,
103                &mut error,
104            );
105            if error.is_null() {
106                Ok(from_glib_full(ret))
107            } else {
108                Err(from_glib_full(error))
109            }
110        }
111    }
112
113    /// Creates a new animation by loading it from an input stream.
114    ///
115    /// The file format is detected automatically.
116    ///
117    /// If `NULL` is returned, then @error will be set.
118    ///
119    /// The @cancellable can be used to abort the operation from another thread.
120    /// If the operation was cancelled, the error `G_IO_ERROR_CANCELLED` will be
121    /// returned. Other possible errors are in the `GDK_PIXBUF_ERROR` and
122    /// `G_IO_ERROR` domains.
123    ///
124    /// The stream is not closed.
125    ///
126    /// # Deprecated since 2.44
127    ///
128    /// Use a different image loading library for animatable assets
129    /// ## `stream`
130    /// a `GInputStream` to load the pixbuf from
131    /// ## `cancellable`
132    /// optional `GCancellable` object
133    ///
134    /// # Returns
135    ///
136    /// A newly-created animation
137    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
138    #[allow(deprecated)]
139    #[doc(alias = "gdk_pixbuf_animation_new_from_stream")]
140    #[doc(alias = "new_from_stream")]
141    pub fn from_stream(
142        stream: &impl IsA<gio::InputStream>,
143        cancellable: Option<&impl IsA<gio::Cancellable>>,
144    ) -> Result<PixbufAnimation, glib::Error> {
145        unsafe {
146            let mut error = std::ptr::null_mut();
147            let ret = ffi::gdk_pixbuf_animation_new_from_stream(
148                stream.as_ref().to_glib_none().0,
149                cancellable.map(|p| p.as_ref()).to_glib_none().0,
150                &mut error,
151            );
152            if error.is_null() {
153                Ok(from_glib_full(ret))
154            } else {
155                Err(from_glib_full(error))
156            }
157        }
158    }
159
160    /// Creates a new animation by asynchronously loading an image from an input stream.
161    ///
162    /// For more details see gdk_pixbuf_new_from_stream(), which is the synchronous
163    /// version of this function.
164    ///
165    /// When the operation is finished, `callback` will be called in the main thread.
166    /// You can then call gdk_pixbuf_animation_new_from_stream_finish() to get the
167    /// result of the operation.
168    ///
169    /// # Deprecated since 2.44
170    ///
171    /// Use a different image loading library for animatable assets
172    /// ## `stream`
173    /// a #GInputStream from which to load the animation
174    /// ## `cancellable`
175    /// optional #GCancellable object
176    /// ## `callback`
177    /// a `GAsyncReadyCallback` to call when the pixbuf is loaded
178    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
179    #[allow(deprecated)]
180    #[doc(alias = "gdk_pixbuf_animation_new_from_stream_async")]
181    #[doc(alias = "new_from_stream_async")]
182    pub fn from_stream_async<P: FnOnce(Result<PixbufAnimation, glib::Error>) + 'static>(
183        stream: &impl IsA<gio::InputStream>,
184        cancellable: Option<&impl IsA<gio::Cancellable>>,
185        callback: P,
186    ) {
187        let main_context = glib::MainContext::ref_thread_default();
188        let is_main_context_owner = main_context.is_owner();
189        let has_acquired_main_context = (!is_main_context_owner)
190            .then(|| main_context.acquire().ok())
191            .flatten();
192        assert!(
193            is_main_context_owner || has_acquired_main_context.is_some(),
194            "Async operations only allowed if the thread is owning the MainContext"
195        );
196
197        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
198            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
199        unsafe extern "C" fn from_stream_async_trampoline<
200            P: FnOnce(Result<PixbufAnimation, glib::Error>) + 'static,
201        >(
202            _source_object: *mut glib::gobject_ffi::GObject,
203            res: *mut gio::ffi::GAsyncResult,
204            user_data: glib::ffi::gpointer,
205        ) {
206            unsafe {
207                let mut error = std::ptr::null_mut();
208                let ret = ffi::gdk_pixbuf_animation_new_from_stream_finish(res, &mut error);
209                let result = if error.is_null() {
210                    Ok(from_glib_full(ret))
211                } else {
212                    Err(from_glib_full(error))
213                };
214                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
215                    Box_::from_raw(user_data as *mut _);
216                let callback: P = callback.into_inner();
217                callback(result);
218            }
219        }
220        let callback = from_stream_async_trampoline::<P>;
221        unsafe {
222            ffi::gdk_pixbuf_animation_new_from_stream_async(
223                stream.as_ref().to_glib_none().0,
224                cancellable.map(|p| p.as_ref()).to_glib_none().0,
225                Some(callback),
226                Box_::into_raw(user_data) as *mut _,
227            );
228        }
229    }
230
231    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
232
233    pub fn from_stream_future(
234        stream: &(impl IsA<gio::InputStream> + Clone + 'static),
235    ) -> Pin<Box_<dyn std::future::Future<Output = Result<PixbufAnimation, glib::Error>> + 'static>>
236    {
237        let stream = stream.clone();
238        Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
239            Self::from_stream_async(&stream, Some(cancellable), move |res| {
240                send.resolve(res);
241            });
242        }))
243    }
244}
245
246/// Trait containing all [`struct@PixbufAnimation`] methods.
247///
248/// # Implementors
249///
250/// [`PixbufAnimation`][struct@crate::PixbufAnimation], [`PixbufNonAnim`][struct@crate::PixbufNonAnim], [`PixbufSimpleAnim`][struct@crate::PixbufSimpleAnim]
251pub trait PixbufAnimationExt: IsA<PixbufAnimation> + 'static {
252    /// Queries the height of the bounding box of a pixbuf animation.
253    ///
254    /// # Deprecated since 2.44
255    ///
256    /// Use a different image loading library for animatable assets
257    ///
258    /// # Returns
259    ///
260    /// Height of the bounding box of the animation.
261    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
262    #[allow(deprecated)]
263    #[doc(alias = "gdk_pixbuf_animation_get_height")]
264    #[doc(alias = "get_height")]
265    fn height(&self) -> i32 {
266        unsafe { ffi::gdk_pixbuf_animation_get_height(self.as_ref().to_glib_none().0) }
267    }
268
269    /// Retrieves a static image for the animation.
270    ///
271    /// If an animation is really just a plain image (has only one frame),
272    /// this function returns that image.
273    ///
274    /// If the animation is an animation, this function returns a reasonable
275    /// image to use as a static unanimated image, which might be the first
276    /// frame, or something more sophisticated depending on the file format.
277    ///
278    /// If an animation hasn't loaded any frames yet, this function will
279    /// return `NULL`.
280    ///
281    /// # Deprecated since 2.44
282    ///
283    /// Use a different image loading library for animatable assets
284    ///
285    /// # Returns
286    ///
287    /// unanimated image representing the animation
288    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
289    #[allow(deprecated)]
290    #[doc(alias = "gdk_pixbuf_animation_get_static_image")]
291    #[doc(alias = "get_static_image")]
292    fn static_image(&self) -> Option<Pixbuf> {
293        unsafe {
294            from_glib_none(ffi::gdk_pixbuf_animation_get_static_image(
295                self.as_ref().to_glib_none().0,
296            ))
297        }
298    }
299
300    /// Queries the width of the bounding box of a pixbuf animation.
301    ///
302    /// # Deprecated since 2.44
303    ///
304    /// Use a different image loading library for animatable assets
305    ///
306    /// # Returns
307    ///
308    /// Width of the bounding box of the animation.
309    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
310    #[allow(deprecated)]
311    #[doc(alias = "gdk_pixbuf_animation_get_width")]
312    #[doc(alias = "get_width")]
313    fn width(&self) -> i32 {
314        unsafe { ffi::gdk_pixbuf_animation_get_width(self.as_ref().to_glib_none().0) }
315    }
316
317    /// Checks whether the animation is a static image.
318    ///
319    /// If you load a file with gdk_pixbuf_animation_new_from_file() and it
320    /// turns out to be a plain, unanimated image, then this function will
321    /// return `TRUE`. Use gdk_pixbuf_animation_get_static_image() to retrieve
322    /// the image.
323    ///
324    /// # Deprecated since 2.44
325    ///
326    /// Use a different image loading library for animatable assets
327    ///
328    /// # Returns
329    ///
330    /// `TRUE` if the "animation" was really just an image
331    #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
332    #[allow(deprecated)]
333    #[doc(alias = "gdk_pixbuf_animation_is_static_image")]
334    fn is_static_image(&self) -> bool {
335        unsafe {
336            from_glib(ffi::gdk_pixbuf_animation_is_static_image(
337                self.as_ref().to_glib_none().0,
338            ))
339        }
340    }
341}
342
343impl<O: IsA<PixbufAnimation>> PixbufAnimationExt for O {}