Skip to main content

gpui_deck/
separator.rs

1//! Static separator semantics with application-owned geometry and appearance.
2
3use gpui::{
4    App, Axis, Div, ElementId, InteractiveElement as _, IntoElement, RenderOnce, Role,
5    StatefulInteractiveElement as _, StyleRefinement, Styled, Window, accesskit, div,
6    prelude::FluentBuilder as _,
7};
8
9use crate::TestSupportExt as _;
10
11/// An unstyled, non-interactive separator between regions of content.
12///
13/// Supply its thickness, length, and color with ordinary GPUI styles. Axis sets
14/// accessibility orientation; it does not choose layout. Semantic separators
15/// expose the native Splitter role without focus or actions. Decorative
16/// separators have no accessibility node. Neither form accepts children.
17#[derive(IntoElement)]
18pub struct Separator {
19    base: crate::ObservedElement<gpui::Stateful<Div>>,
20    axis: Axis,
21    decorative: bool,
22}
23
24impl Separator {
25    /// Creates a semantic horizontal separator.
26    pub fn new(id: impl Into<ElementId>) -> Self {
27        Self {
28            base: div().id(id).test_support(),
29            axis: Axis::Horizontal,
30            decorative: false,
31        }
32    }
33
34    pub fn axis(mut self, axis: Axis) -> Self {
35        self.axis = axis;
36        self
37    }
38
39    /// Omits the separator's native role and orientation when purely decorative.
40    pub fn decorative(mut self, decorative: bool) -> Self {
41        self.decorative = decorative;
42        self
43    }
44}
45
46impl Styled for Separator {
47    fn style(&mut self) -> &mut StyleRefinement {
48        self.base.style()
49    }
50}
51
52impl RenderOnce for Separator {
53    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
54        let orientation = match self.axis {
55            Axis::Horizontal => accesskit::Orientation::Horizontal,
56            Axis::Vertical => accesskit::Orientation::Vertical,
57        };
58        self.base.when(!self.decorative, |base| {
59            base.role(Role::Splitter).aria_orientation(orientation)
60        })
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use gpui::{Element as _, TestAppContext};
67
68    use super::*;
69
70    #[gpui::test]
71    fn semantic_orientation_and_decorative_omission_are_noninteractive(cx: &mut TestAppContext) {
72        let window = cx.add_empty_window();
73        window.update(|window, cx| {
74            for (axis, orientation) in [
75                (Axis::Horizontal, accesskit::Orientation::Horizontal),
76                (Axis::Vertical, accesskit::Orientation::Vertical),
77            ] {
78                for decorative in [false, true] {
79                    let element = Separator::new("separator")
80                        .axis(axis)
81                        .decorative(decorative)
82                        .render(window, cx)
83                        .into_element();
84                    assert_eq!(element.a11y_role(), (!decorative).then_some(Role::Splitter));
85                    let mut node = accesskit::Node::new(Role::Splitter);
86                    element.write_a11y_info(&mut node);
87                    assert_eq!(node.orientation(), (!decorative).then_some(orientation));
88                    assert!(!node.supports_action(accesskit::Action::Focus));
89                    assert!(!node.supports_action(accesskit::Action::Click));
90                }
91            }
92        });
93    }
94}