From f215636b01811a8583c9583eb0d2287a80893ea2 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Mon, 20 May 2024 09:50:37 -0400 Subject: [PATCH] updated Card --- Cargo.lock | 8 ++ Cargo.toml | 6 +- examples/card/src/main.rs | 115 +++++++----------------- src/lib.rs | 2 +- src/style.rs | 3 - src/style/badge.rs | 28 +++--- src/style/card.rs | 183 +++++++++++++++++++++++--------------- src/style/status.rs | 5 ++ src/widgets/badge.rs | 8 +- src/widgets/card.rs | 69 +++++++------- src/widgets/helpers.rs | 2 +- 11 files changed, 211 insertions(+), 218 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 18ad169b..9c7ef400 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -425,6 +425,14 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "card" +version = "0.1.0" +dependencies = [ + "iced", + "iced_aw", +] + [[package]] name = "cc" version = "1.0.97" diff --git a/Cargo.toml b/Cargo.toml index 2f7869a2..dee585d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ drop_down = [] default = [ "badge", - #"card", + "card", #"number_input", #"date_picker", #"color_picker", @@ -78,7 +78,7 @@ opt-level = 2 [workspace] members = [ "examples/badge", - #"examples/card", + "examples/card", #"examples/number_input", #"examples/color_picker", #"examples/font_loading", @@ -101,7 +101,7 @@ git = "https://github.com/iced-rs/iced.git" #rev = "b474a2b7a763dcde6a377cb409001a7b5285ee8d" version = "0.13.0-dev" #default-features = false -features = ["advanced"] +features = ["advanced", "wgpu"] [workspace.dependencies.iced_aw] path = "./" diff --git a/examples/card/src/main.rs b/examples/card/src/main.rs index f4fd2d77..beedad06 100644 --- a/examples/card/src/main.rs +++ b/examples/card/src/main.rs @@ -1,116 +1,61 @@ use iced::{ - alignment, font, - theme::Theme, - widget::{container, text, Button, Column, Container, Scrollable, Text}, - Application, Command, Element, Length, Settings, + widget::{Button, Column, Container, Scrollable, Text}, + Element, Length, }; -use iced_aw::{helpers::card, style::CardStyles}; + +use iced_aw::{helpers::card, style}; fn main() -> iced::Result { - CardExample::run(Settings::default()) + iced::program("Card example", CardExample::update, CardExample::view) + .font(iced_aw::BOOTSTRAP_FONT_BYTES) + .run() } #[derive(Debug, Clone)] enum Message { CloseCard, OpenCard, - #[allow(dead_code)] - Loaded(Result<(), String>), - FontLoaded(Result<(), font::Error>), -} - -#[derive(Debug)] -enum CardExample { - Loading, - Loaded(State), } -#[derive(Debug)] -struct State { +#[derive(Debug, Default)] +struct CardExample { card_open: bool, } -async fn load() -> Result<(), String> { - Ok(()) -} - -impl Application for CardExample { - type Message = Message; - type Theme = Theme; - type Executor = iced::executor::Default; - type Flags = (); - - fn new(_flags: ()) -> (CardExample, Command) { - ( - CardExample::Loading, - Command::batch(vec![ - font::load(iced_aw::BOOTSTRAP_FONT_BYTES).map(Message::FontLoaded), - Command::perform(load(), Message::Loaded), - ]), - ) - } - - fn title(&self) -> String { - String::from("Card example") - } - - fn update(&mut self, message: self::Message) -> Command { - match self { - CardExample::Loading => { - if let Message::Loaded(_) = message { - *self = CardExample::Loaded(State { card_open: false }) - } +impl CardExample { + fn update(&mut self, message: self::Message) { + match message { + Message::CloseCard | Message::OpenCard => { + self.card_open = !self.card_open; } - CardExample::Loaded(State { card_open }) => match message { - Message::CloseCard | Message::OpenCard => { - *card_open = !*card_open; - } - _ => {} - }, } - - Command::none() } fn view(&self) -> Element<'_, self::Message> { - match self { - CardExample::Loading => container( - text("Loading...") - .horizontal_alignment(alignment::Horizontal::Center) - .size(50), - ) - .width(Length::Fill) - .height(Length::Fill) - .center_y() - .center_x() - .into(), - CardExample::Loaded(State { card_open }) => { - let element: Element<'_, Message> = if *card_open { - card( + let element: Element<'_, Message> = if self.card_open { + card( Text::new("Head X"), Column::new() .push(Text::new("Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas, imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.")) ) .foot(Text::new("Foot")) - .style(CardStyles::Primary) + .style(style::card::primary) .on_close(Message::CloseCard) .into() - } else { - Button::new(Text::new("Open card")) - .on_press(Message::OpenCard) - .into() - }; + } else { + Button::new(Text::new("Open card")) + .on_press(Message::OpenCard) + .into() + }; - let content = Scrollable::new(element); + let content = Scrollable::new(element); - Container::new(Column::new().push(content).max_width(600)) - .width(Length::Fill) - .height(Length::Fill) - .padding(10) - .center_x() - .center_y() - .into() - } - } + Container::new(Column::new().push(content).max_width(600)) + .width(Length::Fill) + .height(Length::Fill) + .padding(10) + .center_x(Length::Fill) + .center_y(Length::Fill) + .into() } } diff --git a/src/lib.rs b/src/lib.rs index 050a78e4..67189c9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,7 @@ mod platform { #[doc(no_inline)] #[cfg(feature = "card")] - pub use {crate::style::CardStyles, crate::widgets::card, card::Card}; + pub use {crate::widgets::card, card::Card}; #[doc(no_inline)] #[cfg(feature = "color_picker")] diff --git a/src/style.rs b/src/style.rs index a1f292eb..f4965bff 100644 --- a/src/style.rs +++ b/src/style.rs @@ -9,8 +9,6 @@ pub mod badge; #[cfg(feature = "card")] pub mod card; -#[cfg(feature = "card")] -pub use card::CardStyles; #[cfg(feature = "color_picker")] pub mod color_picker; @@ -56,4 +54,3 @@ pub use spinner::SpinnerStyle; pub mod context_menu; #[cfg(feature = "context_menu")] pub use context_menu::ContextMenuStyle; - diff --git a/src/style/badge.rs b/src/style/badge.rs index 7e002a1c..58147141 100644 --- a/src/style/badge.rs +++ b/src/style/badge.rs @@ -1,7 +1,10 @@ //! Use a badge for color highlighting important information. //! //! *This API requires the following crate features to be activated: badge* -use super::{colors, status::Status}; +use super::{ + colors, + status::{Status, StyleFn}, +}; use iced::{theme::palette, Background, Color, Theme}; /// The style of a [`Badge`](crate::native::badge::Badge). @@ -24,9 +27,6 @@ pub struct Style { pub text_color: Color, } -/// The style function of a [`Badge`](crate::native::badge::Badge). -pub type StyleFn<'a, Theme> = Box Style + 'a>; - /// The Catalog of a [`Badge`](crate::native::badge::Badge). pub trait Catalog { ///Style for the trait to use. @@ -52,7 +52,7 @@ impl std::default::Default for Style { } impl Catalog for Theme { - type Class<'a> = StyleFn<'a, Self>; + type Class<'a> = StyleFn<'a, Self, Style>; fn default<'a>() -> Self::Class<'a> { Box::new(primary) @@ -70,7 +70,7 @@ pub fn primary(theme: &Theme, status: Status) -> Style { let base = styled(palette.primary.strong); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: Background::Color(palette.primary.base.color), ..base @@ -86,7 +86,7 @@ pub fn secondary(theme: &Theme, status: Status) -> Style { let base = styled(palette.secondary.strong); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: Background::Color(palette.primary.base.color), ..base @@ -102,7 +102,7 @@ pub fn success(theme: &Theme, status: Status) -> Style { let base = styled(palette.success.strong); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: Background::Color(palette.primary.base.color), ..base @@ -118,7 +118,7 @@ pub fn danger(theme: &Theme, status: Status) -> Style { let base = styled(palette.danger.strong); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: Background::Color(palette.primary.base.color), ..base @@ -133,7 +133,7 @@ pub fn warning(_theme: &Theme, status: Status) -> Style { let base = from_color(colors::WARNING, colors::BLACK); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: base.background, ..base @@ -148,7 +148,7 @@ pub fn info(_theme: &Theme, status: Status) -> Style { let base = from_color(colors::INFO, colors::BLACK); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: base.background, ..base @@ -163,7 +163,7 @@ pub fn light(_theme: &Theme, status: Status) -> Style { let base = from_color(colors::LIGHT, colors::BLACK); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: base.background, ..base @@ -178,7 +178,7 @@ pub fn dark(_theme: &Theme, status: Status) -> Style { let base = from_color(colors::DARK, colors::WHITE); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: base.background, ..base @@ -193,7 +193,7 @@ pub fn white(_theme: &Theme, status: Status) -> Style { let base = from_color(colors::WHITE, colors::BLACK); match status { - Status::Active | Status::Pressed => base, + Status::Active | Status::Pressed | Status::Focused => base, Status::Hovered => Style { background: base.background, ..base diff --git a/src/style/card.rs b/src/style/card.rs index 39c26ba1..b0adeded 100644 --- a/src/style/card.rs +++ b/src/style/card.rs @@ -2,12 +2,15 @@ //! //! *This API requires the following crate features to be activated: card* -use super::colors; +use super::{ + colors, + status::{Status, StyleFn}, +}; use iced::{Background, Color, Theme}; /// The appearance of a [`Card`](crate::native::card::Card). #[derive(Clone, Copy, Debug)] -pub struct Appearance { +pub struct Style { /// The background of the [`Card`](crate::native::card::Card). pub background: Background, @@ -43,82 +46,18 @@ pub struct Appearance { } /// The appearance of a [`Card`](crate::native::card::Card). -#[allow(missing_docs, clippy::missing_docs_in_private_items)] -pub trait StyleSheet { - type Style: Default; - /// The normal appearance of a [`Card`](crate::native::card::Card). - fn active(&self, style: &Self::Style) -> Appearance; -} - -#[derive(Default)] -#[allow(missing_docs, clippy::missing_docs_in_private_items)] -/// Default Prebuilt ``Card`` Styles -pub enum CardStyles { - Primary, - Secondary, - Success, - Danger, - Warning, - Info, - Light, - Dark, - White, - #[default] - Default, - Custom(Box>), -} +pub trait Catalog { + ///Style for the trait to use. + type Class<'a>; -impl CardStyles { - /// Creates a custom [`BadgeStyles`] style variant. - pub fn custom(style_sheet: impl StyleSheet