|
| 1 | +use crate::*; |
| 2 | + |
| 3 | +enum ProgressBarText { |
| 4 | + Custom(String), |
| 5 | + Percentage, |
| 6 | +} |
| 7 | + |
| 8 | +/// A simple progress bar. |
| 9 | +pub struct ProgressBar { |
| 10 | + progress: f32, |
| 11 | + desired_width: Option<f32>, |
| 12 | + text: Option<ProgressBarText>, |
| 13 | + animate: bool, |
| 14 | +} |
| 15 | + |
| 16 | +impl ProgressBar { |
| 17 | + /// Progress in the `[0, 1]` range, where `1` means "completed". |
| 18 | + pub fn new(progress: f32) -> Self { |
| 19 | + Self { |
| 20 | + progress: progress.clamp(0.0, 1.0), |
| 21 | + desired_width: None, |
| 22 | + text: None, |
| 23 | + animate: false, |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /// The desired width of the bar. Will use all horizonal space if not set. |
| 28 | + pub fn desired_width(mut self, desired_width: f32) -> Self { |
| 29 | + self.desired_width = Some(desired_width); |
| 30 | + self |
| 31 | + } |
| 32 | + |
| 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())); |
| 37 | + self |
| 38 | + } |
| 39 | + |
| 40 | + /// Show the progress in percent on the progress bar. |
| 41 | + pub fn show_percentage(mut self) -> Self { |
| 42 | + self.text = Some(ProgressBarText::Percentage); |
| 43 | + self |
| 44 | + } |
| 45 | + |
| 46 | + /// Whether to display a loading animation when progress `< 1`. |
| 47 | + /// Note that this require the UI to be redrawn. |
| 48 | + /// Defaults to `false`. |
| 49 | + pub fn animate(mut self, animate: bool) -> Self { |
| 50 | + self.animate = animate; |
| 51 | + self |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl Widget for ProgressBar { |
| 56 | + fn ui(self, ui: &mut Ui) -> Response { |
| 57 | + let ProgressBar { |
| 58 | + progress, |
| 59 | + desired_width, |
| 60 | + text, |
| 61 | + mut animate, |
| 62 | + } = self; |
| 63 | + |
| 64 | + animate &= progress < 1.0; |
| 65 | + |
| 66 | + let desired_width = desired_width.unwrap_or(ui.available_size_before_wrap().x); |
| 67 | + let height = ui.spacing().interact_size.y; |
| 68 | + let (outer_rect, response) = |
| 69 | + ui.allocate_exact_size(vec2(desired_width, height), Sense::hover()); |
| 70 | + let visuals = ui.style().visuals.clone(); |
| 71 | + let corner_radius = outer_rect.height() / 2.0; |
| 72 | + ui.painter().rect( |
| 73 | + outer_rect, |
| 74 | + corner_radius, |
| 75 | + visuals.extreme_bg_color, |
| 76 | + Stroke::none(), |
| 77 | + ); |
| 78 | + let inner_rect = Rect::from_min_size( |
| 79 | + outer_rect.min, |
| 80 | + vec2( |
| 81 | + (outer_rect.width() * progress).at_least(outer_rect.height()), |
| 82 | + outer_rect.height(), |
| 83 | + ), |
| 84 | + ); |
| 85 | + |
| 86 | + let (dark, bright) = (0.7, 1.0); |
| 87 | + let color_factor = if animate { |
| 88 | + ui.ctx().request_repaint(); |
| 89 | + lerp(dark..=bright, ui.input().time.cos().abs()) |
| 90 | + } else { |
| 91 | + bright |
| 92 | + }; |
| 93 | + |
| 94 | + ui.painter().rect( |
| 95 | + inner_rect, |
| 96 | + corner_radius, |
| 97 | + Color32::from(Rgba::from(visuals.selection.bg_fill) * color_factor as f32), |
| 98 | + Stroke::none(), |
| 99 | + ); |
| 100 | + |
| 101 | + if animate { |
| 102 | + let n_points = 20; |
| 103 | + let start_angle = ui.input().time as f64 * 360f64.to_radians(); |
| 104 | + let end_angle = start_angle + 240f64.to_radians() * ui.input().time.sin(); |
| 105 | + let circle_radius = corner_radius - 2.0; |
| 106 | + let points: Vec<Pos2> = (0..n_points) |
| 107 | + .map(|i| { |
| 108 | + let angle = lerp(start_angle..=end_angle, i as f64 / n_points as f64); |
| 109 | + let (sin, cos) = angle.sin_cos(); |
| 110 | + inner_rect.right_center() |
| 111 | + + circle_radius * vec2(cos as f32, sin as f32) |
| 112 | + + vec2(-corner_radius, 0.0) |
| 113 | + }) |
| 114 | + .collect(); |
| 115 | + ui.painter().add(Shape::Path { |
| 116 | + points, |
| 117 | + closed: false, |
| 118 | + fill: Color32::TRANSPARENT, |
| 119 | + stroke: Stroke::new(2.0, visuals.faint_bg_color), |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + if let Some(text_kind) = text { |
| 124 | + let text = match text_kind { |
| 125 | + ProgressBarText::Custom(string) => string, |
| 126 | + ProgressBarText::Percentage => format!("{}%", (progress * 100.0) as usize), |
| 127 | + }; |
| 128 | + ui.painter().sub_region(outer_rect).text( |
| 129 | + outer_rect.left_center() + vec2(ui.spacing().item_spacing.x, 0.0), |
| 130 | + Align2::LEFT_CENTER, |
| 131 | + text, |
| 132 | + TextStyle::Button, |
| 133 | + visuals |
| 134 | + .override_text_color |
| 135 | + .unwrap_or(visuals.selection.stroke.color), |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + response |
| 140 | + } |
| 141 | +} |
0 commit comments