fc_ui/capabilities/primitives/
separator.rs1use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum SeparatorOrientation {
9 #[default]
11 Horizontal,
12 Vertical,
14}
15
16#[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 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 pub fn horizontal() -> Self {
55 Self::new()
56 }
57
58 pub fn vertical() -> Self {
60 Self::new().orientation(SeparatorOrientation::Vertical)
61 }
62
63 pub fn orientation(mut self, orientation: SeparatorOrientation) -> Self {
65 self.orientation = orientation;
66 self
67 }
68
69 pub fn label(mut self, label: impl Into<SharedString>) -> Self {
71 self.label = Some(label.into());
72 self
73 }
74
75 pub fn color(mut self, color: impl Into<Hsla>) -> Self {
77 self.color = Some(color.into());
78 self
79 }
80
81 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}