orbtk_utils/
visibility.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// Is used to control the visibility of a widget
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Visibility {
    /// The widget is visible.
    Visible,

    /// The widget will not be displayed, isn't taken into account in the
    /// layout pipeline. It **does** consume memory in the render buffer.
    Hidden,

    /// The widget isn't displayed but `is` rendered. Thus it **does
    /// not** consume space in the layout.
    Collapsed,
}

impl Default for Visibility {
    fn default() -> Visibility {
        Visibility::Visible
    }
}

// --- Conversions ---

impl From<&str> for Visibility {
    fn from(t: &str) -> Self {
        match t {
            "Hidden" | "hidden" => Visibility::Hidden,
            "Collapsed" | "collapsed" => Visibility::Collapsed,
            _ => Visibility::Visible,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_into() {
        let visibility: Visibility = "Hidden".into();
        assert_eq!(visibility, Visibility::Hidden);

        let visibility: Visibility = "hidden".into();
        assert_eq!(visibility, Visibility::Hidden);

        let visibility: Visibility = "Collapsed".into();
        assert_eq!(visibility, Visibility::Collapsed);

        let visibility: Visibility = "collapsed".into();
        assert_eq!(visibility, Visibility::Collapsed);

        let visibility: Visibility = "Visible".into();
        assert_eq!(visibility, Visibility::Visible);

        let visibility: Visibility = "visible".into();
        assert_eq!(visibility, Visibility::Visible);

        let visibility: Visibility = "other".into();
        assert_eq!(visibility, Visibility::Visible);
    }
}