use std::sync::Arc;
use super::{
names,
part_style::Length,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
shared::{fading, glyph, one_line, pressable},
theme::{Themed, current_theme},
tooltip::Tooltip,
};
use crate::{
app::{
ctx::{Ctx, Placement},
theme::{BorderSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
},
components::Row,
layout::{Alignment, layout_kind::ShrinkLimit, text_style::FontWeight},
node::{CursorIcon, Element, FocusGroup, border::Border, dimension::Dimension, outline::Outline},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StatusBarVariant {
#[default]
Neutral,
Primary,
Info,
Success,
Warning,
Destructive,
}
names!(StatusBarVariant {
Neutral => "neutral",
Primary => "primary",
Info => "info",
Success => "success",
Warning => "warning",
Destructive => "destructive",
});
pub type StatusItemVariant = StatusBarVariant;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatusBarPart {
Root,
Item,
Icon,
Label,
Track,
Fill,
}
names!(StatusBarPart {
Root => "root",
Item => "item",
Icon => "icon",
Label => "label",
Track => "track",
Fill => "fill",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatusBarState {
Pressable,
Progress,
Disabled,
}
names!(StatusBarState {
Pressable => "pressable",
Progress => "progress",
Disabled => "disabled",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StatusRegion {
Leading,
#[default]
Trailing,
}
pub type StatusBarRenderer = Arc<dyn Fn(StatusBarView) -> Element + Send + Sync>;
type Call = Arc<dyn Fn() + Send + Sync>;
#[derive(Clone)]
pub struct StatusItem {
label: Arc<str>,
icon: Option<Name>,
icon_only: bool,
variant: Option<Name>,
on_select: Vec<Call>,
tooltip: Option<Arc<str>>,
progress: Option<f64>,
priority: i32,
disabled: bool,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl StatusItem {
pub fn new(label: &str) -> Self {
Self {
label: Arc::from(label),
icon: None,
icon_only: false,
variant: None,
on_select: Vec::new(),
tooltip: None,
progress: None,
priority: 0,
disabled: false,
id: None,
key: None,
}
}
pub fn icon(mut self, name: impl Into<Name>) -> Self {
self.icon = Some(name.into());
self
}
pub fn icon_only(mut self, icon_only: bool) -> Self {
self.icon_only = icon_only;
self
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = Some(variant.into());
self
}
pub fn on_select(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_select.push(Arc::new(handler));
self
}
pub fn tooltip(mut self, text: &str) -> Self {
self.tooltip = Some(Arc::from(text));
self
}
pub fn progress(mut self, done: impl Into<f64>) -> Self {
self.progress = Some(done.into().clamp(0.0, 1.0));
self
}
pub fn priority(mut self, priority: i32) -> Self {
self.priority = priority;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
}
impl From<&str> for StatusItem {
fn from(label: &str) -> Self {
Self::new(label)
}
}
pub struct StatusBar {
leading: Vec<StatusItem>,
trailing: Vec<StatusItem>,
variant: Name,
drops_first: StatusRegion,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Default for StatusBar {
fn default() -> Self {
Self::new()
}
}
impl StatusBar {
pub fn new() -> Self {
Self {
leading: Vec::new(),
trailing: Vec::new(),
variant: StatusBarVariant::default().into(),
drops_first: StatusRegion::default(),
label: None,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn leading(mut self, items: impl IntoIterator<Item = impl Into<StatusItem>>) -> Self {
self.leading.extend(items.into_iter().map(Into::into));
self
}
pub fn trailing(mut self, items: impl IntoIterator<Item = impl Into<StatusItem>>) -> Self {
self.trailing.extend(items.into_iter().map(Into::into));
self
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn drops_first(mut self, region: StatusRegion) -> Self {
self.drops_first = region;
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
fn view(self, theme: &Theme, ctx: Option<&mut Ctx>) -> StatusBarView {
let case = Case {
variant: Some(self.variant),
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
..Case::new()
};
let recipe = theme
.resolve::<StatusBar>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let bar_variant = self.variant;
let leading_first = self.drops_first == StatusRegion::Leading;
let items: Vec<(StatusItem, bool)> = self
.leading
.into_iter()
.map(|item| (item, true))
.chain(self.trailing.into_iter().map(|item| (item, false)))
.collect();
let entry = items
.iter()
.position(|(item, _)| !item.on_select.is_empty() && !item.disabled);
let mut ctx = ctx;
let entries = items
.into_iter()
.enumerate()
.map(|(index, (item, leading))| {
let pressable = !item.on_select.is_empty();
let item_case = Case {
variant: Some(item.variant.unwrap_or(bar_variant)),
..case.clone()
}
.state(StatusBarState::Pressable, pressable)
.state(StatusBarState::Progress, item.progress.is_some())
.state(StatusBarState::Disabled, item.disabled);
let recipe = theme
.resolve::<StatusBar>(&item_case, Some(&self.rules))
.owned_like(&recipe);
let recipe = match &item.key {
Some(key) if item.id.is_none() => recipe.keyed_item(key, StatusBarPart::Item),
_ => recipe.item(item.id.as_ref(), StatusBarPart::Item),
};
let first = leading == leading_first;
let order = item.priority.saturating_mul(2).saturating_add(i32::from(!first));
let forced = self.forced.filter(|_| entry == Some(index));
let element = StatusBarView::item_element(&item, &recipe, forced);
let element = match (&item.tooltip, ctx.as_deref_mut()) {
(Some(text), Some(ctx)) => Tooltip::new(text).placement(Placement::Top).mount(ctx, element),
_ => element,
};
StatusEntry {
label: item.label,
leading,
pressable,
order,
element: Some(element),
}
})
.collect();
StatusBarView {
entries,
label: self.label,
recipe,
id: self.id,
key: self.key,
}
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let theme = current_theme();
let view = self.view(&theme, Some(ctx));
render(&theme, view)
}
pub fn build(self, theme: &Theme) -> Element {
let view = self.view(theme, None);
render(theme, view)
}
}
fn render(theme: &Theme, view: StatusBarView) -> Element {
match theme.renderer::<StatusBar>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
impl From<StatusBar> for Element {
fn from(bar: StatusBar) -> Self {
bar.build(¤t_theme())
}
}
pub struct StatusEntry {
pub label: Arc<str>,
pub leading: bool,
pub pressable: bool,
pub order: i32,
element: Option<Element>,
}
pub struct StatusBarView {
pub entries: Vec<StatusEntry>,
pub label: Option<Arc<str>>,
recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl StatusBarView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
fn item_element(item: &StatusItem, recipe: &ResolvedRecipe, forced: Option<Interaction>) -> Element {
let part = |part: StatusBarPart| recipe.part(part).forced(forced);
let mut children: Vec<Element> = Vec::new();
if let Some(icon) = item.icon {
children.push(glyph(&part(StatusBarPart::Icon), icon, false));
}
if !item.icon_only {
children.push(one_line(&part(StatusBarPart::Label), &item.label));
}
if let Some(done) = item.progress {
let fill = Row::new()
.part(&part(StatusBarPart::Fill))
.width(Dimension::Pct((done * 100.0) as f32));
children.push(Row::new().part(&part(StatusBarPart::Track)).child(fill).into());
}
if item.on_select.is_empty() {
let mut row = Row::new()
.part(&part(StatusBarPart::Item))
.align_items(Alignment::Center)
.describe("a11y_name", item.label.clone());
for child in children {
row = row.child(child);
}
if let Some(id) = &item.id {
row = row.id(id.clone());
}
if let Some(key) = &item.key {
row = row.key(key.clone());
}
return row.into();
}
let handlers = item.on_select.clone();
let mut button = pressable(&part(StatusBarPart::Item), item.disabled)
.focus_entry(forced.is_some())
.describe("a11y_name", item.label.clone())
.on_click(move |_| {
for handler in &handlers {
handler();
}
});
if let Some(progress) = item.progress {
button = button.describe("a11y_valuenow", format!("{}", (progress * 100.0).round()));
}
for child in children {
button = button.child(child);
}
if let Some(id) = &item.id {
button = button.id(id.clone());
}
if let Some(key) = &item.key {
button = button.key(key.clone());
}
button.into()
}
pub fn item(&mut self, index: usize) -> Element {
let entry = &mut self.entries[index];
let element = entry.element.take().unwrap_or_else(Element::new);
Row::new()
.align_items(Alignment::Stretch)
.overflow_visible()
.flex_shrink(1.0)
.shrink_order(entry.order)
.shrink_limit(ShrinkLimit::Drop)
.child(element)
.into()
}
pub fn render(mut self) -> Element {
let mut root = Row::new()
.part(&self.part(StatusBarPart::Root))
.align_items(Alignment::Stretch)
.focus_group(FocusGroup::horizontal().one_stop(true))
.describe("a11y_role", "status")
.describe("a11y_name", self.label.clone().unwrap_or_default());
let split = self
.entries
.iter()
.position(|entry| !entry.leading)
.unwrap_or(self.entries.len());
for index in 0..split {
root = root.child(self.item(index));
}
root = root.child(Row::new().flex(1.0));
for index in split..self.entries.len() {
root = root.child(self.item(index));
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root.into()
}
}
impl crate::ui::Styled for StatusBar {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for StatusBar {
const NAME: &'static str = "status_bar";
type Renderer = StatusBarRenderer;
fn parts() -> crate::ui::Declared {
StatusBarPart::declared()
}
fn states() -> crate::ui::Declared {
StatusBarState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{
Destructive, DestructiveMuted, Foreground, Info, InfoMuted, Input, MutedForeground, Primary, PrimaryForeground,
Ring, StateHover, StatePressed, Success, SuccessMuted, Warning, WarningMuted,
};
use StatusBarPart::{Fill, Icon, Item, Label, Root, Track};
use StatusBarState::{Disabled, Pressable, Progress};
let mut recipe = Recipe::new()
.base(Root, |s| {
s.height(26.0)
.width(Length::Fill)
.background(PaletteColor::Bar)
.top_line(Border::inside(BorderSize::Sm, PaletteColor::Border))
})
.base(Item, |s| fading(s.padding_x(8.0).gap(5.0).foreground(Foreground)))
.base(Icon, |s| s.square(13.0))
.base(Label, |s| {
s.typography(TypographyStyle::Caption)
.weight(FontWeight::Normal)
.max_width(240.0)
})
.base(Track, |s| {
s.width(72.0).height(4.0).radius(RadiusSize::Full).background(Input)
})
.base(Fill, |s| s.height(4.0).background(Foreground))
.state(Pressable, Item, |s| s.cursor(CursorIcon::Pointer))
.state(Progress, Item, |s| s.gap(8.0))
.state(Progress, Label, |s| s.foreground(MutedForeground))
.state(Disabled, Item, |s| {
s.opacity(OpacityLevel::Disabled).cursor(CursorIcon::Default)
})
.when(Selector::new().state(Pressable).hovered(), Item, |s| s.veil(StateHover))
.when(Selector::new().state(Pressable).pressed(), Item, |s| {
s.veil(StatePressed)
})
.focus_visible(Item, |s| s.outline(Outline::new(BorderSize::Md, Ring).offset(-2.0)))
.variant(StatusBarVariant::Primary, Root, |s| s.background(Primary))
.variant(StatusBarVariant::Primary, Item, |s| {
s.background(Primary).foreground(PrimaryForeground)
});
for (variant, color, muted) in [
(StatusBarVariant::Info, Info, InfoMuted),
(StatusBarVariant::Success, Success, SuccessMuted),
(StatusBarVariant::Warning, Warning, WarningMuted),
(StatusBarVariant::Destructive, Destructive, DestructiveMuted),
] {
let (fill, ink) = (muted.clone(), color.clone());
let (label, done) = (color.clone(), color);
recipe = recipe
.variant(variant, Root, |s| s.background(muted))
.variant(variant, Item, |s| s.background(fill).foreground(ink))
.variant(variant, Label, |s| s.foreground(label))
.variant(variant, Fill, |s| s.background(done));
}
recipe
}
}