Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T05:36:21.261Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

progress_bar.rs

143 lines · 4.3 KB · rust
1use documented::Documented;
2use gpui::Hsla;
3
4use crate::components::Label;
5use crate::prelude::*;
6
7/// A progress bar is a horizontal bar that communicates the status of a process.
8///
9/// A progress bar should not be used to represent indeterminate progress.
10#[derive(IntoElement, RegisterComponent, Documented)]
11pub struct ProgressBar {
12    id: ElementId,
13    value: f32,
14    max_value: f32,
15    bg_color: Hsla,
16    over_color: Hsla,
17    fg_color: Hsla,
18}
19
20impl ProgressBar {
21    pub fn new(id: impl Into<ElementId>, value: f32, max_value: f32, cx: &App) -> Self {
22        Self {
23            id: id.into(),
24            value,
25            max_value,
26            bg_color: cx.theme().colors().background,
27            over_color: cx.theme().status().error,
28            fg_color: cx.theme().status().info,
29        }
30    }
31
32    /// Sets the current value of the progress bar.
33    pub fn value(mut self, value: f32) -> Self {
34        self.value = value;
35        self
36    }
37
38    /// Sets the maximum value of the progress bar.
39    pub fn max_value(mut self, max_value: f32) -> Self {
40        self.max_value = max_value;
41        self
42    }
43
44    /// Sets the background color of the progress bar.
45    pub fn bg_color(mut self, color: Hsla) -> Self {
46        self.bg_color = color;
47        self
48    }
49
50    /// Sets the foreground color of the progress bar.
51    pub fn fg_color(mut self, color: Hsla) -> Self {
52        self.fg_color = color;
53        self
54    }
55
56    /// Sets the over limit color of the progress bar.
57    pub fn over_color(mut self, color: Hsla) -> Self {
58        self.over_color = color;
59        self
60    }
61}
62
63impl RenderOnce for ProgressBar {
64    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
65        let fill_width = (self.value / self.max_value).clamp(0.02, 1.0);
66
67        div()
68            .id(self.id.clone())
69            .w_full()
70            .h_2()
71            .p_0p5()
72            .rounded_full()
73            .bg(self.bg_color)
74            .shadow(vec![gpui::BoxShadow::new(
75                px(0.),
76                px(1.),
77                gpui::black().opacity(0.08),
78            )])
79            .child(
80                div()
81                    .h_full()
82                    .rounded_full()
83                    .when(self.value > self.max_value, |div| div.bg(self.over_color))
84                    .when(self.value <= self.max_value, |div| div.bg(self.fg_color))
85                    .w(relative(fill_width)),
86            )
87    }
88}
89
90impl Component for ProgressBar {
91    fn scope() -> ComponentScope {
92        ComponentScope::Status
93    }
94
95    fn description() -> &'static str {
96        Self::DOCS
97    }
98
99    fn preview(_window: &mut Window, cx: &mut App) -> AnyElement {
100        let max_value = 180.0;
101        let container = || v_flex().w_full().gap_1();
102
103        example_group(vec![single_example(
104            "Examples",
105            v_flex()
106                .w_full()
107                .gap_2()
108                .child(
109                    container()
110                        .child(
111                            h_flex()
112                                .justify_between()
113                                .child(Label::new("0%"))
114                                .child(Label::new("Empty")),
115                        )
116                        .child(ProgressBar::new("empty", 0.0, max_value, cx)),
117                )
118                .child(
119                    container()
120                        .child(
121                            h_flex()
122                                .justify_between()
123                                .child(Label::new("38%"))
124                                .child(Label::new("Partial")),
125                        )
126                        .child(ProgressBar::new("partial", max_value * 0.35, max_value, cx)),
127                )
128                .child(
129                    container()
130                        .child(
131                            h_flex()
132                                .justify_between()
133                                .child(Label::new("100%"))
134                                .child(Label::new("Complete")),
135                        )
136                        .child(ProgressBar::new("filled", max_value, max_value, cx)),
137                )
138                .into_any_element(),
139        )])
140        .into_any_element()
141    }
142}
143
Served at tenant.openagents/omega Member data and write actions are omitted.