use std::sync::Arc;
use super::{
button::{Button, ButtonPart, ButtonSize, ButtonVariant},
names,
recipe::{Case, Interaction, Name, Recipe},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{BorderSize, PaletteColor, RadiusSize, Theme},
components::Row,
layout::Alignment,
node::{Element, FocusGroup, border::Border},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonGroupVariant {
#[default]
Outline,
Primary,
Secondary,
Destructive,
}
names!(ButtonGroupVariant {
Outline => "outline",
Primary => "primary",
Secondary => "secondary",
Destructive => "destructive",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ButtonGroupPart {
Root,
Item,
Separator,
}
names!(ButtonGroupPart {
Root => "root",
Item => "item",
Separator => "separator",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ButtonGroupState {
Disabled,
}
names!(ButtonGroupState {
Disabled => "disabled",
});
pub type ButtonGroupSize = ButtonSize;
pub type ButtonGroupRenderer = Arc<dyn Fn(ButtonGroup) -> Element + Send + Sync>;
pub struct ButtonGroup {
items: Vec<Button>,
variant: Name,
size: Name,
label: Option<Arc<str>>,
disabled: bool,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl ButtonGroup {
pub fn new(buttons: impl IntoIterator<Item = Button>) -> Self {
Self {
items: buttons.into_iter().collect(),
variant: ButtonGroupVariant::default().into(),
size: ButtonSize::default().into(),
label: None,
disabled: false,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.size = size.into();
self
}
pub fn button(mut self, button: Button) -> Self {
self.items.push(button);
self
}
pub fn buttons(mut self, buttons: impl IntoIterator<Item = Button>) -> Self {
self.items.extend(buttons);
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
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
}
pub fn build(self, theme: &Theme) -> Element {
let mut case = Case::new()
.variant(self.variant.clone())
.size(self.size.clone())
.breakpoint(super::current_breakpoint())
.state(ButtonGroupState::Disabled, self.disabled);
for state in &self.states {
case = case.state(state.clone(), true);
}
let recipe = theme
.resolve::<ButtonGroup>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let item = recipe.rules_for(ButtonGroupPart::Item, ButtonPart::Root).state(
super::ButtonState::IconOnly,
ButtonPart::Root,
|s| s.padding_x(0.0),
);
let of_button: Name = if self.variant == Name::from(ButtonGroupVariant::Outline) {
ButtonVariant::Ghost.into()
} else {
self.variant
};
let separator = recipe.part(ButtonGroupPart::Separator);
let separated = separator.rest().width.is_some();
let mut root = Row::new().part(&recipe.part(ButtonGroupPart::Root))
.focus_group(FocusGroup::horizontal())
.align_items(Alignment::Stretch)
.clip()
.describe("a11y_role", "group");
if let Some(label) = &self.label {
root = root.describe("a11y_name", label.clone());
}
for (index, mut button) in self.items.into_iter().enumerate() {
if index > 0 && separated {
root = root.child(Row::new().part(&separator));
}
if self.disabled {
button = button.disabled(true);
}
if index == 0 && self.forced.is_some() {
button = button.force(self.forced);
}
root = root.child(
button
.variant(of_button.clone())
.size(self.size.clone())
.rules(&item)
.build(theme),
);
}
if let Some(id) = self.id {
root = root.id(id);
}
if let Some(key) = self.key {
root = root.key(key);
}
root.into()
}
}
impl From<ButtonGroup> for Element {
fn from(group: ButtonGroup) -> Self {
group.build(¤t_theme())
}
}
impl crate::ui::Styled for ButtonGroup {
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 ButtonGroup {
const NAME: &'static str = "button_group";
type Renderer = ButtonGroupRenderer;
fn parts() -> crate::ui::Declared {
ButtonGroupPart::declared()
}
fn states() -> crate::ui::Declared {
ButtonGroupState::declared()
}
fn recipe() -> Recipe {
use ButtonGroupPart::{Item, Root, Separator};
use PaletteColor::{Background, Input};
let filled = |recipe: Recipe, variant: ButtonGroupVariant| recipe.variant(variant, Root, |s| s.gap(1.0));
let recipe = Recipe::new()
.base(Root, |s| s.radius(RadiusSize::Md))
.base(Item, |s| s.radius(0.0).padding_x(14.0))
.variant(ButtonGroupVariant::Outline, Root, |s| {
s.background(Background).border(Border::inside(BorderSize::Sm, Input))
})
.variant(ButtonGroupVariant::Outline, Separator, |s| {
s.width(1.0).background(Input)
});
let recipe = filled(recipe, ButtonGroupVariant::Primary);
let recipe = filled(recipe, ButtonGroupVariant::Secondary);
filled(recipe, ButtonGroupVariant::Destructive)
}
}