aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/views/calendar.rs
blob: b583357ce1bc6e12525f04a9467b1ff4b2c356b1 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::{
    helper::{month_to_str, ordering_suffix, weekday_to_str},
    Globals,
};
use chrono::{Datelike, Duration, NaiveDateTime, Timelike};
use egui::{Color32, Rect, Sense, Slider, Stroke, Ui, Vec2};
use egui_extras::{Size, TableBuilder};
use std::{collections::BTreeMap, ops::Range};

pub struct Calendar {
    offset: i64,
    height: f32,
}

impl Default for Calendar {
    fn default() -> Self {
        Self {
            offset: 0,
            height: 1500.0,
        }
    }
}

impl Calendar {
    pub fn ui(&mut self, ui: &mut Ui, g: &mut Globals) {
        let start_date = chrono::Utc::now().date_naive() + chrono::Duration::days(self.offset);
        let end_date = start_date + chrono::Duration::days(7);
        let start_dt = start_date.and_hms_opt(0, 0, 0).unwrap();
        let end_dt = end_date.and_hms_opt(0, 0, 0).unwrap();

        let task_ids = g.tasks.keys().map(|e| e.to_owned()).collect::<Vec<_>>();
        let instances =
            BTreeMap::from_iter(task_ids.iter().filter_map(|id| {
                Some((id, g.get_instances_range(*id, start_dt, end_dt).to_owned()?))
            }));

        ui.add(
            Slider::new(&mut self.height, 500.0..=5000.0)
                .logarithmic(true)
                .text("Line spacing"),
        );
        ui.add(
            Slider::new(&mut self.offset, -28..=28)
                .logarithmic(true)
                .clamp_to_range(false)
                .text("Offset by days"),
        );

        TableBuilder::new(ui)
            .column(Size::exact(50.0))
            .columns(Size::remainder(), 7)
            .header(50.0, |mut tr| {
                tr.col(|_| {});
                for d in 0..7 {
                    tr.col(|ui| {
                        let d = (start_dt + Duration::days(d as i64)).date();
                        ui.heading(&format!(
                            "{} the {}{} {}",
                            weekday_to_str(d.weekday().num_days_from_monday().into()),
                            d.day(),
                            ordering_suffix(d.day()),
                            &month_to_str(d.month0() as i64)[..3]
                        ));
                    });
                }
            })
            .body(|mut tb| {
                tb.row(self.height, |mut tr| {
                    tr.col(|ui| {
                        let (response, p) =
                            ui.allocate_painter(Vec2::new(50.0, self.height), Sense::hover());
                        for h in 0..24 {
                            p.text(
                                response.rect.min + Vec2::new(0.0, h as f32 / 24.0 * self.height),
                                egui::Align2::LEFT_TOP,
                                format!("{h:02}:00"),
                                egui::FontId::monospace(15.0),
                                Color32::from_gray(150),
                            );
                        }
                    });
                    for d in 0..7 {
                        tr.col(|ui| {
                            let time = start_dt + Duration::days(d as i64);
                            let time_end = time + Duration::days(1) - Duration::seconds(1);
                            let instances_here = instances
                                .iter()
                                .map(|(id, rs)| {
                                    (
                                        id,
                                        rs.iter()
                                            .filter(|r| r.overlaps(time..time_end))
                                            .collect::<Vec<_>>(),
                                    )
                                })
                                .filter(|(_, l)| l.len() != 0);
                            ui.horizontal(|ui| {
                                for (id, rs) in instances_here {
                                    let task = g.tasks.get(id).unwrap();

                                    let (rect, response) = ui.allocate_exact_size(
                                        Vec2::new(10.0, self.height),
                                        Sense::hover(),
                                    );

                                    for r in &rs {
                                        let r = r.start.unwrap_or(time)..r.end.unwrap_or(time_end);
                                        let rect_start = (r.start.hour() as f32
                                            + (r.start.minute() as f32 / 60.0))
                                            / 24.0
                                            * self.height;
                                        let rect_end = (r.end.hour() as f32
                                            + (r.end.minute() as f32 / 60.0))
                                            / 24.0
                                            * self.height;
                                        let rect = Rect::from_two_pos(
                                            rect.min + Vec2::new(0.0, rect_start),
                                            rect.min + Vec2::new(10.0, rect_end),
                                        );

                                        if let Some(p) = response.hover_pos() {
                                            if rect.contains(p) {
                                                response.clone().on_hover_ui_at_pointer(|ui| {
                                                    ui.heading(&task.name);
                                                    ui.label(&format!(
                                                        "from {} to {}",
                                                        r.start, r.end
                                                    ));
                                                    if let Some(d) = &task.description {
                                                        ui.label(d);
                                                    }
                                                });
                                            }
                                        }
                                        ui.painter().rect_filled(rect, 5.0, Color32::KHAKI);
                                    }
                                }
                            });

                            let mut p1 = ui.max_rect().left_top();
                            let mut p2 = ui.max_rect().right_top();
                            for _ in 0..24 {
                                ui.painter().line_segment(
                                    [p1, p2],
                                    Stroke::new(1.0, Color32::from_gray(50).additive()),
                                );
                                p1.y += self.height / 24.0;
                                p2.y += self.height / 24.0;
                            }
                        });
                    }
                });
            });
    }
}

pub trait Overlaps<T> {
    fn overlaps(&self, v: T) -> bool;
}
impl Overlaps<NaiveDateTime> for Range<NaiveDateTime> {
    fn overlaps(&self, v: NaiveDateTime) -> bool {
        self.start <= v && v < self.end
    }
}
impl Overlaps<NaiveDateTime> for Range<Option<NaiveDateTime>> {
    fn overlaps(&self, v: NaiveDateTime) -> bool {
        match (self.start, self.end) {
            (Some(s), Some(e)) => s <= v && v < e,
            (Some(s), None) => s <= v,
            (None, Some(e)) => v < e,
            (None, None) => false,
        }
    }
}
impl Overlaps<Range<NaiveDateTime>> for Range<Option<NaiveDateTime>> {
    fn overlaps(&self, v: Range<NaiveDateTime>) -> bool {
        match (self.start, self.end) {
            (None, None) => false,
            (None, Some(e)) => v.start < e,
            (Some(s), None) => v.end > s,
            (Some(s), Some(e)) => v.start < e && v.end > s,
        }
    }
}