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::{ffi, Pixbuf};
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 let mut error = std::ptr::null_mut();
207 let ret = ffi::gdk_pixbuf_animation_new_from_stream_finish(res, &mut error);
208 let result = if error.is_null() {
209 Ok(from_glib_full(ret))
210 } else {
211 Err(from_glib_full(error))
212 };
213 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
214 Box_::from_raw(user_data as *mut _);
215 let callback: P = callback.into_inner();
216 callback(result);
217 }
218 let callback = from_stream_async_trampoline::<P>;
219 unsafe {
220 ffi::gdk_pixbuf_animation_new_from_stream_async(
221 stream.as_ref().to_glib_none().0,
222 cancellable.map(|p| p.as_ref()).to_glib_none().0,
223 Some(callback),
224 Box_::into_raw(user_data) as *mut _,
225 );
226 }
227 }
228
229 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
230
231 pub fn from_stream_future(
232 stream: &(impl IsA<gio::InputStream> + Clone + 'static),
233 ) -> Pin<Box_<dyn std::future::Future<Output = Result<PixbufAnimation, glib::Error>> + 'static>>
234 {
235 let stream = stream.clone();
236 Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
237 Self::from_stream_async(&stream, Some(cancellable), move |res| {
238 send.resolve(res);
239 });
240 }))
241 }
242}
243
244/// Trait containing all [`struct@PixbufAnimation`] methods.
245///
246/// # Implementors
247///
248/// [`PixbufAnimation`][struct@crate::PixbufAnimation], [`PixbufNonAnim`][struct@crate::PixbufNonAnim], [`PixbufSimpleAnim`][struct@crate::PixbufSimpleAnim]
249pub trait PixbufAnimationExt: IsA<PixbufAnimation> + 'static {
250 /// Queries the height of the bounding box of a pixbuf animation.
251 ///
252 /// # Deprecated since 2.44
253 ///
254 /// Use a different image loading library for animatable assets
255 ///
256 /// # Returns
257 ///
258 /// Height of the bounding box of the animation.
259 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
260 #[allow(deprecated)]
261 #[doc(alias = "gdk_pixbuf_animation_get_height")]
262 #[doc(alias = "get_height")]
263 fn height(&self) -> i32 {
264 unsafe { ffi::gdk_pixbuf_animation_get_height(self.as_ref().to_glib_none().0) }
265 }
266
267 /// Retrieves a static image for the animation.
268 ///
269 /// If an animation is really just a plain image (has only one frame),
270 /// this function returns that image.
271 ///
272 /// If the animation is an animation, this function returns a reasonable
273 /// image to use as a static unanimated image, which might be the first
274 /// frame, or something more sophisticated depending on the file format.
275 ///
276 /// If an animation hasn't loaded any frames yet, this function will
277 /// return `NULL`.
278 ///
279 /// # Deprecated since 2.44
280 ///
281 /// Use a different image loading library for animatable assets
282 ///
283 /// # Returns
284 ///
285 /// unanimated image representing the animation
286 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
287 #[allow(deprecated)]
288 #[doc(alias = "gdk_pixbuf_animation_get_static_image")]
289 #[doc(alias = "get_static_image")]
290 fn static_image(&self) -> Option<Pixbuf> {
291 unsafe {
292 from_glib_none(ffi::gdk_pixbuf_animation_get_static_image(
293 self.as_ref().to_glib_none().0,
294 ))
295 }
296 }
297
298 /// Queries the width of the bounding box of a pixbuf animation.
299 ///
300 /// # Deprecated since 2.44
301 ///
302 /// Use a different image loading library for animatable assets
303 ///
304 /// # Returns
305 ///
306 /// Width of the bounding box of the animation.
307 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
308 #[allow(deprecated)]
309 #[doc(alias = "gdk_pixbuf_animation_get_width")]
310 #[doc(alias = "get_width")]
311 fn width(&self) -> i32 {
312 unsafe { ffi::gdk_pixbuf_animation_get_width(self.as_ref().to_glib_none().0) }
313 }
314
315 /// Checks whether the animation is a static image.
316 ///
317 /// If you load a file with gdk_pixbuf_animation_new_from_file() and it
318 /// turns out to be a plain, unanimated image, then this function will
319 /// return `TRUE`. Use gdk_pixbuf_animation_get_static_image() to retrieve
320 /// the image.
321 ///
322 /// # Deprecated since 2.44
323 ///
324 /// Use a different image loading library for animatable assets
325 ///
326 /// # Returns
327 ///
328 /// `TRUE` if the "animation" was really just an image
329 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
330 #[allow(deprecated)]
331 #[doc(alias = "gdk_pixbuf_animation_is_static_image")]
332 fn is_static_image(&self) -> bool {
333 unsafe {
334 from_glib(ffi::gdk_pixbuf_animation_is_static_image(
335 self.as_ref().to_glib_none().0,
336 ))
337 }
338 }
339}
340
341impl<O: IsA<PixbufAnimation>> PixbufAnimationExt for O {}