Calendar improvements

- Implemented date switching logic within the widget.
- The 3 events now return the date after the user interacts with the
  calendar.
This commit is contained in:
Eduardo Flores 2024-03-15 00:48:58 -07:00
parent e9af889740
commit 98835de7f6
2 changed files with 41 additions and 37 deletions

View file

@ -18,9 +18,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
PrevMonth,
NextMonth,
DaySelected(u32),
PrevMonth(NaiveDate),
NextMonth(NaiveDate),
DateSelected(NaiveDate),
}
/// The [`App`] stores application-specific state.
@ -68,17 +68,19 @@ impl cosmic::Application for App {
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::DaySelected(day) => {
cosmic::widget::calendar::set_day(&mut self.date_selected, day);
Message::DateSelected(date) => {
self.date_selected = date;
}
Message::PrevMonth => {
cosmic::widget::calendar::set_prev_month(&mut self.date_selected);
Message::PrevMonth(date) => {
self.date_selected = date;
}
Message::NextMonth => {
cosmic::widget::calendar::set_next_month(&mut self.date_selected);
Message::NextMonth(date) => {
self.date_selected = date;
}
}
println!("Date selected: {:?}", self.date_selected);
Command::none()
}
@ -88,9 +90,9 @@ impl cosmic::Application for App {
let calendar = cosmic::widget::calendar(
&self.date_selected,
Message::PrevMonth,
Message::NextMonth,
|day| Message::DaySelected(day),
|date| Message::PrevMonth(date),
|date| Message::NextMonth(date),
|date| Message::DateSelected(date),
);
content = content.push(calendar);