Skip to content

Commit

Permalink
updated Card
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed May 20, 2024
1 parent f150192 commit f215636
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 218 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ drop_down = []

default = [
"badge",
#"card",
"card",
#"number_input",
#"date_picker",
#"color_picker",
Expand Down Expand Up @@ -78,7 +78,7 @@ opt-level = 2
[workspace]
members = [
"examples/badge",
#"examples/card",
"examples/card",
#"examples/number_input",
#"examples/color_picker",
#"examples/font_loading",
Expand All @@ -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 = "./"
Expand Down
115 changes: 30 additions & 85 deletions examples/card/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Message>) {
(
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<Message> {
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()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
3 changes: 0 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,4 +54,3 @@ pub use spinner::SpinnerStyle;
pub mod context_menu;
#[cfg(feature = "context_menu")]
pub use context_menu::ContextMenuStyle;

28 changes: 14 additions & 14 deletions src/style/badge.rs
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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<dyn Fn(&Theme, Status) -> Style + 'a>;

/// The Catalog of a [`Badge`](crate::native::badge::Badge).
pub trait Catalog {
///Style for the trait to use.
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit f215636

Please sign in to comment.