summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/widget/progressbar.rs
blob: f621343de8c470c81d56837e553dbb06a7468e8c (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
use crate::{Primitive, Renderer};
use iced_native::{progressbar, Background, Color, MouseCursor, Rectangle};

impl progressbar::Renderer for Renderer {
    fn height(&self) -> u32 {
        30
    }

    fn draw(
        &self,
        bounds: Rectangle,
        range: std::ops::RangeInclusive<f32>,
        value: f32,
        background: Option<Background>,
        active_color: Option<Color>,
    ) -> Self::Output {
        let (range_start, range_end) = range.into_inner();
        let active_progress_width = bounds.width
            * ((value - range_start) / (range_end - range_start).max(1.0));

        let background = Primitive::Group {
            primitives: vec![Primitive::Quad {
                bounds: Rectangle { ..bounds },
                background: background
                    .unwrap_or(Background::Color([0.6, 0.6, 0.6].into()))
                    .into(),
                border_radius: 5,
            }],
        };

        let active_progress = Primitive::Quad {
            bounds: Rectangle {
                width: active_progress_width,
                ..bounds
            },
            background: Background::Color(
                active_color.unwrap_or([0.0, 0.95, 0.0].into()),
            ),
            border_radius: 5,
        };

        (
            Primitive::Group {
                primitives: vec![background, active_progress],
            },
            MouseCursor::OutOfBounds,
        )
    }
}