From 54bcca5c48b4586433a92fe4aad9cddd2e5500dc Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 20 Nov 2021 23:02:52 +0100 Subject: Show event-less calendar --- src/main.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/style.css | 20 ++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/main.rs create mode 100644 src/style.css (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..072fbd5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,101 @@ +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 +} diff --git a/src/style.css b/src/style.css new file mode 100644 index 0000000..bd58799 --- /dev/null +++ b/src/style.css @@ -0,0 +1,20 @@ +.g-Calendar__DayTitle { + border: 1px solid #CCCCCC; + font-weight: bold; + padding: 4px; +} + +.g-Calendar__Day { + background-color: white; + border: 1px solid #CCCCCC; + padding: 4px; +} + +.g-Calendar__Day--Today { + background-color: #FFEC40; +} + +.g-Calendar__DayNumber { + color: #333333; + font-size: 90%; +} -- cgit v1.2.3