Skip to main content

agg_gui/widgets/
progress_bar.rs

1//! `ProgressBar` — a read-only horizontal progress indicator.
2
3use std::sync::Arc;
4
5use crate::color::Color;
6use crate::event::{Event, EventResult};
7use crate::geometry::{Rect, Size};
8use crate::draw_ctx::DrawCtx;
9use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
10use crate::text::Font;
11use crate::widget::Widget;
12
13const BAR_H: f64 = 18.0;
14const WIDGET_H: f64 = 24.0;
15
16/// A horizontal progress bar. `value` is in `[0.0, 1.0]`.
17pub struct ProgressBar {
18    bounds: Rect,
19    children: Vec<Box<dyn Widget>>, // always empty
20    base: WidgetBase,
21    value: f64,
22    show_text: bool,
23    font: Arc<Font>,
24    font_size: f64,
25    fill_color: Color,
26}
27
28impl ProgressBar {
29    pub fn new(value: f64, font: Arc<Font>) -> Self {
30        Self {
31            bounds: Rect::default(),
32            children: Vec::new(),
33            base: WidgetBase::new(),
34            value: value.clamp(0.0, 1.0),
35            show_text: true,
36            font,
37            font_size: 11.0,
38            fill_color: Color::rgb(0.22, 0.45, 0.88),
39        }
40    }
41
42    pub fn with_show_text(mut self, show: bool) -> Self { self.show_text = show; self }
43    pub fn with_fill_color(mut self, color: Color) -> Self { self.fill_color = color; self }
44
45    pub fn with_margin(mut self, m: Insets)    -> Self { self.base.margin   = m; self }
46    pub fn with_h_anchor(mut self, h: HAnchor) -> Self { self.base.h_anchor = h; self }
47    pub fn with_v_anchor(mut self, v: VAnchor) -> Self { self.base.v_anchor = v; self }
48    pub fn with_min_size(mut self, s: Size)    -> Self { self.base.min_size = s; self }
49    pub fn with_max_size(mut self, s: Size)    -> Self { self.base.max_size = s; self }
50
51    pub fn set_value(&mut self, v: f64) {
52        self.value = v.clamp(0.0, 1.0);
53    }
54
55    pub fn value(&self) -> f64 { self.value }
56}
57
58impl Widget for ProgressBar {
59    fn type_name(&self) -> &'static str { "ProgressBar" }
60    fn bounds(&self) -> Rect { self.bounds }
61    fn set_bounds(&mut self, b: Rect) { self.bounds = b; }
62    fn children(&self) -> &[Box<dyn Widget>] { &self.children }
63    fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> { &mut self.children }
64
65    fn margin(&self)   -> Insets  { self.base.margin }
66    fn h_anchor(&self) -> HAnchor { self.base.h_anchor }
67    fn v_anchor(&self) -> VAnchor { self.base.v_anchor }
68    fn min_size(&self) -> Size    { self.base.min_size }
69    fn max_size(&self) -> Size    { self.base.max_size }
70
71    fn layout(&mut self, available: Size) -> Size {
72        Size::new(available.width, WIDGET_H)
73    }
74
75    fn paint(&mut self, ctx: &mut dyn DrawCtx) {
76        let v = ctx.visuals();
77        let w = self.bounds.width;
78        let h = self.bounds.height;
79        let bar_y = (h - BAR_H) * 0.5;
80        let r = BAR_H * 0.5;
81
82        // Track
83        ctx.set_fill_color(v.track_bg);
84        ctx.begin_path();
85        ctx.rounded_rect(0.0, bar_y, w, BAR_H, r);
86        ctx.fill();
87
88        // Fill — use explicit fill_color if set, otherwise fall back to accent.
89        let fill_color = if self.fill_color != Color::rgb(0.22, 0.45, 0.88) {
90            self.fill_color
91        } else {
92            v.accent
93        };
94        let fill_w = (w * self.value).max(0.0);
95        if fill_w >= 1.0 {
96            ctx.set_fill_color(fill_color);
97            ctx.begin_path();
98            ctx.rounded_rect(0.0, bar_y, fill_w, BAR_H, r);
99            ctx.fill();
100        }
101
102        // Percentage text centered over bar
103        if self.show_text {
104            let label = format!("{:.0}%", self.value * 100.0);
105            ctx.set_font(Arc::clone(&self.font));
106            ctx.set_font_size(self.font_size);
107            // Text color: always use theme text contrasted against the bar.
108            let mid = w * 0.5;
109            let text_color = if fill_w > mid {
110                Color::rgba(1.0, 1.0, 1.0, 0.9)
111            } else {
112                v.text_dim
113            };
114            ctx.set_fill_color(text_color);
115            if let Some(m) = ctx.measure_text(&label) {
116                let tx = (w - m.width) * 0.5;
117                let ty = bar_y + BAR_H * 0.5 - (m.ascent - m.descent) * 0.5;
118                ctx.fill_text(&label, tx, ty);
119            }
120        }
121    }
122
123    fn on_event(&mut self, _: &Event) -> EventResult { EventResult::Ignored }
124}