diff options
Diffstat (limited to 'src/gui/calendar.rs')
-rw-r--r-- | src/gui/calendar.rs | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/src/gui/calendar.rs b/src/gui/calendar.rs index 547c087..f5f9c10 100644 --- a/src/gui/calendar.rs +++ b/src/gui/calendar.rs @@ -6,7 +6,7 @@ use gtk::glib; use gtk::prelude::*; use std::collections::HashMap; -use crate::{gui::update, gui::update::Msg, gui::App, model::event, model::event::Event}; +use crate::{gui::update, gui::update::Msg, gui::App, model::event, model::event::Event, model::category::Category}; static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"]; static MONTHES: [&str; 12] = [ @@ -20,6 +20,8 @@ pub fn create( end_date: NaiveDate, events: &[Event], recurring_events: &[Event], + categories: &[Category], + default_color: &str, ) -> gtk::Grid { let grid = gtk::Grid::builder().build(); @@ -28,7 +30,7 @@ pub fn create( } let repetitions = event::repetitions_between(recurring_events, start_date, end_date); - attach_days(tx.clone(), &grid, start_date, today, events, &repetitions); + attach_days(tx.clone(), &grid, start_date, today, events, &repetitions, categories, default_color); let event_controller_key = gtk::EventControllerKey::new(); event_controller_key.connect_key_released(glib::clone!(@strong tx => move |_, _, keycode, _| { @@ -50,12 +52,14 @@ fn attach_days( today: NaiveDate, events: &[Event], repetitions: &HashMap<NaiveDate, Vec<Event>>, + categories: &[Category], + default_color: &str, ) { let mut d = start_date; for row in 1..5 { for col in 0..7 { grid.attach( - &day_entry(tx.clone(), d, today, events, repetitions), + &day_entry(tx.clone(), d, today, events, repetitions, categories, default_color), col, row, 1, @@ -66,14 +70,21 @@ fn attach_days( } } -pub fn refresh_date(app: &App, date: NaiveDate, repetitions: &HashMap<NaiveDate, Vec<Event>>) { +pub fn refresh_date( + app: &App, + date: NaiveDate, + repetitions: &HashMap<NaiveDate, + Vec<Event>>, + categories: &[Category], + default_color: &str +) { let d = date.signed_duration_since(app.start_date).num_days(); let col = (d % 7) as i32; let row = 1 + (d / 7) as i32; - app.grid.attach( - &day_entry(app.tx.clone(), date, app.today, &app.events, repetitions), + app.calendar.attach( + &day_entry(app.tx.clone(), date, app.today, &app.events, repetitions, categories, default_color), col, row, 1, @@ -101,6 +112,8 @@ pub fn day_entry( today: NaiveDate, events: &[Event], repetitions: &HashMap<NaiveDate, Vec<Event>>, + categories: &[Category], + default_color: &str, ) -> gtk::ScrolledWindow { let vbox = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) @@ -131,7 +144,7 @@ pub fn day_entry( events.sort_by_key(|e| e.start); if !events.is_empty() { - vbox.append(&day_events(date, tx, events)); + vbox.append(&day_events(date, tx, events, categories, default_color)); } gtk::ScrolledWindow::builder() @@ -164,7 +177,13 @@ fn day_label(today: NaiveDate, date: NaiveDate) -> gtk::Label { label } -fn day_events(date: NaiveDate, tx: Sender<Msg>, events: Vec<&Event>) -> gtk::Box { +fn day_events( + date: NaiveDate, + tx: Sender<Msg>, + events: Vec<&Event>, + categories: &[Category], + default_color: &str, +) -> gtk::Box { let vbox = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) .build(); @@ -175,6 +194,25 @@ fn day_events(date: NaiveDate, tx: Sender<Msg>, events: Vec<&Event>) -> gtk::Box .hexpand(true) .build(); + let background_color = categories.iter().find(|c| event.category == Some(c.id)).map(|c| c.color.clone()).unwrap_or_else(|| default_color.to_string()); + + let provider = gtk::CssProvider::new(); + provider.load_from_data(format!(" + .event {{ + background-color: {}; + color: white; + border-radius: 4px; + padding: 4px; + margin: 4px; + }} + + .event:hover {{ + filter: brightness(120%); + }} + ", background_color).as_bytes()); + hbox.style_context().add_provider(&provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION); + hbox.style_context().add_class("event"); + let gesture = gtk::GestureClick::new(); gesture.connect_pressed( glib::clone!(@strong event, @strong tx => move |gesture, n, _, _| { @@ -190,8 +228,6 @@ fn day_events(date: NaiveDate, tx: Sender<Msg>, events: Vec<&Event>) -> gtk::Box ); hbox.add_controller(&gesture); - hbox.add_css_class("g-Calendar__DayEvent"); - let event_txt = &event.pprint(); let label = gtk::Label::builder() .label(event_txt) |