aboutsummaryrefslogtreecommitdiff
path: root/src/radio.rs
blob: fcd68561619e7b13c1cdc99ec39a598a91a0050b (plain)
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
use iced_widget::core::{Background, Color};
use iced_widget::radio::{Catalog, Status, Style, StyleFn};

use super::Theme;
use crate::utils::{HOVERED_LAYER_OPACITY, disabled_text, mix};

impl Catalog for Theme {
    type Class<'a> = StyleFn<'a, Self>;

    fn default<'a>() -> Self::Class<'a> {
        Box::new(default)
    }

    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
        class(self, status)
    }
}

pub fn default(theme: &Theme, status: Status) -> Style {
    let surface = theme.colors().surface;
    let primary = theme.colors().primary;

    let active = Style {
        background: Color::TRANSPARENT.into(),
        dot_color: primary.color,
        border_width: 1.0,
        border_color: primary.color,
        text_color: None,
    };

    match status {
        Status::Active { is_selected } => Style {
            border_color: if is_selected {
                active.border_color
            } else {
                surface.text
            },
            ..active
        },
        Status::Hovered { is_selected } => Style {
            dot_color: mix(primary.color, surface.text, HOVERED_LAYER_OPACITY),
            border_color: if is_selected {
                mix(primary.color, surface.text, HOVERED_LAYER_OPACITY)
            } else {
                disabled_text(surface.text)
            },
            background: Background::Color(if is_selected {
                Color {
                    a: HOVERED_LAYER_OPACITY,
                    ..surface.text
                }
            } else {
                Color::TRANSPARENT
            }),
            ..active
        },
    }
}