disclosure.rs

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