summaryrefslogtreecommitdiffstats
path: root/style/src/toggler.rs
blob: 5a1551237432bdd4ffe6af5175846fb9dbe908e5 (plain) (blame)
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
//! Show toggle controls using togglers.
use iced_core::Color;

/// The appearance of a toggler.
#[derive(Debug)]
pub struct Style {
    pub background: Color,
    pub background_border: Option<Color>,
    pub foreground: Color,
    pub foreground_border: Option<Color>,
}

/// A set of rules that dictate the style of a toggler.
pub trait StyleSheet {
    fn active(&self, is_active: bool) -> Style;

    fn hovered(&self, is_active: bool) -> Style;
}

struct Default;

impl StyleSheet for Default {
    fn active(&self, is_active: bool) -> Style {
        Style {
            background: if is_active {
                Color::from_rgb(0.0, 1.0, 0.0)
            } else {
                Color::from_rgb(0.7, 0.7, 0.7)
            },
            background_border: None,
            foreground: Color::WHITE,
            foreground_border: None,
        }
    }

    fn hovered(&self, is_active: bool) -> Style {
        Style {
            foreground: Color::from_rgb(0.95, 0.95, 0.95),
            ..self.active(is_active)
        }
    }
}

impl std::default::Default for Box<dyn StyleSheet> {
    fn default() -> Self {
        Box::new(Default)
    }
}

impl<T> From<T> for Box<dyn StyleSheet>
where
    T: 'static + StyleSheet,
{
    fn from(style: T) -> Self {
        Box::new(style)
    }
}