BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / slot_select.rs

slot_select.rs

slot_select.rs 8.7 KiB Download Raw
1//! Picking a time block on the calendar's hour grid by dragging over empty slots.
2//!
3//! The grid snaps to its time scale (15, 30 or 60 minutes); a selection stays in one
4//! day because the meeting form has a single date. See ADR 0023.
5
6use chrono::{Duration, FixedOffset, NaiveDate, NaiveTime, TimeZone};
7
8use crate::week_grid::{MINUTES_PER_DAY, local_midnight};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct SlotSelection {
12 pub date: NaiveDate,
13 /// Minutes after the grid's midnight, snapped to the grid step.
14 pub start_minute: i64,
15 /// Exclusive end; at most a whole day (24:00).
16 pub end_minute: i64,
17}
18
19/// What the meeting form gets from a picked block, in the zone's wall clock.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct FormTimes {
22 pub date: NaiveDate,
23 pub start: NaiveTime,
24 pub end: NaiveTime,
25}
26
27impl FormTimes {
28 pub fn label(&self, clock_format: &str) -> String {
29 format!(
30 "{} - {}",
31 self.start.format(clock_format),
32 self.end.format(clock_format)
33 )
34 }
35}
36
37impl SlotSelection {
38 /// The block from where the press started to where the pointer is now.
39 pub fn between(date: NaiveDate, anchor: i64, current: i64, step: i64) -> Self {
40 let (start_minute, end_minute) = span_between(anchor, current, step);
41 SlotSelection {
42 date,
43 start_minute,
44 end_minute,
45 }
46 }
47
48 /// A meeting of `length` minutes from `minute`, cut at the end of the day.
49 pub fn starting_at(date: NaiveDate, minute: i64, length: i64) -> Self {
50 SlotSelection {
51 date,
52 start_minute: minute,
53 end_minute: (minute + length.max(1)).min(MINUTES_PER_DAY),
54 }
55 }
56
57 pub fn contains(&self, date: NaiveDate, minute: i64) -> bool {
58 self.date == date && (self.start_minute..self.end_minute).contains(&minute)
59 }
60
61 /// Whether the block still belongs on a grid showing `dates` at `step` minutes;
62 /// a block from another range or another time scale is dropped.
63 pub fn fits(&self, dates: &[NaiveDate], step: i64) -> bool {
64 let step = step.max(1);
65 dates.contains(&self.date)
66 && self.start_minute % step == 0
67 && (self.end_minute - self.start_minute) % step == 0
68 }
69
70 /// Date, start and end for the meeting form.
71 ///
72 /// The grid draws every day with one fixed `offset` (today's), so on the other
73 /// side of a daylight saving switch a row is an hour away from its wall-clock
74 /// time. Going through that origin puts the meeting where it was dragged. A block
75 /// that runs past the form date's midnight ends at 23:59, since the form keeps
76 /// start and end on one date.
77 pub fn form_times<Tz: TimeZone>(&self, offset: FixedOffset, zone: &Tz) -> FormTimes {
78 let origin = local_midnight(self.date, offset);
79 let wall = |minute: i64| {
80 (origin + Duration::minutes(minute))
81 .with_timezone(zone)
82 .naive_local()
83 };
84 let (start, end) = (wall(self.start_minute), wall(self.end_minute));
85 let end = if end.date() == start.date() {
86 end.time()
87 } else {
88 NaiveTime::from_hms_opt(23, 59, 0).unwrap_or_default()
89 };
90 FormTimes {
91 date: start.date(),
92 start: start.time(),
93 end,
94 }
95 }
96}
97
98/// The grid step a pixel offset inside a day column falls in, as its starting minute.
99pub fn minute_at(y: f32, hour_height: f32, step: i64) -> i64 {
100 let step = step.max(1);
101 if hour_height <= 0. {
102 return 0;
103 }
104 let minute = (y / hour_height * 60.).floor() as i64;
105 (minute.div_euclid(step) * step).clamp(0, MINUTES_PER_DAY - step)
106}
107
108/// Both grid steps and everything between them, whichever way the pointer moved.
109fn span_between(anchor: i64, current: i64, step: i64) -> (i64, i64) {
110 let step = step.max(1);
111 (
112 anchor.min(current),
113 (anchor.max(current) + step).min(MINUTES_PER_DAY),
114 )
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 const HOUR: f32 = 48.;
122
123 fn day() -> NaiveDate {
124 NaiveDate::from_ymd_opt(2026, 11, 24).unwrap()
125 }
126
127 fn hours(hours: i32) -> FixedOffset {
128 FixedOffset::east_opt(hours * 3600).unwrap()
129 }
130
131 fn time(hour: u32, minute: u32) -> NaiveTime {
132 NaiveTime::from_hms_opt(hour, minute, 0).unwrap()
133 }
134
135 #[test]
136 fn a_press_snaps_to_the_grid_step_it_lands_in() {
137 // 10:40 on a 48px-per-hour grid.
138 let y = 10. * HOUR + 40. / 60. * HOUR;
139 assert_eq!(minute_at(y, HOUR, 15), 10 * 60 + 30);
140 assert_eq!(minute_at(y, HOUR, 30), 10 * 60 + 30);
141 assert_eq!(minute_at(y, HOUR, 60), 10 * 60);
142 }
143
144 #[test]
145 fn presses_outside_the_column_stay_within_the_day() {
146 assert_eq!(minute_at(-20., HOUR, 30), 0);
147 assert_eq!(minute_at(25. * HOUR, HOUR, 30), 23 * 60 + 30);
148 assert_eq!(minute_at(25. * HOUR, HOUR, 15), 23 * 60 + 45);
149 }
150
151 #[test]
152 fn a_grid_without_height_or_step_does_not_panic() {
153 assert_eq!(minute_at(300., 0., 30), 0);
154 assert_eq!(minute_at(10. * HOUR, HOUR, 0), 600);
155 assert_eq!(span_between(600, 600, 0), (600, 601));
156 }
157
158 #[test]
159 fn dragging_down_or_up_selects_the_same_block() {
160 let down = SlotSelection::between(day(), 600, 630, 30);
161 let up = SlotSelection::between(day(), 630, 600, 30);
162 assert_eq!(down, up);
163 assert_eq!((down.start_minute, down.end_minute), (600, 660));
164 }
165
166 #[test]
167 fn a_click_selects_one_step() {
168 assert_eq!(span_between(600, 600, 15), (600, 615));
169 assert_eq!(span_between(600, 600, 60), (600, 660));
170 }
171
172 #[test]
173 fn an_hour_and_a_half_gives_the_form_its_times() {
174 let selection = SlotSelection::between(day(), 600, 660, 30);
175 let form = selection.form_times(hours(1), &hours(1));
176 assert_eq!(
177 form,
178 FormTimes {
179 date: day(),
180 start: time(10, 0),
181 end: time(11, 30),
182 }
183 );
184 assert_eq!(form.label("%H:%M"), "10:00 - 11:30");
185 assert_eq!(form.label("%-I:%M %p"), "10:00 AM - 11:30 AM");
186 }
187
188 #[test]
189 fn a_block_to_midnight_ends_at_23_59_in_the_form_and_the_label() {
190 let selection = SlotSelection::between(day(), 23 * 60, 23 * 60 + 30, 30);
191 assert_eq!(selection.end_minute, MINUTES_PER_DAY);
192 let form = selection.form_times(hours(1), &hours(1));
193 assert_eq!(form.end, time(23, 59));
194 assert_eq!(form.label("%H:%M"), "23:00 - 23:59");
195 }
196
197 #[test]
198 fn across_a_dst_switch_the_meeting_lands_where_it_was_dragged() {
199 // Grid laid out with today's +01:00; the shown day is on +02:00 summer time.
200 // The 09:00 row there holds 10:00 wall-clock meetings, so a block dragged
201 // in it must become a 10:00 meeting.
202 let selection = SlotSelection::between(day(), 9 * 60, 9 * 60 + 30, 30);
203 let form = selection.form_times(hours(1), &hours(2));
204 assert_eq!(
205 (form.date, form.start, form.end),
206 (day(), time(10, 0), time(11, 0))
207 );
208 }
209
210 #[test]
211 fn a_double_click_meeting_has_the_default_length_from_its_slot() {
212 // 13:45 on a 15-minute grid, inside or outside a picked block alike.
213 let meeting = SlotSelection::starting_at(day(), 13 * 60 + 45, 30);
214 let form = meeting.form_times(hours(1), &hours(1));
215 assert_eq!((form.start, form.end), (time(13, 45), time(14, 15)));
216 // On an hour grid the meeting is still half an hour.
217 let hourly = SlotSelection::starting_at(day(), 13 * 60, 30);
218 assert_eq!(hourly.form_times(hours(1), &hours(1)).end, time(13, 30));
219 // At the end of the day it stops at midnight, which the form shows as 23:59.
220 let late = SlotSelection::starting_at(day(), 23 * 60 + 45, 30);
221 assert_eq!(late.end_minute, MINUTES_PER_DAY);
222 assert_eq!(late.form_times(hours(1), &hours(1)).end, time(23, 59));
223 }
224
225 #[test]
226 fn contains_checks_day_and_minute() {
227 let selection = SlotSelection::between(day(), 600, 660, 30);
228 assert!(selection.contains(day(), 600));
229 assert!(selection.contains(day(), 689));
230 assert!(!selection.contains(day(), 690));
231 assert!(!selection.contains(day().succ_opt().unwrap(), 620));
232 }
233
234 #[test]
235 fn a_block_leaves_with_its_range_or_its_time_scale() {
236 let selection = SlotSelection::between(day(), 615, 615, 15);
237 let shown = [day()];
238 assert!(selection.fits(&shown, 15));
239 assert!(!selection.fits(&shown, 30));
240 assert!(!selection.fits(&[day().succ_opt().unwrap()], 15));
241 // A half-hour block still lines up on a 15-minute grid.
242 assert!(SlotSelection::between(day(), 600, 630, 30).fits(&shown, 15));
243 }
244}