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
65/// Trait containing all [`struct@Filter`] methods.
66///
67/// # Implementors
68///
69/// [`BoolFilter`][struct@crate::BoolFilter], [`CustomFilter`][struct@crate::CustomFilter], [`FileFilter`][struct@crate::FileFilter], [`Filter`][struct@crate::Filter], [`MultiFilter`][struct@crate::MultiFilter], [`StringFilter`][struct@crate::StringFilter]
70pub trait FilterExt: IsA<Filter> + 'static {
71    /// Notifies all users of the filter that it has changed.
72    ///
73    /// This emits the [`changed`][struct@crate::Filter#changed] signal. Users
74    /// of the filter should then check items again via
75    /// [`match_()`][Self::match_()].
76    ///
77    /// Depending on the @change parameter, not all items need to
78    /// be changed, but only some. Refer to the [`FilterChange`][crate::FilterChange]
79    /// documentation for details.
80    ///
81    /// This function is intended for implementers of [`Filter`][crate::Filter]
82    /// subclasses and should not be called from other functions.
83    /// ## `change`
84    /// how the filter changed
85    #[doc(alias = "gtk_filter_changed")]
86    fn changed(&self, change: FilterChange) {
87        unsafe {
88            ffi::gtk_filter_changed(self.as_ref().to_glib_none().0, change.into_glib());
89        }
90    }
91
92    /// Gets the known strictness of a filter.
93    ///
94    /// If the strictness is not known, [enum@Gtk.FilterMatch.some] is returned.
95    ///
96    /// This value may change after emission of the [`changed`][struct@crate::Filter#changed]
97    /// signal.
98    ///
99    /// This function is meant purely for optimization purposes. Filters can
100    /// choose to omit implementing it, but [`FilterListModel`][crate::FilterListModel] uses it.
101    ///
102    /// # Returns
103    ///
104    /// the strictness of @self
105    #[doc(alias = "gtk_filter_get_strictness")]
106    #[doc(alias = "get_strictness")]
107    fn strictness(&self) -> FilterMatch {
108        unsafe {
109            from_glib(ffi::gtk_filter_get_strictness(
110                self.as_ref().to_glib_none().0,
111            ))
112        }
113    }
114
115    /// Checks if the given @item is matched by the filter or not.
116    /// ## `item`
117    /// The item to check
118    ///
119    /// # Returns
120    ///
121    /// true if the filter matches the item
122    #[doc(alias = "gtk_filter_match")]
123    #[doc(alias = "match")]
124    fn match_(&self, item: &impl IsA<glib::Object>) -> bool {
125        unsafe {
126            from_glib(ffi::gtk_filter_match(
127                self.as_ref().to_glib_none().0,
128                item.as_ref().to_glib_none().0,
129            ))
130        }
131    }
132
133    /// Emitted whenever the filter changed.
134    ///
135    /// Users of the filter should then check items again via
136    /// [`match_()`][Self::match_()].
137    ///
138    /// [`FilterListModel`][crate::FilterListModel] handles this signal automatically.
139    ///
140    /// Depending on the @change parameter, not all items need
141    /// to be checked, but only some. Refer to the [`FilterChange`][crate::FilterChange]
142    /// documentation for details.
143    /// ## `change`
144    /// how the filter changed
145    #[doc(alias = "changed")]
146    fn connect_changed<F: Fn(&Self, FilterChange) + 'static>(&self, f: F) -> SignalHandlerId {
147        unsafe extern "C" fn changed_trampoline<
148            P: IsA<Filter>,
149            F: Fn(&P, FilterChange) + 'static,
150        >(
151            this: *mut ffi::GtkFilter,
152            change: ffi::GtkFilterChange,
153            f: glib::ffi::gpointer,
154        ) {
155            let f: &F = &*(f as *const F);
156            f(
157                Filter::from_glib_borrow(this).unsafe_cast_ref(),
158                from_glib(change),
159            )
160        }
161        unsafe {
162            let f: Box_<F> = Box_::new(f);
163            connect_raw(
164                self.as_ptr() as *mut _,
165                c"changed".as_ptr() as *const _,
166                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
167                    changed_trampoline::<Self, F> as *const (),
168                )),
169                Box_::into_raw(f),
170            )
171        }
172    }
173}
174
175impl<O: IsA<Filter>> FilterExt for O {}