1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use eframe::egui;
/// Progress bar widget for displaying loading progress
pub struct ProgressBar {
current: usize,
total: usize,
width: f32,
height: f32,
fill_color: egui::Color32,
}
impl ProgressBar {
/// Create new progress bar with specified dimensions
/// Default fill color: light gray (0.7, 0.7, 0.7)
pub fn new(width: f32, height: f32) -> Self {
Self::with_color(width, height, egui::Color32::from_rgb(178, 178, 178))
}
/// Create new progress bar with custom fill color
pub fn with_color(width: f32, height: f32, fill_color: egui::Color32) -> Self {
Self {
current: 0,
total: 0,
width,
height,
fill_color,
}
}
/// Update progress values
pub fn set_progress(&mut self, current: usize, total: usize) {
self.current = current;
self.total = total;
}
/// Render progress bar
pub fn render(&self, ui: &mut egui::Ui) {
// Calculate progress percentage
let progress = if self.total > 0 {
self.current as f32 / self.total as f32
} else {
0.0
};
// Reserve space for the progress bar
let (rect, _response) = ui.allocate_exact_size(
egui::vec2(self.width, self.height),
egui::Sense::hover(),
);
// Draw background (dark)
let bg_color = egui::Color32::from_gray(40);
ui.painter().rect_filled(
rect,
2.0, // rounding
bg_color,
);
// Draw progress fill
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, // rounding
self.fill_color,
);
}
// Draw text overlay (percentage or count)
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,
);
}
}