dear_imgui_rs/widget/
progress.rs1use crate::sys;
6use crate::ui::Ui;
7use std::borrow::Cow;
8
9fn assert_finite_f32(caller: &str, name: &str, value: f32) {
10 assert!(value.is_finite(), "{caller} {name} must be finite");
11}
12
13fn assert_finite_vec2(caller: &str, name: &str, value: [f32; 2]) {
14 assert!(
15 value[0].is_finite() && value[1].is_finite(),
16 "{caller} {name} must contain finite values"
17 );
18}
19
20impl Ui {
22 #[doc(alias = "ProgressBar")]
26 pub fn progress_bar(&self, fraction: f32) -> ProgressBar<'_> {
27 ProgressBar::new(self, fraction)
28 }
29
30 #[doc(alias = "ProgressBar")]
32 pub fn progress_bar_with_overlay<'ui>(
33 &'ui self,
34 fraction: f32,
35 overlay: impl Into<Cow<'ui, str>>,
36 ) -> ProgressBar<'ui> {
37 ProgressBar::new(self, fraction).overlay_text(overlay)
38 }
39}
40
41#[derive(Clone, Debug)]
55#[must_use]
56pub struct ProgressBar<'ui> {
57 fraction: f32,
58 size: [f32; 2],
59 overlay_text: Option<Cow<'ui, str>>,
60 ui: &'ui Ui,
61}
62
63impl<'ui> ProgressBar<'ui> {
64 #[inline]
70 #[doc(alias = "ProgressBar")]
71 pub fn new(ui: &'ui Ui, fraction: f32) -> Self {
72 ProgressBar {
73 fraction,
74 size: [-1.0, 0.0], overlay_text: None,
76 ui,
77 }
78 }
79
80 pub fn overlay_text(mut self, overlay_text: impl Into<Cow<'ui, str>>) -> Self {
82 self.overlay_text = Some(overlay_text.into());
83 self
84 }
85
86 #[inline]
91 pub fn size(mut self, size: impl Into<[f32; 2]>) -> Self {
92 self.size = size.into();
93 self
94 }
95
96 pub fn fraction(mut self, fraction: f32) -> Self {
98 self.fraction = fraction;
99 self
100 }
101
102 pub fn build(self) {
104 assert_finite_f32("ProgressBar::build()", "fraction", self.fraction);
105 assert_finite_vec2("ProgressBar::build()", "size", self.size);
106
107 let size_vec: sys::ImVec2 = self.size.into();
108 let overlay_ptr = self
109 .overlay_text
110 .as_deref()
111 .map_or(std::ptr::null(), |txt| self.ui.scratch_txt(txt));
112
113 unsafe {
114 sys::igProgressBar(self.fraction, size_vec, overlay_ptr);
115 }
116 }
117}