1use std::ptr;
4
5use glib::translate::*;
6
7use crate::{ffi, CustomFilter};
8
9impl CustomFilter {
10 #[doc(alias = "gtk_custom_filter_new")]
11 pub fn new<F>(filter_func: F) -> Self
12 where
13 F: Fn(&glib::Object) -> bool + 'static,
14 {
15 assert_initialized_main_thread!();
16 unsafe {
17 from_glib_full(ffi::gtk_custom_filter_new(
18 Some(trampoline::<F>),
19 Box::into_raw(Box::new(filter_func)) as *mut _,
20 Some(destroy_closure::<F>),
21 ))
22 }
23 }
24
25 #[doc(alias = "gtk_custom_filter_set_filter_func")]
26 pub fn set_filter_func<F>(&self, filter_func: F)
27 where
28 F: Fn(&glib::Object) -> bool + 'static,
29 {
30 unsafe {
31 ffi::gtk_custom_filter_set_filter_func(
32 self.to_glib_none().0,
33 Some(trampoline::<F>),
34 Box::into_raw(Box::new(filter_func)) as *mut _,
35 Some(destroy_closure::<F>),
36 )
37 }
38 }
39
40 #[doc(alias = "gtk_custom_filter_set_filter_func")]
41 #[doc(alias = "set_filter_func")]
42 pub fn unset_filter_func(&self) {
43 unsafe {
44 ffi::gtk_custom_filter_set_filter_func(
45 self.to_glib_none().0,
46 None,
47 ptr::null_mut(),
48 None,
49 )
50 }
51 }
52}
53
54impl Default for CustomFilter {
55 fn default() -> Self {
56 assert_initialized_main_thread!();
57 unsafe { from_glib_full(ffi::gtk_custom_filter_new(None, ptr::null_mut(), None)) }
58 }
59}
60
61unsafe extern "C" fn destroy_closure<F: Fn(&glib::Object) -> bool + 'static>(
62 ptr: glib::ffi::gpointer,
63) {
64 let _ = Box::<F>::from_raw(ptr as *mut _);
65}
66
67unsafe extern "C" fn trampoline<F: Fn(&glib::Object) -> bool + 'static>(
68 item: *mut glib::gobject_ffi::GObject,
69 f: glib::ffi::gpointer,
70) -> glib::ffi::gboolean {
71 let f: &F = &*(f as *const F);
72 f(&from_glib_borrow(item)).into_glib()
73}