fc_ui/capabilities/effects/
animated_progress.rs1use gpui::{prelude::FluentBuilder as _, *};
2use std::time::Duration;
3
4use crate::capabilities::foundation::theme::use_theme;
5use crate::capabilities::motion::animations::easings;
6use crate::capabilities::primitives::progress::{ProgressSize, ProgressVariant};
7
8#[derive(IntoElement)]
11pub struct AnimatedProgress {
12 id: ElementId,
13 base: Div,
14 value: f32,
15 variant: ProgressVariant,
16 size: ProgressSize,
17 shimmer: bool,
18 color: Option<Hsla>,
19 duration: Duration,
20}
21
22impl AnimatedProgress {
23 pub fn new(id: impl Into<ElementId>) -> Self {
24 Self {
25 id: id.into(),
26 base: div(),
27 value: 0.0,
28 variant: ProgressVariant::Default,
29 size: ProgressSize::Md,
30 shimmer: false,
31 color: None,
32 duration: Duration::from_millis(500),
33 }
34 }
35
36 pub fn value(mut self, value: f32) -> Self {
37 self.value = value.clamp(0.0, 1.0);
38 self
39 }
40
41 pub fn variant(mut self, variant: ProgressVariant) -> Self {
42 self.variant = variant;
43 self
44 }
45
46 pub fn size(mut self, size: ProgressSize) -> Self {
47 self.size = size;
48 self
49 }
50
51 pub fn shimmer(mut self, shimmer: bool) -> Self {
52 self.shimmer = shimmer;
53 self
54 }
55
56 pub fn color(mut self, color: Hsla) -> Self {
57 self.color = Some(color);
58 self
59 }
60
61 pub fn duration(mut self, duration: Duration) -> Self {
62 self.duration = duration;
63 self
64 }
65}
66
67impl Styled for AnimatedProgress {
68 fn style(&mut self) -> &mut StyleRefinement {
69 self.base.style()
70 }
71}
72
73impl RenderOnce for AnimatedProgress {
74 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
75 let theme = use_theme(cx);
76
77 let height = match self.size {
78 ProgressSize::Sm => px(4.0),
79 ProgressSize::Md => px(8.0),
80 ProgressSize::Lg => px(12.0),
81 };
82
83 let bar_color = self.color.unwrap_or_else(|| match self.variant {
84 ProgressVariant::Default => theme.tokens.primary,
85 ProgressVariant::Success => rgb(0x22c55e).into(),
86 ProgressVariant::Warning => rgb(0xf59e0b).into(),
87 ProgressVariant::Destructive => theme.tokens.destructive,
88 });
89
90 let target_value = self.value;
91 let duration = self.duration;
92 let shimmer_enabled = self.shimmer;
93 let value_key = (target_value * 10000.0) as u32;
94
95 self.base.w_full().child(
96 div()
97 .relative()
98 .w_full()
99 .h(height)
100 .rounded(theme.tokens.radius_lg)
101 .bg(theme.tokens.muted)
102 .overflow_hidden()
103 .child(
104 div()
105 .id(self.id.clone())
106 .absolute()
107 .top_0()
108 .left_0()
109 .h_full()
110 .bg(bar_color)
111 .rounded(theme.tokens.radius_lg)
112 .overflow_hidden()
113 .when(shimmer_enabled, |el| {
114 el.child(
115 div()
116 .absolute()
117 .top_0()
118 .bottom_0()
119 .w(px(120.0))
120 .bg(gpui::linear_gradient(
121 90.0,
122 gpui::linear_color_stop(gpui::transparent_black(), 0.0),
123 gpui::linear_color_stop(hsla(0.0, 0.0, 1.0, 0.2), 1.0),
124 ))
125 .with_animation(
126 ElementId::Name(format!("{}-shimmer", self.id).into()),
127 Animation::new(Duration::from_millis(1500))
128 .repeat()
129 .with_easing(gpui::linear),
130 move |el, delta| {
131 let start = px(-120.0);
132 let end = px(600.0);
133 let pos = start + (end - start) * delta;
134 el.left(pos)
135 },
136 ),
137 )
138 })
139 .with_animation(
140 ("progress-fill", value_key),
141 Animation::new(duration).with_easing(easings::ease_out_cubic),
142 move |el, delta| {
143 let width = target_value * delta;
144 el.w(relative(width))
145 },
146 ),
147 ),
148 )
149 }
150}