gtk4/auto/
slice_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
5use crate::ffi;
6#[cfg(feature = "v4_12")]
7#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
8use crate::SectionModel;
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    /// [`SliceListModel`][crate::SliceListModel] is a list model that presents a slice of another model.
20    ///
21    /// This is useful when implementing paging by setting the size to the number
22    /// of elements per page and updating the offset whenever a different page is
23    /// opened.
24    ///
25    /// [`SliceListModel`][crate::SliceListModel] passes through sections from the underlying model.
26    ///
27    /// ## Properties
28    ///
29    ///
30    /// #### `item-type`
31    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
32    ///
33    /// Readable
34    ///
35    ///
36    /// #### `model`
37    ///  Child model to take slice from.
38    ///
39    /// Readable | Writeable
40    ///
41    ///
42    /// #### `n-items`
43    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
44    ///
45    /// Readable
46    ///
47    ///
48    /// #### `offset`
49    ///  Offset of slice.
50    ///
51    /// Readable | Writeable
52    ///
53    ///
54    /// #### `size`
55    ///  Maximum size of slice.
56    ///
57    /// Readable | Writeable
58    ///
59    /// # Implements
60    ///
61    /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`SectionModelExt`][trait@crate::prelude::SectionModelExt]
62    #[doc(alias = "GtkSliceListModel")]
63    pub struct SliceListModel(Object<ffi::GtkSliceListModel, ffi::GtkSliceListModelClass>) @implements gio::ListModel, SectionModel;
64
65    match fn {
66        type_ => || ffi::gtk_slice_list_model_get_type(),
67    }
68}
69
70#[cfg(not(any(feature = "v4_12")))]
71glib::wrapper! {
72    #[doc(alias = "GtkSliceListModel")]
73    pub struct SliceListModel(Object<ffi::GtkSliceListModel, ffi::GtkSliceListModelClass>) @implements gio::ListModel;
74
75    match fn {
76        type_ => || ffi::gtk_slice_list_model_get_type(),
77    }
78}
79
80impl SliceListModel {
81    /// Creates a new slice model.
82    ///
83    /// It presents the slice from @offset to offset + @size
84    /// of the given @model.
85    /// ## `model`
86    /// The model to use
87    /// ## `offset`
88    /// the offset of the slice
89    /// ## `size`
90    /// maximum size of the slice
91    ///
92    /// # Returns
93    ///
94    /// A new [`SliceListModel`][crate::SliceListModel]
95    #[doc(alias = "gtk_slice_list_model_new")]
96    pub fn new(model: Option<impl IsA<gio::ListModel>>, offset: u32, size: u32) -> SliceListModel {
97        assert_initialized_main_thread!();
98        unsafe {
99            from_glib_full(ffi::gtk_slice_list_model_new(
100                model.map(|p| p.upcast()).into_glib_ptr(),
101                offset,
102                size,
103            ))
104        }
105    }
106
107    // rustdoc-stripper-ignore-next
108    /// Creates a new builder-pattern struct instance to construct [`SliceListModel`] objects.
109    ///
110    /// This method returns an instance of [`SliceListModelBuilder`](crate::builders::SliceListModelBuilder) which can be used to create [`SliceListModel`] objects.
111    pub fn builder() -> SliceListModelBuilder {
112        SliceListModelBuilder::new()
113    }
114
115    /// Gets the model that is currently being used or [`None`] if none.
116    ///
117    /// # Returns
118    ///
119    /// The model in use
120    #[doc(alias = "gtk_slice_list_model_get_model")]
121    #[doc(alias = "get_model")]
122    pub fn model(&self) -> Option<gio::ListModel> {
123        unsafe { from_glib_none(ffi::gtk_slice_list_model_get_model(self.to_glib_none().0)) }
124    }
125
126    /// Gets the offset set via gtk_slice_list_model_set_offset().
127    ///
128    /// # Returns
129    ///
130    /// The offset
131    #[doc(alias = "gtk_slice_list_model_get_offset")]
132    #[doc(alias = "get_offset")]
133    pub fn offset(&self) -> u32 {
134        unsafe { ffi::gtk_slice_list_model_get_offset(self.to_glib_none().0) }
135    }
136
137    /// Gets the size set via gtk_slice_list_model_set_size().
138    ///
139    /// # Returns
140    ///
141    /// The size
142    #[doc(alias = "gtk_slice_list_model_get_size")]
143    #[doc(alias = "get_size")]
144    pub fn size(&self) -> u32 {
145        unsafe { ffi::gtk_slice_list_model_get_size(self.to_glib_none().0) }
146    }
147
148    /// Sets the model to show a slice of.
149    ///
150    /// The model's item type must conform to @self's item type.
151    /// ## `model`
152    /// The model to be sliced
153    #[doc(alias = "gtk_slice_list_model_set_model")]
154    #[doc(alias = "model")]
155    pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
156        unsafe {
157            ffi::gtk_slice_list_model_set_model(
158                self.to_glib_none().0,
159                model.map(|p| p.as_ref()).to_glib_none().0,
160            );
161        }
162    }
163
164    /// Sets the offset into the original model for this slice.
165    ///
166    /// If the offset is too large for the sliced model,
167    /// @self will end up empty.
168    /// ## `offset`
169    /// the new offset to use
170    #[doc(alias = "gtk_slice_list_model_set_offset")]
171    #[doc(alias = "offset")]
172    pub fn set_offset(&self, offset: u32) {
173        unsafe {
174            ffi::gtk_slice_list_model_set_offset(self.to_glib_none().0, offset);
175        }
176    }
177
178    /// Sets the maximum size. @self will never have more items
179    /// than @size.
180    ///
181    /// It can however have fewer items if the offset is too large
182    /// or the model sliced from doesn't have enough items.
183    /// ## `size`
184    /// the maximum size
185    #[doc(alias = "gtk_slice_list_model_set_size")]
186    #[doc(alias = "size")]
187    pub fn set_size(&self, size: u32) {
188        unsafe {
189            ffi::gtk_slice_list_model_set_size(self.to_glib_none().0, size);
190        }
191    }
192
193    #[doc(alias = "model")]
194    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
195        unsafe extern "C" fn notify_model_trampoline<F: Fn(&SliceListModel) + 'static>(
196            this: *mut ffi::GtkSliceListModel,
197            _param_spec: glib::ffi::gpointer,
198            f: glib::ffi::gpointer,
199        ) {
200            let f: &F = &*(f as *const F);
201            f(&from_glib_borrow(this))
202        }
203        unsafe {
204            let f: Box_<F> = Box_::new(f);
205            connect_raw(
206                self.as_ptr() as *mut _,
207                b"notify::model\0".as_ptr() as *const _,
208                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
209                    notify_model_trampoline::<F> as *const (),
210                )),
211                Box_::into_raw(f),
212            )
213        }
214    }
215
216    #[doc(alias = "offset")]
217    pub fn connect_offset_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
218        unsafe extern "C" fn notify_offset_trampoline<F: Fn(&SliceListModel) + 'static>(
219            this: *mut ffi::GtkSliceListModel,
220            _param_spec: glib::ffi::gpointer,
221            f: glib::ffi::gpointer,
222        ) {
223            let f: &F = &*(f as *const F);
224            f(&from_glib_borrow(this))
225        }
226        unsafe {
227            let f: Box_<F> = Box_::new(f);
228            connect_raw(
229                self.as_ptr() as *mut _,
230                b"notify::offset\0".as_ptr() as *const _,
231                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
232                    notify_offset_trampoline::<F> as *const (),
233                )),
234                Box_::into_raw(f),
235            )
236        }
237    }
238
239    #[doc(alias = "size")]
240    pub fn connect_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
241        unsafe extern "C" fn notify_size_trampoline<F: Fn(&SliceListModel) + 'static>(
242            this: *mut ffi::GtkSliceListModel,
243            _param_spec: glib::ffi::gpointer,
244            f: glib::ffi::gpointer,
245        ) {
246            let f: &F = &*(f as *const F);
247            f(&from_glib_borrow(this))
248        }
249        unsafe {
250            let f: Box_<F> = Box_::new(f);
251            connect_raw(
252                self.as_ptr() as *mut _,
253                b"notify::size\0".as_ptr() as *const _,
254                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
255                    notify_size_trampoline::<F> as *const (),
256                )),
257                Box_::into_raw(f),
258            )
259        }
260    }
261}
262
263impl Default for SliceListModel {
264    fn default() -> Self {
265        glib::object::Object::new::<Self>()
266    }
267}
268
269// rustdoc-stripper-ignore-next
270/// A [builder-pattern] type to construct [`SliceListModel`] objects.
271///
272/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
273#[must_use = "The builder must be built to be used"]
274pub struct SliceListModelBuilder {
275    builder: glib::object::ObjectBuilder<'static, SliceListModel>,
276}
277
278impl SliceListModelBuilder {
279    fn new() -> Self {
280        Self {
281            builder: glib::object::Object::builder(),
282        }
283    }
284
285    /// Child model to take slice from.
286    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
287        Self {
288            builder: self.builder.property("model", model.clone().upcast()),
289        }
290    }
291
292    /// Offset of slice.
293    pub fn offset(self, offset: u32) -> Self {
294        Self {
295            builder: self.builder.property("offset", offset),
296        }
297    }
298
299    /// Maximum size of slice.
300    pub fn size(self, size: u32) -> Self {
301        Self {
302            builder: self.builder.property("size", size),
303        }
304    }
305
306    // rustdoc-stripper-ignore-next
307    /// Build the [`SliceListModel`].
308    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
309    pub fn build(self) -> SliceListModel {
310        assert_initialized_main_thread!();
311        self.builder.build()
312    }
313}