ZIm/crates/ui/src/components/disclosure.rs
Nate Butler 8376dd2011
ui crate docs & spring cleaning (#18768)
Similar to https://github.com/zed-industries/zed/pull/18690 &
https://github.com/zed-industries/zed/pull/18695, this PR enables
required docs for `ui` and does some cleanup.

Changes:
- Enables the `deny(missing_docs)` crate-wide.
- Adds `allow(missing_docs)` on many modules until folks pick them up to
document them
- Documents some modules (all in `ui/src/styles`)
- Crate root-level organization: Traits move to `traits`, other misc
organization
- Cleaned out a bunch of unused code.

Note: I'd like to remove `utils/format_distance` but the assistant panel
uses it. To move it over to use the `time_format` crate we may need to
update it to use `time` instead of `chrono`. Needs more investigation.

Release Notes:

- N/A
2024-10-05 23:28:34 -04:00

73 lines
1.9 KiB
Rust

#![allow(missing_docs)]
use std::sync::Arc;
use gpui::{ClickEvent, CursorStyle};
use crate::{prelude::*, Color, IconButton, IconButtonShape, IconName, IconSize};
#[derive(IntoElement)]
pub struct Disclosure {
id: ElementId,
is_open: bool,
selected: bool,
on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
cursor_style: CursorStyle,
}
impl Disclosure {
pub fn new(id: impl Into<ElementId>, is_open: bool) -> Self {
Self {
id: id.into(),
is_open,
selected: false,
on_toggle: None,
cursor_style: CursorStyle::PointingHand,
}
}
pub fn on_toggle(
mut self,
handler: impl Into<Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
) -> Self {
self.on_toggle = handler.into();
self
}
}
impl Selectable for Disclosure {
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
}
impl Clickable for Disclosure {
fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
self.on_toggle = Some(Arc::new(handler));
self
}
fn cursor_style(mut self, cursor_style: gpui::CursorStyle) -> Self {
self.cursor_style = cursor_style;
self
}
}
impl RenderOnce for Disclosure {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
IconButton::new(
self.id,
match self.is_open {
true => IconName::ChevronDown,
false => IconName::ChevronRight,
},
)
.shape(IconButtonShape::Square)
.icon_color(Color::Muted)
.icon_size(IconSize::Small)
.selected(self.selected)
.when_some(self.on_toggle, move |this, on_toggle| {
this.on_click(move |event, cx| on_toggle(event, cx))
})
}
}