From 3e5603ddfd644bb8dbed67809bb014555434dbf3 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Tue, 21 May 2024 09:52:23 -0400 Subject: [PATCH] upgraded Date Picker to newest iced changes --- Cargo.lock | 8 ++ Cargo.toml | 3 +- examples/date_picker/src/main.rs | 138 +++++++++-------------------- src/core/date.rs | 8 +- src/style.rs | 2 - src/style/badge.rs | 18 ++-- src/style/date_picker.rs | 135 ++++++++++------------------ src/style/number_input.rs | 2 +- src/style/status.rs | 2 + src/widgets/date_picker.rs | 52 ++++++----- src/widgets/helpers.rs | 8 +- src/widgets/overlay/date_picker.rs | 93 ++++++++++++------- 12 files changed, 215 insertions(+), 254 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96f63f89..692d3dcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,6 +755,14 @@ dependencies = [ "zbus", ] +[[package]] +name = "date_picker" +version = "0.1.0" +dependencies = [ + "iced", + "iced_aw", +] + [[package]] name = "dconf_rs" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 796654a0..c6908c0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ default = [ "badge", "card", "number_input", - #"date_picker", + "date_picker", #"color_picker", #"grid", #"tab_bar", @@ -80,6 +80,7 @@ members = [ "examples/badge", "examples/card", "examples/number_input", + "examples/date_picker", #"examples/color_picker", #"examples/font_loading", #"examples/grid", diff --git a/examples/date_picker/src/main.rs b/examples/date_picker/src/main.rs index 1bfe6cb6..9b218474 100644 --- a/examples/date_picker/src/main.rs +++ b/examples/date_picker/src/main.rs @@ -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)] @@ -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) { - ( - 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 { - 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() } } diff --git a/src/core/date.rs b/src/core/date.rs index 2179fce1..ef30120e 100644 --- a/src/core/date.rs +++ b/src/core/date.rs @@ -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, @@ -15,6 +15,12 @@ pub struct Date { pub day: u32, } +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] diff --git a/src/style.rs b/src/style.rs index 5c456810..71647e7d 100644 --- a/src/style.rs +++ b/src/style.rs @@ -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; diff --git a/src/style/badge.rs b/src/style/badge.rs index 001230b1..83bfbbe0 100644 --- a/src/style/badge.rs +++ b/src/style/badge.rs @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } @@ -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, } } diff --git a/src/style/date_picker.rs b/src/style/date_picker.rs index 1b71d28f..87b05e58 100644 --- a/src/style/date_picker.rs +++ b/src/style/date_picker.rs @@ -1,14 +1,12 @@ //! Use a date picker as an input element for picking dates. //! //! *This API requires the following crate features to be activated: `date_picker`* - -use std::rc::Rc; - +use super::{Status, StyleFn}; use iced::{Background, Color, Theme}; /// The appearance of a [`DatePicker`](crate::native::DatePicker). #[derive(Clone, Copy, Debug)] -pub struct Appearance { +pub struct Style { /// The background of the [`DatePicker`](crate::native::DatePicker). pub background: Background, @@ -33,103 +31,64 @@ pub struct Appearance { pub day_background: Background, } -/// The appearance of a [`DatePicker`](crate::native::DatePicker). -pub trait StyleSheet { - /// The style type of this stylesheet - type Style: Default + Clone; - /// The normal appearance of a [`DatePicker`](crate::native::DatePicker). - fn active(&self, style: &Self::Style) -> Appearance; - - /// The appearance when something is selected of the - /// [`DatePicker`](crate::native::DatePicker). - fn selected(&self, style: &Self::Style) -> Appearance; +/// The Catalog of a [`DatePicker`](crate::native::DatePicker). +pub trait Catalog { + ///Style for the trait to use. + type Class<'a>; - /// The appearance when something is hovered of the - /// [`DatePicker`](crate::native::DatePicker). - fn hovered(&self, style: &Self::Style) -> Appearance; + /// The default class produced by the [`Catalog`]. + fn default<'a>() -> Self::Class<'a>; - /// The appearance when something is focused of the - /// [`DatePicker`](crate::native::DatePicker). - fn focused(&self, style: &Self::Style) -> Appearance; + /// The [`Style`] of a class with the given status. + fn style(&self, class: &Self::Class<'_>, status: Status) -> Style; } -/// The default appearance of the [`DatePicker`](crate::native::DatePicker). -#[derive(Clone, Default)] -#[allow(missing_docs, clippy::missing_docs_in_private_items)] -pub enum DatePickerStyle { - #[default] - Default, - Custom(Rc>), -} +impl Catalog for Theme { + type Class<'a> = StyleFn<'a, Self, Style>; -impl DatePickerStyle { - /// Creates a custom [`DatePickerStyle`] style variant. - pub fn custom(style_sheet: impl StyleSheet