gdk_pixbuf/auto/
pixbuf_animation.rs1#![allow(deprecated)]
5
6use crate::{ffi, Pixbuf};
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 let mut error = std::ptr::null_mut();
114 let ret = ffi::gdk_pixbuf_animation_new_from_stream_finish(res, &mut error);
115 let result = if error.is_null() {
116 Ok(from_glib_full(ret))
117 } else {
118 Err(from_glib_full(error))
119 };
120 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
121 Box_::from_raw(user_data as *mut _);
122 let callback: P = callback.into_inner();
123 callback(result);
124 }
125 let callback = from_stream_async_trampoline::<P>;
126 unsafe {
127 ffi::gdk_pixbuf_animation_new_from_stream_async(
128 stream.as_ref().to_glib_none().0,
129 cancellable.map(|p| p.as_ref()).to_glib_none().0,
130 Some(callback),
131 Box_::into_raw(user_data) as *mut _,
132 );
133 }
134 }
135
136 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
137
138 pub fn from_stream_future(
139 stream: &(impl IsA<gio::InputStream> + Clone + 'static),
140 ) -> Pin<Box_<dyn std::future::Future<Output = Result<PixbufAnimation, glib::Error>> + 'static>>
141 {
142 let stream = stream.clone();
143 Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
144 Self::from_stream_async(&stream, Some(cancellable), move |res| {
145 send.resolve(res);
146 });
147 }))
148 }
149}
150
151pub trait PixbufAnimationExt: IsA<PixbufAnimation> + 'static {
152 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
153 #[allow(deprecated)]
154 #[doc(alias = "gdk_pixbuf_animation_get_height")]
155 #[doc(alias = "get_height")]
156 fn height(&self) -> i32 {
157 unsafe { ffi::gdk_pixbuf_animation_get_height(self.as_ref().to_glib_none().0) }
158 }
159
160 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
161 #[allow(deprecated)]
162 #[doc(alias = "gdk_pixbuf_animation_get_static_image")]
163 #[doc(alias = "get_static_image")]
164 fn static_image(&self) -> Option<Pixbuf> {
165 unsafe {
166 from_glib_none(ffi::gdk_pixbuf_animation_get_static_image(
167 self.as_ref().to_glib_none().0,
168 ))
169 }
170 }
171
172 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
173 #[allow(deprecated)]
174 #[doc(alias = "gdk_pixbuf_animation_get_width")]
175 #[doc(alias = "get_width")]
176 fn width(&self) -> i32 {
177 unsafe { ffi::gdk_pixbuf_animation_get_width(self.as_ref().to_glib_none().0) }
178 }
179
180 #[cfg_attr(feature = "v2_44", deprecated = "Since 2.44")]
181 #[allow(deprecated)]
182 #[doc(alias = "gdk_pixbuf_animation_is_static_image")]
183 fn is_static_image(&self) -> bool {
184 unsafe {
185 from_glib(ffi::gdk_pixbuf_animation_is_static_image(
186 self.as_ref().to_glib_none().0,
187 ))
188 }
189 }
190}
191
192impl<O: IsA<PixbufAnimation>> PixbufAnimationExt for O {}