|
| 1 | +use crate::{ |
| 2 | + math::{vec2, Rect}, |
| 3 | + ui::{ElementState, Layout, Ui}, |
| 4 | +}; |
| 5 | + |
| 6 | +pub struct ProgressBar<'a> { |
| 7 | + label: &'a str, |
| 8 | + label_width: Option<f32>, |
| 9 | +} |
| 10 | + |
| 11 | +impl<'a> ProgressBar<'a> { |
| 12 | + pub const fn new() -> Self { |
| 13 | + ProgressBar { |
| 14 | + label: "", |
| 15 | + label_width: None, |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + pub const fn label<'b>(self, label: &'b str) -> ProgressBar<'b> { |
| 20 | + ProgressBar { label, ..self } |
| 21 | + } |
| 22 | + |
| 23 | + pub const fn label_width(self, width: f32) -> Self { |
| 24 | + Self { |
| 25 | + label_width: Some(width), |
| 26 | + ..self |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn ui(self, ui: &mut Ui, progress: f32, bar_text: &str) { |
| 31 | + let context = ui.get_active_window_context(); |
| 32 | + |
| 33 | + let size = vec2( |
| 34 | + context.window.cursor.area.w - context.style.margin * 3. - context.window.cursor.ident, |
| 35 | + 19., |
| 36 | + ); |
| 37 | + |
| 38 | + let pos = context.window.cursor.fit(size, Layout::Vertical); |
| 39 | + let label_width = self.label_width.unwrap_or(100.); |
| 40 | + let bar_width = size.x - label_width; |
| 41 | + let bar_progress_width = progress.clamp(0., 1.) * bar_width; |
| 42 | + |
| 43 | + let bar_background = Rect::new(pos.x, pos.y, bar_width, 20.); |
| 44 | + let bar_progress = Rect::new(pos.x, pos.y, bar_progress_width, 20.); |
| 45 | + |
| 46 | + // background bar |
| 47 | + context.window.painter.draw_rect( |
| 48 | + bar_background, |
| 49 | + None, |
| 50 | + context.style.progress_bar_style.color(ElementState { |
| 51 | + focused: context.focused, |
| 52 | + hovered: false, |
| 53 | + clicked: false, |
| 54 | + selected: false, |
| 55 | + }), |
| 56 | + ); |
| 57 | + // progress bar |
| 58 | + context.window.painter.draw_rect( |
| 59 | + bar_progress, |
| 60 | + None, |
| 61 | + context.style.progress_bar_style.color(ElementState { |
| 62 | + focused: context.focused, |
| 63 | + hovered: true, |
| 64 | + clicked: false, |
| 65 | + selected: false, |
| 66 | + }), |
| 67 | + ); |
| 68 | + // label |
| 69 | + context.window.painter.draw_element_label( |
| 70 | + &context.style.label_style, |
| 71 | + vec2(pos.x + bar_width + 10., pos.y + 2.), |
| 72 | + self.label, |
| 73 | + ElementState { |
| 74 | + focused: context.focused, |
| 75 | + ..Default::default() |
| 76 | + }, |
| 77 | + ); |
| 78 | + // bar text |
| 79 | + context.window.painter.draw_element_content( |
| 80 | + &context.style.label_style, |
| 81 | + vec2(pos.x, pos.y), |
| 82 | + vec2(bar_width, 20.), |
| 83 | + &bar_text.into(), |
| 84 | + ElementState { |
| 85 | + focused: context.focused, |
| 86 | + ..Default::default() |
| 87 | + }, |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl Ui { |
| 93 | + pub fn progress_bar(&mut self, label: &str, progress: f32) { |
| 94 | + ProgressBar::new().label(label).ui(self, progress, ""); |
| 95 | + } |
| 96 | +} |
0 commit comments