1use iced::{Point, Rectangle, Size};
5use jiff::Timestamp;
6use jiff::civil::{Date, Weekday};
7use jiff::tz::TimeZone;
8
9use super::entry::{Occurrence, add_days, midnight};
10
11pub const DAY_MINUTES: u16 = 24 * 60;
13
14pub const MIN_BLOCK_MINUTES: u16 = 30;
17
18pub fn week_of(date: Date, week_start: Weekday) -> [Date; 7] {
20 let back = i32::from(week_start.until(date.weekday()).rem_euclid(7));
21 let first = add_days(date, -back);
22 std::array::from_fn(|day| add_days(first, day as i32))
23}
24
25pub fn month_weeks(date: Date, week_start: Weekday) -> Vec<[Date; 7]> {
27 let last = date.last_of_month();
28 let mut weeks = vec![week_of(date.first_of_month(), week_start)];
29 while let Some(week) = weeks.last()
32 && week[6] < last
33 && weeks.len() < 6
34 {
35 weeks.push(week_of(add_days(week[6], 1), week_start));
36 }
37 weeks
38}
39
40pub fn weekdays(week_start: Weekday) -> [Weekday; 7] {
42 std::array::from_fn(|day| week_start.wrapping_add(day as i64))
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct Bar {
48 pub occurrence: Occurrence,
49 pub first: usize,
51 pub last: usize,
52 pub lane: usize,
54 pub starts_before: bool,
56 pub ends_after: bool,
57}
58
59impl Bar {
60 pub fn covers(&self, day: usize) -> bool {
61 (self.first..=self.last).contains(&day)
62 }
63
64 pub fn days(&self) -> usize {
66 self.last - self.first + 1
67 }
68}
69
70pub fn bars(days: &[Date], occurrences: &[Occurrence], time_zone: &TimeZone) -> Vec<Bar> {
74 let (Some(&row_first), Some(&row_last)) = (days.first(), days.last()) else {
75 return Vec::new();
76 };
77 let index = |date: Date| days.iter().position(|day| *day == date);
78
79 let mut placed: Vec<Bar> = occurrences
80 .iter()
81 .filter_map(|occurrence| {
82 let (first, last) = occurrence.dates(time_zone);
83 if last < row_first || first > row_last {
84 return None;
85 }
86 Some(Bar {
87 occurrence: *occurrence,
88 first: index(first.max(row_first))?,
89 last: index(last.min(row_last))?,
90 lane: 0,
91 starts_before: first < row_first,
92 ends_after: last > row_last,
93 })
94 })
95 .collect();
96 placed.sort_by_key(|bar| {
97 (
98 bar.first,
99 std::cmp::Reverse(bar.days()),
100 !bar.occurrence.all_day,
101 bar.occurrence.start,
102 bar.occurrence.entry,
103 )
104 });
105
106 let mut lanes: Vec<usize> = Vec::new();
108 for bar in &mut placed {
109 let free = lanes.iter().position(|taken| *taken < bar.first);
110 bar.lane = free.unwrap_or(lanes.len());
111 match free {
112 Some(lane) => lanes[lane] = bar.last,
113 None => lanes.push(bar.last),
114 }
115 }
116 placed
117}
118
119#[derive(Debug, Clone, PartialEq, Eq)]
121pub struct Fitted {
122 pub bars: Vec<Bar>,
123 pub hidden: Vec<usize>,
125}
126
127pub fn fit(bars: Vec<Bar>, days: usize, lanes: usize) -> Fitted {
131 if days == 0 {
132 return Fitted {
133 bars: Vec::new(),
134 hidden: Vec::new(),
135 };
136 }
137 let depth = |day: usize| {
138 bars.iter()
139 .filter(|bar| bar.covers(day))
140 .map(|bar| bar.lane + 1)
141 .max()
142 .unwrap_or(0)
143 };
144 let crowded: Vec<bool> = (0..days).map(|day| depth(day) > lanes).collect();
145 let shown = |bar: &Bar| {
146 let over_a_crowded_day = (bar.first..=bar.last).any(|day| crowded[day.min(days - 1)]);
147 bar.lane + 1 < lanes || (bar.lane + 1 == lanes && !over_a_crowded_day)
148 };
149
150 let hidden = (0..days)
151 .map(|day| {
152 bars.iter()
153 .filter(|bar| bar.covers(day) && !shown(bar))
154 .count()
155 })
156 .collect();
157 let bars = bars.iter().copied().filter(shown).collect();
158 Fitted { bars, hidden }
159}
160
161pub fn lane_count(bars: &[Bar]) -> usize {
163 bars.iter().map(|bar| bar.lane + 1).max().unwrap_or(0)
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
168pub struct Block {
169 pub occurrence: Occurrence,
170 pub top: u16,
172 pub bottom: u16,
173 pub column: usize,
176 pub columns: usize,
177 pub starts_before: bool,
179 pub ends_after: bool,
180}
181
182impl Block {
183 pub fn drawn_top(&self) -> u16 {
186 self.top.min(DAY_MINUTES - MIN_BLOCK_MINUTES)
187 }
188
189 pub fn drawn_bottom(&self) -> u16 {
192 self.bottom.max(self.drawn_top() + MIN_BLOCK_MINUTES)
193 }
194}
195
196pub fn minute_on(date: Date, instant: Timestamp, time_zone: &TimeZone) -> u16 {
199 let zoned = instant.to_zoned_datetime(time_zone.clone());
200 match zoned.date().cmp(&date) {
201 std::cmp::Ordering::Less => 0,
202 std::cmp::Ordering::Greater => DAY_MINUTES,
203 std::cmp::Ordering::Equal => {
204 u16::try_from(i32::from(zoned.hour()) * 60 + i32::from(zoned.minute())).unwrap_or(0)
205 }
206 }
207}
208
209pub fn instant_at(date: Date, minute: u16, time_zone: &TimeZone) -> Timestamp {
211 if minute >= DAY_MINUTES {
212 return midnight(add_days(date, 1), time_zone);
213 }
214 let hour = i8::try_from(minute / 60).unwrap_or(0);
215 let minute = i8::try_from(minute % 60).unwrap_or(0);
216 date.at(hour, minute, 0, 0)
217 .to_zoned_datetime(time_zone.clone())
218 .map(|zoned| zoned.timestamp())
219 .unwrap_or_else(|_| midnight(date, time_zone))
220}
221
222pub fn blocks(date: Date, occurrences: &[Occurrence], time_zone: &TimeZone) -> Vec<Block> {
226 let mut placed: Vec<Block> = occurrences
227 .iter()
228 .filter(|occurrence| !occurrence.all_day)
229 .filter_map(|occurrence| {
230 let (first, last) = occurrence.dates(time_zone);
231 if date < first || date > last {
232 return None;
233 }
234 let top = minute_on(date, occurrence.start, time_zone);
235 let bottom = minute_on(date, occurrence.end, time_zone).max(top);
236 Some(Block {
237 occurrence: *occurrence,
238 top,
239 bottom,
240 column: 0,
241 columns: 1,
242 starts_before: first < date,
243 ends_after: last > date,
244 })
245 })
246 .collect();
247 placed.sort_by_key(|block| {
248 (
249 block.drawn_top(),
250 std::cmp::Reverse(block.bottom),
251 block.occurrence.entry,
252 )
253 });
254
255 let mut run_start = 0;
256 let mut run_end = 0;
257 let mut columns: Vec<u16> = Vec::new();
259 for index in 0..placed.len() {
260 let (top, bottom) = (placed[index].drawn_top(), placed[index].drawn_bottom());
261 if index > run_start && top >= run_end {
262 close_run(&mut placed[run_start..index], columns.len());
263 run_start = index;
264 run_end = 0;
265 columns.clear();
266 }
267 let free = columns.iter().position(|taken| *taken <= top);
268 placed[index].column = free.unwrap_or(columns.len());
269 match free {
270 Some(column) => columns[column] = bottom,
271 None => columns.push(bottom),
272 }
273 run_end = run_end.max(bottom);
274 }
275 close_run(&mut placed[run_start..], columns.len());
276 placed
277}
278
279fn close_run(run: &mut [Block], columns: usize) {
280 for block in run {
281 block.columns = columns.max(1);
282 }
283}
284
285pub fn snap(minutes: f32, step: u16) -> u16 {
287 let step = f32::from(step.max(1));
288 let snapped = (minutes / step).round() * step;
289 snapped.clamp(0.0, f32::from(DAY_MINUTES)) as u16
290}
291
292#[derive(Debug, Clone, Copy, PartialEq)]
295pub struct Grid {
296 pub width: f32,
298 pub gutter: f32,
300 pub days: usize,
301 pub hour_height: f32,
302 pub first_hour: u8,
305 pub last_hour: u8,
306}
307
308impl Grid {
309 pub fn minutes(&self) -> (u16, u16) {
311 let first = u16::from(self.first_hour.min(23)) * 60;
312 let last = (u16::from(self.last_hour) * 60).clamp(first + 60, DAY_MINUTES);
313 (first, last)
314 }
315
316 pub fn height(&self) -> f32 {
317 let (first, last) = self.minutes();
318 f32::from(last - first) / 60.0 * self.hour_height
319 }
320
321 pub fn size(&self) -> Size {
322 Size::new(self.width, self.height())
323 }
324
325 pub fn day_width(&self) -> f32 {
326 ((self.width - self.gutter) / self.days.max(1) as f32).max(0.0)
327 }
328
329 pub fn day_x(&self, day: usize) -> f32 {
331 self.gutter + day as f32 * self.day_width()
332 }
333
334 pub fn day_at(&self, x: f32) -> Option<usize> {
336 let width = self.day_width();
337 if x < self.gutter || width <= 0.0 {
338 return None;
339 }
340 let day = ((x - self.gutter) / width) as usize;
341 (day < self.days).then_some(day)
342 }
343
344 pub fn y_of(&self, minute: u16) -> f32 {
347 let (first, _) = self.minutes();
348 (f32::from(minute) - f32::from(first)) / 60.0 * self.hour_height
349 }
350
351 pub fn minutes_at(&self, y: f32) -> f32 {
353 let (first, last) = self.minutes();
354 if self.hour_height <= 0.0 {
355 return f32::from(first);
356 }
357 (f32::from(first) + y / self.hour_height * 60.0).clamp(f32::from(first), f32::from(last))
358 }
359
360 pub fn block(&self, day: usize, block: &Block) -> Option<Rectangle> {
365 let (first, last) = self.minutes();
366 let (top, bottom) = (block.drawn_top(), block.drawn_bottom());
367 if bottom <= first || top >= last {
368 return None;
369 }
370 let lane = (self.day_width() - BLOCK_GUTTER).max(0.0);
371 let width = lane / block.columns.max(1) as f32;
372 let y = self.y_of(top.max(first));
373 Some(Rectangle::new(
374 Point::new(self.day_x(day) + block.column as f32 * width, y),
375 Size::new(
376 (width - BLOCK_GAP).max(0.0),
377 (self.y_of(bottom.min(last)) - y - BLOCK_GAP).max(0.0),
378 ),
379 ))
380 }
381}
382
383pub const BLOCK_GUTTER: f32 = 8.0;
385pub const BLOCK_GAP: f32 = 2.0;
387
388#[cfg(test)]
389mod tests {
390 use super::*;
391 use crate::data::calendar::entry::{Entries, Entry, EntryId};
392 use jiff::civil::date;
393
394 fn at(day: i8, hour: i8, minute: i8) -> Timestamp {
395 date(2026, 11, day)
396 .at(hour, minute, 0, 0)
397 .to_zoned_datetime(TimeZone::UTC)
398 .map(|zoned| zoned.timestamp())
399 .unwrap_or_default()
400 }
401
402 fn timed(id: u64, from: (i8, i8, i8), to: (i8, i8, i8)) -> Entry {
403 Entry::new(
404 EntryId(id),
405 "Entry",
406 at(from.0, from.1, from.2),
407 at(to.0, to.1, to.2),
408 )
409 }
410
411 fn days(id: u64, first: i8, last: i8) -> Entry {
412 Entry::all_day(
413 EntryId(id),
414 "Days",
415 date(2026, 11, first),
416 date(2026, 11, last),
417 )
418 }
419
420 fn week() -> [Date; 7] {
422 week_of(date(2026, 11, 4), Weekday::Monday)
423 }
424
425 fn week_bars(entries: &Entries) -> Vec<Bar> {
426 let week = week();
427 bars(
428 &week,
429 &entries.between(week[0], week[6]),
430 entries.time_zone(),
431 )
432 }
433
434 fn lanes(bars: &[Bar]) -> Vec<(u64, usize, usize, usize)> {
435 let mut lanes: Vec<_> = bars
436 .iter()
437 .map(|bar| (bar.occurrence.entry.0, bar.first, bar.last, bar.lane))
438 .collect();
439 lanes.sort();
440 lanes
441 }
442
443 #[test]
444 fn a_week_starts_on_the_chosen_day() {
445 let monday = week_of(date(2026, 11, 4), Weekday::Monday);
446 assert_eq!(monday[0], date(2026, 11, 2));
447 assert_eq!(monday[6], date(2026, 11, 8));
448 let sunday = week_of(date(2026, 11, 4), Weekday::Sunday);
449 assert_eq!(sunday[0], date(2026, 11, 1));
450 assert_eq!(sunday[6], date(2026, 11, 7));
451 assert_eq!(week_of(date(2026, 11, 2), Weekday::Monday), monday);
452 assert_eq!(week_of(date(2026, 11, 8), Weekday::Monday), monday);
453 }
454
455 #[test]
456 fn weekdays_start_on_the_chosen_day() {
457 assert_eq!(weekdays(Weekday::Monday)[0], Weekday::Monday);
458 assert_eq!(weekdays(Weekday::Monday)[6], Weekday::Sunday);
459 assert_eq!(weekdays(Weekday::Sunday)[0], Weekday::Sunday);
460 assert_eq!(weekdays(Weekday::Sunday)[6], Weekday::Saturday);
461 }
462
463 #[test]
464 fn a_month_is_covered_by_four_to_six_whole_weeks() {
465 let weeks = month_weeks(date(2026, 11, 19), Weekday::Monday);
467 assert_eq!(weeks.len(), 6);
468 assert_eq!(weeks[0][0], date(2026, 10, 26));
469 assert_eq!(weeks[5][6], date(2026, 12, 6));
470
471 let weeks = month_weeks(date(2026, 11, 19), Weekday::Sunday);
472 assert_eq!(weeks.len(), 5);
473 assert_eq!(weeks[0][0], date(2026, 11, 1));
474
475 for edge in [Date::MIN, Date::MAX] {
477 let weeks = month_weeks(edge, Weekday::Monday);
478 assert!((4..=6).contains(&weeks.len()), "{edge}");
479 }
480
481 let weeks = month_weeks(date(2027, 2, 10), Weekday::Monday);
483 assert_eq!(weeks.len(), 4);
484 assert_eq!(weeks[0][0], date(2027, 2, 1));
485 assert_eq!(weeks[3][6], date(2027, 2, 28));
486 }
487
488 #[test]
489 fn bars_take_the_first_free_lane() {
490 let entries = Entries::utc().with([
491 days(1, 2, 4),
492 days(2, 3, 3),
493 days(3, 5, 6),
494 timed(4, (3, 9, 0), (3, 10, 0)),
495 ]);
496 assert_eq!(
497 lanes(&week_bars(&entries)),
498 vec![(1, 0, 2, 0), (2, 1, 1, 1), (3, 3, 4, 0), (4, 1, 1, 2)]
499 );
500 assert_eq!(lane_count(&week_bars(&entries)), 3);
501 assert_eq!(lane_count(&[]), 0);
502 }
503
504 #[test]
505 fn bars_are_clipped_to_the_row_and_say_so() {
506 let entries = Entries::utc().with([
507 Entry::all_day(EntryId(1), "Long", date(2026, 10, 30), date(2026, 11, 12)),
508 days(2, 8, 9),
509 days(3, 20, 21),
510 ]);
511 let bars = week_bars(&entries);
512 assert_eq!(bars.len(), 2);
513 let long = bars[0];
514 assert_eq!((long.first, long.last, long.days()), (0, 6, 7));
515 assert!(long.starts_before && long.ends_after);
516 let tail = bars[1];
517 assert_eq!((tail.first, tail.last), (6, 6));
518 assert!(!tail.starts_before && tail.ends_after);
519 assert!(tail.covers(6) && !tail.covers(5));
520 }
521
522 #[test]
523 fn a_timed_entry_past_midnight_covers_both_days() {
524 let entries = Entries::utc().with([timed(1, (3, 22, 0), (4, 2, 0))]);
525 assert_eq!(lanes(&week_bars(&entries)), vec![(1, 1, 2, 0)]);
526 }
527
528 #[test]
529 fn no_days_no_bars() {
530 let entries = Entries::utc().with([days(1, 2, 4)]);
531 let all = entries.between(date(2026, 11, 2), date(2026, 11, 8));
532 assert!(bars(&[], &all, entries.time_zone()).is_empty());
533 let fitted = fit(Vec::new(), 0, 3);
534 assert!(fitted.bars.is_empty() && fitted.hidden.is_empty());
535 }
536
537 #[test]
538 fn everything_fits_when_there_are_enough_lanes() {
539 let entries = Entries::utc().with([days(1, 2, 4), days(2, 3, 3), days(3, 3, 3)]);
540 let fitted = fit(week_bars(&entries), 7, 3);
541 assert_eq!(fitted.bars.len(), 3);
542 assert_eq!(fitted.hidden, vec![0; 7]);
543 }
544
545 #[test]
546 fn a_crowded_day_gives_its_last_lane_to_the_count() {
547 let entries = Entries::utc().with([
548 days(1, 2, 4),
549 days(2, 3, 3),
550 days(3, 3, 3),
551 days(4, 3, 3),
552 days(5, 5, 5),
553 ]);
554 let fitted = fit(week_bars(&entries), 7, 3);
555 let shown: Vec<_> = fitted
556 .bars
557 .iter()
558 .map(|bar| bar.occurrence.entry.0)
559 .collect();
560 assert_eq!(shown, vec![1, 2, 5]);
561 assert_eq!(fitted.hidden, vec![0, 2, 0, 0, 0, 0, 0]);
562 }
563
564 #[test]
565 fn a_bar_on_the_last_lane_hides_if_any_of_its_days_is_crowded() {
566 let entries = Entries::utc().with([
567 days(1, 2, 2),
568 days(2, 2, 2),
569 days(3, 2, 2),
570 days(4, 3, 3),
571 days(5, 3, 4),
574 ]);
575 let bars = week_bars(&entries);
576 let two = fit(bars.clone(), 7, 2);
577 assert_eq!(two.hidden, vec![2, 0, 0, 0, 0, 0, 0]);
578 assert_eq!(two.bars.len(), 3);
579
580 let none = fit(bars.clone(), 7, 0);
581 assert!(none.bars.is_empty());
582 assert_eq!(none.hidden, vec![3, 2, 1, 0, 0, 0, 0]);
583
584 let one = fit(bars, 7, 1);
587 assert!(one.bars.is_empty());
588 assert_eq!(one.hidden, vec![3, 2, 1, 0, 0, 0, 0]);
589 }
590
591 #[test]
592 fn minutes_and_instants_convert_both_ways() {
593 let day = date(2026, 11, 3);
594 assert_eq!(minute_on(day, at(3, 9, 30), &TimeZone::UTC), 570);
595 assert_eq!(minute_on(day, at(2, 23, 0), &TimeZone::UTC), 0);
596 assert_eq!(minute_on(day, at(4, 0, 0), &TimeZone::UTC), DAY_MINUTES);
597 assert_eq!(instant_at(day, 570, &TimeZone::UTC), at(3, 9, 30));
598 assert_eq!(instant_at(day, DAY_MINUTES, &TimeZone::UTC), at(4, 0, 0));
599 assert_eq!(instant_at(day, 0, &TimeZone::UTC), at(3, 0, 0));
600 }
601
602 #[test]
603 fn snapping_rounds_to_the_step_within_the_day() {
604 assert_eq!(snap(7.0, 15), 0);
605 assert_eq!(snap(8.0, 15), 15);
606 assert_eq!(snap(577.0, 15), 570);
607 assert_eq!(snap(-20.0, 15), 0);
608 assert_eq!(snap(5000.0, 15), DAY_MINUTES);
609 assert_eq!(snap(61.4, 0), 61);
610 }
611
612 fn day_blocks(entries: &Entries) -> Vec<Block> {
613 let day = date(2026, 11, 3);
614 blocks(day, &entries.on(day), entries.time_zone())
615 }
616
617 fn columns(blocks: &[Block]) -> Vec<(u64, usize, usize)> {
618 let mut columns: Vec<_> = blocks
619 .iter()
620 .map(|block| (block.occurrence.entry.0, block.column, block.columns))
621 .collect();
622 columns.sort();
623 columns
624 }
625
626 #[test]
627 fn entries_apart_take_the_whole_width() {
628 let entries = Entries::utc().with([
629 timed(1, (3, 9, 0), (3, 10, 0)),
630 timed(2, (3, 10, 0), (3, 11, 0)),
631 days(3, 3, 3),
632 ]);
633 let blocks = day_blocks(&entries);
634 assert_eq!(columns(&blocks), vec![(1, 0, 1), (2, 0, 1)]);
635 assert_eq!((blocks[0].top, blocks[0].bottom), (540, 600));
636 }
637
638 #[test]
639 fn overlapping_entries_share_the_width() {
640 let entries = Entries::utc().with([
641 timed(1, (3, 9, 0), (3, 11, 0)),
642 timed(2, (3, 9, 30), (3, 10, 30)),
643 timed(3, (3, 10, 30), (3, 11, 30)),
644 timed(4, (3, 14, 0), (3, 15, 0)),
645 ]);
646 assert_eq!(
647 columns(&day_blocks(&entries)),
648 vec![(1, 0, 2), (2, 1, 2), (3, 1, 2), (4, 0, 1)]
649 );
650 }
651
652 #[test]
653 fn short_entries_are_drawn_tall_enough_and_kept_apart() {
654 let entries = Entries::utc().with([
655 timed(1, (3, 9, 0), (3, 9, 5)),
656 timed(2, (3, 9, 10), (3, 9, 15)),
657 timed(3, (3, 23, 50), (3, 23, 55)),
658 ]);
659 let blocks = day_blocks(&entries);
660 assert_eq!(columns(&blocks), vec![(1, 0, 2), (2, 1, 2), (3, 0, 1)]);
661 assert_eq!(blocks[0].drawn_bottom(), 570);
662 assert_eq!(
663 (blocks[2].drawn_top(), blocks[2].drawn_bottom()),
664 (1410, DAY_MINUTES),
665 "moved up to keep its height before midnight"
666 );
667 }
668
669 #[test]
670 fn an_entry_across_midnight_is_cut_at_the_day() {
671 let entries = Entries::utc().with([
672 timed(1, (2, 22, 0), (3, 2, 0)),
673 timed(2, (3, 22, 0), (4, 2, 0)),
674 timed(3, (2, 22, 0), (3, 0, 0)),
675 ]);
676 let blocks = day_blocks(&entries);
677 assert_eq!(blocks.len(), 2, "one ending at midnight is not on the day");
678 let morning = blocks[0];
679 assert_eq!((morning.top, morning.bottom), (0, 120));
680 assert!(morning.starts_before && !morning.ends_after);
681 let night = blocks[1];
682 assert_eq!((night.top, night.bottom), (1320, DAY_MINUTES));
683 assert!(!night.starts_before && night.ends_after);
684 }
685
686 fn grid() -> Grid {
687 Grid {
688 width: 756.0,
689 gutter: 56.0,
690 days: 7,
691 hour_height: 48.0,
692 first_hour: 0,
693 last_hour: 24,
694 }
695 }
696
697 #[test]
698 fn a_grid_can_show_part_of_the_day() {
699 let grid = Grid {
700 first_hour: 8,
701 last_hour: 18,
702 ..grid()
703 };
704 assert_eq!(grid.minutes(), (480, 1080));
705 assert_eq!(grid.height(), 480.0);
706 assert_eq!(grid.y_of(540), 48.0);
707 assert_eq!(grid.y_of(420), -48.0);
708 assert_eq!(grid.minutes_at(48.0), 540.0);
709 assert_eq!(grid.minutes_at(-100.0), 480.0);
710 assert_eq!(grid.minutes_at(9000.0), 1080.0);
711
712 let entries = Entries::utc().with([
713 timed(1, (3, 6, 0), (3, 7, 0)),
714 timed(2, (3, 7, 0), (3, 9, 0)),
715 timed(3, (3, 17, 0), (3, 20, 0)),
716 ]);
717 let blocks = day_blocks(&entries);
718 assert_eq!(grid.block(0, &blocks[0]), None, "before the first hour");
719 let early = grid.block(0, &blocks[1]);
720 assert_eq!(early.map(|r| (r.y, r.height)), Some((0.0, 46.0)));
721 let late = grid.block(0, &blocks[2]);
722 assert_eq!(late.map(|r| (r.y, r.height)), Some((432.0, 46.0)));
723 }
724
725 #[test]
726 fn hours_out_of_order_still_show_an_hour() {
727 let grid = Grid {
728 first_hour: 30,
729 last_hour: 2,
730 ..grid()
731 };
732 assert_eq!(grid.minutes(), (1380, 1440));
733 assert_eq!(grid.height(), 48.0);
734 }
735
736 #[test]
737 fn the_grid_maps_points_to_days_and_minutes() {
738 let grid = grid();
739 assert_eq!(grid.height(), 1152.0);
740 assert_eq!(grid.size(), Size::new(756.0, 1152.0));
741 assert_eq!(grid.day_width(), 100.0);
742 assert_eq!(grid.day_x(2), 256.0);
743 assert_eq!(grid.day_at(10.0), None);
744 assert_eq!(grid.day_at(56.0), Some(0));
745 assert_eq!(grid.day_at(355.0), Some(2));
746 assert_eq!(grid.day_at(800.0), None);
747 assert_eq!(grid.y_of(90), 72.0);
748 assert_eq!(grid.minutes_at(72.0), 90.0);
749 assert_eq!(grid.minutes_at(-5.0), 0.0);
750 assert_eq!(grid.minutes_at(5000.0), 1440.0);
751 }
752
753 #[test]
754 fn a_grid_with_no_room_does_not_divide_by_zero() {
755 let grid = Grid {
756 width: 20.0,
757 gutter: 56.0,
758 days: 0,
759 hour_height: 0.0,
760 first_hour: 0,
761 last_hour: 24,
762 };
763 assert_eq!(grid.day_width(), 0.0);
764 assert_eq!(grid.day_at(60.0), None);
765 assert_eq!(grid.minutes_at(10.0), 0.0);
766 }
767
768 #[test]
769 fn blocks_split_the_column_and_leave_a_sliver_free() {
770 let entries = Entries::utc().with([
771 timed(1, (3, 9, 0), (3, 11, 0)),
772 timed(2, (3, 9, 30), (3, 10, 30)),
773 ]);
774 let blocks = day_blocks(&entries);
775 let grid = grid();
776 let (Some(first), Some(second)) = (grid.block(1, &blocks[0]), grid.block(1, &blocks[1]))
777 else {
778 panic!("both blocks are on show");
779 };
780 assert_eq!(first.x, 156.0);
781 assert_eq!(first.width, 44.0);
782 assert_eq!(first.y, 432.0);
783 assert_eq!(first.height, 94.0);
784 assert_eq!(second.x, 202.0);
785 assert!(second.x + second.width <= 256.0 - BLOCK_GUTTER);
786 }
787}