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