disclosure.rs

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