Skip to main content

material_ui_rs/design/style/
radio.rs

1use iced_widget::core::{Background, Color};
2use iced_widget::radio::{Catalog, Status, Style, StyleFn};
3
4use crate::Theme;
5use crate::tokens;
6use crate::utils::{HOVERED_LAYER_OPACITY, state_layer};
7
8impl Catalog for Theme {
9    type Class<'a> = StyleFn<'a, Self>;
10
11    fn default<'a>() -> Self::Class<'a> {
12        Box::new(default)
13    }
14
15    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
16        class(self, status)
17    }
18}
19
20pub fn default(theme: &Theme, status: Status) -> Style {
21    let surface = theme.colors().surface;
22    let primary = theme.colors().primary;
23
24    let active = Style {
25        background: Color::TRANSPARENT.into(),
26        dot_color: primary.color,
27        border_width: tokens::component::radio::OUTER_RING_WIDTH,
28        border_color: primary.color,
29        text_color: None,
30    };
31
32    match status {
33        Status::Active { is_selected } => Style {
34            border_color: if is_selected {
35                primary.color
36            } else {
37                surface.text_variant
38            },
39            ..active
40        },
41        Status::Hovered { is_selected } => Style {
42            background: Background::Color(state_layer(
43                if is_selected {
44                    primary.color
45                } else {
46                    surface.text
47                },
48                HOVERED_LAYER_OPACITY,
49            )),
50            dot_color: primary.color,
51            border_color: if is_selected {
52                primary.color
53            } else {
54                surface.text
55            },
56            ..active
57        },
58    }
59}
60
61#[cfg(test)]
62#[path = "../../../tests/design/style/radio.rs"]
63mod tests;