diff --git a/Cargo.lock b/Cargo.lock index 74c05fe7..7d7cff0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2988,6 +2988,14 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" +[[package]] +name = "time_picker" +version = "0.1.0" +dependencies = [ + "iced", + "iced_aw", +] + [[package]] name = "tiny-skia" version = "0.11.4" diff --git a/Cargo.toml b/Cargo.toml index 5d21a031..47cd17d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ default = [ "grid", #"tab_bar", #"tabs", - #"time_picker", + "time_picker", "slide_bar", "wrap", "selection_list", @@ -85,7 +85,7 @@ members = [ "examples/grid", #"examples/tab_bar", #"examples/tabs", - #"examples/time_picker", + "examples/time_picker", "examples/sliderbar", "examples/wrap", "examples/selection_list", diff --git a/examples/time_picker/src/main.rs b/examples/time_picker/src/main.rs index 27e0e008..493f57fd 100644 --- a/examples/time_picker/src/main.rs +++ b/examples/time_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::{time_picker::Time, TimePicker}; fn main() -> iced::Result { - TimePickerExample::run(Settings::default()) + iced::program( + "TimePicker example", + TimePickerExample::update, + TimePickerExample::view, + ) + .font(iced_aw::BOOTSTRAP_FONT_BYTES) + .run() } #[derive(Clone, Debug)] @@ -15,113 +20,53 @@ enum Message { ChooseTime, SubmitTime(Time), CancelTime, - #[allow(dead_code)] - Loaded(Result<(), String>), - FontLoaded(Result<(), font::Error>), } -#[derive(Debug)] -enum TimePickerExample { - Loading, - Loaded(State), -} - -#[derive(Debug)] -struct State { +#[derive(Debug, Default)] +struct TimePickerExample { time: Time, show_picker: bool, } -async fn load() -> Result<(), String> { - Ok(()) -} - -impl Application for TimePickerExample { - type Message = Message; - type Theme = Theme; - type Executor = iced::executor::Default; - type Flags = (); - - fn new(_flags: ()) -> (TimePickerExample, Command) { - ( - TimePickerExample::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("TimePicker example") - } - - fn update(&mut self, message: Self::Message) -> Command { - match self { - TimePickerExample::Loading => { - if let Message::Loaded(_) = message { - *self = TimePickerExample::Loaded(State { - time: Time::now_hm(true), - show_picker: false, - }) - } +impl TimePickerExample { + fn update(&mut self, message: Message) { + match message { + Message::ChooseTime => { + self.show_picker = true; + } + Message::SubmitTime(time) => { + self.time = time; + self.show_picker = false; + } + Message::CancelTime => { + self.show_picker = false; } - TimePickerExample::Loaded(state) => match message { - Message::ChooseTime => { - state.show_picker = true; - } - Message::SubmitTime(time) => { - state.time = time; - state.show_picker = false; - } - Message::CancelTime => { - state.show_picker = false; - } - _ => {} - }, } - - Command::none() } - fn view(&self) -> Element<'_, Self::Message> { - match self { - TimePickerExample::Loading => container( - text("Loading...") - .horizontal_alignment(alignment::Horizontal::Center) - .size(50), - ) - .width(Length::Fill) - .height(Length::Fill) - .center_y() - .center_x() - .into(), - TimePickerExample::Loaded(state) => { - let but = Button::new(Text::new("Set Time")).on_press(Message::ChooseTime); + fn view(&self) -> Element<'_, Message> { + let but = Button::new(Text::new("Set Time")).on_press(Message::ChooseTime); - let timepicker = TimePicker::new( - state.show_picker, - state.time, - but, - Message::CancelTime, - Message::SubmitTime, - ) - //.show_seconds() - .use_24h(); + let timepicker = TimePicker::new( + self.show_picker, + self.time, + but, + Message::CancelTime, + Message::SubmitTime, + ) + .use_24h(); - let row = Row::new() - .align_items(Alignment::Center) - .spacing(10) - .push(timepicker) - .push(Text::new(format!("Time: {}", state.time))); + let row = Row::new() + .align_items(Alignment::Center) + .spacing(10) + .push(timepicker) + .push(Text::new(format!("Time: {}", self.time))); - 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/time.rs b/src/core/time.rs index 32e79ae3..0bcaa298 100644 --- a/src/core/time.rs +++ b/src/core/time.rs @@ -31,6 +31,16 @@ pub enum Time { }, } +impl Default for Time { + fn default() -> Self { + Self::Hm { + hour: 1, + minute: 0, + period: Period::Am, + } + } +} + impl Time { /// Creates a new time (hours, minutes) from the current timestamp. #[must_use] diff --git a/src/style.rs b/src/style.rs index 0e4c2d89..a11da852 100644 --- a/src/style.rs +++ b/src/style.rs @@ -25,8 +25,6 @@ pub use tab_bar::TabBarStyles; #[cfg(feature = "time_picker")] pub mod time_picker; -#[cfg(feature = "time_picker")] -pub use time_picker::TimePickerStyle; #[cfg(feature = "number_input")] pub mod number_input; diff --git a/src/style/time_picker.rs b/src/style/time_picker.rs index 454e5014..44937b74 100644 --- a/src/style/time_picker.rs +++ b/src/style/time_picker.rs @@ -2,13 +2,12 @@ //! //! *This API requires the following crate features to be activated: `time_picker`* #![allow(clippy::doc_markdown)] -use std::rc::Rc; - +use super::{Status, StyleFn}; use iced::{Background, Color, Theme}; -/// The appearance of a [`TimePicker`](crate::widgets::TimePicker). +/// The style of a [`TimePicker`](crate::widgets::TimePicker). #[derive(Clone, Copy, Debug)] -pub struct Appearance { +pub struct Style { /// The background of the [`TimePicker`](crate::widgets::TimePicker). pub background: Background, @@ -45,103 +44,64 @@ pub struct Appearance { pub clock_hand_width: f32, } -/// The appearance of a [`TimePicker`](crate::widgets::TimePicker). -pub trait StyleSheet { - /// The style type of this stylesheet - type Style: Default + Clone; - /// The normal appearance of a [`TimePicker`](crate::widgets::TimePicker). - fn active(&self, style: &Self::Style) -> Appearance; - - /// The appearance when something is selected of the - /// [`TimePicker`](crate::widgets::TimePicker) - fn selected(&self, style: &Self::Style) -> Appearance; +/// The Catalog of a [`TimePicker`](crate::widgets::TimePicker). +pub trait Catalog { + ///Style for the trait to use. + type Class<'a>; - /// The appearance when something is hovered of the - /// [`TimePicker`](crate::widgets::TimePicker). - 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 - /// [`TimePicker`](crate::widgets::TimePicker). - 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 style appearance of the [`TimePicker`](crate::widgets::TimePicker) -#[derive(Clone, Default)] -#[allow(missing_docs, clippy::missing_docs_in_private_items)] -pub enum TimePickerStyle { - #[default] - Default, - Custom(Rc>), -} +impl Catalog for Theme { + type Class<'a> = StyleFn<'a, Self, Style>; -impl TimePickerStyle { - /// Creates a custom [`TimePickerStyle`] style variant. - pub fn custom(style_sheet: impl StyleSheet