imgui-rs/src/progressbar.rs
Joonas Javanainen 109e232422
Abolish ImVec2/ImVec4 from safe APIs
[f32; 2] and [f32; 4] are now the canonical types
2019-06-28 00:05:10 +03:00

60 lines
1.7 KiB
Rust

#![warn(missing_docs)]
use std::marker::PhantomData;
use std::ptr;
use sys;
use super::{ImStr, Ui};
/// Progress bar widget.
#[must_use]
pub struct ProgressBar<'ui, 'p> {
fraction: f32,
size: [f32; 2],
overlay_text: Option<&'p ImStr>,
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> ProgressBar<'ui, 'p> {
/// 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(_: &Ui<'ui>, fraction: f32) -> Self {
ProgressBar {
fraction,
size: [-1.0, 0.0],
overlay_text: None,
_phantom: PhantomData,
}
}
/// Sets an optional text that will be drawn over the progress bar.
#[inline]
pub fn overlay_text(mut self, overlay_text: &'p ImStr) -> Self {
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. This has to be called after setting all parameters
/// of the progress bar, otherwise the it will not be shown.
pub fn build(self) {
unsafe {
sys::igProgressBar(
self.fraction,
self.size.into(),
self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()),
);
}
}
}