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    /// 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 | Writeable
37    ///
38    ///
39    /// #### `incremental`
40    ///  If the model should filter items incrementally.
41    ///
42    /// Readable | Writeable
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 | Writeable
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 | Writeable
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(any(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            let f: &F = &*(f as *const F);
308            f(&from_glib_borrow(this))
309        }
310        unsafe {
311            let f: Box_<F> = Box_::new(f);
312            connect_raw(
313                self.as_ptr() as *mut _,
314                c"notify::filter".as_ptr() as *const _,
315                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
316                    notify_filter_trampoline::<F> as *const (),
317                )),
318                Box_::into_raw(f),
319            )
320        }
321    }
322
323    #[doc(alias = "incremental")]
324    pub fn connect_incremental_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
325        unsafe extern "C" fn notify_incremental_trampoline<F: Fn(&FilterListModel) + 'static>(
326            this: *mut ffi::GtkFilterListModel,
327            _param_spec: glib::ffi::gpointer,
328            f: glib::ffi::gpointer,
329        ) {
330            let f: &F = &*(f as *const F);
331            f(&from_glib_borrow(this))
332        }
333        unsafe {
334            let f: Box_<F> = Box_::new(f);
335            connect_raw(
336                self.as_ptr() as *mut _,
337                c"notify::incremental".as_ptr() as *const _,
338                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
339                    notify_incremental_trampoline::<F> as *const (),
340                )),
341                Box_::into_raw(f),
342            )
343        }
344    }
345
346    #[doc(alias = "model")]
347    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
348        unsafe extern "C" fn notify_model_trampoline<F: Fn(&FilterListModel) + 'static>(
349            this: *mut ffi::GtkFilterListModel,
350            _param_spec: glib::ffi::gpointer,
351            f: glib::ffi::gpointer,
352        ) {
353            let f: &F = &*(f as *const F);
354            f(&from_glib_borrow(this))
355        }
356        unsafe {
357            let f: Box_<F> = Box_::new(f);
358            connect_raw(
359                self.as_ptr() as *mut _,
360                c"notify::model".as_ptr() as *const _,
361                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
362                    notify_model_trampoline::<F> as *const (),
363                )),
364                Box_::into_raw(f),
365            )
366        }
367    }
368
369    #[doc(alias = "pending")]
370    pub fn connect_pending_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
371        unsafe extern "C" fn notify_pending_trampoline<F: Fn(&FilterListModel) + 'static>(
372            this: *mut ffi::GtkFilterListModel,
373            _param_spec: glib::ffi::gpointer,
374            f: glib::ffi::gpointer,
375        ) {
376            let f: &F = &*(f as *const F);
377            f(&from_glib_borrow(this))
378        }
379        unsafe {
380            let f: Box_<F> = Box_::new(f);
381            connect_raw(
382                self.as_ptr() as *mut _,
383                c"notify::pending".as_ptr() as *const _,
384                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
385                    notify_pending_trampoline::<F> as *const (),
386                )),
387                Box_::into_raw(f),
388            )
389        }
390    }
391
392    #[cfg(feature = "v4_20")]
393    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
394    #[doc(alias = "watch-items")]
395    pub fn connect_watch_items_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
396        unsafe extern "C" fn notify_watch_items_trampoline<F: Fn(&FilterListModel) + 'static>(
397            this: *mut ffi::GtkFilterListModel,
398            _param_spec: glib::ffi::gpointer,
399            f: glib::ffi::gpointer,
400        ) {
401            let f: &F = &*(f as *const F);
402            f(&from_glib_borrow(this))
403        }
404        unsafe {
405            let f: Box_<F> = Box_::new(f);
406            connect_raw(
407                self.as_ptr() as *mut _,
408                c"notify::watch-items".as_ptr() as *const _,
409                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
410                    notify_watch_items_trampoline::<F> as *const (),
411                )),
412                Box_::into_raw(f),
413            )
414        }
415    }
416}
417
418impl Default for FilterListModel {
419    fn default() -> Self {
420        glib::object::Object::new::<Self>()
421    }
422}
423
424// rustdoc-stripper-ignore-next
425/// A [builder-pattern] type to construct [`FilterListModel`] objects.
426///
427/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
428#[must_use = "The builder must be built to be used"]
429pub struct FilterListModelBuilder {
430    builder: glib::object::ObjectBuilder<'static, FilterListModel>,
431}
432
433impl FilterListModelBuilder {
434    fn new() -> Self {
435        Self {
436            builder: glib::object::Object::builder(),
437        }
438    }
439
440    /// The filter for this model.
441    pub fn filter(self, filter: &impl IsA<Filter>) -> Self {
442        Self {
443            builder: self.builder.property("filter", filter.clone().upcast()),
444        }
445    }
446
447    /// If the model should filter items incrementally.
448    pub fn incremental(self, incremental: bool) -> Self {
449        Self {
450            builder: self.builder.property("incremental", incremental),
451        }
452    }
453
454    /// The model being filtered.
455    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
456        Self {
457            builder: self.builder.property("model", model.clone().upcast()),
458        }
459    }
460
461    /// Monitor the list items for changes. It may impact performance.
462    #[cfg(feature = "v4_20")]
463    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
464    pub fn watch_items(self, watch_items: bool) -> Self {
465        Self {
466            builder: self.builder.property("watch-items", watch_items),
467        }
468    }
469
470    // rustdoc-stripper-ignore-next
471    /// Build the [`FilterListModel`].
472    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
473    pub fn build(self) -> FilterListModel {
474        assert_initialized_main_thread!();
475        self.builder.build()
476    }
477}