aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 072fbd5cd2ada05f94d03828b704d70c6760adc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
}