Skip to content

Commit 011e0d2

Browse files
authored
egui_extras: Enable setting DatePickerButton start and end year explicitly (#7061)
Add the ability to set the `DatePickerButton`'s start and end years via new `start_year` and `end_year` methods. Continue to use the existing today - 100 years and today + 10 years behavior if a year is not specified. * This more fully closes <#3597> and expands on <#3599>. * [x] I have followed the instructions in the PR template
1 parent 5194c0d commit 011e0d2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

crates/egui_extras/src/datepicker/button.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::popup::DatePickerPopup;
22
use chrono::NaiveDate;
33
use egui::{Area, Button, Frame, InnerResponse, Key, Order, RichText, Ui, Widget};
4+
use std::ops::RangeInclusive;
45

56
#[derive(Default, Clone)]
67
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -19,6 +20,7 @@ pub struct DatePickerButton<'a> {
1920
show_icon: bool,
2021
format: String,
2122
highlight_weekends: bool,
23+
start_end_years: Option<RangeInclusive<i32>>,
2224
}
2325

2426
impl<'a> DatePickerButton<'a> {
@@ -33,6 +35,7 @@ impl<'a> DatePickerButton<'a> {
3335
show_icon: true,
3436
format: "%Y-%m-%d".to_owned(),
3537
highlight_weekends: true,
38+
start_end_years: None,
3639
}
3740
}
3841

@@ -101,6 +104,17 @@ impl<'a> DatePickerButton<'a> {
101104
self.highlight_weekends = highlight_weekends;
102105
self
103106
}
107+
108+
/// Set the start and end years for the date picker. (Default: today's year - 100 to today's year + 10)
109+
/// This will limit the years you can choose from in the dropdown to the specified range.
110+
///
111+
/// For example, if you want to provide the range of years from 2000 to 2035, you can use:
112+
/// `start_end_years(2000..=2035)`.
113+
#[inline]
114+
pub fn start_end_years(mut self, start_end_years: RangeInclusive<i32>) -> Self {
115+
self.start_end_years = Some(start_end_years);
116+
self
117+
}
104118
}
105119

106120
impl Widget for DatePickerButton<'_> {
@@ -167,6 +181,7 @@ impl Widget for DatePickerButton<'_> {
167181
calendar: self.calendar,
168182
calendar_week: self.calendar_week,
169183
highlight_weekends: self.highlight_weekends,
184+
start_end_years: self.start_end_years,
170185
}
171186
.draw(ui)
172187
})

crates/egui_extras/src/datepicker/popup.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) struct DatePickerPopup<'a> {
3535
pub calendar: bool,
3636
pub calendar_week: bool,
3737
pub highlight_weekends: bool,
38+
pub start_end_years: Option<std::ops::RangeInclusive<i32>>,
3839
}
3940

4041
impl DatePickerPopup<'_> {
@@ -84,7 +85,11 @@ impl DatePickerPopup<'_> {
8485
ComboBox::from_id_salt("date_picker_year")
8586
.selected_text(popup_state.year.to_string())
8687
.show_ui(ui, |ui| {
87-
for year in today.year() - 100..today.year() + 10 {
88+
let (start_year, end_year) = match &self.start_end_years {
89+
Some(range) => (*range.start(), *range.end()),
90+
None => (today.year() - 100, today.year() + 10),
91+
};
92+
for year in start_year..=end_year {
8893
if ui
8994
.selectable_value(
9095
&mut popup_state.year,

0 commit comments

Comments
 (0)