Skip to content

Commit

Permalink
Manually implement CustomFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jan 25, 2021
1 parent dd62c77 commit f05e90e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 90 deletions.
11 changes: 10 additions & 1 deletion gtk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ generate = [
"Gtk.CssParserError",
"Gtk.CssParserWarning",
"Gtk.CssProvider",
"Gtk.CustomFilter",
"Gtk.CustomLayout",
"Gtk.DebugFlags",
"Gtk.DeleteType",
Expand Down Expand Up @@ -775,6 +774,16 @@ name = "Gtk.CssSection"
status = "generate"
trust_return_value_nullability = false

[[object]]
name = "Gtk.CustomFilter"
status = "generate"
[[object.function]]
name = "new"
manual = true
[[object.function]]
name = "set_filter_func"
manual = true

[[object]]
name = "Gtk.CustomSorter"
status = "generate"
Expand Down
86 changes: 1 addition & 85 deletions gtk4/src/auto/custom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use crate::Filter;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;

glib::wrapper! {
Expand All @@ -15,90 +14,7 @@ glib::wrapper! {
}
}

impl CustomFilter {
#[doc(alias = "gtk_custom_filter_new")]
pub fn new(match_func: Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>) -> CustomFilter {
assert_initialized_main_thread!();
let match_func_data: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
Box_::new(match_func);
unsafe extern "C" fn match_func_func(
item: *mut glib::gobject_ffi::GObject,
user_data: glib::ffi::gpointer,
) -> glib::ffi::gboolean {
let item = from_glib_borrow(item);
let callback: &Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>> =
&*(user_data as *mut _);
let res = if let Some(ref callback) = *callback {
callback(&item)
} else {
panic!("cannot get closure...")
};
res.to_glib()
}
let match_func = if match_func_data.is_some() {
Some(match_func_func as _)
} else {
None
};
unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
let _callback: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
Box_::from_raw(data as *mut _);
}
let destroy_call2 = Some(user_destroy_func as _);
let super_callback0: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
match_func_data;
unsafe {
from_glib_full(ffi::gtk_custom_filter_new(
match_func,
Box_::into_raw(super_callback0) as *mut _,
destroy_call2,
))
}
}

#[doc(alias = "gtk_custom_filter_set_filter_func")]
pub fn set_filter_func(
&self,
match_func: Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>,
) {
let match_func_data: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
Box_::new(match_func);
unsafe extern "C" fn match_func_func(
item: *mut glib::gobject_ffi::GObject,
user_data: glib::ffi::gpointer,
) -> glib::ffi::gboolean {
let item = from_glib_borrow(item);
let callback: &Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>> =
&*(user_data as *mut _);
let res = if let Some(ref callback) = *callback {
callback(&item)
} else {
panic!("cannot get closure...")
};
res.to_glib()
}
let match_func = if match_func_data.is_some() {
Some(match_func_func as _)
} else {
None
};
unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
let _callback: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
Box_::from_raw(data as *mut _);
}
let destroy_call3 = Some(user_destroy_func as _);
let super_callback0: Box_<Option<Box_<dyn Fn(&glib::Object) -> bool + 'static>>> =
match_func_data;
unsafe {
ffi::gtk_custom_filter_set_filter_func(
self.to_glib_none().0,
match_func,
Box_::into_raw(super_callback0) as *mut _,
destroy_call3,
);
}
}
}
impl CustomFilter {}

impl fmt::Display for CustomFilter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
8 changes: 4 additions & 4 deletions gtk4/src/custom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ impl CustomFilter {
}

#[doc(alias = "gtk_custom_filter_set_filter_func")]
fn set_filter_func<F>(&self, filter_func: F)
pub fn set_filter_func<F>(&self, filter_func: F)
where
F: Fn(&glib::Object) -> bool + 'static,
{
unsafe {
ffi::gtk_custom_filter_set_filter_func(
self.as_ref().to_glib_none().0,
self.to_glib_none().0,
Some(trampoline::<F>),
Box::into_raw(Box::new(filter_func)) as *mut _,
Some(destroy_closure::<F>),
)
}
}

fn unset_filter_func(&self) {
pub fn unset_filter_func(&self) {
unsafe {
ffi::gtk_custom_filter_set_filter_func(
self.as_ref().to_glib_none().0,
self.to_glib_none().0,
None,
ptr::null_mut(),
None,
Expand Down
1 change: 1 addition & 0 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod color_chooser;
mod combo_box;
mod constraint_guide;
mod css_location;
mod custom_filter;
mod custom_sorter;
mod dialog;
mod drawing_area;
Expand Down

0 comments on commit f05e90e

Please sign in to comment.