iced_aw/style/
card.rs

1//! Displays a [`Card`](crate::widget::Card).
2//!
3//! *This API requires the following crate features to be activated: card*
4
5use super::{Status, StyleFn, colors};
6use iced_core::{Background, Color, Theme};
7
8/// The appearance of a [`Card`](crate::widget::card::Card).
9#[derive(Clone, Copy, Debug)]
10pub struct Style {
11    /// The background of the [`Card`](crate::widget::card::Card).
12    pub background: Background,
13
14    /// The border radius of the [`Card`](crate::widget::card::Card).
15    pub border_radius: f32,
16
17    /// The border width of the [`Card`](crate::widget::card::Card).
18    pub border_width: f32,
19
20    /// The border color of the [`Card`](crate::widget::card::Card).
21    pub border_color: Color,
22
23    /// The background of the head of the [`Card`](crate::widget::card::Card).
24    pub head_background: Background,
25
26    /// The text color of the head of the [`Card`](crate::widget::card::Card).
27    pub head_text_color: Color,
28
29    /// The background of the body of the [`Card`](crate::widget::card::Card).
30    pub body_background: Background,
31
32    /// The text color of the body of the [`Card`](crate::widget::card::Card).
33    pub body_text_color: Color,
34
35    /// The background of the foot of the [`Card`](crate::widget::card::Card).
36    pub foot_background: Background,
37
38    /// The text color of the foot of the [`Card`](crate::widget::card::Card).
39    pub foot_text_color: Color,
40
41    /// The color of the close icon of the [`Card`](crate::widget::card::Card).
42    pub close_color: Color,
43}
44
45/// The appearance of a [`Card`](crate::widget::card::Card).
46pub trait Catalog {
47    ///Style for the trait to use.
48    type Class<'a>;
49
50    /// The default class produced by the [`Catalog`].
51    fn default<'a>() -> Self::Class<'a>;
52
53    /// The [`Style`] of a class with the given status.
54    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
55}
56
57impl Default for Style {
58    fn default() -> Self {
59        Self {
60            background: Color::WHITE.into(),
61            border_radius: 10.0,
62            border_width: 1.0,
63            border_color: [0.87, 0.87, 0.87].into(),
64            head_background: Background::Color([0.87, 0.87, 0.87].into()),
65            head_text_color: Color::BLACK,
66            body_background: Color::TRANSPARENT.into(),
67            body_text_color: Color::BLACK,
68            foot_background: Color::TRANSPARENT.into(),
69            foot_text_color: Color::BLACK,
70            close_color: Color::BLACK,
71        }
72    }
73}
74
75impl Catalog for Theme {
76    type Class<'a> = StyleFn<'a, Self, Style>;
77
78    fn default<'a>() -> Self::Class<'a> {
79        Box::new(primary)
80    }
81
82    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
83        class(self, status)
84    }
85}
86
87/// The primary theme of a [`Card`](crate::widget::card::Card).
88#[must_use]
89pub fn primary(theme: &Theme, _status: Status) -> Style {
90    backing_with_text(theme, colors::PRIMARY, colors::WHITE)
91}
92
93/// The secondary theme of a [`Card`](crate::widget::card::Card).
94#[must_use]
95pub fn secondary(theme: &Theme, _status: Status) -> Style {
96    backing_with_text(theme, colors::SECONDARY, colors::WHITE)
97}
98
99/// The success theme of a [`Card`](crate::widget::card::Card).
100#[must_use]
101pub fn success(theme: &Theme, _status: Status) -> Style {
102    backing_with_text(theme, colors::SUCCESS, colors::WHITE)
103}
104
105/// The danger theme of a [`Card`](crate::widget::card::Card).
106#[must_use]
107pub fn danger(theme: &Theme, _status: Status) -> Style {
108    backing_with_text(theme, colors::DANGER, colors::WHITE)
109}
110
111/// The warning theme of a [`Card`](crate::widget::card::Card).
112#[must_use]
113pub fn warning(theme: &Theme, _status: Status) -> Style {
114    backing_only(theme, colors::WARNING)
115}
116
117/// The info theme of a [`Card`](crate::widget::card::Card).
118#[must_use]
119pub fn info(theme: &Theme, _status: Status) -> Style {
120    backing_only(theme, colors::INFO)
121}
122
123/// The light theme of a [`Card`](crate::widget::card::Card).
124#[must_use]
125pub fn light(theme: &Theme, _status: Status) -> Style {
126    backing_only(theme, colors::LIGHT)
127}
128
129/// The dark theme of a [`Card`](crate::widget::card::Card).
130#[must_use]
131pub fn dark(theme: &Theme, _status: Status) -> Style {
132    backing_with_text(theme, colors::DARK, colors::WHITE)
133}
134
135/// The white theme of a [`Card`](crate::widget::card::Card).
136#[must_use]
137pub fn white(theme: &Theme, _status: Status) -> Style {
138    backing_only(theme, colors::WHITE)
139}
140
141fn backing_with_text(theme: &Theme, color: Color, text_color: Color) -> Style {
142    let palette = theme.extended_palette();
143    let foreground = theme.palette();
144
145    Style {
146        border_color: color,
147        head_background: color.into(),
148        head_text_color: text_color,
149        close_color: text_color,
150        background: palette.background.base.color.into(),
151        body_text_color: foreground.text,
152        foot_text_color: foreground.text,
153        ..Style::default()
154    }
155}
156
157fn backing_only(theme: &Theme, color: Color) -> Style {
158    let palette = theme.extended_palette();
159    let foreground = theme.palette();
160
161    Style {
162        border_color: color,
163        head_background: color.into(),
164        background: palette.background.base.color.into(),
165        body_text_color: foreground.text,
166        foot_text_color: foreground.text,
167        ..Style::default()
168    }
169}