gtk4/auto/
numeric_sorter.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, Expression, SortType, Sorter};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// [`NumericSorter`][crate::NumericSorter] is a [`Sorter`][crate::Sorter] that compares numbers.
15    ///
16    /// To obtain the numbers to compare, this sorter evaluates a
17    /// [`Expression`][crate::Expression].
18    ///
19    /// ## Properties
20    ///
21    ///
22    /// #### `expression`
23    ///  The expression to evaluate on items to get a number to compare with.
24    ///
25    /// Readable | Writeable
26    ///
27    ///
28    /// #### `sort-order`
29    ///  Whether the sorter will sort smaller numbers first.
30    ///
31    /// Readable | Writeable
32    ///
33    /// # Implements
34    ///
35    /// [`SorterExt`][trait@crate::prelude::SorterExt], [`trait@glib::ObjectExt`]
36    #[doc(alias = "GtkNumericSorter")]
37    pub struct NumericSorter(Object<ffi::GtkNumericSorter, ffi::GtkNumericSorterClass>) @extends Sorter;
38
39    match fn {
40        type_ => || ffi::gtk_numeric_sorter_get_type(),
41    }
42}
43
44impl NumericSorter {
45    /// Creates a new numeric sorter using the given @expression.
46    ///
47    /// Smaller numbers will be sorted first. You can call
48    /// [`set_sort_order()`][Self::set_sort_order()] to change this.
49    /// ## `expression`
50    /// The expression to evaluate
51    ///
52    /// # Returns
53    ///
54    /// a new [`NumericSorter`][crate::NumericSorter]
55    #[doc(alias = "gtk_numeric_sorter_new")]
56    pub fn new(expression: Option<impl AsRef<Expression>>) -> NumericSorter {
57        assert_initialized_main_thread!();
58        unsafe {
59            from_glib_full(ffi::gtk_numeric_sorter_new(
60                expression
61                    .map(|p| p.as_ref().clone().upcast())
62                    .into_glib_ptr(),
63            ))
64        }
65    }
66
67    // rustdoc-stripper-ignore-next
68    /// Creates a new builder-pattern struct instance to construct [`NumericSorter`] objects.
69    ///
70    /// This method returns an instance of [`NumericSorterBuilder`](crate::builders::NumericSorterBuilder) which can be used to create [`NumericSorter`] objects.
71    pub fn builder() -> NumericSorterBuilder {
72        NumericSorterBuilder::new()
73    }
74
75    /// Gets the expression that is evaluated to obtain numbers from items.
76    ///
77    /// # Returns
78    ///
79    /// a [`Expression`][crate::Expression]
80    #[doc(alias = "gtk_numeric_sorter_get_expression")]
81    #[doc(alias = "get_expression")]
82    pub fn expression(&self) -> Option<Expression> {
83        unsafe {
84            from_glib_none(ffi::gtk_numeric_sorter_get_expression(
85                self.to_glib_none().0,
86            ))
87        }
88    }
89
90    /// Gets whether this sorter will sort smaller numbers first.
91    ///
92    /// # Returns
93    ///
94    /// the order of the numbers
95    #[doc(alias = "gtk_numeric_sorter_get_sort_order")]
96    #[doc(alias = "get_sort_order")]
97    #[doc(alias = "sort-order")]
98    pub fn sort_order(&self) -> SortType {
99        unsafe {
100            from_glib(ffi::gtk_numeric_sorter_get_sort_order(
101                self.to_glib_none().0,
102            ))
103        }
104    }
105
106    /// Sets the expression that is evaluated to obtain numbers from items.
107    ///
108    /// Unless an expression is set on @self, the sorter will always
109    /// compare items as invalid.
110    ///
111    /// The expression must have a return type that can be compared
112    /// numerically, such as `G_TYPE_INT` or `G_TYPE_DOUBLE`.
113    /// ## `expression`
114    /// a [`Expression`][crate::Expression]
115    #[doc(alias = "gtk_numeric_sorter_set_expression")]
116    #[doc(alias = "expression")]
117    pub fn set_expression(&self, expression: Option<impl AsRef<Expression>>) {
118        unsafe {
119            ffi::gtk_numeric_sorter_set_expression(
120                self.to_glib_none().0,
121                expression.as_ref().map(|p| p.as_ref()).to_glib_none().0,
122            );
123        }
124    }
125
126    /// Sets whether to sort smaller numbers before larger ones.
127    /// ## `sort_order`
128    /// whether to sort smaller numbers first
129    #[doc(alias = "gtk_numeric_sorter_set_sort_order")]
130    #[doc(alias = "sort-order")]
131    pub fn set_sort_order(&self, sort_order: SortType) {
132        unsafe {
133            ffi::gtk_numeric_sorter_set_sort_order(self.to_glib_none().0, sort_order.into_glib());
134        }
135    }
136
137    #[doc(alias = "expression")]
138    pub fn connect_expression_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
139        unsafe extern "C" fn notify_expression_trampoline<F: Fn(&NumericSorter) + 'static>(
140            this: *mut ffi::GtkNumericSorter,
141            _param_spec: glib::ffi::gpointer,
142            f: glib::ffi::gpointer,
143        ) {
144            let f: &F = &*(f as *const F);
145            f(&from_glib_borrow(this))
146        }
147        unsafe {
148            let f: Box_<F> = Box_::new(f);
149            connect_raw(
150                self.as_ptr() as *mut _,
151                b"notify::expression\0".as_ptr() as *const _,
152                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
153                    notify_expression_trampoline::<F> as *const (),
154                )),
155                Box_::into_raw(f),
156            )
157        }
158    }
159
160    #[doc(alias = "sort-order")]
161    pub fn connect_sort_order_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
162        unsafe extern "C" fn notify_sort_order_trampoline<F: Fn(&NumericSorter) + 'static>(
163            this: *mut ffi::GtkNumericSorter,
164            _param_spec: glib::ffi::gpointer,
165            f: glib::ffi::gpointer,
166        ) {
167            let f: &F = &*(f as *const F);
168            f(&from_glib_borrow(this))
169        }
170        unsafe {
171            let f: Box_<F> = Box_::new(f);
172            connect_raw(
173                self.as_ptr() as *mut _,
174                b"notify::sort-order\0".as_ptr() as *const _,
175                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
176                    notify_sort_order_trampoline::<F> as *const (),
177                )),
178                Box_::into_raw(f),
179            )
180        }
181    }
182}
183
184impl Default for NumericSorter {
185    fn default() -> Self {
186        glib::object::Object::new::<Self>()
187    }
188}
189
190// rustdoc-stripper-ignore-next
191/// A [builder-pattern] type to construct [`NumericSorter`] objects.
192///
193/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
194#[must_use = "The builder must be built to be used"]
195pub struct NumericSorterBuilder {
196    builder: glib::object::ObjectBuilder<'static, NumericSorter>,
197}
198
199impl NumericSorterBuilder {
200    fn new() -> Self {
201        Self {
202            builder: glib::object::Object::builder(),
203        }
204    }
205
206    /// The expression to evaluate on items to get a number to compare with.
207    pub fn expression(self, expression: impl AsRef<Expression>) -> Self {
208        Self {
209            builder: self
210                .builder
211                .property("expression", expression.as_ref().clone()),
212        }
213    }
214
215    /// Whether the sorter will sort smaller numbers first.
216    pub fn sort_order(self, sort_order: SortType) -> Self {
217        Self {
218            builder: self.builder.property("sort-order", sort_order),
219        }
220    }
221
222    // rustdoc-stripper-ignore-next
223    /// Build the [`NumericSorter`].
224    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
225    pub fn build(self) -> NumericSorter {
226        assert_initialized_main_thread!();
227        self.builder.build()
228    }
229}