Skip to main content

gio/auto/
loadable_icon.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
5use crate::{AsyncResult, Cancellable, Icon, InputStream, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// `GLoadableIcon` extends the [`Icon`][crate::Icon] interface and adds the ability
11    /// to load icons from streams.
12    ///
13    /// # Implements
14    ///
15    /// [`LoadableIconExt`][trait@crate::prelude::LoadableIconExt], [`IconExt`][trait@crate::prelude::IconExt]
16    #[doc(alias = "GLoadableIcon")]
17    pub struct LoadableIcon(Interface<ffi::GLoadableIcon, ffi::GLoadableIconIface>) @requires Icon;
18
19    match fn {
20        type_ => || ffi::g_loadable_icon_get_type(),
21    }
22}
23
24impl LoadableIcon {
25    pub const NONE: Option<&'static LoadableIcon> = None;
26}
27
28/// Trait containing all [`struct@LoadableIcon`] methods.
29///
30/// # Implementors
31///
32/// [`BytesIcon`][struct@crate::BytesIcon], [`FileIcon`][struct@crate::FileIcon], [`LoadableIcon`][struct@crate::LoadableIcon]
33pub trait LoadableIconExt: IsA<LoadableIcon> + 'static {
34    /// Loads a loadable icon. For the asynchronous version of this function,
35    /// see g_loadable_icon_load_async().
36    /// ## `size`
37    /// an integer.
38    /// ## `cancellable`
39    /// optional #GCancellable object, [`None`] to
40    /// ignore.
41    ///
42    /// # Returns
43    ///
44    /// a #GInputStream to read the icon from.
45    ///
46    /// ## `type_`
47    /// return location for the content type of
48    ///   the loaded icon, or [`None`] to ignore the result. The returned content type
49    ///   may be [`None`] if it is unknown.
50    #[doc(alias = "g_loadable_icon_load")]
51    fn load(
52        &self,
53        size: i32,
54        cancellable: Option<&impl IsA<Cancellable>>,
55    ) -> Result<(InputStream, Option<glib::GString>), glib::Error> {
56        unsafe {
57            let mut type_ = std::ptr::null_mut();
58            let mut error = std::ptr::null_mut();
59            let ret = ffi::g_loadable_icon_load(
60                self.as_ref().to_glib_none().0,
61                size,
62                &mut type_,
63                cancellable.map(|p| p.as_ref()).to_glib_none().0,
64                &mut error,
65            );
66            if error.is_null() {
67                Ok((from_glib_full(ret), from_glib_full(type_)))
68            } else {
69                Err(from_glib_full(error))
70            }
71        }
72    }
73
74    /// Loads an icon asynchronously. To finish this function, see
75    /// g_loadable_icon_load_finish(). For the synchronous, blocking
76    /// version of this function, see g_loadable_icon_load().
77    /// ## `size`
78    /// an integer.
79    /// ## `cancellable`
80    /// optional #GCancellable object, [`None`] to ignore.
81    /// ## `callback`
82    /// a #GAsyncReadyCallback
83    ///   to call when the request is satisfied
84    #[doc(alias = "g_loadable_icon_load_async")]
85    fn load_async<
86        P: FnOnce(Result<(InputStream, Option<glib::GString>), glib::Error>) + 'static,
87    >(
88        &self,
89        size: i32,
90        cancellable: Option<&impl IsA<Cancellable>>,
91        callback: P,
92    ) {
93        let main_context = glib::MainContext::ref_thread_default();
94        let is_main_context_owner = main_context.is_owner();
95        let has_acquired_main_context = (!is_main_context_owner)
96            .then(|| main_context.acquire().ok())
97            .flatten();
98        assert!(
99            is_main_context_owner || has_acquired_main_context.is_some(),
100            "Async operations only allowed if the thread is owning the MainContext"
101        );
102
103        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
104            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
105        unsafe extern "C" fn load_async_trampoline<
106            P: FnOnce(Result<(InputStream, Option<glib::GString>), glib::Error>) + 'static,
107        >(
108            _source_object: *mut glib::gobject_ffi::GObject,
109            res: *mut crate::ffi::GAsyncResult,
110            user_data: glib::ffi::gpointer,
111        ) {
112            unsafe {
113                let mut error = std::ptr::null_mut();
114                let mut type_ = std::ptr::null_mut();
115                let ret = ffi::g_loadable_icon_load_finish(
116                    _source_object as *mut _,
117                    res,
118                    &mut type_,
119                    &mut error,
120                );
121                let result = if error.is_null() {
122                    Ok((from_glib_full(ret), from_glib_full(type_)))
123                } else {
124                    Err(from_glib_full(error))
125                };
126                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
127                    Box_::from_raw(user_data as *mut _);
128                let callback: P = callback.into_inner();
129                callback(result);
130            }
131        }
132        let callback = load_async_trampoline::<P>;
133        unsafe {
134            ffi::g_loadable_icon_load_async(
135                self.as_ref().to_glib_none().0,
136                size,
137                cancellable.map(|p| p.as_ref()).to_glib_none().0,
138                Some(callback),
139                Box_::into_raw(user_data) as *mut _,
140            );
141        }
142    }
143
144    fn load_future(
145        &self,
146        size: i32,
147    ) -> Pin<
148        Box_<
149            dyn std::future::Future<
150                    Output = Result<(InputStream, Option<glib::GString>), glib::Error>,
151                > + 'static,
152        >,
153    > {
154        Box_::pin(crate::GioFuture::new(
155            self,
156            move |obj, cancellable, send| {
157                obj.load_async(size, Some(cancellable), move |res| {
158                    send.resolve(res);
159                });
160            },
161        ))
162    }
163}
164
165impl<O: IsA<LoadableIcon>> LoadableIconExt for O {}