gtk4/auto/
filter.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, FilterChange, FilterMatch};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Describes the filtering to be performed by a [`FilterListModel`][crate::FilterListModel].
16    ///
17    /// The model will use the filter to determine if it should include items
18    /// or not by calling [`FilterExt::match_()`][crate::prelude::FilterExt::match_()] for each item and only
19    /// keeping the ones that the function returns true for.
20    ///
21    /// Filters may change what items they match through their lifetime. In that
22    /// case, they will emit the [`changed`][struct@crate::Filter#changed] signal to notify
23    /// that previous filter results are no longer valid and that items should
24    /// be checked again via [`FilterExt::match_()`][crate::prelude::FilterExt::match_()].
25    ///
26    /// GTK provides various pre-made filter implementations for common filtering
27    /// operations. These filters often include properties that can be linked to
28    /// various widgets to easily allow searches.
29    ///
30    /// However, in particular for large lists or complex search methods, it is
31    /// also possible to subclass [`Filter`][crate::Filter] and provide one's own filter.
32    ///
33    /// ## Signals
34    ///
35    ///
36    /// #### `changed`
37    ///  Emitted whenever the filter changed.
38    ///
39    /// Users of the filter should then check items again via
40    /// [`FilterExt::match_()`][crate::prelude::FilterExt::match_()].
41    ///
42    /// [`FilterListModel`][crate::FilterListModel] handles this signal automatically.
43    ///
44    /// Depending on the @change parameter, not all items need
45    /// to be checked, but only some. Refer to the [`FilterChange`][crate::FilterChange]
46    /// documentation for details.
47    ///
48    ///
49    ///
50    /// # Implements
51    ///
52    /// [`FilterExt`][trait@crate::prelude::FilterExt], [`trait@glib::ObjectExt`]
53    #[doc(alias = "GtkFilter")]
54    pub struct Filter(Object<ffi::GtkFilter, ffi::GtkFilterClass>);
55
56    match fn {
57        type_ => || ffi::gtk_filter_get_type(),
58    }
59}
60
61impl Filter {
62    pub const NONE: Option<&'static Filter> = None;
63}
64
65mod sealed {
66    pub trait Sealed {}
67    impl<T: super::IsA<super::Filter>> Sealed for T {}
68}
69
70/// Trait containing all [`struct@Filter`] methods.
71///
72/// # Implementors
73///
74/// [`BoolFilter`][struct@crate::BoolFilter], [`CustomFilter`][struct@crate::CustomFilter], [`FileFilter`][struct@crate::FileFilter], [`Filter`][struct@crate::Filter], [`MultiFilter`][struct@crate::MultiFilter], [`StringFilter`][struct@crate::StringFilter]
75pub trait FilterExt: IsA<Filter> + sealed::Sealed + 'static {
76    /// Notifies all users of the filter that it has changed.
77    ///
78    /// This emits the [`changed`][struct@crate::Filter#changed] signal. Users
79    /// of the filter should then check items again via
80    /// [`match_()`][Self::match_()].
81    ///
82    /// Depending on the @change parameter, not all items need to
83    /// be changed, but only some. Refer to the [`FilterChange`][crate::FilterChange]
84    /// documentation for details.
85    ///
86    /// This function is intended for implementers of [`Filter`][crate::Filter]
87    /// subclasses and should not be called from other functions.
88    /// ## `change`
89    /// how the filter changed
90    #[doc(alias = "gtk_filter_changed")]
91    fn changed(&self, change: FilterChange) {
92        unsafe {
93            ffi::gtk_filter_changed(self.as_ref().to_glib_none().0, change.into_glib());
94        }
95    }
96
97    /// Gets the known strictness of a filter.
98    ///
99    /// If the strictness is not known, [enum@Gtk.FilterMatch.some] is returned.
100    ///
101    /// This value may change after emission of the [`changed`][struct@crate::Filter#changed]
102    /// signal.
103    ///
104    /// This function is meant purely for optimization purposes. Filters can
105    /// choose to omit implementing it, but [`FilterListModel`][crate::FilterListModel] uses it.
106    ///
107    /// # Returns
108    ///
109    /// the strictness of @self
110    #[doc(alias = "gtk_filter_get_strictness")]
111    #[doc(alias = "get_strictness")]
112    fn strictness(&self) -> FilterMatch {
113        unsafe {
114            from_glib(ffi::gtk_filter_get_strictness(
115                self.as_ref().to_glib_none().0,
116            ))
117        }
118    }
119
120    /// Checks if the given @item is matched by the filter or not.
121    /// ## `item`
122    /// The item to check
123    ///
124    /// # Returns
125    ///
126    /// true if the filter matches the item
127    #[doc(alias = "gtk_filter_match")]
128    #[doc(alias = "match")]
129    fn match_(&self, item: &impl IsA<glib::Object>) -> bool {
130        unsafe {
131            from_glib(ffi::gtk_filter_match(
132                self.as_ref().to_glib_none().0,
133                item.as_ref().to_glib_none().0,
134            ))
135        }
136    }
137
138    /// Emitted whenever the filter changed.
139    ///
140    /// Users of the filter should then check items again via
141    /// [`match_()`][Self::match_()].
142    ///
143    /// [`FilterListModel`][crate::FilterListModel] handles this signal automatically.
144    ///
145    /// Depending on the @change parameter, not all items need
146    /// to be checked, but only some. Refer to the [`FilterChange`][crate::FilterChange]
147    /// documentation for details.
148    /// ## `change`
149    /// how the filter changed
150    #[doc(alias = "changed")]
151    fn connect_changed<F: Fn(&Self, FilterChange) + 'static>(&self, f: F) -> SignalHandlerId {
152        unsafe extern "C" fn changed_trampoline<
153            P: IsA<Filter>,
154            F: Fn(&P, FilterChange) + 'static,
155        >(
156            this: *mut ffi::GtkFilter,
157            change: ffi::GtkFilterChange,
158            f: glib::ffi::gpointer,
159        ) {
160            let f: &F = &*(f as *const F);
161            f(
162                Filter::from_glib_borrow(this).unsafe_cast_ref(),
163                from_glib(change),
164            )
165        }
166        unsafe {
167            let f: Box_<F> = Box_::new(f);
168            connect_raw(
169                self.as_ptr() as *mut _,
170                b"changed\0".as_ptr() as *const _,
171                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
172                    changed_trampoline::<Self, F> as *const (),
173                )),
174                Box_::into_raw(f),
175            )
176        }
177    }
178}
179
180impl<O: IsA<Filter>> FilterExt for O {}