Skip to content

Commit

Permalink
upgraded Date Picker to newest iced changes
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed May 21, 2024
1 parent 63247c8 commit 3e5603d
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 254 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.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ default = [
"badge",
"card",
"number_input",
#"date_picker",
"date_picker",
#"color_picker",
#"grid",
#"tab_bar",
Expand Down Expand Up @@ -80,6 +80,7 @@ members = [
"examples/badge",
"examples/card",
"examples/number_input",
"examples/date_picker",
#"examples/color_picker",
#"examples/font_loading",
#"examples/grid",
Expand Down
138 changes: 43 additions & 95 deletions examples/date_picker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use iced::{
alignment, font,
widget::{container, text, Button, Container, Row, Text},
Alignment, Application, Command, Element, Length, Settings, Theme,
widget::{Button, Container, Row, Text},
Alignment, Element, Length,
};
use iced_aw::{date_picker::Date, helpers::date_picker};

fn main() -> iced::Result {
DatePickerExample::run(Settings::default())
iced::program(
"DatePicker example",
DatePickerExample::update,
DatePickerExample::view,
)
.font(iced_aw::BOOTSTRAP_FONT_BYTES)
.run()
}

#[derive(Clone, Debug)]
Expand All @@ -15,109 +20,52 @@ enum Message {
ChooseDate,
SubmitDate(Date),
CancelDate,
#[allow(dead_code)]
Loaded(Result<(), String>),
FontLoaded(Result<(), font::Error>),
}

enum DatePickerExample {
Loading,
Loaded(State),
}

struct State {
#[derive(Default)]
struct DatePickerExample {
date: Date,
show_picker: bool,
}

async fn load() -> Result<(), String> {
Ok(())
}

impl Application for DatePickerExample {
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();

fn new(_flags: ()) -> (DatePickerExample, Command<Message>) {
(
DatePickerExample::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("DatePicker example")
}

fn update(&mut self, message: Self::Message) -> Command<Message> {
match self {
DatePickerExample::Loading => {
if let Message::Loaded(_) = message {
*self = DatePickerExample::Loaded(State {
date: Date::today(),
show_picker: false,
})
}
impl DatePickerExample {
fn update(&mut self, message: Message) {
match message {
Message::ChooseDate => {
self.show_picker = true;
}
Message::SubmitDate(date) => {
self.date = date;
self.show_picker = false;
}
Message::CancelDate => {
self.show_picker = false;
}
DatePickerExample::Loaded(state) => match message {
Message::ChooseDate => {
state.show_picker = true;
}
Message::SubmitDate(date) => {
state.date = date;
state.show_picker = false;
}
Message::CancelDate => {
state.show_picker = false;
}
_ => {}
},
}

Command::none()
}

fn view(&self) -> Element<'_, Self::Message> {
match self {
DatePickerExample::Loading => container(
text("Loading...")
.horizontal_alignment(alignment::Horizontal::Center)
.size(50),
)
.width(Length::Fill)
.height(Length::Fill)
.center_y()
.center_x()
.into(),
DatePickerExample::Loaded(state) => {
let but = Button::new(Text::new("Set Date")).on_press(Message::ChooseDate);
fn view(&self) -> Element<'_, Message> {
let but = Button::new(Text::new("Set Date")).on_press(Message::ChooseDate);

let datepicker = date_picker(
state.show_picker,
state.date,
but,
Message::CancelDate,
Message::SubmitDate,
);
let datepicker = date_picker(
self.show_picker,
self.date,
but,
Message::CancelDate,
Message::SubmitDate,
);

let row = Row::new()
.align_items(Alignment::Center)
.spacing(10)
.push(datepicker)
.push(Text::new(format!("Date: {}", state.date,)));
let row = Row::new()
.align_items(Alignment::Center)
.spacing(10)
.push(datepicker)
.push(Text::new(format!("Date: {}", self.date,)));

Container::new(row)
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}
Container::new(row)
.center_x(Length::Fill)
.center_y(Length::Fill)
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}
8 changes: 7 additions & 1 deletion src/core/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use once_cell::sync::Lazy;
use std::fmt::Display;

/// The date value
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug)]
pub struct Date {
/// The year value of the date.
pub year: i32,
Expand All @@ -15,6 +15,12 @@ pub struct Date {
pub day: u32,
}

Check failure on line 17 in src/core/date.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced_aw/iced_aw/src/core/date.rs
impl Default for Date {
fn default() -> Self {
Self { year: 2024, month: 1, day: 1 }
}
}

impl Date {
/// Creates a new date from the current timestamp.
#[must_use]
Expand Down
2 changes: 0 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ pub use color_picker::ColorPickerStyles;

#[cfg(feature = "date_picker")]
pub mod date_picker;
#[cfg(feature = "date_picker")]
pub use date_picker::DatePickerStyle;

#[cfg(feature = "tab_bar")]
pub mod tab_bar;
Expand Down
18 changes: 9 additions & 9 deletions src/style/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ pub fn primary(theme: &Theme, status: Status) -> Style {
let base = styled(palette.primary.strong);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -83,12 +83,12 @@ pub fn secondary(theme: &Theme, status: Status) -> Style {
let base = styled(palette.secondary.strong);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -99,12 +99,12 @@ pub fn success(theme: &Theme, status: Status) -> Style {
let base = styled(palette.success.strong);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -115,12 +115,12 @@ pub fn danger(theme: &Theme, status: Status) -> Style {
let base = styled(palette.danger.strong);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -130,12 +130,12 @@ pub fn warning(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::WARNING, colors::BLACK);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: base.background,
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -145,12 +145,12 @@ pub fn info(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::INFO, colors::BLACK);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: base.background,
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -160,12 +160,12 @@ pub fn light(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::LIGHT, colors::BLACK);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: base.background,
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -175,12 +175,12 @@ pub fn dark(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::DARK, colors::WHITE);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: base.background,
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand All @@ -190,12 +190,12 @@ pub fn white(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::WHITE, colors::BLACK);

match status {
Status::Active | Status::Pressed | Status::Focused => base,
Status::Hovered => Style {
background: base.background,
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}

Expand Down
Loading

0 comments on commit 3e5603d

Please sign in to comment.