1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{Toplevel, ToplevelSize};
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::IsA;
use std::boxed::Box as Box_;
use std::mem::transmute;

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of [`Toplevel`](crate::Toplevel).
pub trait ToplevelExtManual {
    /// Emitted when the size for the surface needs to be computed, when
    /// it is present.
    ///
    /// It will normally be emitted during or after [`ToplevelExt::present()`][crate::prelude::ToplevelExt::present()],
    /// depending on the configuration received by the windowing system.
    /// It may also be emitted at any other point in time, in response
    /// to the windowing system spontaneously changing the configuration.
    ///
    /// It is the responsibility of the toplevel user to handle this signal
    /// and compute the desired size of the toplevel, given the information
    /// passed via the [`ToplevelSize`][crate::ToplevelSize] object. Failing to do so
    /// will result in an arbitrary size being used as a result.
    ///
    /// # Returns
    ///
    ///
    /// ## `size`
    /// a [`ToplevelSize`][crate::ToplevelSize]
    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<Toplevel>> ToplevelExtManual for O {
    fn connect_compute_size<F: Fn(&Toplevel, &mut ToplevelSize) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn compute_size_trampoline<
            F: Fn(&Toplevel, &mut ToplevelSize) + 'static,
        >(
            this: *mut ffi::GdkToplevel,
            size: *mut ffi::GdkToplevelSize,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &mut *(size as *mut ToplevelSize))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"compute-size\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    compute_size_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}