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