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 <fabrizio.iannetti@gmail.com>
This commit is contained in:
Fabrizio Iannetti 2023-09-11 07:49:01 +02:00
parent 5788a8fffa
commit 977c3f4c02
1 changed files with 29 additions and 3 deletions

View File

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