imgui_ext/
progress.rs

1//!
2//! Works on `f32` and `Option<f32>`.
3//!
4//! # Optional params
5//!
6//! * `overlay` override default overlay text.
7//! * `size` path to a function that returns the size.
8//! * `map` Applies a mapping function to `&mut Self`.
9//!
10//! ## Example
11//!
12//! ```
13//! #[derive(imgui_ext::Gui)]
14//! struct Progress {
15//!     #[imgui(progress)]
16//!     progress: f32,
17//!     #[imgui(progress)]
18//!     _progress: f32,
19//! }
20//! ```
21//!
22//! ### Result
23//!
24//! ![][result]
25//!
26//! [result]: https://i.imgur.com/SyaN1Nt.png
27use imgui::{ImStr, ProgressBar, Ui};
28
29pub struct ProgressParams<'a> {
30    pub overlay: Option<&'a ImStr>,
31    pub size: Option<[f32; 2]>,
32}
33
34pub trait Progress {
35    fn build(ui: &Ui, elem: &Self, params: ProgressParams);
36}
37
38impl Progress for f32 {
39    fn build(ui: &Ui, elem: &Self, params: ProgressParams) {
40        let mut pro = ProgressBar::new(ui, *elem);
41        if let Some(overlay) = params.overlay {
42            pro = pro.overlay_text(overlay);
43        }
44        if let Some(size) = params.size {
45            pro = pro.size(size);
46        }
47        pro.build();
48    }
49}
50
51impl<T: Progress> Progress for Box<T> {
52    #[inline]
53    fn build(ui: &Ui, elem: &Self, params: ProgressParams) {
54        T::build(ui, elem, params)
55    }
56}
57
58impl<T: Progress> Progress for Option<T> {
59    #[inline]
60    fn build(ui: &Ui, elem: &Self, params: ProgressParams) {
61        if let Some(elem) = elem {
62            T::build(ui, elem, params)
63        }
64    }
65}