|
1 | 1 | use crate::*;
|
2 | 2 |
|
3 | 3 | enum ProgressBarText {
|
4 |
| - Custom(String), |
| 4 | + Custom(WidgetText), |
5 | 5 | Percentage,
|
6 | 6 | }
|
7 | 7 |
|
@@ -31,9 +31,8 @@ impl ProgressBar {
|
31 | 31 | }
|
32 | 32 |
|
33 | 33 | /// A custom text to display on the progress bar.
|
34 |
| - #[allow(clippy::needless_pass_by_value)] |
35 |
| - pub fn text(mut self, text: impl ToString) -> Self { |
36 |
| - self.text = Some(ProgressBarText::Custom(text.to_string())); |
| 34 | + pub fn text(mut self, text: impl Into<WidgetText>) -> Self { |
| 35 | + self.text = Some(ProgressBarText::Custom(text.into())); |
37 | 36 | self
|
38 | 37 | }
|
39 | 38 |
|
@@ -124,18 +123,16 @@ impl Widget for ProgressBar {
|
124 | 123 |
|
125 | 124 | if let Some(text_kind) = text {
|
126 | 125 | let text = match text_kind {
|
127 |
| - ProgressBarText::Custom(string) => string, |
128 |
| - ProgressBarText::Percentage => format!("{}%", (progress * 100.0) as usize), |
| 126 | + ProgressBarText::Custom(text) => text, |
| 127 | + ProgressBarText::Percentage => format!("{}%", (progress * 100.0) as usize).into(), |
129 | 128 | };
|
130 |
| - ui.painter().sub_region(outer_rect).text( |
131 |
| - outer_rect.left_center() + vec2(ui.spacing().item_spacing.x, 0.0), |
132 |
| - Align2::LEFT_CENTER, |
133 |
| - text, |
134 |
| - TextStyle::Button, |
135 |
| - visuals |
136 |
| - .override_text_color |
137 |
| - .unwrap_or(visuals.selection.stroke.color), |
138 |
| - ); |
| 129 | + let galley = text.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Button); |
| 130 | + let text_pos = outer_rect.left_center() - Vec2::new(0.0, galley.size().y / 2.0) |
| 131 | + + vec2(ui.spacing().item_spacing.x, 0.0); |
| 132 | + let text_color = visuals |
| 133 | + .override_text_color |
| 134 | + .unwrap_or(visuals.selection.stroke.color); |
| 135 | + galley.paint_with_color(&ui.painter().sub_region(outer_rect), text_pos, text_color); |
139 | 136 | }
|
140 | 137 |
|
141 | 138 | response
|
|
0 commit comments