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
62
63
64
65
66
67
use std::ptr;

use crate::string::ImStr;
use crate::sys;
use crate::Ui;

/// Builder for a progress bar widget.
///
/// # Examples
///
/// ```no_run
/// # use imgui::*;
/// # let mut imgui = Context::create();
/// # let ui = imgui.frame();
/// ProgressBar::new(0.6)
///     .size([100.0, 12.0])
///     .overlay_text(im_str!("Progress!"))
///     .build(&ui);
/// ```
#[derive(Copy, Clone, Debug)]
#[must_use]
pub struct ProgressBar<'a> {
    fraction: f32,
    size: [f32; 2],
    overlay_text: Option<&'a ImStr>,
}

impl<'a> ProgressBar<'a> {
    /// Creates a progress bar with a given fraction showing
    /// the progress (0.0 = 0%, 1.0 = 100%).
    ///
    /// The progress bar will be automatically sized to fill the entire width of the window if no
    /// custom size is specified.
    pub fn new(fraction: f32) -> ProgressBar<'a> {
        ProgressBar {
            fraction,
            size: [-1.0, 0.0],
            overlay_text: None,
        }
    }
    /// Sets an optional text that will be drawn over the progress bar.
    #[inline]
    pub fn overlay_text(mut self, overlay_text: &'a ImStr) -> ProgressBar {
        self.overlay_text = Some(overlay_text);
        self
    }

    /// Sets the size of the progress bar.
    ///
    /// Negative values will automatically align to the end of the axis, zero will let the progress
    /// bar choose a size, and positive values will use the given size.
    #[inline]
    pub fn size(mut self, size: [f32; 2]) -> Self {
        self.size = size;
        self
    }
    /// Builds the progress bar
    pub fn build(self, _: &Ui) {
        unsafe {
            sys::igProgressBar(
                self.fraction,
                self.size.into(),
                self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()),
            );
        }
    }
}