aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/views/calendar.rs
diff options
context:
space:
mode:
Diffstat (limited to 'karlgui/src/views/calendar.rs')
-rw-r--r--karlgui/src/views/calendar.rs77
1 files changed, 53 insertions, 24 deletions
diff --git a/karlgui/src/views/calendar.rs b/karlgui/src/views/calendar.rs
index 6bec067..2eca0cc 100644
--- a/karlgui/src/views/calendar.rs
+++ b/karlgui/src/views/calendar.rs
@@ -1,17 +1,29 @@
-use crate::{helper::weekday_to_str, Globals};
+use crate::{
+ helper::{month_to_str, ordering_suffix, weekday_to_str},
+ Globals,
+};
use chrono::{Datelike, Duration, NaiveDateTime, Timelike};
-use egui::{Color32, Rect, Sense, Stroke, Ui, Vec2};
+use egui::{Color32, Rect, Sense, Slider, Stroke, Ui, Vec2};
use egui_extras::{Size, TableBuilder};
use std::{collections::BTreeMap, ops::Range};
-#[derive(Default)]
pub struct Calendar {
- // offset: NaiveDate,
+ 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(); //self.offset;
+ 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(0, 0, 0);
let end_dt = end_date.and_hms(0, 0, 0);
@@ -22,7 +34,17 @@ impl Calendar {
Some((id, g.get_instances_range(*id, start_dt, end_dt).to_owned()?))
}));
- let height = 1500.0;
+ 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))
@@ -31,24 +53,25 @@ impl Calendar {
tr.col(|_| {});
for d in 0..7 {
tr.col(|ui| {
- ui.heading(weekday_to_str(
- (start_dt + Duration::days(d as i64))
- .date()
- .weekday()
- .num_days_from_monday()
- .into(),
+ 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(height, |mut tr| {
+ tb.row(self.height, |mut tr| {
tr.col(|ui| {
let (response, p) =
- ui.allocate_painter(Vec2::new(50.0, height), Sense::hover());
+ 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 * height),
+ 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),
@@ -76,7 +99,7 @@ impl Calendar {
let task = g.tasks.get(id).unwrap();
let (rect, response) = ui.allocate_exact_size(
- Vec2::new(10.0, height),
+ Vec2::new(10.0, self.height),
Sense::hover(),
);
@@ -85,11 +108,11 @@ impl Calendar {
let rect_start = (r.start.hour() as f32
+ (r.start.minute() as f32 / 60.0))
/ 24.0
- * height;
+ * self.height;
let rect_end = (r.end.hour() as f32
+ (r.end.minute() as f32 / 60.0))
/ 24.0
- * height;
+ * self.height;
let rect = Rect::from_two_pos(
rect.min + Vec2::new(0.0, rect_start),
rect.min + Vec2::new(10.0, rect_end),
@@ -109,15 +132,21 @@ impl Calendar {
});
}
}
- ui.painter().rect(
- rect,
- 0.0,
- Color32::KHAKI,
- Stroke::new(0.0, Color32::WHITE),
- );
+ 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;
+ }
});
}
});