use chrono::{NaiveDate, Datelike, Weekday}; use gtk4::gdk::Display; use gtk4::prelude::*; use gtk4::{ Align, Application, ApplicationWindow, CssProvider, Grid, Label, Orientation, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Box as GtkBox }; static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"]; static MONTHES: [&str; 12] = ["Jan", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Aoû", "Sep", "Oct", "Nov", "Déc"]; fn main() { let application = Application::new(Some("me.guyonvarch.calendar"), Default::default()); application.connect_startup(build_ui); application.run(); } fn build_ui(application: &Application) { let window = ApplicationWindow::new(application); window.set_title(Some("Calendar")); window.set_default_size(800, 600); window.set_hexpand(true); window.set_vexpand(true); load_style(); let grid = Grid::builder().build(); window.set_child(Some(&grid)); for col in 0..7 { grid.attach(&day_title(col), col, 0, 1, 1); } let today = chrono::offset::Local::today().naive_utc(); let last_monday = NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon); let mut d = last_monday; for row in 1..5 { for col in 0..7 { grid.attach(&day_entry(&today, &d), col, row, 1, 1); d = d.succ(); } } window.show(); } fn load_style() { let provider = CssProvider::new(); provider.load_from_data(include_bytes!("style.css")); StyleContext::add_provider_for_display( &Display::default().expect("Error initializing gtk css provider."), &provider, STYLE_PROVIDER_PRIORITY_APPLICATION, ); } fn day_title(col: i32) -> GtkBox { let vbox = GtkBox::builder() .orientation(Orientation::Vertical) .hexpand(true) .build(); vbox.add_css_class("g-Calendar__DayTitle"); let label = Label::builder() .label(DAYS[col as usize]) .halign(Align::Center) .valign(Align::Center) .build(); vbox.append(&label); vbox } fn day_entry(today: &NaiveDate, d: &NaiveDate) -> GtkBox { let vbox = GtkBox::builder() .orientation(Orientation::Vertical) .hexpand(true) .vexpand(true) .build(); vbox.add_css_class("g-Calendar__Day"); if d == today { vbox.add_css_class("g-Calendar__Day--Today"); } let label = Label::builder() .label(&format!("{} {}", d.day(), MONTHES[d.month0() as usize])) .halign(Align::Start) .valign(Align::Center) .build(); label.add_css_class("g-Calendar__DayNumber"); vbox.append(&label); vbox }