Skip to main content

fc_ui/capabilities/controls/
collapsible.rs

1//! Collapsible component - Expandable/collapsible section with trigger and content.
2
3use gpui::{prelude::FluentBuilder as _, *};
4use std::rc::Rc;
5
6use crate::capabilities::foundation::theme::use_theme;
7
8#[derive(IntoElement)]
9pub struct Collapsible {
10    id: Option<ElementId>,
11    trigger: Option<AnyElement>,
12    content: Option<AnyElement>,
13    is_open: bool,
14    disabled: bool,
15    show_icon: bool,
16    on_toggle: Option<Rc<dyn Fn(bool, &mut Window, &mut App)>>,
17    style: StyleRefinement,
18}
19
20impl Collapsible {
21    pub fn new() -> Self {
22        Self {
23            id: None,
24            trigger: None,
25            content: None,
26            is_open: false,
27            disabled: false,
28            show_icon: true,
29            on_toggle: None,
30            style: StyleRefinement::default(),
31        }
32    }
33
34    /// Set the trigger element id. Required to enable keyboard toggling
35    /// (Enter/Space) and focus support.
36    pub fn id(mut self, id: impl Into<ElementId>) -> Self {
37        self.id = Some(id.into());
38        self
39    }
40
41    pub fn trigger(mut self, trigger: impl IntoElement) -> Self {
42        self.trigger = Some(trigger.into_any_element());
43        self
44    }
45
46    pub fn content(mut self, content: impl IntoElement) -> Self {
47        self.content = Some(content.into_any_element());
48        self
49    }
50
51    pub fn open(mut self, open: bool) -> Self {
52        self.is_open = open;
53        self
54    }
55
56    pub fn disabled(mut self, disabled: bool) -> Self {
57        self.disabled = disabled;
58        self
59    }
60
61    pub fn show_icon(mut self, show: bool) -> Self {
62        self.show_icon = show;
63        self
64    }
65
66    pub fn on_toggle<F>(mut self, handler: F) -> Self
67    where
68        F: Fn(bool, &mut Window, &mut App) + 'static,
69    {
70        self.on_toggle = Some(Rc::new(handler));
71        self
72    }
73}
74
75impl Default for Collapsible {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81impl Styled for Collapsible {
82    fn style(&mut self) -> &mut StyleRefinement {
83        &mut self.style
84    }
85}
86
87impl RenderOnce for Collapsible {
88    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
89        let theme = use_theme(cx);
90        let user_style = self.style;
91        let Collapsible {
92            id,
93            trigger,
94            content,
95            is_open,
96            disabled,
97            show_icon,
98            on_toggle,
99            style: _,
100        } = self;
101
102        let focus_handle = id.as_ref().map(|id| {
103            window
104                .use_keyed_state(id.clone(), cx, |_, cx| cx.focus_handle())
105                .read(cx)
106                .clone()
107        });
108
109        div()
110            .flex()
111            .flex_col()
112            .w_full()
113            .when_some(trigger, |this: Div, trigger| {
114                let row = div()
115                    .flex()
116                    .items_center()
117                    .gap(px(8.0))
118                    .cursor(if disabled {
119                        CursorStyle::Arrow
120                    } else {
121                        CursorStyle::PointingHand
122                    })
123                    .when(!disabled, |this: Div| {
124                        this.hover(|style| style.bg(theme.tokens.muted.opacity(0.5)))
125                    })
126                    .when(disabled, |this: Div| this.opacity(0.5))
127                    .when(show_icon, |this: Div| {
128                        this.child(
129                            div()
130                                .flex()
131                                .items_center()
132                                .justify_center()
133                                .text_size(px(16.0))
134                                .text_color(theme.tokens.muted_foreground)
135                                .when(is_open, |this: Div| this.child("▼"))
136                                .when(!is_open, |this: Div| this.child("▶")),
137                        )
138                    })
139                    .child(div().flex_1().child(trigger));
140
141                if let (Some(id), Some(focus_handle)) = (id.clone(), focus_handle.clone()) {
142                    this.child(
143                        row.id(id)
144                            .role(Role::Button)
145                            .aria_expanded(is_open)
146                            .aria_disabled(disabled)
147                            .when(!disabled, |this| {
148                                this.track_focus(&focus_handle.clone().tab_index(0).tab_stop(true))
149                            })
150                            .when(!disabled, {
151                                let on_toggle = on_toggle.clone();
152                                move |this| {
153                                    this.when_some(on_toggle, |this, handler| {
154                                        this.on_click({
155                                            let handler = handler.clone();
156                                            move |_, window, cx| handler(!is_open, window, cx)
157                                        })
158                                        .on_key_down(
159                                            move |event: &KeyDownEvent, window, cx| {
160                                                if event.keystroke.key == "space"
161                                                    || event.keystroke.key == "enter"
162                                                {
163                                                    (handler)(!is_open, window, cx);
164                                                    cx.stop_propagation();
165                                                }
166                                            },
167                                        )
168                                    })
169                                }
170                            }),
171                    )
172                } else {
173                    this.child(row.when(!disabled, {
174                        let on_toggle = on_toggle.clone();
175                        move |this| {
176                            this.when_some(on_toggle, |this, handler| {
177                                this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
178                                    (handler)(!is_open, window, cx)
179                                })
180                            })
181                        }
182                    }))
183                }
184            })
185            .when(is_open, |this: Div| {
186                this.when_some(content, |this: Div, content| {
187                    this.child(div().overflow_hidden().child(content))
188                })
189            })
190            .map(|this| {
191                let mut div = this;
192                div.style().refine(&user_style);
193                div
194            })
195    }
196}