diff --git a/src/main.rs b/src/main.rs index 75c0303..6c44a19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ //! Simple calendar applications. mod ui; +mod model; + use ui::calendar; +use model::events::EventsCollection; use chrono::{Datelike, NaiveDate, Months, Utc}; use calendar::{CalendarMonthView, CalendarYearView, CalendarParams }; @@ -27,6 +30,7 @@ pub fn main() -> iced::Result { struct CalendarApp { month: NaiveDate, controls: Controls, + events: EventsCollection, } #[derive(Debug, Clone, Copy)] @@ -119,6 +123,7 @@ impl Application for CalendarApp { let month = Utc::today().naive_local(); (CalendarApp { month, + events: EventsCollection::new(), ..CalendarApp::default() }, Command::none()) } @@ -163,7 +168,7 @@ impl Application for CalendarApp { ; match self.controls.mode { - Some(ViewMode::Year) => content = content.push(CalendarYearView::new(&CalendarParams::new(), self.month)), + Some(ViewMode::Year) => content = content.push(CalendarYearView::new(&CalendarParams::new(), self.month, &self.events)), Some(ViewMode::Month) | None => content = content.push(CalendarMonthView::new(&CalendarParams::new(), self.month)), }; diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..6226263 --- /dev/null +++ b/src/model.rs @@ -0,0 +1 @@ +pub mod events; \ No newline at end of file diff --git a/src/model/events.rs b/src/model/events.rs new file mode 100644 index 0000000..e9d0eaf --- /dev/null +++ b/src/model/events.rs @@ -0,0 +1,51 @@ +use chrono::NaiveDate; +use std::vec::Vec; +use std::default::Default; + +pub struct Event { + begin: NaiveDate, + end: NaiveDate, +} + +impl Event { + fn is_in_day(&self, day: NaiveDate) -> bool { + day >= self.begin && day <= self.end + } +} + +pub struct EventsCollection { + events: Vec, +} + +impl EventsCollection { + pub fn new() -> Self { + EventsCollection { + // TODO: hard-coded events + events: vec![ + Event { + begin: NaiveDate::from_ymd_opt(2023, 01, 15).unwrap(), + end: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(), + }, + Event { + begin: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(), + end: NaiveDate::from_ymd_opt(2023, 01, 17).unwrap(), + }, + ], + } + } + + pub fn is_any_in_day(&self, day: NaiveDate) -> bool { + for ev_day in &self.events { + if ev_day.is_in_day(day) { + return true; + } + } + false + } +} + +impl Default for EventsCollection { + fn default() -> EventsCollection { + EventsCollection::new() + } +} \ No newline at end of file diff --git a/src/ui/calendar.rs b/src/ui/calendar.rs index 5ede437..6b4e142 100644 --- a/src/ui/calendar.rs +++ b/src/ui/calendar.rs @@ -16,6 +16,7 @@ use iced_native::alignment; use iced_native::widget::Tree; use chrono::{NaiveDate, Datelike, Duration, Local}; use super::basics::CellGrid; +use crate::model::events::{Event, EventsCollection}; const MONTH_NAMES: [&str;12] = [ "gen", @@ -46,6 +47,8 @@ pub struct CalendarParams { day_weekend_bg: Color, day_today_bg: Color, day_text_margin: f32, + + ev_bg: Color, } impl CalendarParams { @@ -59,6 +62,7 @@ impl CalendarParams { day_other_month_fg: Color::from_rgb8(220, 220, 220), day_weekend_bg: Color::from_rgb8(245, 245, 245), day_text_margin: 5.0, + ev_bg: Color::from_rgb8(100, 100, 245) } } } @@ -350,16 +354,17 @@ where //------------------------------------------------------------------------- -pub struct CalendarYearView { +pub struct CalendarYearView<'a> { first_day: NaiveDate, first_day_in_view: NaiveDate, params: CalendarParams, month_column_font_size: f32, margin: f32, + events: &'a EventsCollection, } -impl CalendarYearView { - pub fn new(params: &CalendarParams, day: NaiveDate) -> Self { +impl<'a> CalendarYearView<'a> { + pub fn new(params: &CalendarParams, day: NaiveDate, events: &'a EventsCollection) -> Self { // first day of the year let first_day = NaiveDate::from_ymd(day.year(), 1, 1); @@ -374,7 +379,8 @@ impl CalendarYearView { first_day_in_view, params: params.clone(), month_column_font_size: 24.0, - margin: 10.0 + margin: 10.0, + events } } @@ -537,6 +543,8 @@ impl CalendarYearView { self.params.day_today_bg } else if weekday > 4 { self.params.day_weekend_bg + } else if self.events.is_any_in_day(current_day) { + self.params.ev_bg } else { Color::TRANSPARENT }; @@ -580,7 +588,7 @@ impl CalendarYearView { } // CalendarYearView -impl Widget for CalendarYearView +impl Widget for CalendarYearView<'_> where Renderer: text::Renderer, { @@ -646,11 +654,11 @@ where } } -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where Renderer: text::Renderer, { - fn from(year_view: CalendarYearView) -> Self { + fn from(year_view: CalendarYearView<'a>) -> Self { Self::new(year_view) } }