Skip to main content

fractal/utils/
template_callbacks.rs

1//! Collection of GTK template callbacks.
2
3use gtk::glib;
4
5/// Struct used as a collection of GTK template callbacks.
6pub struct TemplateCallbacks {}
7
8#[gtk::template_callbacks(functions)]
9impl TemplateCallbacks {
10    /// Returns `true` when the given string is not empty.
11    #[template_callback]
12    pub fn string_not_empty(string: Option<&str>) -> bool {
13        !string.unwrap_or_default().is_empty()
14    }
15
16    /// Returns the contained string or an empty string.
17    #[template_callback]
18    pub fn unwrap_string_or_empty(string: Option<&str>) -> &str {
19        string.unwrap_or_default()
20    }
21
22    /// Returns `true` when the given `Option<glib::Object>` is `Some`.
23    #[template_callback]
24    pub fn object_is_some(obj: Option<&glib::Object>) -> bool {
25        obj.is_some()
26    }
27
28    /// Inverts the given boolean.
29    #[template_callback]
30    pub fn invert_boolean(boolean: bool) -> bool {
31        !boolean
32    }
33
34    /// Whether the given `guint` is equal to zero.
35    #[template_callback]
36    pub fn guint_is_zero(u: u32) -> bool {
37        u == 0
38    }
39
40    /// Returns logical AND of two boolean values.
41    #[template_callback]
42    pub fn logical_and(lhs: bool, rhs: bool) -> bool {
43        lhs && rhs
44    }
45
46    /// Returns logical OR of two boolean values.
47    #[template_callback]
48    pub fn logical_or(lhs: bool, rhs: bool) -> bool {
49        lhs || rhs
50    }
51
52    /// String ternary operator returns one of the two arguments based on truth
53    /// value of test.
54    #[template_callback]
55    pub fn ternary_string(test: bool, positive: String, negative: String) -> String {
56        if test { positive } else { negative }
57    }
58}