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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Determinate progress bar with an in-bar percent label.
//!
//! The bar is a rounded pill: a dim track plus an accent-coloured fill
//! that grows from the left. Text inside the bar renders in two colours
//! — bright over the filled region, muted over the empty region — so the
//! label stays legible regardless of the fill level.
use egui::{
Color32, CornerRadius, Pos2, Rect, Response, Sense, Stroke, StrokeKind, Ui, Vec2, Widget,
WidgetInfo, WidgetType,
};
use crate::theme::{Accent, Theme};
/// A horizontal determinate progress bar.
///
/// ```no_run
/// # use elegance::{Accent, ProgressBar};
/// # egui::__run_test_ui(|ui| {
/// ui.add(ProgressBar::new(0.42));
/// ui.add(ProgressBar::new(0.8).accent(Accent::Green).text("Uploading…"));
/// # });
/// ```
#[derive(Debug, Clone)]
#[must_use = "Add with `ui.add(...)`."]
pub struct ProgressBar {
fraction: f32,
height: f32,
desired_width: Option<f32>,
color: Option<Color32>,
accent: Option<Accent>,
text: Option<String>,
}
impl ProgressBar {
/// Create a progress bar at `fraction` (0..=1). NaN and out-of-range
/// values are clamped.
pub fn new(fraction: f32) -> Self {
Self {
fraction: if fraction.is_nan() {
0.0
} else {
fraction.clamp(0.0, 1.0)
},
height: 22.0,
desired_width: None,
color: None,
accent: None,
text: None,
}
}
/// Bar height in points. Default: 22.
pub fn height(mut self, height: f32) -> Self {
self.height = height;
self
}
/// Override the bar width. Defaults to `ui.available_width()`.
pub fn desired_width(mut self, width: f32) -> Self {
self.desired_width = Some(width);
self
}
/// Paint the fill with an explicit colour. Clears any previously set accent.
pub fn color(mut self, color: Color32) -> Self {
self.color = Some(color);
self.accent = None;
self
}
/// Pick the fill colour from one of the theme's accents. Clears any
/// previously set explicit colour.
pub fn accent(mut self, accent: Accent) -> Self {
self.accent = Some(accent);
self.color = None;
self
}
/// Override the in-bar text. By default the bar shows the rounded
/// percent (e.g. "42%"); passing `""` hides the text entirely.
pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
}
impl Widget for ProgressBar {
fn ui(self, ui: &mut Ui) -> Response {
let theme = Theme::current(ui.ctx());
let p = &theme.palette;
let fill_color = match (self.color, self.accent) {
(Some(c), _) => c,
(_, Some(a)) => p.accent_fill(a),
_ => p.sky,
};
let width = self
.desired_width
.unwrap_or_else(|| ui.available_width())
.max(self.height * 2.0);
let (rect, response) =
ui.allocate_exact_size(Vec2::new(width, self.height), Sense::hover());
if ui.is_rect_visible(rect) {
let painter = ui.painter();
let radius = CornerRadius::same((self.height * 0.5).round() as u8);
// Track: input-bg with a subtle border.
painter.rect(
rect,
radius,
p.input_bg,
Stroke::new(1.0, p.border),
StrokeKind::Inside,
);
// Fill clipped from the left.
let fill_w = rect.width() * self.fraction;
let fill_rect = Rect::from_min_size(rect.min, Vec2::new(fill_w, rect.height()));
if fill_w > 0.5 {
painter
.with_clip_rect(fill_rect)
.rect_filled(rect, radius, fill_color);
}
// Label.
let label = match self.text.as_deref() {
Some(s) => s.to_owned(),
None => format!("{}%", (self.fraction * 100.0).round() as u32),
};
if !label.is_empty() {
let font_size = (self.height * 0.55).max(11.0);
let galley =
crate::theme::placeholder_galley(ui, &label, font_size, true, f32::INFINITY);
let text_pos = Pos2::new(
rect.center().x - galley.size().x * 0.5,
rect.center().y - galley.size().y * 0.5,
);
// Muted pass over the empty region.
let empty_rect =
Rect::from_min_max(Pos2::new(rect.min.x + fill_w, rect.min.y), rect.max);
painter
.with_clip_rect(empty_rect)
.galley(text_pos, galley.clone(), p.text_muted);
// Bright pass over the filled region.
painter
.with_clip_rect(fill_rect)
.galley(text_pos, galley, Color32::WHITE);
}
}
response
.widget_info(|| WidgetInfo::labeled(WidgetType::ProgressIndicator, true, "progress"));
response
}
}