use std::collections::HashMap;
use teksilo_canvas::{EdgeInsets, Size};
use teksilo_core::styles::density::{density_min_size, spacing};
use teksilo_core::styles::{
BorderRecipe, ButtonRecipe, ButtonVariant, FillRecipe, PerStateRecipe, RecipeColor, ShapeRecipe,
};
use teksilo_tokens::{BorderRole, InputTokens, SurfaceRole, TargetAxes, TextRole};
use teksilo_widgets::styles::RecipeButtonStyle;
const HEIGHT: f32 = 40.0;
fn height(tokens: &InputTokens) -> f32 {
density_min_size(Size::new(0.0, HEIGHT), TargetAxes::HEIGHT, tokens).height
}
const PADDING_H: f32 = 24.0;
const PADDING_H_TEXT: f32 = 16.0;
const FOCUS_WIDTH: f32 = 3.0;
pub fn m3_button_style(tokens: &InputTokens) -> RecipeButtonStyle {
let mut recipes = HashMap::new();
recipes.insert(ButtonVariant::Filled, filled(tokens));
recipes.insert(ButtonVariant::Destructive, destructive(tokens));
recipes.insert(ButtonVariant::Tinted, tonal(tokens));
recipes.insert(ButtonVariant::Plain, outlined(tokens));
recipes.insert(ButtonVariant::Outlined, outlined(tokens));
recipes.insert(ButtonVariant::Ghost, text(tokens));
recipes.insert(ButtonVariant::Link, text(tokens));
let mut label_roles = HashMap::new();
label_roles.insert(ButtonVariant::Plain, TextRole::Accent);
label_roles.insert(ButtonVariant::Outlined, TextRole::Accent);
label_roles.insert(ButtonVariant::Ghost, TextRole::Accent);
label_roles.insert(ButtonVariant::Destructive, TextRole::OnError);
RecipeButtonStyle {
recipes,
label_roles,
}
}
fn filled(tokens: &InputTokens) -> ButtonRecipe {
ButtonRecipe {
shape: ShapeRecipe::Pill,
fill: PerStateRecipe {
idle: FillRecipe::solid(SurfaceRole::Accent),
hover: Some(FillRecipe::solid(SurfaceRole::AccentHover)),
pressed: Some(FillRecipe::solid(SurfaceRole::AccentPressed)),
focused: None,
disabled: Some(FillRecipe::solid(SurfaceRole::AccentDisabled)),
},
border: PerStateRecipe::uniform(BorderRecipe::none()),
shadow: PerStateRecipe::uniform(None),
padding: EdgeInsets::symmetric(spacing(PADDING_H, tokens), 0.0),
min_size: Size::new(0.0, height(tokens)),
}
}
fn destructive(tokens: &InputTokens) -> ButtonRecipe {
ButtonRecipe {
shape: ShapeRecipe::Pill,
fill: PerStateRecipe {
idle: FillRecipe::solid(RecipeColor::Text(TextRole::Error)),
hover: None,
pressed: None,
focused: None,
disabled: Some(FillRecipe::solid(SurfaceRole::AccentDisabled)),
},
border: PerStateRecipe::uniform(BorderRecipe::none()),
shadow: PerStateRecipe::uniform(None),
padding: EdgeInsets::symmetric(spacing(PADDING_H, tokens), 0.0),
min_size: Size::new(0.0, height(tokens)),
}
}
fn tonal(tokens: &InputTokens) -> ButtonRecipe {
ButtonRecipe {
shape: ShapeRecipe::Pill,
fill: PerStateRecipe {
idle: FillRecipe::solid(SurfaceRole::AccentSubtle),
hover: None,
pressed: None,
focused: None,
disabled: Some(FillRecipe::solid(SurfaceRole::AccentDisabled)),
},
border: PerStateRecipe::uniform(BorderRecipe::none()),
shadow: PerStateRecipe::uniform(None),
padding: EdgeInsets::symmetric(spacing(PADDING_H, tokens), 0.0),
min_size: Size::new(0.0, height(tokens)),
}
}
fn outlined(tokens: &InputTokens) -> ButtonRecipe {
ButtonRecipe {
shape: ShapeRecipe::Pill,
fill: PerStateRecipe {
idle: FillRecipe::solid(SurfaceRole::Transparent),
hover: Some(FillRecipe::solid(SurfaceRole::Hover)),
pressed: None,
focused: None,
disabled: None,
},
border: PerStateRecipe {
idle: BorderRecipe::solid(1.0, RecipeColor::Border(BorderRole::Strong)),
hover: None,
pressed: None,
focused: Some(BorderRecipe::solid(
FOCUS_WIDTH,
RecipeColor::Border(BorderRole::Focused),
)),
disabled: None,
},
shadow: PerStateRecipe::uniform(None),
padding: EdgeInsets::symmetric(spacing(PADDING_H, tokens), 0.0),
min_size: Size::new(0.0, height(tokens)),
}
}
fn text(tokens: &InputTokens) -> ButtonRecipe {
ButtonRecipe {
shape: ShapeRecipe::Pill,
fill: PerStateRecipe {
idle: FillRecipe::solid(SurfaceRole::Transparent),
hover: Some(FillRecipe::solid(SurfaceRole::Hover)),
pressed: None,
focused: None,
disabled: None,
},
border: PerStateRecipe {
idle: BorderRecipe::none(),
hover: None,
pressed: None,
focused: Some(BorderRecipe::solid(
FOCUS_WIDTH,
RecipeColor::Border(BorderRole::Focused),
)),
disabled: None,
},
shadow: PerStateRecipe::uniform(None),
padding: EdgeInsets::symmetric(spacing(PADDING_H_TEXT, tokens), 0.0),
min_size: Size::new(0.0, height(tokens)),
}
}