gdk4/
toplevel.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{boxed::Box as Box_, mem::transmute};
4
5use glib::{
6    signal::{connect_raw, SignalHandlerId},
7    translate::*,
8};
9
10use crate::{ffi, prelude::*, Toplevel, ToplevelSize};
11
12// rustdoc-stripper-ignore-next
13/// Trait containing manually implemented methods of
14/// [`Toplevel`](crate::Toplevel).
15pub trait ToplevelExtManual: IsA<Toplevel> {
16    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
17        &self,
18        f: F,
19    ) -> SignalHandlerId {
20        unsafe extern "C" fn compute_size_trampoline<
21            F: Fn(&Toplevel, &mut ToplevelSize) + 'static,
22        >(
23            this: *mut ffi::GdkToplevel,
24            size: *mut ffi::GdkToplevelSize,
25            f: glib::ffi::gpointer,
26        ) {
27            let f: &F = &*(f as *const F);
28            f(&from_glib_borrow(this), &mut *(size as *mut ToplevelSize))
29        }
30        unsafe {
31            let f: Box_<F> = Box_::new(f);
32            connect_raw(
33                self.as_ptr() as *mut _,
34                c"compute-size".as_ptr() as *const _,
35                Some(transmute::<*const (), unsafe extern "C" fn()>(
36                    compute_size_trampoline::<F> as *const (),
37                )),
38                Box_::into_raw(f),
39            )
40        }
41    }
42}
43
44impl<O: IsA<Toplevel>> ToplevelExtManual for O {}