1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
zaaarf
2024-03-20 13:54:14 +01:00
committed by GitHub
parent bce257adb3
commit 8ca270e78e

View File

@@ -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<String>) -> 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 {