Skip to content

Commit

Permalink
When the selected date changes externally it should update the month/…
Browse files Browse the repository at this point in the history
…date view
  • Loading branch information
giannissc committed Sep 2, 2024
1 parent e784966 commit 5910d2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 0 additions & 2 deletions xilem/examples/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ impl Calendar {
let now = OffsetDateTime::now_utc();
Self {
selected_date: now.date(),
// month: now.month(),
// year: now.year(),
date: DatePicker::new(now.month(), now.year()),
}
}
Expand Down
24 changes: 20 additions & 4 deletions xilem/src/view/date_selector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use masonry::widget::{Axis, CrossAxisAlignment, MainAxisAlignment};
use time::{Date, Month};
use time::{Date, Month, OffsetDateTime};

use crate::{view::sized_box, WidgetView};

Expand All @@ -12,16 +12,30 @@ pub enum DatePickerMessage {
}

pub struct DatePicker {
previous_date: Date,
month: Month,
year: i32,
}

impl DatePicker {
pub fn new(month: Month, year: i32) -> Self {
Self { month, year }
let previous_date = OffsetDateTime::now_utc().date();
Self {
previous_date,
month,
year,
}
}

pub fn view(&self, selected_date: &mut Date) -> impl WidgetView<DatePicker, DatePickerMessage> {
pub fn view(
&mut self,
selected_date: &mut Date,
) -> impl WidgetView<DatePicker, DatePickerMessage> {
if self.previous_date != *selected_date {
self.month = selected_date.month();
self.year = selected_date.year();
self.previous_date = selected_date.clone();
}
flex((self.date_controls(), self.date_grid(selected_date)))
.direction(Axis::Vertical)
.cross_axis_alignment(CrossAxisAlignment::Center)
Expand All @@ -31,6 +45,7 @@ impl DatePicker {
fn date_controls(&self) -> impl WidgetView<DatePicker, DatePickerMessage> {
let month = self.month;
let year = self.year;

flex((
button("<", |data: &mut DatePicker| {
data.month = data.month.previous();
Expand Down Expand Up @@ -80,8 +95,9 @@ impl DatePicker {
columns.push(
sized_box(button(
format!("{day_number}"),
move |_data: &mut DatePicker| {
move |data: &mut DatePicker| {
// Set the selected_date
data.previous_date = date;
DatePickerMessage::Select(date)
},
))
Expand Down

0 comments on commit 5910d2b

Please sign in to comment.