Skip to content

Commit

Permalink
gtk: add Filter subclassing support
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Nov 25, 2020
1 parent 5f65745 commit 09e9701
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
78 changes: 78 additions & 0 deletions gtk4/src/subclass/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::{Filter, FilterMatch};
use glib::subclass::object::ObjectImpl;
use glib::subclass::prelude::*;
use glib::translate::*;
use glib::{Cast, Object};

pub trait FilterImpl: FilterImplExt + ObjectImpl {
fn get_strictness(&self, filter: &Self::Type) -> FilterMatch {
self.parent_get_strictness(filter)
}
fn match_(&self, filter: &Self::Type, item: &Object) -> bool {
self.parent_match_(filter, item)
}
}

pub trait FilterImplExt: ObjectSubclass {
fn parent_get_strictness(&self, filter: &Self::Type) -> FilterMatch;
fn parent_match_(&self, filter: &Self::Type, item: &Object) -> bool;
}

impl<T: FilterImpl> FilterImplExt for T {
fn parent_get_strictness(&self, filter: &Self::Type) -> FilterMatch {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GtkFilterClass;
let f = (*parent_class)
.get_strictness
.expect("No parent class impl for \"get_strictness\"");
from_glib(f(filter.unsafe_cast_ref::<Filter>().to_glib_none().0))
}
}

fn parent_match_(&self, filter: &Self::Type, item: &Object) -> bool {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GtkFilterClass;
let f = (*parent_class)
.match_
.expect("No parent class impl for \"match\"");
from_glib(f(
filter.unsafe_cast_ref::<Filter>().to_glib_none().0,
item.to_glib_none().0,
))
}
}
}

unsafe impl<T: FilterImpl> IsSubclassable<T> for Filter {
fn override_vfuncs(class: &mut glib::Class<Self>) {
<Object as IsSubclassable<T>>::override_vfuncs(class);

let klass = class.as_mut();
klass.match_ = Some(filter_match::<T>);
klass.get_strictness = Some(filter_get_strictness::<T>);
}
}

unsafe extern "C" fn filter_get_strictness<T: FilterImpl>(
ptr: *mut ffi::GtkFilter,
) -> ffi::GtkFilterMatch {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Filter> = from_glib_borrow(ptr);

imp.get_strictness(wrap.unsafe_cast_ref()).to_glib()
}

unsafe extern "C" fn filter_match<T: FilterImpl>(
ptr: *mut ffi::GtkFilter,
itemptr: *mut glib::gobject_ffi::GObject,
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Filter> = from_glib_borrow(ptr);

imp.match_(wrap.unsafe_cast_ref(), &from_glib_borrow(itemptr))
.to_glib()
}
2 changes: 2 additions & 0 deletions gtk4/src/subclass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod check_button;
pub mod dialog;
pub mod drawing_area;
pub mod entry;
pub mod filter;
pub mod flow_box_child;
pub mod layout_manager;
pub mod list_box_row;
Expand All @@ -33,6 +34,7 @@ pub mod prelude {
pub use super::dialog::DialogImpl;
pub use super::drawing_area::DrawingAreaImpl;
pub use super::entry::EntryImpl;
pub use super::filter::FilterImpl;
pub use super::flow_box_child::FlowBoxChildImpl;
pub use super::layout_manager::LayoutManagerImpl;
pub use super::list_box_row::ListBoxRowImpl;
Expand Down

0 comments on commit 09e9701

Please sign in to comment.