1use iced::widget::{column, container, text};
4use iced::{Background, Border, Color, Element, Length, Shadow, Vector};
5
6use crate::theme::{Tokens, fade, heading, mix, space, supporting, text_size};
7
8const HEADER_WRAPPING: text::Wrapping = text::Wrapping::WordOrGlyph;
11
12pub struct Card<'a, Message> {
14 title: Option<text::Fragment<'a>>,
15 description: Option<text::Fragment<'a>>,
16 body: Option<Element<'a, Message>>,
17 footer: Option<Element<'a, Message>>,
18 width: Length,
19}
20
21impl<Message> std::fmt::Debug for Card<'_, Message> {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 f.debug_struct("Card")
24 .field("title", &self.title)
25 .field("description", &self.description)
26 .field("body", &self.body.is_some())
27 .field("footer", &self.footer.is_some())
28 .field("width", &self.width)
29 .finish()
30 }
31}
32
33pub fn card<'a, Message>() -> Card<'a, Message> {
35 Card {
36 title: None,
37 description: None,
38 body: None,
39 footer: None,
40 width: Length::Shrink,
41 }
42}
43
44impl<'a, Message> Card<'a, Message> {
45 pub fn title(mut self, title: impl text::IntoFragment<'a>) -> Self {
47 self.title = Some(title.into_fragment());
48 self
49 }
50
51 pub fn description(mut self, description: impl text::IntoFragment<'a>) -> Self {
53 self.description = Some(description.into_fragment());
54 self
55 }
56
57 pub fn body(mut self, body: impl Into<Element<'a, Message>>) -> Self {
59 self.body = Some(body.into());
60 self
61 }
62
63 pub fn footer(mut self, footer: impl Into<Element<'a, Message>>) -> Self {
65 self.footer = Some(footer.into());
66 self
67 }
68
69 pub fn width(mut self, width: impl Into<Length>) -> Self {
70 self.width = width.into();
71 self
72 }
73
74 pub fn has_header(&self) -> bool {
76 self.title.is_some() || self.description.is_some()
77 }
78}
79
80impl<'a, Message: 'a> From<Card<'a, Message>> for Element<'a, Message> {
81 fn from(card: Card<'a, Message>) -> Self {
82 let has_header = card.has_header();
83 let mut sections = column![].spacing(space::LG).width(Length::Fill);
84
85 if has_header {
86 let mut header = column![].spacing(space::XS);
87 if let Some(title) = card.title {
88 header = header.push(heading(title, text_size::LG).wrapping(HEADER_WRAPPING));
89 }
90 if let Some(description) = card.description {
91 header = header.push(supporting(description).wrapping(HEADER_WRAPPING));
92 }
93 sections = sections.push(header);
94 }
95 if let Some(body) = card.body {
96 sections = sections.push(body);
97 }
98 if let Some(footer) = card.footer {
99 sections = sections.push(footer);
100 }
101
102 container(sections)
104 .padding(space::XL)
105 .width(card.width)
106 .clip(true)
107 .style(|theme| style(&Tokens::of(theme)))
108 .into()
109 }
110}
111
112#[derive(Debug, Clone, Copy, PartialEq)]
114pub struct Colours {
115 pub background: Color,
116 pub border: Color,
117 pub shadow: Color,
118}
119
120pub fn colours(tokens: &Tokens) -> Colours {
123 if tokens.is_dark {
124 let background = mix(tokens.background, tokens.foreground, 0.055);
125 return Colours {
126 background,
127 border: mix(background, tokens.border, 0.65),
128 shadow: fade(tokens.background, 0.5),
129 };
130 }
131 Colours {
132 background: tokens.background,
133 border: mix(tokens.background, tokens.border, 0.65),
134 shadow: fade(tokens.foreground, 0.08),
135 }
136}
137
138pub fn style(tokens: &Tokens) -> container::Style {
140 let colours = colours(tokens);
141 container::Style {
142 text_color: Some(tokens.foreground),
143 background: Some(Background::Color(colours.background)),
144 border: Border {
145 color: colours.border,
146 width: 1.0,
147 radius: 12.0.into(),
148 },
149 shadow: Shadow {
150 color: colours.shadow,
151 offset: Vector::new(0.0, 4.0),
152 blur_radius: 16.0,
153 },
154 snap: true,
155 }
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161 use crate::theme::{Config, dark, light};
162
163 fn luminance(color: Color) -> f32 {
164 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b
165 }
166
167 #[test]
168 fn default_builder_is_empty_and_shrinks() {
169 let c: Card<'_, ()> = card();
170 assert!(!c.has_header());
171 assert!(c.body.is_none());
172 assert!(c.footer.is_none());
173 assert_eq!(c.width, Length::Shrink);
174 }
175
176 #[test]
177 fn title_or_description_makes_a_header() {
178 assert!(card::<()>().title("Team").has_header());
179 assert!(card::<()>().description("Members").has_header());
180 }
181
182 #[test]
183 fn dark_surface_is_lighter_than_background() {
184 let tokens = Tokens::of(&dark());
185 let colours = colours(&tokens);
186 assert!(luminance(colours.background) > luminance(tokens.background));
187 }
188
189 #[test]
190 fn dark_cards_stay_between_the_page_and_floating_surfaces() {
191 for theme in [
192 dark(),
193 Config::dark()
194 .background(iced::color!(0x18202b))
195 .foreground(iced::color!(0xe3e9f3))
196 .build(),
197 ] {
198 let tokens = Tokens::of(&theme);
199 let card = colours(&tokens);
200 assert!(luminance(card.background) > luminance(tokens.background));
201 assert!(luminance(card.background) < luminance(tokens.popover));
202 assert!(luminance(card.shadow) < luminance(card.background));
203 assert!(card.shadow.a > 0.0);
204 }
205 }
206
207 #[test]
208 fn light_surface_matches_background_and_casts_a_shadow() {
209 let tokens = Tokens::of(&light());
210 let colours = colours(&tokens);
211 assert_eq!(colours.background, tokens.background);
212 assert!(colours.shadow.a > 0.0);
213 }
214
215 #[test]
216 fn border_is_subtle_but_visible() {
217 for theme in [light(), dark()] {
218 let tokens = Tokens::of(&theme);
219 let colours = colours(&tokens);
220 assert_ne!(colours.border, colours.background);
221 let contrast = (luminance(colours.border) - luminance(colours.background)).abs();
222 assert!(contrast < 0.2, "{contrast}");
223 assert_eq!(style(&tokens).border.width, 1.0);
224 }
225 }
226}