use std::{collections::HashMap, sync::Arc, time::Duration};
use super::{PaletteColor, role_name::RoleName};
use crate::{animation::Easing, node::Outline};
macro_rules! token_table {
(
$(#[$role_doc:meta])* $role:ident,
$(#[$table_doc:meta])* $table:ident,
$value:ty,
$what:literal,
{ $( $(#[$doc:meta])* $variant:ident, $field:ident, $name:literal, $default:expr; )* }
) => {
$(#[$role_doc])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum $role {
$( $(#[$doc])* $variant, )*
Extra(RoleName),
}
impl $role {
pub fn extra(name: impl AsRef<str>) -> Self {
Self::Extra(RoleName::new(name))
}
pub fn as_str(self) -> &'static str {
match self {
$( Self::$variant => $name, )*
Self::Extra(name) => name.as_str(),
}
}
pub fn all() -> &'static [$role] {
&[ $( Self::$variant, )* ]
}
}
impl From<&str> for $role {
fn from(name: &str) -> Self {
Self::extra(name)
}
}
impl From<Arc<str>> for $role {
fn from(name: Arc<str>) -> Self {
Self::extra(name)
}
}
$(#[$table_doc])*
#[derive(Clone, Debug, PartialEq)]
pub struct $table {
$( $(#[$doc])* pub $field: $value, )*
pub extra: HashMap<Arc<str>, $value>,
}
impl $table {
pub fn new() -> Self {
Self::default()
}
pub fn get(&self, role: impl Into<$role>) -> $value {
let role = role.into();
self
.try_get(role)
.unwrap_or_else(|| panic!(concat!($what, " not found: {}"), role.as_str()))
}
pub fn try_get(&self, role: impl Into<$role>) -> Option<$value> {
match role.into() {
$( $role::$variant => Some(self.$field), )*
$role::Extra(name) => self.extra.get(name.as_str()).copied(),
}
}
pub fn set(&mut self, role: impl Into<$role>, value: $value) {
match role.into() {
$( $role::$variant => self.$field = value, )*
$role::Extra(name) => {
self.extra.insert(Arc::from(name.as_str()), value);
}
}
}
}
impl Default for $table {
fn default() -> Self {
Self {
$( $field: $default, )*
extra: HashMap::new(),
}
}
}
};
}
token_table! {
ControlSize,
ThemeControlSizes,
f32,
"control size",
{
Sm, sm, "sm", 32.0;
Md, md, "md", 36.0;
Lg, lg, "lg", 40.0;
}
}
token_table! {
IconSize,
ThemeIconSizes,
f32,
"icon size",
{
Xs, xs, "xs", 12.0;
Sm, sm, "sm", 14.0;
Md, md, "md", 16.0;
Lg, lg, "lg", 20.0;
Xl, xl, "xl", 24.0;
}
}
token_table! {
Density,
ThemeDensities,
f32,
"density",
{
Compact, compact, "compact", 28.0;
Default, default, "default", 36.0;
Comfortable, comfortable, "comfortable", 44.0;
Touch, touch, "touch", 48.0;
}
}
token_table! {
MotionDuration,
ThemeDurations,
Duration,
"duration",
{
Fast, fast, "fast", Duration::from_millis(100);
Base, base, "base", Duration::from_millis(160);
Slow, slow, "slow", Duration::from_millis(240);
Slower, slower, "slower", Duration::from_millis(400);
}
}
token_table! {
MotionEasing,
ThemeEasings,
Easing,
"easing",
{
Standard, standard, "standard", Easing::CubicBezier { x1: 0.2, y1: 0.0, x2: 0.0, y2: 1.0 };
Enter, enter, "enter", Easing::CubicBezier { x1: 0.0, y1: 0.0, x2: 0.0, y2: 1.0 };
Exit, exit, "exit", Easing::CubicBezier { x1: 0.3, y1: 0.0, x2: 1.0, y2: 1.0 };
}
}
token_table! {
OpacityLevel,
ThemeOpacities,
f32,
"opacity level",
{
Disabled, disabled, "disabled", 0.5;
Muted, muted, "muted", 0.7;
}
}
token_table! {
BlurSize,
ThemeBlurs,
f32,
"blur size",
{
Thin, thin, "thin", 8.0;
Regular, regular, "regular", 20.0;
Thick, thick, "thick", 40.0;
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeFocusRing {
pub width: f32,
pub offset: f32,
pub color: PaletteColor,
}
impl ThemeFocusRing {
pub fn outline(&self) -> Outline {
Outline::new(self.width, self.color.clone()).offset(self.offset)
}
}
impl Default for ThemeFocusRing {
fn default() -> Self {
Self {
width: 2.0,
offset: 2.0,
color: PaletteColor::Ring,
}
}
}
#[cfg(test)]
mod tests {
use super::{ControlSize, MotionDuration, ThemeControlSizes, ThemeDurations};
#[test]
fn a_table_holds_a_value_for_each_role_and_for_the_roles_an_app_names() {
let mut sizes = ThemeControlSizes::default();
assert_eq!(sizes.get(ControlSize::Md), 36.0);
sizes.set(ControlSize::Md, 44.0);
assert_eq!(sizes.md, 44.0);
assert_eq!(sizes.try_get("hero"), None);
sizes.set("hero", 56.0);
assert_eq!(sizes.get(ControlSize::extra("hero")), 56.0);
assert_eq!(ControlSize::extra("hero").as_str(), "hero");
}
#[test]
fn the_roles_of_a_scale_are_listed_in_its_order() {
let durations = ThemeDurations::default();
let lengths: Vec<_> = MotionDuration::all()
.iter()
.map(|role| durations.get(*role).as_millis())
.collect();
assert_eq!(lengths, [100, 160, 240, 400]);
}
}