diff --git a/crates/egui_extras/src/datepicker/button.rs b/crates/egui_extras/src/datepicker/button.rs index 601c16f6..0ec0680f 100644 --- a/crates/egui_extras/src/datepicker/button.rs +++ b/crates/egui_extras/src/datepicker/button.rs @@ -1,6 +1,7 @@ use super::popup::DatePickerPopup; use chrono::NaiveDate; use egui::{Area, Button, Frame, InnerResponse, Key, Order, RichText, Ui, Widget}; +use std::ops::RangeInclusive; #[derive(Default, Clone)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -19,6 +20,7 @@ pub struct DatePickerButton<'a> { show_icon: bool, format: String, highlight_weekends: bool, + start_end_years: Option>, } impl<'a> DatePickerButton<'a> { @@ -33,6 +35,7 @@ impl<'a> DatePickerButton<'a> { show_icon: true, format: "%Y-%m-%d".to_owned(), highlight_weekends: true, + start_end_years: None, } } @@ -101,6 +104,17 @@ impl<'a> DatePickerButton<'a> { self.highlight_weekends = highlight_weekends; self } + + /// Set the start and end years for the date picker. (Default: today's year - 100 to today's year + 10) + /// This will limit the years you can choose from in the dropdown to the specified range. + /// + /// For example, if you want to provide the range of years from 2000 to 2035, you can use: + /// `start_end_years(2000..=2035)`. + #[inline] + pub fn start_end_years(mut self, start_end_years: RangeInclusive) -> Self { + self.start_end_years = Some(start_end_years); + self + } } impl Widget for DatePickerButton<'_> { @@ -167,6 +181,7 @@ impl Widget for DatePickerButton<'_> { calendar: self.calendar, calendar_week: self.calendar_week, highlight_weekends: self.highlight_weekends, + start_end_years: self.start_end_years, } .draw(ui) }) diff --git a/crates/egui_extras/src/datepicker/popup.rs b/crates/egui_extras/src/datepicker/popup.rs index 79f3d37f..c63de3a9 100644 --- a/crates/egui_extras/src/datepicker/popup.rs +++ b/crates/egui_extras/src/datepicker/popup.rs @@ -35,6 +35,7 @@ pub(crate) struct DatePickerPopup<'a> { pub calendar: bool, pub calendar_week: bool, pub highlight_weekends: bool, + pub start_end_years: Option>, } impl DatePickerPopup<'_> { @@ -84,7 +85,11 @@ impl DatePickerPopup<'_> { ComboBox::from_id_salt("date_picker_year") .selected_text(popup_state.year.to_string()) .show_ui(ui, |ui| { - for year in today.year() - 100..today.year() + 10 { + let (start_year, end_year) = match &self.start_end_years { + Some(range) => (*range.start(), *range.end()), + None => (today.year() - 100, today.year() + 10), + }; + for year in start_year..=end_year { if ui .selectable_value( &mut popup_state.year,