arcdps_imgui\widget/progress_bar.rs
1use crate::sys;
2// use crate::ImStr;
3use crate::Ui;
4
5/// Builder for a progress bar widget.
6///
7/// # Examples
8///
9/// ```no_run
10/// # use arcdps_imgui::*;
11/// # let mut imgui = Context::create();
12/// # let ui = imgui.frame();
13/// ProgressBar::new(0.6)
14/// .size([100.0, 12.0])
15/// .overlay_text("Progress!")
16/// .build(&ui);
17/// ```
18#[derive(Copy, Clone, Debug)]
19#[must_use]
20pub struct ProgressBar<T = &'static str> {
21 fraction: f32,
22 size: [f32; 2],
23 overlay_text: Option<T>,
24}
25
26impl ProgressBar {
27 /// Creates a progress bar with a given fraction showing
28 /// the progress (0.0 = 0%, 1.0 = 100%).
29 ///
30 /// The progress bar will be automatically sized to fill the entire width of the window if no
31 /// custom size is specified.
32 #[inline]
33 #[doc(alias = "ProgressBar")]
34 pub fn new(fraction: f32) -> Self {
35 ProgressBar {
36 fraction,
37 size: [-1.0, 0.0],
38 overlay_text: None,
39 }
40 }
41}
42
43impl<T: AsRef<str>> ProgressBar<T> {
44 /// Sets an optional text that will be drawn over the progress bar.
45 pub fn overlay_text<T2: AsRef<str>>(self, overlay_text: T2) -> ProgressBar<T2> {
46 ProgressBar {
47 fraction: self.fraction,
48 size: self.size,
49 overlay_text: Some(overlay_text),
50 }
51 }
52
53 /// Sets the size of the progress bar.
54 ///
55 /// Negative values will automatically align to the end of the axis, zero will let the progress
56 /// bar choose a size, and positive values will use the given size.
57 #[inline]
58 pub fn size(mut self, size: [f32; 2]) -> Self {
59 self.size = size;
60 self
61 }
62
63 /// Builds the progress bar
64 pub fn build(self, ui: &Ui<'_>) {
65 unsafe {
66 sys::igProgressBar(
67 self.fraction,
68 self.size.into(),
69 ui.scratch_txt_opt(self.overlay_text),
70 );
71 }
72 }
73}