Rework Toggle into Toggleable and ToggleState

This commit is contained in:
Marshall Bowers 2023-11-29 14:33:52 -05:00
parent 6f5cc0af94
commit e77846d2dc
10 changed files with 95 additions and 93 deletions

View file

@ -1,24 +1,31 @@
use std::rc::Rc;
use gpui::{ClickEvent, Div};
use gpui::ClickEvent;
use crate::prelude::*;
use crate::{Color, Icon, IconButton, IconSize, Toggle};
use crate::{Color, Icon, IconButton, IconSize, ToggleState, Toggleable};
#[derive(IntoElement)]
pub struct Disclosure {
toggle: Toggle,
state: ToggleState,
on_toggle: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
}
impl Disclosure {
pub fn new(toggle: Toggle) -> Self {
pub fn new(state: ToggleState) -> Self {
Self {
toggle,
state,
on_toggle: None,
}
}
pub fn from_toggleable(toggleable: Toggleable) -> Option<Self> {
match toggleable {
Toggleable::Toggleable(state) => Some(Self::new(state)),
Toggleable::NotToggleable => None,
}
}
pub fn on_toggle(
mut self,
handler: impl Into<Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
@ -29,27 +36,20 @@ impl Disclosure {
}
impl RenderOnce for Disclosure {
type Rendered = Div;
type Rendered = IconButton;
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
if !self.toggle.is_toggleable() {
return div();
}
div().child(
IconButton::new(
"toggle",
if self.toggle.is_toggled() {
Icon::ChevronDown
} else {
Icon::ChevronRight
},
)
.color(Color::Muted)
.size(IconSize::Small)
.when_some(self.on_toggle, move |this, on_toggle| {
this.on_click(move |event, cx| on_toggle(event, cx))
}),
IconButton::new(
"toggle",
match self.state {
ToggleState::Toggled => Icon::ChevronDown,
ToggleState::NotToggled => Icon::ChevronRight,
},
)
.color(Color::Muted)
.size(IconSize::Small)
.when_some(self.on_toggle, move |this, on_toggle| {
this.on_click(move |event, cx| on_toggle(event, cx))
})
}
}