[go: up one dir, main page]

gtk4/
overlay.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{mem::transmute, ptr};
4
5use glib::{
6    signal::{connect_raw, SignalHandlerId},
7    translate::*,
8};
9
10use crate::{ffi, prelude::*, Overlay, Widget};
11
12impl Overlay {
13    pub fn connect_get_child_position<F>(&self, f: F) -> SignalHandlerId
14    where
15        F: Fn(&Self, &Widget) -> Option<gdk::Rectangle> + 'static,
16    {
17        unsafe {
18            let f: Box<F> = Box::new(f);
19            connect_raw(
20                self.as_ptr() as *mut _,
21                c"get-child-position".as_ptr() as *mut _,
22                Some(transmute::<usize, unsafe extern "C" fn()>(
23                    get_child_position_trampoline::<F> as usize,
24                )),
25                Box::into_raw(f),
26            )
27        }
28    }
29}
30
31unsafe extern "C" fn get_child_position_trampoline<
32    F: Fn(&Overlay, &Widget) -> Option<gdk::Rectangle> + 'static,
33>(
34    this: *mut ffi::GtkOverlay,
35    widget: *mut ffi::GtkWidget,
36    allocation: *mut gdk::ffi::GdkRectangle,
37    f: glib::ffi::gpointer,
38) -> glib::ffi::gboolean {
39    let f: &F = &*(f as *const F);
40    match f(
41        Overlay::from_glib_borrow(this).unsafe_cast_ref(),
42        &from_glib_borrow(widget),
43    ) {
44        Some(rect) => {
45            ptr::write(allocation, ptr::read(rect.to_glib_none().0));
46            true
47        }
48        None => false,
49    }
50    .into_glib()
51}