dear_imgui_rs/widget/
progress.rs

1//! Progress bars
2//!
3//! Simple progress indicators with size and overlay text customization.
4//!
5use crate::sys;
6use crate::ui::Ui;
7
8/// # Progress Bar Widgets
9impl Ui {
10    /// Creates a progress bar widget.
11    ///
12    /// The fraction should be between 0.0 (0%) and 1.0 (100%).
13    #[doc(alias = "ProgressBar")]
14    pub fn progress_bar(&self, fraction: f32) -> ProgressBar<'_> {
15        ProgressBar::new(self, fraction)
16    }
17
18    /// Creates a progress bar with overlay text.
19    #[doc(alias = "ProgressBar")]
20    pub fn progress_bar_with_overlay(
21        &self,
22        fraction: f32,
23        overlay: impl AsRef<str>,
24    ) -> ProgressBar<'_> {
25        ProgressBar::new(self, fraction).overlay_text(overlay)
26    }
27}
28
29/// Builder for a progress bar widget.
30///
31/// # Examples
32///
33/// ```no_run
34/// # use dear_imgui_rs::*;
35/// # let mut ctx = Context::create();
36/// # let ui = ctx.frame();
37/// ui.progress_bar(0.6)
38///     .size([100.0, 12.0])
39///     .overlay_text("Progress!")
40///     .build();
41/// ```
42#[derive(Clone, Debug)]
43#[must_use]
44pub struct ProgressBar<'ui> {
45    fraction: f32,
46    size: [f32; 2],
47    overlay_text: Option<String>,
48    ui: &'ui Ui,
49}
50
51impl<'ui> ProgressBar<'ui> {
52    /// Creates a progress bar with a given fraction showing
53    /// the progress (0.0 = 0%, 1.0 = 100%).
54    ///
55    /// The progress bar will be automatically sized to fill the entire width of the window if no
56    /// custom size is specified.
57    #[inline]
58    #[doc(alias = "ProgressBar")]
59    pub fn new(ui: &'ui Ui, fraction: f32) -> Self {
60        ProgressBar {
61            fraction,
62            size: [-1.0, 0.0], // -1.0 means auto-size to fill width
63            overlay_text: None,
64            ui,
65        }
66    }
67
68    /// Sets an optional text that will be drawn over the progress bar.
69    pub fn overlay_text(mut self, overlay_text: impl AsRef<str>) -> Self {
70        self.overlay_text = Some(overlay_text.as_ref().to_string());
71        self
72    }
73
74    /// Sets the size of the progress bar.
75    ///
76    /// Negative values will automatically align to the end of the axis, zero will let the progress
77    /// bar choose a size, and positive values will use the given size.
78    #[inline]
79    pub fn size(mut self, size: impl Into<[f32; 2]>) -> Self {
80        self.size = size.into();
81        self
82    }
83
84    /// Sets the progress fraction (0.0 to 1.0)
85    pub fn fraction(mut self, fraction: f32) -> Self {
86        self.fraction = fraction;
87        self
88    }
89
90    /// Builds the progress bar
91    pub fn build(self) {
92        let size_vec: sys::ImVec2 = self.size.into();
93        let overlay_ptr = self
94            .overlay_text
95            .as_ref()
96            .map(|s| self.ui.scratch_txt(s))
97            .unwrap_or(std::ptr::null());
98
99        unsafe {
100            sys::igProgressBar(self.fraction, size_vec, overlay_ptr);
101        }
102    }
103}