use iced::widget::button::Status;
use iced::widget::{button, container};
use iced::{Background, Border, Color, Shadow};
use super::entry::{Emphasis, Tone};
use crate::theme::{Tokens, fade, mix, on, radius};
pub fn tone(tokens: &Tokens, tone: Tone) -> Color {
match tone {
Tone::Primary => tokens.primary,
Tone::Neutral => tokens.muted_foreground,
Tone::Success => tokens.success,
Tone::Warning => tokens.warning,
Tone::Destructive => tokens.destructive,
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Chip {
pub background: Color,
pub foreground: Color,
pub stripe: Color,
}
pub fn chip(tokens: &Tokens, entry_tone: Tone, selected: bool, status: Status) -> Chip {
let colour = tone(tokens, entry_tone);
if selected {
return Chip {
background: colour,
foreground: on(colour),
stripe: colour,
};
}
let wash = match status {
Status::Active | Status::Disabled => 0.16,
Status::Hovered => 0.26,
Status::Pressed => 0.34,
};
Chip {
background: mix(tokens.background, colour, wash),
foreground: tokens.foreground,
stripe: colour,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Look {
pub selected: bool,
pub emphasis: Emphasis,
pub preview: bool,
}
impl Look {
pub fn hatched(self) -> bool {
self.emphasis == Emphasis::Hatched && !self.selected && !self.preview
}
pub fn struck(self) -> bool {
self.emphasis == Emphasis::Struck
}
}
pub const HATCH_ALPHA: f32 = 0.35;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Marked {
pub chip: Chip,
pub border: Color,
pub hatch: Option<Color>,
pub strike: Option<Color>,
}
pub fn marked(tokens: &Tokens, entry_tone: Tone, look: Look, status: Status) -> Marked {
let colour = tone(tokens, entry_tone);
let solid = chip(tokens, entry_tone, look.selected, status);
let mut marked = Marked {
chip: solid,
border: Color::TRANSPARENT,
hatch: None,
strike: None,
};
if look.preview {
let colours = preview(tokens);
marked.chip = Chip {
background: colours.background,
foreground: tokens.muted_foreground,
stripe: colours.border,
};
marked.border = colours.border;
if look.struck() {
marked.strike = Some(tokens.muted_foreground);
}
return marked;
}
if look.selected {
if look.struck() {
marked.strike = Some(solid.foreground);
}
return marked;
}
match look.emphasis {
Emphasis::Solid => {}
Emphasis::Outlined => {
let hovered = matches!(status, Status::Hovered | Status::Pressed);
marked.chip.background = if hovered {
mix(tokens.background, colour, 0.12)
} else {
tokens.background
};
marked.border = colour;
}
Emphasis::Struck => {
marked.chip.foreground = tokens.muted_foreground;
marked.strike = Some(tokens.muted_foreground);
}
Emphasis::Faded => {
marked.chip.background = mix(tokens.background, solid.background, 0.5);
marked.chip.foreground = tokens.muted_foreground;
marked.chip.stripe = mix(tokens.background, colour, 0.5);
}
Emphasis::Hatched => marked.hatch = Some(fade(colour, HATCH_ALPHA)),
}
marked
}
pub fn marked_button(
tokens: &Tokens,
entry_tone: Tone,
look: Look,
status: Status,
) -> button::Style {
let marked = marked(tokens, entry_tone, look, status);
button::Style {
background: Some(Background::Color(marked.chip.background)),
text_color: marked.chip.foreground,
border: Border {
color: marked.border,
width: if marked.border == Color::TRANSPARENT {
0.0
} else {
1.0
},
radius: radius::SM.into(),
},
shadow: Shadow::default(),
snap: true,
}
}
pub fn chip_button(
tokens: &Tokens,
entry_tone: Tone,
selected: bool,
status: Status,
) -> button::Style {
let chip = chip(tokens, entry_tone, selected, status);
button::Style {
background: Some(Background::Color(chip.background)),
text_color: chip.foreground,
border: Border {
radius: radius::SM.into(),
..Border::default()
},
shadow: Shadow::default(),
snap: true,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Day {
pub today: bool,
pub selected: bool,
pub in_month: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DayNumber {
pub background: Option<Color>,
pub foreground: Color,
}
pub fn day_number(tokens: &Tokens, day: Day) -> DayNumber {
if day.today {
return DayNumber {
background: Some(tokens.primary),
foreground: tokens.primary_foreground,
};
}
DayNumber {
background: None,
foreground: if day.in_month {
tokens.foreground
} else {
tokens.muted_foreground
},
}
}
pub fn cell(tokens: &Tokens, day: Day, status: Status) -> button::Style {
let hovered = matches!(status, Status::Hovered | Status::Pressed);
let background = if day.selected {
Some(tokens.accent)
} else if hovered {
Some(mix(tokens.background, tokens.accent, 0.6))
} else if !day.in_month {
Some(mix(tokens.background, tokens.muted, 0.4))
} else {
None
};
button::Style {
background: background.map(Background::Color),
text_color: day_number(tokens, day).foreground,
border: Border {
color: if day.selected {
tokens.ring
} else {
Color::TRANSPARENT
},
width: 1.0,
radius: 0.0.into(),
},
shadow: Shadow::default(),
snap: true,
}
}
pub fn year_day(tokens: &Tokens, day: Day, status: Status) -> button::Style {
let hovered = matches!(status, Status::Hovered | Status::Pressed);
let number = day_number(tokens, day);
let background = if day.today {
number.background
} else if day.selected || hovered {
Some(tokens.accent)
} else {
None
};
button::Style {
background: background.map(Background::Color),
text_color: number.foreground,
border: Border {
color: if day.selected {
tokens.ring
} else {
Color::TRANSPARENT
},
width: 1.0,
radius: radius::FULL.into(),
},
shadow: Shadow::default(),
snap: true,
}
}
pub fn row(tokens: &Tokens, selected: bool, hovered: bool) -> container::Style {
let background = if selected {
Some(tokens.accent)
} else if hovered {
Some(mix(tokens.background, tokens.accent, 0.6))
} else {
None
};
container::Style {
background: background.map(Background::Color),
border: Border {
radius: radius::MD.into(),
..Border::default()
},
..container::Style::default()
}
}
pub fn frame(tokens: &Tokens) -> container::Style {
container::Style {
border: Border {
color: tokens.border,
width: 1.0,
radius: radius::LG.into(),
},
..container::Style::default()
}
}
pub fn entry_line(
tokens: &Tokens,
entry_tone: Tone,
selected: bool,
status: Status,
) -> button::Style {
if selected {
return chip_button(tokens, entry_tone, true, status);
}
let background = match status {
Status::Hovered | Status::Pressed => Some(tokens.accent),
Status::Active | Status::Disabled => None,
};
button::Style {
background: background.map(Background::Color),
text_color: tokens.foreground,
border: Border {
radius: radius::SM.into(),
..Border::default()
},
shadow: Shadow::default(),
snap: true,
}
}
pub fn marked_line(tokens: &Tokens, entry_tone: Tone, look: Look, status: Status) -> button::Style {
if look.selected {
return marked_button(tokens, entry_tone, look, status);
}
let mut style = entry_line(tokens, entry_tone, false, status);
if look.preview {
let colours = preview(tokens);
style.background = Some(Background::Color(colours.background));
style.border = Border {
color: colours.border,
width: 1.0,
radius: radius::SM.into(),
};
}
if look.preview || matches!(look.emphasis, Emphasis::Struck | Emphasis::Faded) {
style.text_color = tokens.muted_foreground;
}
style
}
pub fn line(tokens: &Tokens) -> Color {
tokens.border
}
pub fn now_line(tokens: &Tokens) -> Color {
tokens.destructive
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Preview {
pub background: Color,
pub border: Color,
}
pub fn preview(tokens: &Tokens) -> Preview {
Preview {
background: fade(tokens.primary, 0.16),
border: tokens.primary,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::{dark, light};
#[test]
fn every_tone_has_its_own_colour() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let colours: Vec<_> = Tone::ALL.iter().map(|t| tone(&tokens, *t)).collect();
for (index, colour) in colours.iter().enumerate() {
assert!(!colours[..index].contains(colour), "{:?}", Tone::ALL[index]);
}
}
}
#[test]
fn a_chip_is_a_wash_that_deepens_under_the_pointer() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for entry_tone in Tone::ALL {
let rest = chip(&tokens, entry_tone, false, Status::Active);
let hovered = chip(&tokens, entry_tone, false, Status::Hovered);
let pressed = chip(&tokens, entry_tone, false, Status::Pressed);
assert_eq!(rest.stripe, tone(&tokens, entry_tone));
assert_eq!(rest.foreground, tokens.foreground);
assert_ne!(rest.background, tokens.background);
assert_ne!(rest.background, hovered.background);
assert_ne!(hovered.background, pressed.background);
assert_ne!(rest.background, rest.stripe);
}
}
}
#[test]
fn a_selected_chip_is_filled_and_keeps_its_text_readable() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for entry_tone in Tone::ALL {
let colour = tone(&tokens, entry_tone);
for status in [Status::Active, Status::Hovered, Status::Pressed] {
let selected = chip(&tokens, entry_tone, true, status);
assert_eq!(selected.background, colour);
assert_eq!(selected.foreground, on(colour));
}
}
}
}
#[test]
fn a_read_only_calendar_keeps_its_entries_at_full_colour() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for entry_tone in Tone::ALL {
assert_eq!(
chip(&tokens, entry_tone, false, Status::Disabled),
chip(&tokens, entry_tone, false, Status::Active)
);
assert_eq!(
entry_line(&tokens, entry_tone, false, Status::Disabled),
entry_line(&tokens, entry_tone, false, Status::Active)
);
}
}
}
#[test]
fn a_timed_entry_in_the_month_is_plain_until_hovered_or_selected() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for entry_tone in Tone::ALL {
let rest = entry_line(&tokens, entry_tone, false, Status::Active);
assert_eq!(rest.background, None);
assert_eq!(rest.text_color, tokens.foreground);
for status in [Status::Hovered, Status::Pressed] {
let hovered = entry_line(&tokens, entry_tone, false, status);
assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
}
let selected = entry_line(&tokens, entry_tone, true, Status::Active);
assert_eq!(
selected,
chip_button(&tokens, entry_tone, true, Status::Active)
);
}
}
}
#[test]
fn the_chip_button_uses_the_chip_colours() {
let tokens = Tokens::of(&dark());
let colours = chip(&tokens, Tone::Warning, false, Status::Hovered);
let style = chip_button(&tokens, Tone::Warning, false, Status::Hovered);
assert_eq!(
style.background,
Some(Background::Color(colours.background))
);
assert_eq!(style.text_color, colours.foreground);
assert_eq!(style.border.width, 0.0);
}
#[test]
fn today_is_a_filled_pill_and_other_months_sit_back() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let today = day_number(
&tokens,
Day {
today: true,
in_month: true,
selected: false,
},
);
assert_eq!(today.background, Some(tokens.primary));
assert_eq!(today.foreground, tokens.primary_foreground);
let inside = day_number(
&tokens,
Day {
in_month: true,
..Day::default()
},
);
let outside = day_number(&tokens, Day::default());
assert_eq!(inside.background, None);
assert_eq!(inside.foreground, tokens.foreground);
assert_eq!(outside.foreground, tokens.muted_foreground);
}
}
#[test]
fn cells_show_selection_hover_and_other_months() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let inside = Day {
in_month: true,
..Day::default()
};
let rest = cell(&tokens, inside, Status::Active);
assert_eq!(rest.background, None);
assert_eq!(rest.border.color, Color::TRANSPARENT);
assert_eq!(cell(&tokens, inside, Status::Disabled), rest);
let hovered = cell(&tokens, inside, Status::Hovered);
assert!(hovered.background.is_some());
assert_eq!(cell(&tokens, inside, Status::Pressed), hovered);
let selected = cell(
&tokens,
Day {
selected: true,
..inside
},
Status::Hovered,
);
assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
assert_eq!(selected.border.color, tokens.ring);
assert_ne!(selected.background, hovered.background);
let outside = cell(&tokens, Day::default(), Status::Active);
assert!(outside.background.is_some());
assert_ne!(outside.background, selected.background);
assert_eq!(outside.text_color, tokens.muted_foreground);
}
}
#[test]
fn days_of_the_year_mark_today_selection_and_hover() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let plain = Day {
in_month: true,
..Day::default()
};
let rest = year_day(&tokens, plain, Status::Active);
assert_eq!(rest.background, None);
assert_eq!(rest.text_color, tokens.foreground);
assert_eq!(year_day(&tokens, plain, Status::Disabled), rest);
let hovered = year_day(&tokens, plain, Status::Hovered);
assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
assert_eq!(year_day(&tokens, plain, Status::Pressed), hovered);
let today = year_day(
&tokens,
Day {
today: true,
..plain
},
Status::Hovered,
);
assert_eq!(today.background, Some(Background::Color(tokens.primary)));
assert_eq!(today.text_color, tokens.primary_foreground);
let selected = year_day(
&tokens,
Day {
selected: true,
..plain
},
Status::Active,
);
assert_eq!(selected.border.color, tokens.ring);
assert_eq!(rest.border.color, Color::TRANSPARENT);
}
}
#[test]
fn rows_show_selection_and_hover() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
assert_eq!(row(&tokens, false, false).background, None);
let hovered = row(&tokens, false, true);
let selected = row(&tokens, true, false);
assert!(hovered.background.is_some());
assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
assert_ne!(hovered.background, selected.background);
assert_eq!(row(&tokens, true, true), selected);
}
}
#[test]
fn a_custom_theme_reaches_every_part() {
use crate::theme::Config;
use iced::color;
let theme = Config::light()
.primary(color!(0x6d28d9))
.accent(color!(0xede9fe))
.border(color!(0xddd6fe))
.success(color!(0x0d9488))
.warning(color!(0xea580c))
.destructive(color!(0xe11d48))
.foreground(color!(0x1e1b2e))
.build();
let tokens = Tokens::of(&theme);
assert_eq!(tone(&tokens, Tone::Primary), color!(0x6d28d9));
assert_eq!(tone(&tokens, Tone::Success), color!(0x0d9488));
assert_eq!(tone(&tokens, Tone::Warning), color!(0xea580c));
assert_eq!(tone(&tokens, Tone::Destructive), color!(0xe11d48));
assert_eq!(
chip(&tokens, Tone::Primary, true, Status::Active).background,
color!(0x6d28d9)
);
assert_eq!(
chip(&tokens, Tone::Success, false, Status::Active).foreground,
color!(0x1e1b2e)
);
let today = Day {
today: true,
in_month: true,
selected: false,
};
assert_eq!(
day_number(&tokens, today).background,
Some(color!(0x6d28d9))
);
assert_eq!(
year_day(&tokens, today, Status::Active).background,
Some(Background::Color(color!(0x6d28d9)))
);
let selected = Day {
selected: true,
in_month: true,
today: false,
};
assert_eq!(
cell(&tokens, selected, Status::Active).background,
Some(Background::Color(color!(0xede9fe)))
);
assert_eq!(
row(&tokens, true, false).background,
Some(Background::Color(color!(0xede9fe)))
);
assert_eq!(
entry_line(&tokens, Tone::Primary, false, Status::Hovered).background,
Some(Background::Color(color!(0xede9fe)))
);
assert_eq!(line(&tokens), color!(0xddd6fe));
assert_eq!(frame(&tokens).border.color, color!(0xddd6fe));
assert_eq!(now_line(&tokens), color!(0xe11d48));
assert_eq!(preview(&tokens).border, color!(0x6d28d9));
}
#[test]
fn lines_frames_and_previews_come_from_the_tokens() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
assert_eq!(line(&tokens), tokens.border);
assert_eq!(now_line(&tokens), tokens.destructive);
assert_eq!(frame(&tokens).border.color, tokens.border);
let preview = preview(&tokens);
assert_eq!(preview.border, tokens.primary);
assert!(preview.background.a < 0.5);
}
}
fn look(emphasis: Emphasis) -> Look {
Look {
emphasis,
..Look::default()
}
}
#[test]
fn a_solid_entry_looks_as_a_plain_chip() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
for status in [
Status::Active,
Status::Hovered,
Status::Pressed,
Status::Disabled,
] {
for selected in [false, true] {
let marked = marked(
&tokens,
Tone::Success,
Look {
selected,
..Look::default()
},
status,
);
assert_eq!(marked.chip, chip(&tokens, Tone::Success, selected, status));
assert_eq!(marked.border, Color::TRANSPARENT);
assert_eq!((marked.hatch, marked.strike), (None, None));
}
}
}
}
#[test]
fn each_emphasis_marks_the_entry_its_own_way() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let colour = tone(&tokens, Tone::Warning);
let at = |emphasis| marked(&tokens, Tone::Warning, look(emphasis), Status::Active);
let outlined = at(Emphasis::Outlined);
assert_eq!(outlined.border, colour);
assert_eq!(outlined.chip.background, tokens.background);
let struck = at(Emphasis::Struck);
assert_eq!(struck.strike, Some(tokens.muted_foreground));
assert_eq!(struck.chip.foreground, tokens.muted_foreground);
let faded = at(Emphasis::Faded);
let solid = at(Emphasis::Solid);
assert_eq!(faded.chip.foreground, tokens.muted_foreground);
assert_ne!(faded.chip.background, solid.chip.background);
let hatched = at(Emphasis::Hatched);
assert_eq!(
hatched.hatch.map(|hatch| hatch.a),
Some(colour.a * HATCH_ALPHA)
);
assert_eq!(hatched.chip, solid.chip);
}
}
#[test]
fn a_selected_entry_is_filled_and_keeps_its_strike() {
let tokens = Tokens::of(&light());
for emphasis in Emphasis::ALL {
let selected = Look {
selected: true,
..look(emphasis)
};
let marked = marked(&tokens, Tone::Primary, selected, Status::Active);
assert_eq!(
marked.chip,
chip(&tokens, Tone::Primary, true, Status::Active)
);
assert_eq!(marked.hatch, None, "{emphasis:?}");
assert_eq!(marked.strike.is_some(), emphasis == Emphasis::Struck);
}
}
#[test]
fn a_preview_is_a_faint_outline_whatever_its_emphasis() {
for theme in [light(), dark()] {
let tokens = Tokens::of(&theme);
let colours = preview(&tokens);
for emphasis in Emphasis::ALL {
let pending = Look {
preview: true,
..look(emphasis)
};
let marked = marked(&tokens, Tone::Success, pending, Status::Hovered);
assert_eq!(marked.border, colours.border);
assert_eq!(marked.chip.background, colours.background);
assert_eq!(marked.chip.foreground, tokens.muted_foreground);
assert!(!pending.hatched());
}
}
}
#[test]
fn button_and_line_styles_carry_the_look() {
let tokens = Tokens::of(&dark());
let outlined = marked_button(
&tokens,
Tone::Primary,
look(Emphasis::Outlined),
Status::Active,
);
assert_eq!(outlined.border.width, 1.0);
let solid = marked_button(
&tokens,
Tone::Primary,
look(Emphasis::Solid),
Status::Active,
);
assert_eq!(solid.border.width, 0.0);
let struck = marked_line(
&tokens,
Tone::Primary,
look(Emphasis::Struck),
Status::Active,
);
assert_eq!(struck.text_color, tokens.muted_foreground);
let plain = marked_line(
&tokens,
Tone::Primary,
look(Emphasis::Solid),
Status::Active,
);
assert_eq!(
plain,
entry_line(&tokens, Tone::Primary, false, Status::Active)
);
}
}