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 <https://github.com/emilk/egui/issues/3597> and expands on <https://github.com/emilk/egui/pull/3599>. * [x] I have followed the instructions in the PR template
This commit is contained in:
parent
5194c0df3e
commit
011e0d261a
|
|
@ -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<RangeInclusive<i32>>,
|
||||
}
|
||||
|
||||
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<i32>) -> 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)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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<std::ops::RangeInclusive<i32>>,
|
||||
}
|
||||
|
||||
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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue