Skip to main content

fc_ui/capabilities/primitives/
separator.rs

1//! Separator component - Visual dividers for content sections.
2
3use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6/// Orientation of the separator
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum SeparatorOrientation {
9    /// Horizontal separator (default)
10    #[default]
11    Horizontal,
12    /// Vertical separator
13    Vertical,
14}
15
16/// Visual divider for separating content
17///
18/// # Example
19///
20/// ```rust,ignore
21/// // Horizontal separator
22/// Separator::new()
23///
24/// // Vertical separator
25/// Separator::new()
26///     .orientation(SeparatorOrientation::Vertical)
27///
28/// // With label
29/// Separator::new()
30///     .label("OR")
31/// ```
32#[derive(IntoElement)]
33pub struct Separator {
34    orientation: SeparatorOrientation,
35    label: Option<SharedString>,
36    color: Option<Hsla>,
37    decorative: bool,
38    style: StyleRefinement,
39}
40
41impl Separator {
42    /// Create a new horizontal separator
43    pub fn new() -> Self {
44        Self {
45            orientation: SeparatorOrientation::default(),
46            label: None,
47            color: None,
48            decorative: true,
49            style: StyleRefinement::default(),
50        }
51    }
52
53    /// Create a horizontal separator
54    pub fn horizontal() -> Self {
55        Self::new()
56    }
57
58    /// Create a vertical separator
59    pub fn vertical() -> Self {
60        Self::new().orientation(SeparatorOrientation::Vertical)
61    }
62
63    /// Set the orientation
64    pub fn orientation(mut self, orientation: SeparatorOrientation) -> Self {
65        self.orientation = orientation;
66        self
67    }
68
69    /// Set an optional label to display on the separator
70    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
71        self.label = Some(label.into());
72        self
73    }
74
75    /// Set custom color for the separator line
76    pub fn color(mut self, color: impl Into<Hsla>) -> Self {
77        self.color = Some(color.into());
78        self
79    }
80
81    /// Set whether this is a decorative separator (for accessibility)
82    /// Decorative separators are hidden from screen readers
83    pub fn decorative(mut self, decorative: bool) -> Self {
84        self.decorative = decorative;
85        self
86    }
87}
88
89impl Styled for Separator {
90    fn style(&mut self) -> &mut StyleRefinement {
91        &mut self.style
92    }
93}
94
95impl RenderOnce for Separator {
96    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
97        let theme = use_theme(cx);
98        let line_color = self.color.unwrap_or(theme.tokens.border);
99        let user_style = self.style;
100
101        div()
102            .flex()
103            .flex_shrink_0()
104            .items_center()
105            .justify_center()
106            .relative()
107            .when(
108                self.orientation == SeparatorOrientation::Horizontal,
109                |this| this.w_full().h(px(1.0)),
110            )
111            .when(self.orientation == SeparatorOrientation::Vertical, |this| {
112                this.h_full().w(px(1.0))
113            })
114            .child(
115                div()
116                    .absolute()
117                    .when(
118                        self.orientation == SeparatorOrientation::Horizontal,
119                        |this| this.w_full().h(px(1.0)),
120                    )
121                    .when(self.orientation == SeparatorOrientation::Vertical, |this| {
122                        this.h_full().w(px(1.0))
123                    })
124                    .bg(line_color),
125            )
126            .when_some(self.label, |this, label| {
127                this.child(
128                    div()
129                        .px(px(8.0))
130                        .py(px(4.0))
131                        .text_xs()
132                        .bg(theme.tokens.background)
133                        .text_color(theme.tokens.muted_foreground)
134                        .child(label),
135                )
136            })
137            .map(|this| {
138                let mut div = this;
139                div.style().refine(&user_style);
140                div
141            })
142    }
143}
144
145impl Default for Separator {
146    fn default() -> Self {
147        Self::new()
148    }
149}