Skip to content

Commit f17d52f

Browse files
committed
card widget
1 parent 56d24b2 commit f17d52f

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

src/theme/mod.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub enum Button {
193193
Link,
194194
LinkActive,
195195
Transparent,
196+
Card,
196197
Custom {
197198
active: Box<dyn Fn(&Theme) -> button::Appearance>,
198199
hover: Box<dyn Fn(&Theme) -> button::Appearance>,
@@ -220,6 +221,7 @@ impl Button {
220221
Button::LinkActive => &cosmic.accent,
221222
Button::Transparent => &TRANSPARENT_COMPONENT,
222223
Button::Deactivated => &theme.current_container().component,
224+
Button::Card => &theme.current_container().component,
223225
Button::Custom { .. } => &TRANSPARENT_COMPONENT,
224226
}
225227
}
@@ -237,6 +239,7 @@ impl button::StyleSheet for Theme {
237239
button::Appearance {
238240
border_radius: match style {
239241
Button::Link => 0.0.into(),
242+
Button::Card => 8.0.into(),
240243
_ => 24.0.into(),
241244
},
242245
background: match style {
@@ -286,6 +289,30 @@ impl button::StyleSheet for Theme {
286289
..active
287290
}
288291
}
292+
293+
fn disabled(&self, style: &Self::Style) -> button::Appearance {
294+
let active = self.active(style);
295+
296+
if matches!(style, Button::Card) {
297+
return active;
298+
}
299+
300+
button::Appearance {
301+
shadow_offset: iced_core::Vector::default(),
302+
background: active.background.map(|background| match background {
303+
Background::Color(color) => Background::Color(Color {
304+
a: color.a * 0.5,
305+
..color
306+
}),
307+
Background::Gradient(gradient) => Background::Gradient(gradient.mul_alpha(0.5)),
308+
}),
309+
text_color: Color {
310+
a: active.text_color.a * 0.5,
311+
..active.text_color
312+
},
313+
..active
314+
}
315+
}
289316
}
290317

291318
/*
@@ -494,6 +521,7 @@ pub enum Container {
494521
Transparent,
495522
HeaderBar,
496523
Custom(Box<dyn Fn(&Theme) -> container::Appearance>),
524+
Card,
497525
}
498526

499527
impl Container {
@@ -560,6 +588,39 @@ impl container::StyleSheet for Theme {
560588
border_color: Color::TRANSPARENT,
561589
}
562590
}
591+
Container::Card => {
592+
let palette = self.cosmic();
593+
594+
match self.layer {
595+
cosmic_theme::Layer::Background => container::Appearance {
596+
text_color: Some(Color::from(palette.background.component.on)),
597+
background: Some(iced::Background::Color(
598+
palette.background.component.base.into(),
599+
)),
600+
border_radius: 8.0.into(),
601+
border_width: 0.0,
602+
border_color: Color::TRANSPARENT,
603+
},
604+
cosmic_theme::Layer::Primary => container::Appearance {
605+
text_color: Some(Color::from(palette.primary.component.on)),
606+
background: Some(iced::Background::Color(
607+
palette.primary.component.base.into(),
608+
)),
609+
border_radius: 8.0.into(),
610+
border_width: 0.0,
611+
border_color: Color::TRANSPARENT,
612+
},
613+
cosmic_theme::Layer::Secondary => container::Appearance {
614+
text_color: Some(Color::from(palette.secondary.component.on)),
615+
background: Some(iced::Background::Color(
616+
palette.secondary.component.base.into(),
617+
)),
618+
border_radius: 8.0.into(),
619+
border_width: 0.0,
620+
border_color: Color::TRANSPARENT,
621+
},
622+
}
623+
}
563624
}
564625
}
565626
}
@@ -784,7 +845,7 @@ impl pane_grid::StyleSheet for Theme {
784845
})
785846
}
786847

787-
fn hovered_region(&self, style: &Self::Style) -> pane_grid::Appearance {
848+
fn hovered_region(&self, _style: &Self::Style) -> pane_grid::Appearance {
788849
let theme = self.cosmic();
789850
pane_grid::Appearance {
790851
background: Background::Color(theme.bg_color().into()),
@@ -1176,3 +1237,24 @@ pub fn theme_subscription(id: u64) -> Subscription<crate::theme::Theme> {
11761237
crate::theme::Theme::custom(Arc::new(theme))
11771238
})
11781239
}
1240+
1241+
impl crate::widget::card::style::StyleSheet for Theme {
1242+
fn default(&self) -> crate::widget::card::style::Appearance {
1243+
let cosmic = self.cosmic();
1244+
1245+
match self.layer {
1246+
cosmic_theme::Layer::Background => crate::widget::card::style::Appearance {
1247+
card_1: Background::Color(cosmic.background.component.hover.into()),
1248+
card_2: Background::Color(cosmic.background.component.pressed.into()),
1249+
},
1250+
cosmic_theme::Layer::Primary => crate::widget::card::style::Appearance {
1251+
card_1: Background::Color(cosmic.primary.component.hover.into()),
1252+
card_2: Background::Color(cosmic.primary.component.pressed.into()),
1253+
},
1254+
cosmic_theme::Layer::Secondary => crate::widget::card::style::Appearance {
1255+
card_1: Background::Color(cosmic.secondary.component.hover.into()),
1256+
card_2: Background::Color(cosmic.secondary.component.pressed.into()),
1257+
},
1258+
}
1259+
}
1260+
}

src/widget/card/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod style;

src/widget/card/style.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use iced_core::{Background, Color};
2+
3+
/// Appearance of the cards.
4+
#[derive(Clone, Copy)]
5+
pub struct Appearance {
6+
pub card_1: Background,
7+
pub card_2: Background,
8+
}
9+
10+
impl Default for Appearance {
11+
fn default() -> Self {
12+
Self {
13+
card_1: Background::Color(Color::WHITE),
14+
card_2: Background::Color(Color::WHITE),
15+
}
16+
}
17+
}
18+
19+
/// Defines the [`Appearance`] of a cards.
20+
pub trait StyleSheet {
21+
/// The default [`Appearance`] of the cards.
22+
fn default(&self) -> Appearance;
23+
}

src/widget/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pub mod aspect_ratio;
88
mod button;
99
pub use button::*;
1010

11+
pub mod card;
12+
pub use card::*;
13+
1114
pub mod flex_row;
1215
pub use flex_row::{flex_row, FlexRow};
1316

0 commit comments

Comments
 (0)