gtk4/subclass/
filter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`Filter`](crate::Filter).

use glib::{translate::*, Object};

use crate::{ffi, prelude::*, subclass::prelude::*, Filter, FilterMatch};

pub trait FilterImpl: FilterImplExt + ObjectImpl {
    /// Gets the known strictness of @filters.
    ///
    /// If the strictness is not known, [`FilterMatch::Some`][crate::FilterMatch::Some] is returned.
    ///
    /// This value may change after emission of the [`changed`][struct@crate::Filter#changed]
    /// signal.
    ///
    /// This function is meant purely for optimization purposes, filters can
    /// choose to omit implementing it, but [`FilterListModel`][crate::FilterListModel] uses it.
    ///
    /// # Returns
    ///
    /// the strictness of @self
    #[doc(alias = "get_strictness")]
    fn strictness(&self) -> FilterMatch {
        self.parent_strictness()
    }
    /// Checks if the given @item is matched by the filter or not.
    /// ## `item`
    /// The item to check
    ///
    /// # Returns
    ///
    /// [`true`] if the filter matches the item and a filter model should
    ///   keep it, [`false`] if not.
    fn match_(&self, item: &Object) -> bool {
        self.parent_match_(item)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::FilterImplExt> Sealed for T {}
}

pub trait FilterImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_strictness(&self) -> FilterMatch {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkFilterClass;
            let f = (*parent_class)
                .get_strictness
                .expect("No parent class impl for \"get_strictness\"");
            from_glib(f(self.obj().unsafe_cast_ref::<Filter>().to_glib_none().0))
        }
    }

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

impl<T: FilterImpl> FilterImplExt for T {}

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

        assert_initialized_main_thread!();

        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.imp();

    imp.strictness().into_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.imp();

    imp.match_(&from_glib_borrow(itemptr)).into_glib()
}