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    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
32        IconButton::new(
33            self.id,
34            match self.is_open {
35                true => Icon::ChevronDown,
36                false => Icon::ChevronRight,
37            },
38        )
39        .icon_color(Color::Muted)
40        .icon_size(IconSize::Small)
41        .when_some(self.on_toggle, move |this, on_toggle| {
42            this.on_click(move |event, cx| on_toggle(event, cx))
43        })
44    }
45}