A bar showing how far a task has completed.
use gpui::{div, prelude::*, px, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::progress::Progress;
pub fn progress_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let upload = |name: &'static str, percent: f32| {
div()
.flex()
.flex_col()
.gap_2()
.child(
div()
.flex()
.justify_between()
.text_sm()
.child(name)
.child(div().text_color(theme.muted_foreground).child(format!("{percent}%"))),
)
.child(Progress::new(percent))
};
div()
.flex()
.flex_col()
.gap_5()
.w(px(360.0))
.child(upload("invoice-2026.pdf", 100.0))
.child(upload("design-review.fig", 66.0))
.child(upload("onboarding.mp4", 13.0))
}Installation
Copies src/ui/progress.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/progress.rs.src/ui/progress.rs use std::sync::Arc; use gpui::{ div, prelude::*, px, relative, AnimationExt, App, ElementId, FontWeight, SharedString, SpringAnimation, SpringConfig, Window, }; use crate::theme::ActiveTheme; /// Stands in for shadcn's `transition-all` (150ms): critically damped (damping = /// 2 * sqrt(stiffness)), so the bar glides to a new value without overshooting it. const FILL_SPRING: SpringConfig = SpringConfig::new(900.0, 60.0, 1.0); /// A horizontal progress bar. `value` is a percentage from 0 to 100; anything outside is clamped. #[derive(IntoElement)] pub struct Progress { id: Option<ElementId>, value: f32, label: Option<SharedString>, show_value: bool, } impl Progress { pub fn new(value: f32) -> Self { Self { id: None, value, label: None, show_value: false, } } /// Animates the bar to each new value, like shadcn's `transition-all`, instead of /// redrawing it at the new width. The id keeps the animation's state between renders, so /// it must be unique among the bars on screen. Without one the bar snaps. pub fn id(mut self, id: impl Into<ElementId>) -> Self { self.id = Some(id.into()); self } /// A caption above the bar, at its left, such as "Upload progress". pub fn label(mut self, label: impl Into<SharedString>) -> Self { self.label = Some(label.into()); self } /// Shows the value as a whole percentage above the bar, at its right. pub fn show_value(mut self, show_value: bool) -> Self { self.show_value = show_value; self } } impl RenderOnce for Progress { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); // NaN would otherwise survive `clamp` and reach layout. let fraction = if self.value.is_nan() { 0.0 } else { (self.value / 100.0).clamp(0.0, 1.0) }; let fill = div().absolute().left_0().top_0().bottom_0().rounded_full().bg(theme.primary); // A freshly mounted spring starts at its target, so a bar doesn't fill up on first paint. let fill = match self.id { Some(id) => fill .with_spring( ElementId::NamedChild(Arc::new(id), "fill".into()), SpringAnimation::new(FILL_SPRING).to(fraction), |this, width| this.w(relative(width.clamp(0.0, 1.0))), ) .into_any_element(), None => fill.w(relative(fraction)).into_any_element(), }; let bar = div() .relative() .w_full() .h(px(8.0)) .rounded_full() .overflow_hidden() .bg(theme.primary.opacity(0.2)) .child(fill); if self.label.is_none() && !self.show_value { return bar.into_any_element(); } let percent = format!("{}%", (fraction * 100.0).round()); div() .flex() .flex_col() .gap_3() .w_full() .child( div() .flex() .items_center() .justify_between() .gap_2() .text_sm() .child(div().font_weight(FontWeight::MEDIUM).children(self.label)) .when(self.show_value, |this| { this.child(div().text_color(theme.muted_foreground).child(percent)) }), ) .child(bar) .into_any_element() } }Register the module
// src/ui/mod.rs pub mod progress;
Usage
use crate::ui::progress::*;Examples
Label
label puts a caption above the bar and show_value adds the percentage on the right.
use gpui::{div, prelude::*, px};
use crate::ui::progress::Progress;
pub fn progress_label() -> impl IntoElement {
div()
.w(px(384.0))
.child(Progress::new(56.0).label("Upload progress").show_value(true))
}Controlled
The bar redraws from whatever value the view holds. Here a slider sets it.
use gpui::{div, prelude::*, px, Context, Window};
use crate::ui::progress::Progress;
use crate::ui::slider::Slider;
pub struct ProgressControlled {
value: f32,
}
// Starts half full, so `Default` is written out instead of derived.
impl Default for ProgressControlled {
fn default() -> Self {
Self { value: 50.0 }
}
}
impl Render for ProgressControlled {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_6()
.w(px(384.0))
.child(Progress::new(self.value).id("progress-controlled").label("Progress").show_value(true))
.child(
Slider::new("progress-controlled", self.value).on_change(cx.listener(
|this, value: &f32, _, cx| {
this.value = *value;
cx.notify();
},
)),
)
}
}API Reference
Progress
A horizontal progress bar. value is a percentage from 0 to 100; anything outside is clamped.
Progress::new(value: f32) -> Self.id(id: impl Into<ElementId>)Animates the bar to each new value, like shadcn's transition-all, instead of redrawing it at the new width. The id keeps the animation's state between renders, so it must be unique among the bars on screen. Without one the bar snaps.
.label(label: impl Into<SharedString>)A caption above the bar, at its left, such as "Upload progress".
.show_value(show_value: bool)Shows the value as a whole percentage above the bar, at its right.