Skip to main content

gtk4/auto/
string_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
5#[cfg(feature = "v4_10")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
7use crate::Collation;
8use crate::{Expression, Sorter, ffi};
9use glib::{
10    prelude::*,
11    signal::{SignalHandlerId, connect_raw},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17    /// Sorts items by comparing strings.
18    ///
19    /// To obtain the strings to compare, this sorter evaluates a
20    /// [`Expression`][crate::Expression].
21    ///
22    /// It does the comparison in a linguistically correct way using the
23    /// current locale by normalizing Unicode strings and possibly case-folding
24    /// them before performing the comparison.
25    ///
26    /// ## Properties
27    ///
28    ///
29    /// #### `collation`
30    ///  The collation method to use for sorting.
31    ///
32    /// The `GTK_COLLATION_NONE` value is useful when the expression already
33    /// returns collation keys, or strings that need to be compared byte-by-byte.
34    ///
35    /// The default value, `GTK_COLLATION_UNICODE`, compares strings according
36    /// to the [Unicode collation algorithm](https://www.unicode.org/reports/tr10/).
37    ///
38    /// Readable | Writeable
39    ///
40    ///
41    /// #### `expression`
42    ///  The expression to evaluate on item to get a string to compare with.
43    ///
44    /// Readable | Writeable
45    ///
46    ///
47    /// #### `ignore-case`
48    ///  If sorting is case sensitive.
49    ///
50    /// Readable | Writeable
51    ///
52    /// # Implements
53    ///
54    /// [`SorterExt`][trait@crate::prelude::SorterExt], [`trait@glib::ObjectExt`]
55    #[doc(alias = "GtkStringSorter")]
56    pub struct StringSorter(Object<ffi::GtkStringSorter, ffi::GtkStringSorterClass>) @extends Sorter;
57
58    match fn {
59        type_ => || ffi::gtk_string_sorter_get_type(),
60    }
61}
62
63impl StringSorter {
64    /// Creates a new string sorter that compares items using the given
65    /// @expression.
66    ///
67    /// Unless an expression is set on it, this sorter will always
68    /// compare items as invalid.
69    /// ## `expression`
70    /// The expression to evaluate
71    ///
72    /// # Returns
73    ///
74    /// a new [`StringSorter`][crate::StringSorter]
75    #[doc(alias = "gtk_string_sorter_new")]
76    pub fn new(expression: Option<impl AsRef<Expression>>) -> StringSorter {
77        assert_initialized_main_thread!();
78        unsafe {
79            from_glib_full(ffi::gtk_string_sorter_new(
80                expression
81                    .map(|p| p.as_ref().clone().upcast())
82                    .into_glib_ptr(),
83            ))
84        }
85    }
86
87    // rustdoc-stripper-ignore-next
88    /// Creates a new builder-pattern struct instance to construct [`StringSorter`] objects.
89    ///
90    /// This method returns an instance of [`StringSorterBuilder`](crate::builders::StringSorterBuilder) which can be used to create [`StringSorter`] objects.
91    pub fn builder() -> StringSorterBuilder {
92        StringSorterBuilder::new()
93    }
94
95    /// Gets which collation method the sorter uses.
96    ///
97    /// # Returns
98    ///
99    /// The collation method
100    #[cfg(feature = "v4_10")]
101    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
102    #[doc(alias = "gtk_string_sorter_get_collation")]
103    #[doc(alias = "get_collation")]
104    pub fn collation(&self) -> Collation {
105        unsafe { from_glib(ffi::gtk_string_sorter_get_collation(self.to_glib_none().0)) }
106    }
107
108    /// Gets the expression that is evaluated to obtain strings from items.
109    ///
110    /// # Returns
111    ///
112    /// a [`Expression`][crate::Expression]
113    #[doc(alias = "gtk_string_sorter_get_expression")]
114    #[doc(alias = "get_expression")]
115    pub fn expression(&self) -> Option<Expression> {
116        unsafe { from_glib_none(ffi::gtk_string_sorter_get_expression(self.to_glib_none().0)) }
117    }
118
119    /// Gets whether the sorter ignores case differences.
120    ///
121    /// # Returns
122    ///
123    /// [`true`] if @self is ignoring case differences
124    #[doc(alias = "gtk_string_sorter_get_ignore_case")]
125    #[doc(alias = "get_ignore_case")]
126    #[doc(alias = "ignore-case")]
127    pub fn ignores_case(&self) -> bool {
128        unsafe {
129            from_glib(ffi::gtk_string_sorter_get_ignore_case(
130                self.to_glib_none().0,
131            ))
132        }
133    }
134
135    /// Sets the collation method to use for sorting.
136    /// ## `collation`
137    /// the collation method
138    #[cfg(feature = "v4_10")]
139    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
140    #[doc(alias = "gtk_string_sorter_set_collation")]
141    #[doc(alias = "collation")]
142    pub fn set_collation(&self, collation: Collation) {
143        unsafe {
144            ffi::gtk_string_sorter_set_collation(self.to_glib_none().0, collation.into_glib());
145        }
146    }
147
148    /// Sets the expression that is evaluated to obtain strings from items.
149    ///
150    /// The expression must have the type `G_TYPE_STRING`.
151    /// ## `expression`
152    /// a [`Expression`][crate::Expression]
153    #[doc(alias = "gtk_string_sorter_set_expression")]
154    #[doc(alias = "expression")]
155    pub fn set_expression(&self, expression: Option<impl AsRef<Expression>>) {
156        unsafe {
157            ffi::gtk_string_sorter_set_expression(
158                self.to_glib_none().0,
159                expression.as_ref().map(|p| p.as_ref()).to_glib_none().0,
160            );
161        }
162    }
163
164    /// Sets whether the sorter will ignore case differences.
165    /// ## `ignore_case`
166    /// [`true`] to ignore case differences
167    #[doc(alias = "gtk_string_sorter_set_ignore_case")]
168    #[doc(alias = "ignore-case")]
169    pub fn set_ignore_case(&self, ignore_case: bool) {
170        unsafe {
171            ffi::gtk_string_sorter_set_ignore_case(self.to_glib_none().0, ignore_case.into_glib());
172        }
173    }
174
175    #[cfg(feature = "v4_10")]
176    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
177    #[doc(alias = "collation")]
178    pub fn connect_collation_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
179        unsafe extern "C" fn notify_collation_trampoline<F: Fn(&StringSorter) + 'static>(
180            this: *mut ffi::GtkStringSorter,
181            _param_spec: glib::ffi::gpointer,
182            f: glib::ffi::gpointer,
183        ) {
184            unsafe {
185                let f: &F = &*(f as *const F);
186                f(&from_glib_borrow(this))
187            }
188        }
189        unsafe {
190            let f: Box_<F> = Box_::new(f);
191            connect_raw(
192                self.as_ptr() as *mut _,
193                c"notify::collation".as_ptr() as *const _,
194                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
195                    notify_collation_trampoline::<F> as *const (),
196                )),
197                Box_::into_raw(f),
198            )
199        }
200    }
201
202    #[doc(alias = "expression")]
203    pub fn connect_expression_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
204        unsafe extern "C" fn notify_expression_trampoline<F: Fn(&StringSorter) + 'static>(
205            this: *mut ffi::GtkStringSorter,
206            _param_spec: glib::ffi::gpointer,
207            f: glib::ffi::gpointer,
208        ) {
209            unsafe {
210                let f: &F = &*(f as *const F);
211                f(&from_glib_borrow(this))
212            }
213        }
214        unsafe {
215            let f: Box_<F> = Box_::new(f);
216            connect_raw(
217                self.as_ptr() as *mut _,
218                c"notify::expression".as_ptr() as *const _,
219                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
220                    notify_expression_trampoline::<F> as *const (),
221                )),
222                Box_::into_raw(f),
223            )
224        }
225    }
226
227    #[doc(alias = "ignore-case")]
228    pub fn connect_ignore_case_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
229        unsafe extern "C" fn notify_ignore_case_trampoline<F: Fn(&StringSorter) + 'static>(
230            this: *mut ffi::GtkStringSorter,
231            _param_spec: glib::ffi::gpointer,
232            f: glib::ffi::gpointer,
233        ) {
234            unsafe {
235                let f: &F = &*(f as *const F);
236                f(&from_glib_borrow(this))
237            }
238        }
239        unsafe {
240            let f: Box_<F> = Box_::new(f);
241            connect_raw(
242                self.as_ptr() as *mut _,
243                c"notify::ignore-case".as_ptr() as *const _,
244                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
245                    notify_ignore_case_trampoline::<F> as *const (),
246                )),
247                Box_::into_raw(f),
248            )
249        }
250    }
251}
252
253impl Default for StringSorter {
254    fn default() -> Self {
255        glib::object::Object::new::<Self>()
256    }
257}
258
259// rustdoc-stripper-ignore-next
260/// A [builder-pattern] type to construct [`StringSorter`] objects.
261///
262/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
263#[must_use = "The builder must be built to be used"]
264pub struct StringSorterBuilder {
265    builder: glib::object::ObjectBuilder<'static, StringSorter>,
266}
267
268impl StringSorterBuilder {
269    fn new() -> Self {
270        Self {
271            builder: glib::object::Object::builder(),
272        }
273    }
274
275    /// The collation method to use for sorting.
276    ///
277    /// The `GTK_COLLATION_NONE` value is useful when the expression already
278    /// returns collation keys, or strings that need to be compared byte-by-byte.
279    ///
280    /// The default value, `GTK_COLLATION_UNICODE`, compares strings according
281    /// to the [Unicode collation algorithm](https://www.unicode.org/reports/tr10/).
282    #[cfg(feature = "v4_10")]
283    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
284    pub fn collation(self, collation: Collation) -> Self {
285        Self {
286            builder: self.builder.property("collation", collation),
287        }
288    }
289
290    /// The expression to evaluate on item to get a string to compare with.
291    pub fn expression(self, expression: impl AsRef<Expression>) -> Self {
292        Self {
293            builder: self
294                .builder
295                .property("expression", expression.as_ref().clone()),
296        }
297    }
298
299    /// If sorting is case sensitive.
300    pub fn ignore_case(self, ignore_case: bool) -> Self {
301        Self {
302            builder: self.builder.property("ignore-case", ignore_case),
303        }
304    }
305
306    // rustdoc-stripper-ignore-next
307    /// Build the [`StringSorter`].
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) -> StringSorter {
310        assert_initialized_main_thread!();
311        self.builder.build()
312    }
313}