Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing ProgressBar fill color #2618

Merged
emilk:master from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow changing ProgressBar fill color
  • Loading branch information
Uriopass committed Jan 23, 2023
commit 3a4e5010561d5e7f0b182d544c783d88df2ae94f
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.

### Changed 🔧
* Improved plot grid appearance ([#2412](https://github.com/emilk/egui/pull/2412)).
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