Animated Progress Bar in Rust egui — Download Manager App | Ep 15
Video: Animated Progress Bar in Rust egui — Download Manager App | Ep 15 by Taught by Celeste AI - AI Coding Coach
Watch full page →Animated Progress Bar in Rust egui — Download Manager App
Learn how to create a smooth, animated progress bar in Rust using the egui library, perfect for a download manager interface. This example demonstrates how to display progress, show percentage text, add shimmer animation, and update the UI continuously with state-driven buttons.
Code
use egui::{ProgressBar, Ui};
struct DownloadManager {
progress: f32, // current progress from 0.0 to 1.0
downloading: bool,
}
impl DownloadManager {
fn ui(&mut self, ui: &mut Ui, ctx: &egui::Context) {
// Create a progress bar with current progress
let mut progress_bar = ProgressBar::new(self.progress)
.show_percentage(); // show progress as "XX%"
// Animate the progress bar if downloading
if self.downloading {
progress_bar = progress_bar.animate(true);
// Request repaint to update animation continuously
ctx.request_repaint();
}
ui.add(progress_bar);
// State-driven buttons
if self.downloading {
if ui.button("Cancel").clicked() {
self.downloading = false;
self.progress = 0.0;
}
} else {
if ui.button("Start Download").clicked() {
self.downloading = true;
self.progress = 0.0;
}
}
// Simulate progress increment
if self.downloading && self.progress < 1.0 {
self.progress += 0.005; // increase progress smoothly
} else if self.progress >= 1.0 {
self.downloading = false; // stop when complete
}
}
}
Key Points
- Use
ProgressBar::new(progress)to create a progress bar with a value between 0.0 and 1.0. .show_percentage()adds a textual percentage display inside the progress bar..animate(true)applies a shimmer animation effect to indicate active progress.- Call
ctx.request_repaint()each frame to keep the animation and UI updates smooth and continuous. - Drive UI state with boolean flags to toggle buttons and progress behavior dynamically during downloads.