Fixed datepicker not being marked as changed (#2208)
This commit is contained in:
parent
1fb19f08ce
commit
76d0cf5034
2 changed files with 32 additions and 20 deletions
|
@ -1,6 +1,6 @@
|
||||||
use super::popup::DatePickerPopup;
|
use super::popup::DatePickerPopup;
|
||||||
use chrono::{Date, Utc};
|
use chrono::{Date, Utc};
|
||||||
use egui::{Area, Button, Frame, Key, Order, RichText, Ui, Widget};
|
use egui::{Area, Button, Frame, InnerResponse, Key, Order, RichText, Ui, Widget};
|
||||||
|
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||||
|
@ -79,7 +79,7 @@ impl<'a> Widget for DatePickerButton<'a> {
|
||||||
if button_state.picker_visible {
|
if button_state.picker_visible {
|
||||||
button = button.fill(visuals.bg_fill).stroke(visuals.bg_stroke);
|
button = button.fill(visuals.bg_fill).stroke(visuals.bg_stroke);
|
||||||
}
|
}
|
||||||
let button_response = ui.add(button);
|
let mut button_response = ui.add(button);
|
||||||
if button_response.clicked() {
|
if button_response.clicked() {
|
||||||
button_state.picker_visible = true;
|
button_state.picker_visible = true;
|
||||||
ui.memory().data.insert_persisted(id, button_state.clone());
|
ui.memory().data.insert_persisted(id, button_state.clone());
|
||||||
|
@ -101,27 +101,35 @@ impl<'a> Widget for DatePickerButton<'a> {
|
||||||
|
|
||||||
//TODO(elwerene): Better positioning
|
//TODO(elwerene): Better positioning
|
||||||
|
|
||||||
let area_response = Area::new(ui.make_persistent_id(&self.id_source))
|
let InnerResponse {
|
||||||
|
inner: saved,
|
||||||
|
response: area_response,
|
||||||
|
} = Area::new(ui.make_persistent_id(&self.id_source))
|
||||||
.order(Order::Foreground)
|
.order(Order::Foreground)
|
||||||
.fixed_pos(pos)
|
.fixed_pos(pos)
|
||||||
.show(ui.ctx(), |ui| {
|
.show(ui.ctx(), |ui| {
|
||||||
let frame = Frame::popup(ui.style());
|
let frame = Frame::popup(ui.style());
|
||||||
frame.show(ui, |ui| {
|
frame
|
||||||
ui.set_min_width(width);
|
.show(ui, |ui| {
|
||||||
ui.set_max_width(width);
|
ui.set_min_width(width);
|
||||||
|
ui.set_max_width(width);
|
||||||
|
|
||||||
DatePickerPopup {
|
DatePickerPopup {
|
||||||
selection: self.selection,
|
selection: self.selection,
|
||||||
button_id: id,
|
button_id: id,
|
||||||
combo_boxes: self.combo_boxes,
|
combo_boxes: self.combo_boxes,
|
||||||
arrows: self.arrows,
|
arrows: self.arrows,
|
||||||
calendar: self.calendar,
|
calendar: self.calendar,
|
||||||
calendar_week: self.calendar_week,
|
calendar_week: self.calendar_week,
|
||||||
}
|
}
|
||||||
.draw(ui);
|
.draw(ui)
|
||||||
})
|
})
|
||||||
})
|
.inner
|
||||||
.response;
|
});
|
||||||
|
|
||||||
|
if saved {
|
||||||
|
button_response.mark_changed();
|
||||||
|
}
|
||||||
|
|
||||||
if !button_response.clicked()
|
if !button_response.clicked()
|
||||||
&& (ui.input().key_pressed(Key::Escape) || area_response.clicked_elsewhere())
|
&& (ui.input().key_pressed(Key::Escape) || area_response.clicked_elsewhere())
|
||||||
|
|
|
@ -33,7 +33,8 @@ pub(crate) struct DatePickerPopup<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> DatePickerPopup<'a> {
|
impl<'a> DatePickerPopup<'a> {
|
||||||
pub fn draw(&mut self, ui: &mut Ui) {
|
/// Returns `true` if user pressed `Save` button.
|
||||||
|
pub fn draw(&mut self, ui: &mut Ui) -> bool {
|
||||||
let id = ui.make_persistent_id("date_picker");
|
let id = ui.make_persistent_id("date_picker");
|
||||||
let today = chrono::offset::Utc::now().date();
|
let today = chrono::offset::Utc::now().date();
|
||||||
let mut popup_state = ui
|
let mut popup_state = ui
|
||||||
|
@ -50,7 +51,7 @@ impl<'a> DatePickerPopup<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let weeks = month_data(popup_state.year, popup_state.month);
|
let weeks = month_data(popup_state.year, popup_state.month);
|
||||||
let mut close = false;
|
let (mut close, mut saved) = (false, false);
|
||||||
let height = 20.0;
|
let height = 20.0;
|
||||||
let spacing = 2.0;
|
let spacing = 2.0;
|
||||||
ui.spacing_mut().item_spacing = Vec2::splat(spacing);
|
ui.spacing_mut().item_spacing = Vec2::splat(spacing);
|
||||||
|
@ -375,6 +376,7 @@ impl<'a> DatePickerPopup<'a> {
|
||||||
),
|
),
|
||||||
Utc,
|
Utc,
|
||||||
);
|
);
|
||||||
|
saved = true;
|
||||||
close = true;
|
close = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -392,6 +394,8 @@ impl<'a> DatePickerPopup<'a> {
|
||||||
.get_persisted_mut_or_default::<DatePickerButtonState>(self.button_id)
|
.get_persisted_mut_or_default::<DatePickerButtonState>(self.button_id)
|
||||||
.picker_visible = false;
|
.picker_visible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
saved && close
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue