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