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                let mut size = ToplevelSize(std::ptr::NonNull::new_unchecked(size));
45                f(&from_glib_borrow(this), &mut size)
46            }
47        }
48        unsafe {
49            let f: Box_<F> = Box_::new(f);
50            connect_raw(
51                self.as_ptr() as *mut _,
52                c"compute-size".as_ptr() as *const _,
53                Some(transmute::<*const (), unsafe extern "C" fn()>(
54                    compute_size_trampoline::<F> as *const (),
55                )),
56                Box_::into_raw(f),
57            )
58        }
59    }
60}
61
62impl<O: IsA<Toplevel>> ToplevelExtManual for O {}