From 8ca270e78ef00d8413143742ae5aff2d777e6823 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 20 Mar 2024 13:54:14 +0100 Subject: [PATCH] Option to change date picker format (#4180) Simply allows date picker buttons to show other formats than `%Y-%m-%d`, while keeping that as default to not break compatibility. I'm not that experienced with Rust, so I was unsure whether you'd prefer `&'a str` rather than a `String`, let me know if I should change that. --------- Co-authored-by: Emil Ernerfeldt --- crates/egui_extras/src/datepicker/button.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/egui_extras/src/datepicker/button.rs b/crates/egui_extras/src/datepicker/button.rs index 5aa6eaea..a81747ff 100644 --- a/crates/egui_extras/src/datepicker/button.rs +++ b/crates/egui_extras/src/datepicker/button.rs @@ -16,6 +16,7 @@ pub struct DatePickerButton<'a> { calendar: bool, calendar_week: bool, show_icon: bool, + format: String, } impl<'a> DatePickerButton<'a> { @@ -28,6 +29,7 @@ impl<'a> DatePickerButton<'a> { calendar: true, calendar_week: true, show_icon: true, + format: "%Y-%m-%d".to_owned(), } } @@ -73,6 +75,14 @@ impl<'a> DatePickerButton<'a> { self.show_icon = show_icon; self } + + /// Change the format shown on the button. (Default: %Y-%m-%d) + /// See [`chrono::format::strftime`] for valid formats. + #[inline] + pub fn format(mut self, format: impl Into) -> Self { + self.format = format.into(); + self + } } impl<'a> Widget for DatePickerButton<'a> { @@ -83,9 +93,9 @@ impl<'a> Widget for DatePickerButton<'a> { .unwrap_or_default(); let mut text = if self.show_icon { - RichText::new(format!("{} 📆", self.selection.format("%Y-%m-%d"))) + RichText::new(format!("{} 📆", self.selection.format(&self.format))) } else { - RichText::new(format!("{}", self.selection.format("%Y-%m-%d"))) + RichText::new(format!("{}", self.selection.format(&self.format))) }; let visuals = ui.visuals().widgets.open; if button_state.picker_visible {