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
use {
    crate::{
        CompoundStyle,
        ScrollBarStyle,
        StyledChar,
    },
    serde::{
        Deserialize,
        Serialize,
    },
};

/// A variable-complexity definition of a scrollbar,
/// allowing a simplified representation covering most
/// cases.
///
/// You should not use this enum unless you're writing
/// your own skin type Serialize/Deserialize impls.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ScrollBarStyleDef {
    Simple(StyledChar),
    Rich {
        track: StyledChar,
        thumb: StyledChar,
    },
}

impl From<&ScrollBarStyle> for ScrollBarStyleDef {
    fn from(sc: &ScrollBarStyle) -> Self {
        let simple = sc.track.nude_char() == sc.thumb.nude_char()
            && sc.track.get_bg().is_none()
            && sc.thumb.get_bg().is_none();
        if simple {
            Self::Simple(StyledChar::new(
                CompoundStyle::new(
                    sc.thumb.get_fg(),
                    sc.track.get_fg(),
                    Default::default(),
                ),
                sc.track.nude_char(),
            ))
        } else {
            Self::Rich {
                track: sc.track.clone(),
                thumb: sc.thumb.clone(),
            }
        }
    }
}

impl ScrollBarStyleDef {
    pub fn into_scrollbar_style(self) -> ScrollBarStyle {
        match self {
            Self::Simple(sc) => sc.into(),
            Self::Rich{ track, thumb } => ScrollBarStyle { track, thumb },
        }
    }
}