use std::sync::Arc;
use super::{
button::{Button, ButtonPart, ButtonShape, ButtonVariant},
names,
part_style::Length,
recipe::{Case, Interaction, Name, Recipe},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{Density, IconSize, ShadowStyle, Theme},
node::{Element, EventHandler, IntoMouseEventHandler},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FabPart {
Root,
Icon,
Label,
}
names!(FabPart {
Root => "root",
Icon => "icon",
Label => "label",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FabState {
Extended,
Disabled,
Loading,
}
names!(FabState {
Extended => "extended",
Disabled => "disabled",
Loading => "loading",
});
pub type FabRenderer = Arc<dyn Fn(Fab) -> Element + Send + Sync>;
pub struct Fab {
label: Arc<str>,
icon: Name,
variant: Name,
extended: bool,
disabled: bool,
loading: bool,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
on_click: Vec<EventHandler<crate::app::events::MouseEvent>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Fab {
pub fn new(label: &str, icon: impl Into<Name>) -> Self {
Self {
label: Arc::from(label),
icon: icon.into(),
variant: ButtonVariant::Primary.into(),
extended: false,
disabled: false,
loading: false,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
on_click: Vec::new(),
id: None,
key: None,
}
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn extended(mut self, extended: bool) -> Self {
self.extended = extended;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn on_click(mut self, handler: impl IntoMouseEventHandler) -> Self {
self.on_click.push(handler.into_event_handler());
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 rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
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 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
}
pub fn build(self, theme: &Theme) -> Element {
let mut case = Case::new()
.variant(self.variant)
.breakpoint(super::current_breakpoint())
.state(FabState::Extended, self.extended)
.state(FabState::Disabled, self.disabled)
.state(FabState::Loading, self.loading);
for state in &self.states {
case = case.state(state.clone(), true);
}
let recipe = theme
.resolve::<Fab>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let rules = recipe
.rules_for(FabPart::Root, ButtonPart::Root)
.extend(&recipe.rules_for(FabPart::Icon, ButtonPart::Icon))
.extend(&recipe.rules_for(FabPart::Icon, ButtonPart::Spinner))
.extend(&recipe.rules_for(FabPart::Label, ButtonPart::Label));
let mut button = Button::new(&self.label)
.variant(self.variant)
.icon(self.icon)
.icon_only(!self.extended)
.shape(ButtonShape::Pill)
.disabled(self.disabled)
.loading(self.loading)
.force(self.forced)
.rules(&rules);
for handler in self.on_click {
button = button.on_click(handler);
}
if let Some(id) = self.id {
button = button.id(id);
}
if let Some(key) = self.key {
button = button.key(key);
}
button.build(theme)
}
}
impl From<Fab> for Element {
fn from(fab: Fab) -> Self {
fab.build(¤t_theme())
}
}
impl crate::ui::Styled for Fab {
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 Fab {
const NAME: &'static str = "fab";
type Renderer = FabRenderer;
fn parts() -> crate::ui::Declared {
FabPart::declared()
}
fn states() -> crate::ui::Declared {
FabState::declared()
}
fn recipe() -> Recipe {
use FabPart::{Icon, Label, Root};
Recipe::new()
.base(Root, |s| s.square(Density::Touch).shadow(ShadowStyle::Lg))
.base(Icon, |s| s.square(IconSize::Lg))
.state(FabState::Extended, Root, |s| {
s.width(Length::Auto).padding_x(20.0).gap(8.0)
})
.state(FabState::Extended, Label, |s| s.font_size(14.0))
}
}