diff options
author | 2024-03-05 03:48:08 +0100 | |
---|---|---|
committer | 2024-03-05 03:48:08 +0100 | |
commit | 29326215ccf13e1d1e25bf3bf5ada007856bff69 (patch) | |
tree | 9a286433affc511da2d8926d0f6862b664184b56 /examples/toast | |
parent | 1f0a0c235a7729cf2d5716efeecb9c4dc972fdfa (diff) | |
download | iced-29326215ccf13e1d1e25bf3bf5ada007856bff69.tar.gz iced-29326215ccf13e1d1e25bf3bf5ada007856bff69.tar.bz2 iced-29326215ccf13e1d1e25bf3bf5ada007856bff69.zip |
Simplify theming for `Container` widget
Diffstat (limited to 'examples/toast')
-rw-r--r-- | examples/toast/src/main.rs | 76 |
1 files changed, 51 insertions, 25 deletions
diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index c1d29193..49626710 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -209,27 +209,6 @@ mod toast { &[Self::Primary, Self::Secondary, Self::Success, Self::Danger]; } - impl container::StyleSheet for Status { - type Style = Theme; - - fn appearance(&self, theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - let pair = match self { - Status::Primary => palette.primary.weak, - Status::Secondary => palette.secondary.weak, - Status::Success => palette.success.weak, - Status::Danger => palette.danger.weak, - }; - - container::Appearance { - background: Some(pair.color.into()), - text_color: pair.text.into(), - ..Default::default() - } - } - } - impl fmt::Display for Status { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -282,14 +261,17 @@ mod toast { ) .width(Length::Fill) .padding(5) - .style( - theme::Container::Custom(Box::new(toast.status)) - ), + .style(match toast.status { + Status::Primary => primary, + Status::Secondary => secondary, + Status::Success => success, + Status::Danger => danger, + }), horizontal_rule(1), container(text(toast.body.as_str())) .width(Length::Fill) .padding(5) - .style(theme::Container::Box), + .style(container::box_), ]) .max_width(200) .into() @@ -676,4 +658,48 @@ mod toast { Element::new(manager) } } + + fn styled(pair: theme::palette::Pair) -> container::Appearance { + container::Appearance { + background: Some(pair.color.into()), + text_color: pair.text.into(), + ..Default::default() + } + } + + fn primary( + theme: &Theme, + _status: container::Status, + ) -> container::Appearance { + let palette = theme.extended_palette(); + + styled(palette.primary.weak) + } + + fn secondary( + theme: &Theme, + _status: container::Status, + ) -> container::Appearance { + let palette = theme.extended_palette(); + + styled(palette.secondary.weak) + } + + fn success( + theme: &Theme, + _status: container::Status, + ) -> container::Appearance { + let palette = theme.extended_palette(); + + styled(palette.success.weak) + } + + fn danger( + theme: &Theme, + _status: container::Status, + ) -> container::Appearance { + let palette = theme.extended_palette(); + + styled(palette.danger.weak) + } } |