gtk4/auto/
filter_list_model.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
5#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::SectionModel;
8use crate::{ffi, Filter};
9use glib::{
10    prelude::*,
11    signal::{connect_raw, SignalHandlerId},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16#[cfg(feature = "v4_12")]
17#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
18glib::wrapper! {
19    /// [`FilterListModel`][crate::FilterListModel] is a list model that filters the elements of
20    /// the underlying model according to a [`Filter`][crate::Filter].
21    ///
22    /// It hides some elements from the other model according to
23    /// criteria given by a [`Filter`][crate::Filter].
24    ///
25    /// The model can be set up to do incremental filtering, so that
26    /// filtering long lists doesn't block the UI. See
27    /// [`set_incremental()`][Self::set_incremental()] for details.
28    ///
29    /// [`FilterListModel`][crate::FilterListModel] passes through sections from the underlying model.
30    ///
31    /// ## Properties
32    ///
33    ///
34    /// #### `filter`
35    ///  The filter for this model.
36    ///
37    /// Readable | Writeable
38    ///
39    ///
40    /// #### `incremental`
41    ///  If the model should filter items incrementally.
42    ///
43    /// Readable | Writeable
44    ///
45    ///
46    /// #### `item-type`
47    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
48    ///
49    /// Readable
50    ///
51    ///
52    /// #### `model`
53    ///  The model being filtered.
54    ///
55    /// Readable | Writeable
56    ///
57    ///
58    /// #### `n-items`
59    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
60    ///
61    /// Readable
62    ///
63    ///
64    /// #### `pending`
65    ///  Number of items not yet filtered.
66    ///
67    /// Readable
68    ///
69    /// # Implements
70    ///
71    /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`SectionModelExt`][trait@crate::prelude::SectionModelExt]
72    #[doc(alias = "GtkFilterListModel")]
73    pub struct FilterListModel(Object<ffi::GtkFilterListModel, ffi::GtkFilterListModelClass>) @implements gio::ListModel, SectionModel;
74
75    match fn {
76        type_ => || ffi::gtk_filter_list_model_get_type(),
77    }
78}
79
80#[cfg(not(any(feature = "v4_12")))]
81glib::wrapper! {
82    #[doc(alias = "GtkFilterListModel")]
83    pub struct FilterListModel(Object<ffi::GtkFilterListModel, ffi::GtkFilterListModelClass>) @implements gio::ListModel;
84
85    match fn {
86        type_ => || ffi::gtk_filter_list_model_get_type(),
87    }
88}
89
90impl FilterListModel {
91    /// Creates a new [`FilterListModel`][crate::FilterListModel] that will filter @model using the given
92    /// @filter.
93    /// ## `model`
94    /// the model to sort
95    /// ## `filter`
96    /// filter
97    ///
98    /// # Returns
99    ///
100    /// a new [`FilterListModel`][crate::FilterListModel]
101    #[doc(alias = "gtk_filter_list_model_new")]
102    pub fn new(
103        model: Option<impl IsA<gio::ListModel>>,
104        filter: Option<impl IsA<Filter>>,
105    ) -> FilterListModel {
106        assert_initialized_main_thread!();
107        unsafe {
108            from_glib_full(ffi::gtk_filter_list_model_new(
109                model.map(|p| p.upcast()).into_glib_ptr(),
110                filter.map(|p| p.upcast()).into_glib_ptr(),
111            ))
112        }
113    }
114
115    // rustdoc-stripper-ignore-next
116    /// Creates a new builder-pattern struct instance to construct [`FilterListModel`] objects.
117    ///
118    /// This method returns an instance of [`FilterListModelBuilder`](crate::builders::FilterListModelBuilder) which can be used to create [`FilterListModel`] objects.
119    pub fn builder() -> FilterListModelBuilder {
120        FilterListModelBuilder::new()
121    }
122
123    /// Gets the [`Filter`][crate::Filter] currently set on @self.
124    ///
125    /// # Returns
126    ///
127    /// The filter currently in use
128    #[doc(alias = "gtk_filter_list_model_get_filter")]
129    #[doc(alias = "get_filter")]
130    pub fn filter(&self) -> Option<Filter> {
131        unsafe { from_glib_none(ffi::gtk_filter_list_model_get_filter(self.to_glib_none().0)) }
132    }
133
134    /// Returns whether incremental filtering is enabled.
135    ///
136    /// See [`set_incremental()`][Self::set_incremental()].
137    ///
138    /// # Returns
139    ///
140    /// [`true`] if incremental filtering is enabled
141    #[doc(alias = "gtk_filter_list_model_get_incremental")]
142    #[doc(alias = "get_incremental")]
143    #[doc(alias = "incremental")]
144    pub fn is_incremental(&self) -> bool {
145        unsafe {
146            from_glib(ffi::gtk_filter_list_model_get_incremental(
147                self.to_glib_none().0,
148            ))
149        }
150    }
151
152    /// Gets the model currently filtered or [`None`] if none.
153    ///
154    /// # Returns
155    ///
156    /// The model that gets filtered
157    #[doc(alias = "gtk_filter_list_model_get_model")]
158    #[doc(alias = "get_model")]
159    pub fn model(&self) -> Option<gio::ListModel> {
160        unsafe { from_glib_none(ffi::gtk_filter_list_model_get_model(self.to_glib_none().0)) }
161    }
162
163    /// Returns the number of items that have not been filtered yet.
164    ///
165    /// You can use this value to check if @self is busy filtering by
166    /// comparing the return value to 0 or you can compute the percentage
167    /// of the filter remaining by dividing the return value by the total
168    /// number of items in the underlying model:
169    ///
170    /// **⚠️ The following code is in c ⚠️**
171    ///
172    /// ```c
173    /// pending = gtk_filter_list_model_get_pending (self);
174    /// model = gtk_filter_list_model_get_model (self);
175    /// percentage = pending / (double) g_list_model_get_n_items (model);
176    /// ```
177    ///
178    /// If no filter operation is ongoing - in particular when
179    /// [`incremental`][struct@crate::FilterListModel#incremental] is [`false`] - this
180    /// function returns 0.
181    ///
182    /// # Returns
183    ///
184    /// The number of items not yet filtered
185    #[doc(alias = "gtk_filter_list_model_get_pending")]
186    #[doc(alias = "get_pending")]
187    pub fn pending(&self) -> u32 {
188        unsafe { ffi::gtk_filter_list_model_get_pending(self.to_glib_none().0) }
189    }
190
191    /// Sets the filter used to filter items.
192    /// ## `filter`
193    /// filter to use
194    #[doc(alias = "gtk_filter_list_model_set_filter")]
195    #[doc(alias = "filter")]
196    pub fn set_filter(&self, filter: Option<&impl IsA<Filter>>) {
197        unsafe {
198            ffi::gtk_filter_list_model_set_filter(
199                self.to_glib_none().0,
200                filter.map(|p| p.as_ref()).to_glib_none().0,
201            );
202        }
203    }
204
205    /// Sets the filter model to do an incremental sort.
206    ///
207    /// When incremental filtering is enabled, the [`FilterListModel`][crate::FilterListModel] will not
208    /// run filters immediately, but will instead queue an idle handler that
209    /// incrementally filters the items and adds them to the list. This of course
210    /// means that items are not instantly added to the list, but only appear
211    /// incrementally.
212    ///
213    /// When your filter blocks the UI while filtering, you might consider
214    /// turning this on. Depending on your model and filters, this may become
215    /// interesting around 10,000 to 100,000 items.
216    ///
217    /// By default, incremental filtering is disabled.
218    ///
219    /// See [`pending()`][Self::pending()] for progress information
220    /// about an ongoing incremental filtering operation.
221    /// ## `incremental`
222    /// [`true`] to enable incremental filtering
223    #[doc(alias = "gtk_filter_list_model_set_incremental")]
224    #[doc(alias = "incremental")]
225    pub fn set_incremental(&self, incremental: bool) {
226        unsafe {
227            ffi::gtk_filter_list_model_set_incremental(
228                self.to_glib_none().0,
229                incremental.into_glib(),
230            );
231        }
232    }
233
234    /// Sets the model to be filtered.
235    ///
236    /// Note that GTK makes no effort to ensure that @model conforms to
237    /// the item type of @self. It assumes that the caller knows what they
238    /// are doing and have set up an appropriate filter to ensure that item
239    /// types match.
240    /// ## `model`
241    /// The model to be filtered
242    #[doc(alias = "gtk_filter_list_model_set_model")]
243    #[doc(alias = "model")]
244    pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
245        unsafe {
246            ffi::gtk_filter_list_model_set_model(
247                self.to_glib_none().0,
248                model.map(|p| p.as_ref()).to_glib_none().0,
249            );
250        }
251    }
252
253    #[doc(alias = "filter")]
254    pub fn connect_filter_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
255        unsafe extern "C" fn notify_filter_trampoline<F: Fn(&FilterListModel) + 'static>(
256            this: *mut ffi::GtkFilterListModel,
257            _param_spec: glib::ffi::gpointer,
258            f: glib::ffi::gpointer,
259        ) {
260            let f: &F = &*(f as *const F);
261            f(&from_glib_borrow(this))
262        }
263        unsafe {
264            let f: Box_<F> = Box_::new(f);
265            connect_raw(
266                self.as_ptr() as *mut _,
267                b"notify::filter\0".as_ptr() as *const _,
268                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
269                    notify_filter_trampoline::<F> as *const (),
270                )),
271                Box_::into_raw(f),
272            )
273        }
274    }
275
276    #[doc(alias = "incremental")]
277    pub fn connect_incremental_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
278        unsafe extern "C" fn notify_incremental_trampoline<F: Fn(&FilterListModel) + 'static>(
279            this: *mut ffi::GtkFilterListModel,
280            _param_spec: glib::ffi::gpointer,
281            f: glib::ffi::gpointer,
282        ) {
283            let f: &F = &*(f as *const F);
284            f(&from_glib_borrow(this))
285        }
286        unsafe {
287            let f: Box_<F> = Box_::new(f);
288            connect_raw(
289                self.as_ptr() as *mut _,
290                b"notify::incremental\0".as_ptr() as *const _,
291                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
292                    notify_incremental_trampoline::<F> as *const (),
293                )),
294                Box_::into_raw(f),
295            )
296        }
297    }
298
299    #[doc(alias = "model")]
300    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
301        unsafe extern "C" fn notify_model_trampoline<F: Fn(&FilterListModel) + 'static>(
302            this: *mut ffi::GtkFilterListModel,
303            _param_spec: glib::ffi::gpointer,
304            f: glib::ffi::gpointer,
305        ) {
306            let f: &F = &*(f as *const F);
307            f(&from_glib_borrow(this))
308        }
309        unsafe {
310            let f: Box_<F> = Box_::new(f);
311            connect_raw(
312                self.as_ptr() as *mut _,
313                b"notify::model\0".as_ptr() as *const _,
314                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
315                    notify_model_trampoline::<F> as *const (),
316                )),
317                Box_::into_raw(f),
318            )
319        }
320    }
321
322    #[doc(alias = "pending")]
323    pub fn connect_pending_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
324        unsafe extern "C" fn notify_pending_trampoline<F: Fn(&FilterListModel) + 'static>(
325            this: *mut ffi::GtkFilterListModel,
326            _param_spec: glib::ffi::gpointer,
327            f: glib::ffi::gpointer,
328        ) {
329            let f: &F = &*(f as *const F);
330            f(&from_glib_borrow(this))
331        }
332        unsafe {
333            let f: Box_<F> = Box_::new(f);
334            connect_raw(
335                self.as_ptr() as *mut _,
336                b"notify::pending\0".as_ptr() as *const _,
337                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
338                    notify_pending_trampoline::<F> as *const (),
339                )),
340                Box_::into_raw(f),
341            )
342        }
343    }
344}
345
346impl Default for FilterListModel {
347    fn default() -> Self {
348        glib::object::Object::new::<Self>()
349    }
350}
351
352// rustdoc-stripper-ignore-next
353/// A [builder-pattern] type to construct [`FilterListModel`] objects.
354///
355/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
356#[must_use = "The builder must be built to be used"]
357pub struct FilterListModelBuilder {
358    builder: glib::object::ObjectBuilder<'static, FilterListModel>,
359}
360
361impl FilterListModelBuilder {
362    fn new() -> Self {
363        Self {
364            builder: glib::object::Object::builder(),
365        }
366    }
367
368    /// The filter for this model.
369    pub fn filter(self, filter: &impl IsA<Filter>) -> Self {
370        Self {
371            builder: self.builder.property("filter", filter.clone().upcast()),
372        }
373    }
374
375    /// If the model should filter items incrementally.
376    pub fn incremental(self, incremental: bool) -> Self {
377        Self {
378            builder: self.builder.property("incremental", incremental),
379        }
380    }
381
382    /// The model being filtered.
383    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
384        Self {
385            builder: self.builder.property("model", model.clone().upcast()),
386        }
387    }
388
389    // rustdoc-stripper-ignore-next
390    /// Build the [`FilterListModel`].
391    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
392    pub fn build(self) -> FilterListModel {
393        assert_initialized_main_thread!();
394        self.builder.build()
395    }
396}