gpui_component/
divider.rs

1use crate::{ActiveTheme, StyledExt};
2use gpui::{
3    div, prelude::FluentBuilder as _, px, App, Axis, Div, Hsla, IntoElement, ParentElement,
4    RenderOnce, SharedString, StyleRefinement, Styled, Window,
5};
6
7/// A divider that can be either vertical or horizontal.
8#[derive(IntoElement)]
9pub struct Divider {
10    base: Div,
11    style: StyleRefinement,
12    label: Option<SharedString>,
13    axis: Axis,
14    color: Option<Hsla>,
15}
16
17impl Divider {
18    /// Creates a vertical divider.
19    pub fn vertical() -> Self {
20        Self {
21            base: div().h_full(),
22            axis: Axis::Vertical,
23            label: None,
24            color: None,
25            style: StyleRefinement::default(),
26        }
27    }
28
29    /// Creates a horizontal divider.
30    pub fn horizontal() -> Self {
31        Self {
32            base: div(),
33            axis: Axis::Horizontal,
34            label: None,
35            color: None,
36            style: StyleRefinement::default(),
37        }
38    }
39
40    /// Sets the label for the divider.
41    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
42        self.label = Some(label.into());
43        self
44    }
45
46    /// Sets the color for the divider line.
47    pub fn color(mut self, color: impl Into<Hsla>) -> Self {
48        self.color = Some(color.into());
49        self
50    }
51}
52
53impl Styled for Divider {
54    fn style(&mut self) -> &mut StyleRefinement {
55        &mut self.style
56    }
57}
58
59impl RenderOnce for Divider {
60    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
61        self.base
62            .flex()
63            .flex_shrink_0()
64            .items_center()
65            .justify_center()
66            .refine_style(&self.style)
67            .child(
68                div()
69                    .absolute()
70                    .map(|this| match self.axis {
71                        Axis::Vertical => this.w(px(1.)).h_full(),
72                        Axis::Horizontal => this.h(px(1.)).w_full(),
73                    })
74                    .bg(self.color.unwrap_or(cx.theme().border)),
75            )
76            .when_some(self.label, |this, label| {
77                this.child(
78                    div()
79                        .px_2()
80                        .py_1()
81                        .mx_auto()
82                        .text_xs()
83                        .bg(cx.theme().background)
84                        .text_color(cx.theme().muted_foreground)
85                        .child(label),
86                )
87            })
88    }
89}