Skip to main content

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
5#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::SectionModel;
8use crate::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 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            unsafe {
201                let f: &F = &*(f as *const F);
202                f(&from_glib_borrow(this))
203            }
204        }
205        unsafe {
206            let f: Box_<F> = Box_::new(f);
207            connect_raw(
208                self.as_ptr() as *mut _,
209                c"notify::model".as_ptr() as *const _,
210                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
211                    notify_model_trampoline::<F> as *const (),
212                )),
213                Box_::into_raw(f),
214            )
215        }
216    }
217
218    #[doc(alias = "offset")]
219    pub fn connect_offset_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
220        unsafe extern "C" fn notify_offset_trampoline<F: Fn(&SliceListModel) + 'static>(
221            this: *mut ffi::GtkSliceListModel,
222            _param_spec: glib::ffi::gpointer,
223            f: glib::ffi::gpointer,
224        ) {
225            unsafe {
226                let f: &F = &*(f as *const F);
227                f(&from_glib_borrow(this))
228            }
229        }
230        unsafe {
231            let f: Box_<F> = Box_::new(f);
232            connect_raw(
233                self.as_ptr() as *mut _,
234                c"notify::offset".as_ptr() as *const _,
235                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
236                    notify_offset_trampoline::<F> as *const (),
237                )),
238                Box_::into_raw(f),
239            )
240        }
241    }
242
243    #[doc(alias = "size")]
244    pub fn connect_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
245        unsafe extern "C" fn notify_size_trampoline<F: Fn(&SliceListModel) + 'static>(
246            this: *mut ffi::GtkSliceListModel,
247            _param_spec: glib::ffi::gpointer,
248            f: glib::ffi::gpointer,
249        ) {
250            unsafe {
251                let f: &F = &*(f as *const F);
252                f(&from_glib_borrow(this))
253            }
254        }
255        unsafe {
256            let f: Box_<F> = Box_::new(f);
257            connect_raw(
258                self.as_ptr() as *mut _,
259                c"notify::size".as_ptr() as *const _,
260                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
261                    notify_size_trampoline::<F> as *const (),
262                )),
263                Box_::into_raw(f),
264            )
265        }
266    }
267}
268
269impl Default for SliceListModel {
270    fn default() -> Self {
271        glib::object::Object::new::<Self>()
272    }
273}
274
275// rustdoc-stripper-ignore-next
276/// A [builder-pattern] type to construct [`SliceListModel`] objects.
277///
278/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
279#[must_use = "The builder must be built to be used"]
280pub struct SliceListModelBuilder {
281    builder: glib::object::ObjectBuilder<'static, SliceListModel>,
282}
283
284impl SliceListModelBuilder {
285    fn new() -> Self {
286        Self {
287            builder: glib::object::Object::builder(),
288        }
289    }
290
291    /// Child model to take slice from.
292    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
293        Self {
294            builder: self.builder.property("model", model.clone().upcast()),
295        }
296    }
297
298    /// Offset of slice.
299    pub fn offset(self, offset: u32) -> Self {
300        Self {
301            builder: self.builder.property("offset", offset),
302        }
303    }
304
305    /// Maximum size of slice.
306    pub fn size(self, size: u32) -> Self {
307        Self {
308            builder: self.builder.property("size", size),
309        }
310    }
311
312    // rustdoc-stripper-ignore-next
313    /// Build the [`SliceListModel`].
314    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
315    pub fn build(self) -> SliceListModel {
316        assert_initialized_main_thread!();
317        self.builder.build()
318    }
319}