use std::{cell::RefCell, rc::Rc};
use gpui::{div, prelude::*, AnyElement, App, ElementId, SharedString, StyleRefinement, Window};
use super::{row_item, NavigationDestination, NAVIGATION};
use crate::components::interaction::Callback;
use crate::{
hooks::EventHandler,
styles::ApplyStyleOverrides,
sx::{Sx, SxStyled},
};
enum DrawerEntry {
Section(SharedString),
Divider,
Destination(NavigationDestination),
}
#[derive(IntoElement)]
pub struct NavigationDrawer {
id: ElementId,
header: Vec<AnyElement>,
entries: Vec<DrawerEntry>,
selected_index: usize,
on_change: Option<EventHandler<usize>>,
sx: Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(NavigationDrawer);
impl NavigationDrawer {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
header: Vec::new(),
entries: Vec::new(),
selected_index: 0,
on_change: None,
sx: Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn header(mut self, element: impl IntoElement) -> Self {
self.header.push(element.into_any_element());
self
}
#[must_use]
pub fn section(mut self, label: impl Into<SharedString>) -> Self {
self.entries.push(DrawerEntry::Section(label.into()));
self
}
#[must_use]
pub fn divider(mut self) -> Self {
self.entries.push(DrawerEntry::Divider);
self
}
#[must_use]
pub fn destination(mut self, destination: NavigationDestination) -> Self {
self.entries.push(DrawerEntry::Destination(destination));
self
}
#[must_use]
pub fn destinations(
mut self,
destinations: impl IntoIterator<Item = NavigationDestination>,
) -> Self {
self.entries
.extend(destinations.into_iter().map(DrawerEntry::Destination));
self
}
#[must_use]
pub fn selected_index(mut self, index: usize) -> Self {
self.selected_index = index;
self
}
#[must_use]
pub fn on_change(mut self, handler: impl Fn(&usize, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Rc::new(handler));
self
}
}
impl RenderOnce for NavigationDrawer {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let close_modal = current_drawer_closer();
let mut index = 0;
let mut rows: Vec<AnyElement> = Vec::new();
for entry in &self.entries {
match entry {
DrawerEntry::Section(label) => rows.push(
div()
.sx(&NAVIGATION.drawer_section)
.child(crate::components::bidi_text::text(label.clone()))
.into_any_element(),
),
DrawerEntry::Divider => {
rows.push(div().sx(&NAVIGATION.drawer_divider).into_any_element());
}
DrawerEntry::Destination(destination) => {
rows.push(row_item(
ElementId::NamedChild(Box::new(self.id.clone()), index.to_string().into()),
index,
destination,
index == self.selected_index,
self.on_change.as_ref(),
close_modal.clone(),
cx,
));
index += 1;
}
}
}
div()
.id(self.id)
.sx((&NAVIGATION.drawer, &self.sx))
.when(!self.header.is_empty(), |drawer| {
drawer.child(div().sx(&NAVIGATION.drawer_header).children(self.header))
})
.children(rows)
.apply_style_overrides(&self.style_overrides)
}
}
thread_local! {
static DRAWER_CLOSER: RefCell<Option<Callback>> = const { RefCell::new(None) };
}
#[cfg_attr(not(feature = "scaffold"), allow(dead_code))]
#[doc(hidden)]
pub struct DrawerScope {
pub closer: Callback,
pub child: AnyElement,
}
impl IntoElement for DrawerScope {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}
impl gpui::Element for DrawerScope {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_id: Option<&gpui::GlobalElementId>,
_inspector_id: Option<&gpui::InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
let previous = DRAWER_CLOSER.with(|current| current.replace(Some(self.closer.clone())));
let layout_id = self.child.request_layout(window, cx);
DRAWER_CLOSER.with(|current| *current.borrow_mut() = previous);
(layout_id, ())
}
fn prepaint(
&mut self,
_id: Option<&gpui::GlobalElementId>,
_inspector_id: Option<&gpui::InspectorElementId>,
_bounds: gpui::Bounds<gpui::Pixels>,
_request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
self.child.prepaint(window, cx);
}
fn paint(
&mut self,
_id: Option<&gpui::GlobalElementId>,
_inspector_id: Option<&gpui::InspectorElementId>,
_bounds: gpui::Bounds<gpui::Pixels>,
_request_layout: &mut Self::RequestLayoutState,
_prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
self.child.paint(window, cx);
}
}
fn current_drawer_closer() -> Option<Callback> {
DRAWER_CLOSER.with(|current| current.borrow().clone())
}