gpui_component/
progress.rs

1use crate::ActiveTheme;
2use gpui::{
3    div, prelude::FluentBuilder, px, relative, App, IntoElement, ParentElement, RenderOnce, Styled,
4    Window,
5};
6
7/// A Progress bar element.
8#[derive(IntoElement)]
9pub struct Progress {
10    value: f32,
11    height: f32,
12}
13
14impl Progress {
15    pub fn new() -> Self {
16        Progress {
17            value: Default::default(),
18            height: 8.,
19        }
20    }
21
22    pub fn value(mut self, value: f32) -> Self {
23        self.value = value;
24        self
25    }
26}
27
28impl RenderOnce for Progress {
29    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
30        // Match the theme radius, if theme radius is zero use it.
31        let radius = px(self.height / 2.).min(cx.theme().radius);
32        let relative_w = relative(match self.value {
33            v if v < 0. => 0.,
34            v if v > 100. => 1.,
35            v => v / 100.,
36        });
37
38        div()
39            .w_full()
40            .relative()
41            .h(px(self.height))
42            .rounded(radius)
43            .bg(cx.theme().progress_bar.opacity(0.2))
44            .child(
45                div()
46                    .absolute()
47                    .top_0()
48                    .left_0()
49                    .h_full()
50                    .w(relative_w)
51                    .bg(cx.theme().progress_bar)
52                    .map(|this| match self.value {
53                        v if v >= 100. => this.rounded(radius),
54                        _ => this.rounded_l(radius),
55                    }),
56            )
57    }
58}