aboutsummaryrefslogtreecommitdiff
path: root/src/slider.rs
blob: 11639f206f033863ca7339950da64052990600ee (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
59
60
61
use iced_widget::core::{Background, Color, border};
use iced_widget::slider::{
    Catalog, Handle, HandleShape, Rail, Status, Style, StyleFn,
};

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

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

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

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

pub fn styled(left: Color, right: Color, handle_radius: f32) -> Style {
    Style {
        rail: Rail {
            backgrounds: (left.into(), right.into()),
            width: 8.0,
            border: border::rounded(9999),
        },
        handle: Handle {
            shape: HandleShape::Circle {
                radius: handle_radius,
            },
            background: Background::Color(left),
            border_width: 0.0,
            border_color: Color::TRANSPARENT,
        },
    }
}

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

    match status {
        Status::Active => styled(primary.color, secondary.container, 12.0),
        Status::Hovered => styled(
            mix(primary.color, surface.text, HOVERED_LAYER_OPACITY),
            secondary.container,
            12.0,
        ),
        Status::Dragged => styled(
            mix(primary.color, surface.text, PRESSED_LAYER_OPACITY),
            secondary.container,
            11.0,
        ),
    }
}