disclosure.rs

 1use gpui::ClickEvent;
 2
 3use crate::{prelude::*, Color, Icon, IconButton, IconSize};
 4
 5#[derive(IntoElement)]
 6pub struct Disclosure {
 7    id: ElementId,
 8    is_open: bool,
 9    on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
10}
11
12impl Disclosure {
13    pub fn new(id: impl Into<ElementId>, is_open: bool) -> Self {
14        Self {
15            id: id.into(),
16            is_open,
17            on_toggle: None,
18        }
19    }
20
21    pub fn on_toggle(
22        mut self,
23        handler: impl Into<Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
24    ) -> Self {
25        self.on_toggle = handler.into();
26        self
27    }
28}
29
30impl RenderOnce for Disclosure {
31    type Rendered = IconButton;
32
33    fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
34        IconButton::new(
35            self.id,
36            match self.is_open {
37                true => Icon::ChevronDown,
38                false => Icon::ChevronRight,
39            },
40        )
41        .icon_color(Color::Muted)
42        .icon_size(IconSize::Small)
43        .when_some(self.on_toggle, move |this, on_toggle| {
44            this.on_click(move |event, cx| on_toggle(event, cx))
45        })
46    }
47}