use eframe::egui;
pub struct ProgressBar {
current: usize,
total: usize,
width: f32,
height: f32,
fill_color: egui::Color32,
}
impl ProgressBar {
pub fn new(width: f32, height: f32) -> Self {
Self::with_color(width, height, egui::Color32::from_rgb(178, 178, 178))
}
pub fn with_color(width: f32, height: f32, fill_color: egui::Color32) -> Self {
Self {
current: 0,
total: 0,
width,
height,
fill_color,
}
}
pub fn set_progress(&mut self, current: usize, total: usize) {
self.current = current;
self.total = total;
}
pub fn render(&self, ui: &mut egui::Ui) {
let progress = if self.total > 0 {
self.current as f32 / self.total as f32
} else {
0.0
};
let (rect, _response) =
ui.allocate_exact_size(egui::vec2(self.width, self.height), egui::Sense::hover());
let bg_color = egui::Color32::from_gray(40);
ui.painter().rect_filled(
rect, 2.0, bg_color,
);
if progress > 0.0 {
let fill_width = rect.width() * progress.clamp(0.0, 1.0);
let fill_rect =
egui::Rect::from_min_size(rect.min, egui::vec2(fill_width, rect.height()));
ui.painter().rect_filled(
fill_rect,
2.0, self.fill_color,
);
}
let text = if self.total > 0 {
format!("{}/{}", self.current, self.total)
} else {
"0/0".to_string()
};
let text_color = egui::Color32::from_gray(220);
let font_id = egui::FontId::monospace(9.0);
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
text,
font_id,
text_color,
);
}
}