Skip to main content

fc_ui/capabilities/effects/
gradient_border.rs

1use crate::capabilities::foundation::theme::use_theme;
2use gpui::*;
3
4#[derive(IntoElement)]
5pub struct GradientBorder {
6    base: Div,
7    inner: Div,
8    start_color: Option<Hsla>,
9    end_color: Option<Hsla>,
10    border_width: Pixels,
11    angle: f32,
12    corner_radius: Option<Pixels>,
13}
14
15impl GradientBorder {
16    pub fn new() -> Self {
17        Self {
18            base: div(),
19            inner: div(),
20            start_color: None,
21            end_color: None,
22            border_width: px(2.0),
23            angle: 135.0,
24            corner_radius: None,
25        }
26    }
27
28    pub fn colors(mut self, start: Hsla, end: Hsla) -> Self {
29        self.start_color = Some(start);
30        self.end_color = Some(end);
31        self
32    }
33
34    pub fn width(mut self, width: Pixels) -> Self {
35        self.border_width = width;
36        self
37    }
38
39    pub fn angle(mut self, angle: f32) -> Self {
40        self.angle = angle;
41        self
42    }
43
44    pub fn rounded(mut self, radius: Pixels) -> Self {
45        self.corner_radius = Some(radius);
46        self
47    }
48}
49
50impl Default for GradientBorder {
51    fn default() -> Self {
52        Self::new()
53    }
54}
55
56impl RenderOnce for GradientBorder {
57    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
58        let theme = use_theme(cx);
59        let start = self.start_color.unwrap_or(theme.tokens.primary);
60        let end = self.end_color.unwrap_or(theme.tokens.accent);
61        let radius = self.corner_radius.unwrap_or(theme.tokens.radius_lg);
62        let border_w = self.border_width;
63
64        self.base
65            .bg(gpui::linear_gradient(
66                self.angle,
67                gpui::linear_color_stop(start, 0.0),
68                gpui::linear_color_stop(end, 1.0),
69            ))
70            .rounded(radius)
71            .p(border_w)
72            .child(
73                self.inner
74                    .flex_grow()
75                    .bg(theme.tokens.card)
76                    .rounded((radius - border_w).max(px(0.0))),
77            )
78    }
79}
80
81impl Styled for GradientBorder {
82    fn style(&mut self) -> &mut StyleRefinement {
83        self.base.style()
84    }
85}
86
87impl InteractiveElement for GradientBorder {
88    fn interactivity(&mut self) -> &mut Interactivity {
89        self.base.interactivity()
90    }
91}
92
93impl StatefulInteractiveElement for GradientBorder {}
94
95impl ParentElement for GradientBorder {
96    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
97        self.inner.extend(elements)
98    }
99}