rosace_widgets/tree/
progress_bar.rs1use rosace_core::types::{Rect, Size};
2use rosace_render::Color;
3use super::{Widget, LayoutCtx, PaintCtx, avail_w};
4
5pub struct ProgressBar {
7 pub value: f32,
8 pub track_color: Color,
9 pub fill_color: Color,
10 pub height: f32,
11 pub radius: f32,
12 pub width: Option<f32>,
13}
14
15impl ProgressBar {
16 pub fn new(value: f32) -> Self {
17 Self {
18 value: value.clamp(0.0, 1.0),
19 track_color: Color::rgb(32, 35, 58),
20 fill_color: Color::rgb(110, 75, 210),
21 height: 6.0,
22 radius: 3.0,
23 width: None,
24 }
25 }
26 pub fn color(mut self, c: Color) -> Self { self.fill_color = c; self }
27 pub fn track_color(mut self, c: Color) -> Self { self.track_color = c; self }
28 pub fn height(mut self, h: f32) -> Self { self.height = h; self }
29 pub fn width(mut self, w: f32) -> Self { self.width = Some(w); self }
30}
31
32impl Widget for ProgressBar {
33 fn layout(&self, ctx: &LayoutCtx) -> Size {
34 let constraints = ctx.constraints;
35 Size {
36 width: self.width.unwrap_or(avail_w(constraints)),
37 height: self.height,
38 }
39 }
40
41 fn paint(&self, ctx: &mut PaintCtx) {
42 ctx.semantics(super::Semantics::new(rosace_core::Role::ProgressBar)
43 .value(format!("{:.0}%", self.value * 100.0)));
44 use super::container::draw_rounded_rect_pub;
45 let r = ctx.rect;
46 draw_rounded_rect_pub(ctx,r, self.track_color, self.radius);
48 if self.value > 0.001 {
50 let fill = Rect {
51 origin: r.origin,
52 size: Size { width: r.size.width * self.value, height: r.size.height },
53 };
54 draw_rounded_rect_pub(ctx,fill, self.fill_color, self.radius);
55 }
56 }
57}