use std::{collections::HashMap, rc::Rc};
use gpui::{AnyElement, App, ElementId, Entity, IntoElement, Pixels, SharedString, Window, px};
use gpui_material_theme::LogicalEdges;
use crate::{DialogCloseReason, DialogProperties, DialogState};
use super::items::NavigationItems;
mod render;
mod state;
mod types;
pub use state::NavigationRailState;
pub(in crate::navigation) use types::RailPresentation;
pub use types::{
NavigationRailArrangement, NavigationRailColors, NavigationRailShapes, RailModalOptions,
RailMode,
};
use types::{Closed, Destinations, DismissRequest};
#[derive(IntoElement)]
pub struct NavigationRail {
pub(super) id: ElementId,
pub(super) items: NavigationItems,
pub(super) mode: RailMode,
pub(super) state: Option<Entity<NavigationRailState>>,
pub(super) title: Option<SharedString>,
pub(super) header: Option<AnyElement>,
pub(super) fab: Option<AnyElement>,
pub(super) expanded_width: Pixels,
pub(super) modal: Option<Entity<DialogState>>,
pub(super) modal_options: RailModalOptions,
pub(super) destination_content: Option<Destinations>,
pub(super) dividers: HashMap<SharedString, AnyElement>,
pub(super) arrangement: NavigationRailArrangement,
pub(super) destination_spacing: Pixels,
pub(super) content_padding: Option<LogicalEdges>,
pub(super) header_spacing: Pixels,
pub(super) expanded_header_top_padding: Pixels,
pub(super) shapes: NavigationRailShapes,
pub(super) colors: NavigationRailColors,
pub(super) dismiss: Option<DismissRequest>,
pub(super) closed: Option<Closed>,
}
impl NavigationRail {
pub fn new(id: impl Into<ElementId>, items: NavigationItems) -> Self {
Self {
id: id.into(),
items,
mode: RailMode::Collapsed,
state: None,
title: None,
header: None,
fab: None,
expanded_width: px(240.),
modal: None,
modal_options: RailModalOptions::default(),
destination_content: None,
dividers: HashMap::new(),
arrangement: NavigationRailArrangement::default(),
destination_spacing: px(4.),
content_padding: None,
header_spacing: px(4.),
expanded_header_top_padding: px(0.),
shapes: NavigationRailShapes::default(),
colors: NavigationRailColors::default(),
dismiss: None,
closed: None,
}
}
pub fn mode(mut self, mode: RailMode) -> Self {
self.mode = mode;
self
}
pub fn state(mut self, state: &Entity<NavigationRailState>) -> Self {
self.state = Some(state.clone());
self
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn headline(mut self, headline: impl Into<SharedString>) -> Self {
self.title = Some(headline.into());
self
}
pub fn header(mut self, header: impl IntoElement) -> Self {
self.header = Some(header.into_any_element());
self
}
pub fn fab(mut self, fab: impl IntoElement) -> Self {
self.fab = Some(fab.into_any_element());
self
}
pub fn expanded_width(mut self, width: Pixels) -> Self {
if f32::from(width).is_finite() {
self.expanded_width = width.clamp(px(220.), px(360.));
}
self
}
pub fn destination_content<E: IntoElement + 'static>(
mut self,
build: impl FnOnce(RailMode) -> E + 'static,
) -> Self {
self.destination_content = Some(Box::new(move |mode| build(mode).into_any_element()));
self
}
pub fn divider_after(
mut self,
destination: impl Into<SharedString>,
divider: impl IntoElement,
) -> Self {
self.dividers
.insert(destination.into(), divider.into_any_element());
self
}
pub fn arrangement(mut self, arrangement: NavigationRailArrangement) -> Self {
self.arrangement = arrangement;
self
}
pub fn destination_spacing(mut self, spacing: Pixels) -> Self {
self.destination_spacing = length(spacing);
self
}
pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
self.content_padding = Some(padding.sanitize());
self
}
pub fn header_spacing(mut self, spacing: Pixels) -> Self {
self.header_spacing = length(spacing);
self
}
pub fn expanded_header_top_padding(mut self, padding: Pixels) -> Self {
self.expanded_header_top_padding = length(padding);
self
}
pub fn shapes(mut self, shapes: NavigationRailShapes) -> Self {
self.shapes = shapes;
self
}
pub fn colors(mut self, colors: NavigationRailColors) -> Self {
self.colors = colors;
self
}
pub fn modal_state(mut self, state: &Entity<DialogState>) -> Self {
self.modal = Some(state.clone());
self
}
pub fn modal_properties(mut self, properties: DialogProperties) -> Self {
self.modal_options.properties = properties;
self
}
pub fn modal_options(mut self, options: RailModalOptions) -> Self {
self.modal_options = options;
self
}
pub fn on_dismiss_request(
mut self,
callback: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
) -> Self {
self.dismiss = Some(Rc::new(callback));
self
}
pub fn on_closed(
mut self,
callback: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
) -> Self {
self.closed = Some(Rc::new(callback));
self
}
}
pub(super) fn length(value: Pixels) -> Pixels {
if f32::from(value).is_finite() {
value.max(px(0.))
} else {
px(0.)
}
}