From 9c224905c16f79657dc3751d4d475b339877acb9 Mon Sep 17 00:00:00 2001 From: marius david Date: Mon, 11 Apr 2022 16:25:43 +0200 Subject: [PATCH 1/2] keep the same day when changing month --- src/lib.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2f34262..f585dac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,7 +227,14 @@ where /// Draw label(will be combobox in future) with current month and two buttons which substract and add 30 days /// to current date. fn show_month_control(&mut self, ui: &mut Ui) { - self.date_step_button(ui, "<", Duration::days(-30)); + // previous month button + if ui.button("<".to_string()).clicked() { + let day = self.date.day(); + *self.date = self.date.clone() - Duration::days(day.into()); + if let Some(date) = self.date.with_day(day) { + *self.date = date; + }; + } let month_string = chrono::Month::from_u32(self.date.month()).unwrap().name(); // TODO: When https://github.com/emilk/egui/pull/543 is merged try to change label to combo box. ui.add(egui::Label::new( @@ -242,7 +249,18 @@ where // if selected != self.date.month0() as usize { // *self.date = self.date.with_month0(selected as u32).unwrap(); // } - self.date_step_button(ui, ">", Duration::days(30)); + + // next month button + if ui.button(">".to_string()).clicked() { + let day = self.date.day(); + let day_to_add = get_days_from_month(self.date.year(), self.date.month()) + .checked_sub(self.date.day0().into()) + .unwrap_or(1); + *self.date = self.date.clone() + Duration::days(day_to_add); + if let Some(date) = self.date.with_day(day) { + *self.date = date; + }; + }; } } From 4f26809aa367f81d53458c6f76ef22d97e71460a Mon Sep 17 00:00:00 2001 From: marius david Date: Tue, 12 Apr 2022 10:06:00 +0200 Subject: [PATCH 2/2] When increasing the month with the selected day not existing, now select the last day instead of the first --- src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f585dac..074dfcd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ //! //! [ex]: ./examples/simple.rs -use std::{fmt::Display, hash::Hash}; +use std::{convert::TryInto, fmt::Display, hash::Hash}; pub use chrono::{ offset::{FixedOffset, Local, Utc}, @@ -259,6 +259,15 @@ where *self.date = self.date.clone() + Duration::days(day_to_add); if let Some(date) = self.date.with_day(day) { *self.date = date; + } else { + *self.date = self + .date + .with_day( + get_days_from_month(self.date.year(), self.date.month()) + .try_into() + .unwrap_or(1), + ) + .unwrap_or_else(|| self.date.clone()); }; }; }