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
12mod sealed {
13    pub trait Sealed {}
14    impl<T: super::IsA<super::Toplevel>> Sealed for T {}
15}
16
17// rustdoc-stripper-ignore-next
18/// Trait containing manually implemented methods of
19/// [`Toplevel`](crate::Toplevel).
20pub trait ToplevelExtManual: sealed::Sealed + IsA<Toplevel> {
21    /// Emitted when the size for the surface needs to be computed, when
22    /// it is present.
23    ///
24    /// This signal will normally be emitted during or after a call to
25    /// [`ToplevelExt::present()`][crate::prelude::ToplevelExt::present()], depending on the configuration
26    /// received by the windowing system. It may also be emitted at any
27    /// other point in time, in response to the windowing system
28    /// spontaneously changing the configuration of the toplevel surface.
29    ///
30    /// It is the responsibility of the toplevel user to handle this signal
31    /// and compute the desired size of the toplevel, given the information
32    /// passed via the [`ToplevelSize`][crate::ToplevelSize] object. Failing to do so
33    /// will result in an arbitrary size being used as a result.
34    /// ## `size`
35    /// a [`ToplevelSize`][crate::ToplevelSize]
36    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
37        &self,
38        f: F,
39    ) -> SignalHandlerId {
40        unsafe extern "C" fn compute_size_trampoline<
41            F: Fn(&Toplevel, &mut ToplevelSize) + 'static,
42        >(
43            this: *mut ffi::GdkToplevel,
44            size: *mut ffi::GdkToplevelSize,
45            f: glib::ffi::gpointer,
46        ) {
47            let f: &F = &*(f as *const F);
48            f(&from_glib_borrow(this), &mut *(size as *mut ToplevelSize))
49        }
50        unsafe {
51            let f: Box_<F> = Box_::new(f);
52            connect_raw(
53                self.as_ptr() as *mut _,
54                b"compute-size\0".as_ptr() as *const _,
55                Some(transmute::<*const (), unsafe extern "C" fn()>(
56                    compute_size_trampoline::<F> as *const (),
57                )),
58                Box_::into_raw(f),
59            )
60        }
61    }
62}
63
64impl<O: IsA<Toplevel>> ToplevelExtManual for O {}