Skip to main content

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::{SignalHandlerId, connect_raw},
7    translate::*,
8};
9
10use crate::{Toplevel, ToplevelSize, ffi, prelude::*};
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            unsafe {
28                let f: &F = &*(f as *const F);
29                let mut size = ToplevelSize(std::ptr::NonNull::new_unchecked(size));
30                f(&from_glib_borrow(this), &mut size)
31            }
32        }
33        unsafe {
34            let f: Box_<F> = Box_::new(f);
35            connect_raw(
36                self.as_ptr() as *mut _,
37                c"compute-size".as_ptr() as *const _,
38                Some(transmute::<*const (), unsafe extern "C" fn()>(
39                    compute_size_trampoline::<F> as *const (),
40                )),
41                Box_::into_raw(f),
42            )
43        }
44    }
45}
46
47impl<O: IsA<Toplevel>> ToplevelExtManual for O {}