Skip to main content

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