Skip to main content

gtk4/subclass/
filter.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for subclassing [`Filter`].
5
6use glib::{Object, translate::*};
7
8use crate::{Filter, FilterMatch, ffi, prelude::*, subclass::prelude::*};
9
10pub trait FilterImpl: ObjectImpl + ObjectSubclass<Type: IsA<Filter>> {
11    /// Gets the known strictness of a filter.
12    ///
13    /// If the strictness is not known, [enum@Gtk.FilterMatch.some] is returned.
14    ///
15    /// This value may change after emission of the [`changed`][struct@crate::Filter#changed]
16    /// signal.
17    ///
18    /// This function is meant purely for optimization purposes. Filters can
19    /// choose to omit implementing it, but [`FilterListModel`][crate::FilterListModel] uses it.
20    ///
21    /// # Returns
22    ///
23    /// the strictness of @self
24    #[doc(alias = "get_strictness")]
25    fn strictness(&self) -> FilterMatch {
26        self.parent_strictness()
27    }
28    /// Checks if the given @item is matched by the filter or not.
29    /// ## `item`
30    /// The item to check
31    ///
32    /// # Returns
33    ///
34    /// true if the filter matches the item
35    fn match_(&self, item: &Object) -> bool {
36        self.parent_match_(item)
37    }
38}
39
40pub trait FilterImplExt: FilterImpl {
41    fn parent_strictness(&self) -> FilterMatch {
42        unsafe {
43            let data = Self::type_data();
44            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkFilterClass;
45            let f = (*parent_class)
46                .get_strictness
47                .expect("No parent class impl for \"get_strictness\"");
48            from_glib(f(self.obj().unsafe_cast_ref::<Filter>().to_glib_none().0))
49        }
50    }
51
52    fn parent_match_(&self, item: &Object) -> bool {
53        unsafe {
54            let data = Self::type_data();
55            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkFilterClass;
56            let f = (*parent_class)
57                .match_
58                .expect("No parent class impl for \"match\"");
59            from_glib(f(
60                self.obj().unsafe_cast_ref::<Filter>().to_glib_none().0,
61                item.to_glib_none().0,
62            ))
63        }
64    }
65}
66
67impl<T: FilterImpl> FilterImplExt for T {}
68
69unsafe impl<T: FilterImpl> IsSubclassable<T> for Filter {
70    fn class_init(class: &mut glib::Class<Self>) {
71        Self::parent_class_init::<T>(class);
72
73        assert_initialized_main_thread!();
74
75        let klass = class.as_mut();
76        klass.match_ = Some(filter_match::<T>);
77        klass.get_strictness = Some(filter_get_strictness::<T>);
78    }
79}
80
81unsafe extern "C" fn filter_get_strictness<T: FilterImpl>(
82    ptr: *mut ffi::GtkFilter,
83) -> ffi::GtkFilterMatch {
84    unsafe {
85        let instance = &*(ptr as *mut T::Instance);
86        let imp = instance.imp();
87
88        imp.strictness().into_glib()
89    }
90}
91
92unsafe extern "C" fn filter_match<T: FilterImpl>(
93    ptr: *mut ffi::GtkFilter,
94    itemptr: *mut glib::gobject_ffi::GObject,
95) -> glib::ffi::gboolean {
96    unsafe {
97        let instance = &*(ptr as *mut T::Instance);
98        let imp = instance.imp();
99
100        imp.match_(&from_glib_borrow(itemptr)).into_glib()
101    }
102}