disclosure.rs

 1use gpui::ClickEvent;
 2
 3use crate::{prelude::*, Color, Icon, IconButton, IconSize};
 4
 5#[derive(IntoElement)]
 6pub struct Disclosure {
 7    is_open: bool,
 8    on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
 9}
10
11impl Disclosure {
12    pub fn new(is_open: bool) -> Self {
13        Self {
14            is_open,
15            on_toggle: None,
16        }
17    }
18
19    pub fn on_toggle(
20        mut self,
21        handler: impl Into<Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
22    ) -> Self {
23        self.on_toggle = handler.into();
24        self
25    }
26}
27
28impl RenderOnce for Disclosure {
29    type Rendered = IconButton;
30
31    fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
32        IconButton::new(
33            "toggle",
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}