Skip to content

Commit

Permalink
Allow changing ProgressBar fill color (#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass authored Jan 23, 2023
1 parent d4f9f69 commit 518b4f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
* Add `ScrollArea::drag_to_scroll` if you want to turn off that feature.
* Add `Response::on_hover_and_drag_cursor`.
* Add `Window::default_open` ([#2539](https://github.com/emilk/egui/pull/2539))
* Add `ProgressBar::fill` if you want to set the fill color manually. ([#2618](https://github.com/emilk/egui/pull/2618))
* Add `Button::rounding` to enable round buttons ([#2539](https://github.com/emilk/egui/pull/2539))

### Changed 🔧
Expand Down
13 changes: 12 additions & 1 deletion crates/egui/src/widgets/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct ProgressBar {
progress: f32,
desired_width: Option<f32>,
text: Option<ProgressBarText>,
fill: Option<Color32>,
animate: bool,
}

Expand All @@ -23,6 +24,7 @@ impl ProgressBar {
progress: progress.clamp(0.0, 1.0),
desired_width: None,
text: None,
fill: None,
animate: false,
}
}
Expand All @@ -33,6 +35,12 @@ impl ProgressBar {
self
}

/// The fill color of the bar.
pub fn fill(mut self, color: Color32) -> Self {
self.fill = Some(color);
self
}

/// A custom text to display on the progress bar.
pub fn text(mut self, text: impl Into<WidgetText>) -> Self {
self.text = Some(ProgressBarText::Custom(text.into()));
Expand Down Expand Up @@ -60,6 +68,7 @@ impl Widget for ProgressBar {
progress,
desired_width,
text,
fill,
animate,
} = self;

Expand Down Expand Up @@ -98,7 +107,9 @@ impl Widget for ProgressBar {
ui.painter().rect(
inner_rect,
rounding,
Color32::from(Rgba::from(visuals.selection.bg_fill) * color_factor as f32),
Color32::from(
Rgba::from(fill.unwrap_or(visuals.selection.bg_fill)) * color_factor as f32,
),
Stroke::NONE,
);

Expand Down

0 comments on commit 518b4f4

Please sign in to comment.