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`](crate::Filter).
5
6use glib::{translate::*, Object};
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Filter, FilterMatch};
9
10pub trait FilterImpl: FilterImplExt + ObjectImpl {
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
40mod sealed {
41    pub trait Sealed {}
42    impl<T: super::FilterImplExt> Sealed for T {}
43}
44
45pub trait FilterImplExt: sealed::Sealed + ObjectSubclass {
46    fn parent_strictness(&self) -> FilterMatch {
47        unsafe {
48            let data = Self::type_data();
49            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkFilterClass;
50            let f = (*parent_class)
51                .get_strictness
52                .expect("No parent class impl for \"get_strictness\"");
53            from_glib(f(self.obj().unsafe_cast_ref::<Filter>().to_glib_none().0))
54        }
55    }
56
57    fn parent_match_(&self, item: &Object) -> bool {
58        unsafe {
59            let data = Self::type_data();
60            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkFilterClass;
61            let f = (*parent_class)
62                .match_
63                .expect("No parent class impl for \"match\"");
64            from_glib(f(
65                self.obj().unsafe_cast_ref::<Filter>().to_glib_none().0,
66                item.to_glib_none().0,
67            ))
68        }
69    }
70}
71
72impl<T: FilterImpl> FilterImplExt for T {}
73
74unsafe impl<T: FilterImpl> IsSubclassable<T> for Filter {
75    fn class_init(class: &mut glib::Class<Self>) {
76        Self::parent_class_init::<T>(class);
77
78        assert_initialized_main_thread!();
79
80        let klass = class.as_mut();
81        klass.match_ = Some(filter_match::<T>);
82        klass.get_strictness = Some(filter_get_strictness::<T>);
83    }
84}
85
86unsafe extern "C" fn filter_get_strictness<T: FilterImpl>(
87    ptr: *mut ffi::GtkFilter,
88) -> ffi::GtkFilterMatch {
89    let instance = &*(ptr as *mut T::Instance);
90    let imp = instance.imp();
91
92    imp.strictness().into_glib()
93}
94
95unsafe extern "C" fn filter_match<T: FilterImpl>(
96    ptr: *mut ffi::GtkFilter,
97    itemptr: *mut glib::gobject_ffi::GObject,
98) -> glib::ffi::gboolean {
99    let instance = &*(ptr as *mut T::Instance);
100    let imp = instance.imp();
101
102    imp.match_(&from_glib_borrow(itemptr)).into_glib()
103}