From 977c3f4c02649dccb18cf4b6793894c2fe8d5702 Mon Sep 17 00:00:00 2001 From: Fabrizio Iannetti Date: Mon, 11 Sep 2023 07:49:01 +0200 Subject: [PATCH] differentiate top-bar between month and year views * prev and next buttons change year in year view * do not show month name in year view Signed-off-by: Fabrizio Iannetti --- src/main.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) 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); }