use gpui::{AnyElement, ElementId, Entity, IntoElement, Pixels, ScrollHandle, SharedString};
use gpui_material_theme::{LogicalEdges, MotionSpec};
mod actions;
mod render;
mod scroll;
mod types;
pub use actions::{AppBarColumn, AppBarItem, AppBarMenuState, AppBarRow};
pub(super) use scroll::length;
pub use scroll::{AppBarScrollBehavior, AppBarScrollState};
pub use types::{AppBarAlignment, AppBarColors, AppBarVariant};
use types::Heading;
#[derive(IntoElement)]
pub struct AppBar {
id: ElementId,
title: Heading,
subtitle: Heading,
variant: AppBarVariant,
navigation: Option<AnyElement>,
actions: Option<AnyElement>,
search: Option<AnyElement>,
logo: Option<AnyElement>,
second_row: Option<AnyElement>,
colors: AppBarColors,
padding: Option<LogicalEdges>,
collapsed_height: Option<f32>,
expanded_height: Option<f32>,
scroll: Option<ScrollHandle>,
scroll_state: Option<Entity<AppBarScrollState>>,
pub(super) scroll_behavior: Option<AppBarScrollBehavior>,
pub(super) scroll_enabled: bool,
pub(super) scroll_motion: Option<MotionSpec>,
pub(super) font_scale: f32,
}
impl AppBar {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: Heading {
text: Some(title.into()),
..Heading::default()
},
subtitle: Heading::default(),
variant: AppBarVariant::Small,
navigation: None,
actions: None,
search: None,
logo: None,
second_row: None,
colors: AppBarColors::default(),
padding: None,
collapsed_height: None,
expanded_height: None,
scroll: None,
scroll_state: None,
scroll_behavior: None,
scroll_enabled: true,
scroll_motion: None,
font_scale: 1.,
}
}
pub fn variant(mut self, variant: AppBarVariant) -> Self {
self.variant = variant;
self
}
pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
self.subtitle.text = Some(subtitle.into());
self.subtitle.content = None;
self
}
pub fn title_content(self, content: impl IntoElement + 'static) -> Self {
self.title_for_rows(move |_| content)
}
pub fn subtitle_content(self, content: impl IntoElement + 'static) -> Self {
self.subtitle_for_rows(move |_| content)
}
pub fn title_for_rows<E: IntoElement>(
mut self,
build: impl FnOnce(bool) -> E + 'static,
) -> Self {
self.title.content = Some(Box::new(move |expanded| build(expanded).into_any_element()));
self
}
pub fn subtitle_for_rows<E: IntoElement>(
mut self,
build: impl FnOnce(bool) -> E + 'static,
) -> Self {
self.subtitle.content = Some(Box::new(move |expanded| build(expanded).into_any_element()));
self
}
pub fn title_alignment(mut self, alignment: AppBarAlignment) -> Self {
self.title.alignment = Some(alignment);
self
}
pub fn subtitle_alignment(mut self, alignment: AppBarAlignment) -> Self {
self.subtitle.alignment = Some(alignment);
self
}
pub fn title_max_lines(mut self, lines: usize) -> Self {
self.title.max_lines = Some(lines.max(1));
self
}
pub fn subtitle_max_lines(mut self, lines: usize) -> Self {
self.subtitle.max_lines = Some(lines.max(1));
self
}
pub fn navigation(mut self, element: impl IntoElement) -> Self {
self.navigation = Some(element.into_any_element());
self
}
pub fn actions(mut self, element: impl IntoElement) -> Self {
self.actions = Some(element.into_any_element());
self
}
pub fn search(mut self, element: impl IntoElement) -> Self {
self.search = Some(element.into_any_element());
self
}
pub fn logo(mut self, element: impl IntoElement) -> Self {
self.logo = Some(element.into_any_element());
self
}
pub fn second_row(mut self, element: impl IntoElement) -> Self {
self.second_row = Some(element.into_any_element());
self
}
pub fn colors(mut self, colors: AppBarColors) -> Self {
self.colors = colors;
self
}
pub fn content_padding(mut self, padding: LogicalEdges) -> Self {
self.padding = Some(padding.sanitize());
self
}
pub fn collapsed_height(mut self, height: Pixels) -> Self {
self.collapsed_height = length(height).filter(|height| *height > 0.);
self
}
pub fn expanded_height(mut self, height: Pixels) -> Self {
self.expanded_height = length(height).filter(|height| *height > 0.);
self
}
pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
self.scroll = Some(handle.clone());
self
}
pub fn scroll_state(mut self, state: &Entity<AppBarScrollState>) -> Self {
self.scroll_state = Some(state.clone());
self
}
pub fn scroll_behavior(mut self, behavior: AppBarScrollBehavior) -> Self {
self.scroll_behavior = Some(behavior);
self
}
pub fn scroll_enabled(mut self, enabled: bool) -> Self {
self.scroll_enabled = enabled;
self
}
pub fn scroll_motion(mut self, motion: MotionSpec) -> Self {
self.scroll_motion = Some(motion);
self
}
pub fn font_scale(mut self, scale: f32) -> Self {
self.font_scale = if scale.is_finite() && scale > 0. {
scale
} else {
1.
};
self
}
}