ferrix_app/widgets/
card.rs1use crate::messages::Message;
24use iced::{
25 Element, Font, Length, Vector, color,
26 font::Weight,
27 never,
28 widget::{button, column, container, rich_text, space, span},
29};
30
31pub struct Card {
32 header: String,
33 on_press: Message,
34 is_transparent: bool,
35}
36
37impl Card {
38 pub fn new(header: String, on_press: Message) -> Self {
39 Self {
40 is_transparent: false,
41 on_press,
42 header,
43 }
44 }
45
46 pub fn set_transparent(mut self, trans: bool) -> Self {
47 self.is_transparent = trans;
48 self
49 }
50
51 pub fn widget<'a, C>(&self, contents: C) -> Element<'a, Message>
52 where
53 C: Into<Element<'a, Message>>,
54 {
55 let is_transparent = self.is_transparent;
56 let cont = container(column![
57 rich_text![
58 span(self.header.clone())
59 .font(Font {
60 weight: Weight::Bold,
61 ..Default::default()
62 })
63 .size(16)
64 ]
65 .on_link_click(never),
66 space().width(Length::Fill).height(Length::Fill),
67 contents.into(),
68 ])
69 .padding(5)
70 .width(135)
71 .height(135)
72 .max_width(135)
73 .max_height(135)
74 .style(move |theme| default(theme, is_transparent));
75
76 button(cont)
77 .padding(0)
78 .style(button::text)
79 .on_press(self.on_press.clone())
80 .into()
81 }
82}
83
84pub fn default(theme: &iced::Theme, is_transparent: bool) -> container::Style {
85 let is_dark = theme.extended_palette().is_dark;
86 let shadow = match is_transparent {
87 true => iced::Shadow::default(),
88 false => iced::Shadow {
89 color: match is_dark {
90 true => color!(0x1d2021),
91 false => color!(0xebdbb2),
92 },
93 offset: Vector::new(2., 2.),
94 blur_radius: 2.,
95 },
96 };
97 let mut style = container::rounded_box(theme);
98 style.shadow = shadow;
99 if is_transparent {
100 style.background = style.background.and_then(|b| Some(b.scale_alpha(0.6)));
101 }
102 style
103}