gdk_pixbuf/auto/
pixbuf_animation.rs1#![allow(deprecated)]
5
6use crate::{Pixbuf, ffi};
7use glib::{prelude::*, translate::*};
8use std::{boxed::Box as Box_, pin::Pin};
9
10glib::wrapper! {
11 #[doc(alias = "GdkPixbufAnimation")]
12 pub struct PixbufAnimation(Object<ffi::GdkPixbufAnimation, ffi::GdkPixbufAnimationClass>);
13
14 match fn {
15 type_ => || ffi::gdk_pixbuf_animation_get_type(),
16 }
17}
18
19impl PixbufAnimation {
20 pub const NONE: Option<&'static PixbufAnimation> = None;
21
22 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
23 #[allow(deprecated)]
24 #[doc(alias = "gdk_pixbuf_animation_new_from_file")]
25 #[doc(alias = "new_from_file")]
26 pub fn from_file(
27 filename: impl AsRef<std::path::Path>,
28 ) -> Result<PixbufAnimation, glib::Error> {
29 unsafe {
30 let mut error = std::ptr::null_mut();
31 let ret = ffi::gdk_pixbuf_animation_new_from_file(
32 filename.as_ref().to_glib_none().0,
33 &mut error,
34 );
35 if error.is_null() {
36 Ok(from_glib_full(ret))
37 } else {
38 Err(from_glib_full(error))
39 }
40 }
41 }
42
43 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
44 #[allow(deprecated)]
45 #[doc(alias = "gdk_pixbuf_animation_new_from_resource")]
46 #[doc(alias = "new_from_resource")]
47 pub fn from_resource(resource_path: &str) -> Result<PixbufAnimation, glib::Error> {
48 unsafe {
49 let mut error = std::ptr::null_mut();
50 let ret = ffi::gdk_pixbuf_animation_new_from_resource(
51 resource_path.to_glib_none().0,
52 &mut error,
53 );
54 if error.is_null() {
55 Ok(from_glib_full(ret))
56 } else {
57 Err(from_glib_full(error))
58 }
59 }
60 }
61
62 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
63 #[allow(deprecated)]
64 #[doc(alias = "gdk_pixbuf_animation_new_from_stream")]
65 #[doc(alias = "new_from_stream")]
66 pub fn from_stream(
67 stream: &impl IsA<gio::InputStream>,
68 cancellable: Option<&impl IsA<gio::Cancellable>>,
69 ) -> Result<PixbufAnimation, glib::Error> {
70 unsafe {
71 let mut error = std::ptr::null_mut();
72 let ret = ffi::gdk_pixbuf_animation_new_from_stream(
73 stream.as_ref().to_glib_none().0,
74 cancellable.map(|p| p.as_ref()).to_glib_none().0,
75 &mut error,
76 );
77 if error.is_null() {
78 Ok(from_glib_full(ret))
79 } else {
80 Err(from_glib_full(error))
81 }
82 }
83 }
84
85 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
86 #[allow(deprecated)]
87 #[doc(alias = "gdk_pixbuf_animation_new_from_stream_async")]
88 #[doc(alias = "new_from_stream_async")]
89 pub fn from_stream_async<P: FnOnce(Result<PixbufAnimation, glib::Error>) + 'static>(
90 stream: &impl IsA<gio::InputStream>,
91 cancellable: Option<&impl IsA<gio::Cancellable>>,
92 callback: P,
93 ) {
94 let main_context = glib::MainContext::ref_thread_default();
95 let is_main_context_owner = main_context.is_owner();
96 let has_acquired_main_context = (!is_main_context_owner)
97 .then(|| main_context.acquire().ok())
98 .flatten();
99 assert!(
100 is_main_context_owner || has_acquired_main_context.is_some(),
101 "Async operations only allowed if the thread is owning the MainContext"
102 );
103
104 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
105 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
106 unsafe extern "C" fn from_stream_async_trampoline<
107 P: FnOnce(Result<PixbufAnimation, glib::Error>) + 'static,
108 >(
109 _source_object: *mut glib::gobject_ffi::GObject,
110 res: *mut gio::ffi::GAsyncResult,
111 user_data: glib::ffi::gpointer,
112 ) {
113 unsafe {
114 let mut error = std::ptr::null_mut();
115 let ret = ffi::gdk_pixbuf_animation_new_from_stream_finish(res, &mut error);
116 let result = if error.is_null() {
117 Ok(from_glib_full(ret))
118 } else {
119 Err(from_glib_full(error))
120 };
121 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
122 Box_::from_raw(user_data as *mut _);
123 let callback: P = callback.into_inner();
124 callback(result);
125 }
126 }
127 let callback = from_stream_async_trampoline::<P>;
128 unsafe {
129 ffi::gdk_pixbuf_animation_new_from_stream_async(
130 stream.as_ref().to_glib_none().0,
131 cancellable.map(|p| p.as_ref()).to_glib_none().0,
132 Some(callback),
133 Box_::into_raw(user_data) as *mut _,
134 );
135 }
136 }
137
138 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
139
140 pub fn from_stream_future(
141 stream: &(impl IsA<gio::InputStream> + Clone + 'static),
142 ) -> Pin<Box_<dyn std::future::Future<Output = Result<PixbufAnimation, glib::Error>> + 'static>>
143 {
144 let stream = stream.clone();
145 Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
146 Self::from_stream_async(&stream, Some(cancellable), move |res| {
147 send.resolve(res);
148 });
149 }))
150 }
151}
152
153pub trait PixbufAnimationExt: IsA<PixbufAnimation> + 'static {
154 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
155 #[allow(deprecated)]
156 #[doc(alias = "gdk_pixbuf_animation_get_height")]
157 #[doc(alias = "get_height")]
158 fn height(&self) -> i32 {
159 unsafe { ffi::gdk_pixbuf_animation_get_height(self.as_ref().to_glib_none().0) }
160 }
161
162 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
163 #[allow(deprecated)]
164 #[doc(alias = "gdk_pixbuf_animation_get_static_image")]
165 #[doc(alias = "get_static_image")]
166 fn static_image(&self) -> Option<Pixbuf> {
167 unsafe {
168 from_glib_none(ffi::gdk_pixbuf_animation_get_static_image(
169 self.as_ref().to_glib_none().0,
170 ))
171 }
172 }
173
174 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
175 #[allow(deprecated)]
176 #[doc(alias = "gdk_pixbuf_animation_get_width")]
177 #[doc(alias = "get_width")]
178 fn width(&self) -> i32 {
179 unsafe { ffi::gdk_pixbuf_animation_get_width(self.as_ref().to_glib_none().0) }
180 }
181
182 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
183 #[allow(deprecated)]
184 #[doc(alias = "gdk_pixbuf_animation_is_static_image")]
185 fn is_static_image(&self) -> bool {
186 unsafe {
187 from_glib(ffi::gdk_pixbuf_animation_is_static_image(
188 self.as_ref().to_glib_none().0,
189 ))
190 }
191 }
192}
193
194impl<O: IsA<PixbufAnimation>> PixbufAnimationExt for O {}