diff --git a/src/main.rs b/src/main.rs index 31673e4..1761f84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,8 @@ struct CalendarApp { enum Message { NextMonth, PrevMonth, + NextYear, + PrevYear, ViewModeSelected(ViewMode), } @@ -107,6 +109,10 @@ impl Controls { const MODES : [ViewMode; 2] = [ViewMode::Month, ViewMode::Year]; fn view<'a>(&'a self, month_name: &'a str, year: i32) -> Element { + let description = match self.mode { + Some(ViewMode::Month) => month_name, + _ => "" + }; Row::new() .align_items(Alignment::Center) .padding(5) @@ -120,16 +126,16 @@ impl Controls { ) .push( Button::new(Text::new("<")) - .on_press(Message::PrevMonth) + .on_press(self.get_msg_prev()) .style(theme::Button::Secondary), ) .push( Button::new(Text::new(">")) - .on_press(Message::NextMonth) + .on_press(self.get_msg_next()) .style(theme::Button::Secondary), ) .push( - Text::new(month_name) + Text::new(description) .width(Length::Fill) .horizontal_alignment(alignment::Horizontal::Left) .size(40), @@ -142,6 +148,20 @@ impl Controls { ) .into() } + + fn get_msg_next(&self) -> Message { + match self.mode { + Some(ViewMode::Month) => Message::NextMonth, + _ => Message::NextYear + } + } + + fn get_msg_prev(&self) -> Message { + match self.mode { + Some(ViewMode::Month) => Message::PrevMonth, + _ => Message::PrevYear + } + } } impl Application for CalendarApp { @@ -175,6 +195,12 @@ impl Application for CalendarApp { Message::NextMonth => { self.month = self.month + Months::new(1); } + Message::PrevYear => { + self.month = self.month - Months::new(12); + } + Message::NextYear => { + self.month = self.month + Months::new(12); + } Message::ViewModeSelected(mode) => { self.controls.mode = Some(mode); }