| 1 | use super::week_grid::CalendarSpan; |
| 2 | |
| 3 | /// Fraction of the hour-grid viewport used to keep «now» below the top edge. |
| 4 | pub const NOW_BIAS_FRACTION: f32 = 1. / 3.; |
| 5 | |
| 6 | /// Fallback grid height before layout reports a real viewport. |
| 7 | pub const DEFAULT_GRID_VIEWPORT: f32 = 480.; |
| 8 | |
| 9 | /// Pixels of scroll-offset slack before a change counts as manual scroll. |
| 10 | pub const MANUAL_SCROLL_EPSILON: f32 = 2.; |
| 11 | |
| 12 | /// Vertical scroll offset (negative Y) so local wall time sits near `bias_fraction` |
| 13 | /// down the visible hour grid. Clamped to the day extent. |
| 14 | pub fn now_scroll_offset_y( |
| 15 | hour: u32, |
| 16 | minute: u32, |
| 17 | hour_height: f32, |
| 18 | viewport_height: f32, |
| 19 | bias_fraction: f32, |
| 20 | ) -> f32 { |
| 21 | let hour = hour.min(23); |
| 22 | let minute = minute.min(59); |
| 23 | let day_height = 24. * hour_height; |
| 24 | let now_y = (hour as f32 + minute as f32 / 60.) * hour_height; |
| 25 | let viewport = viewport_height.max(0.); |
| 26 | let bias = (viewport * bias_fraction.clamp(0., 1.)).clamp(0., viewport); |
| 27 | let min_offset = -(day_height - viewport).max(0.); |
| 28 | (bias - now_y).clamp(min_offset, 0.) |
| 29 | } |
| 30 | |
| 31 | pub fn shows_hour_grid(span: CalendarSpan) -> bool { |
| 32 | !matches!(span, CalendarSpan::Month) |
| 33 | } |
| 34 | |
| 35 | /// Whether auto-follow should keep aligning while today is on screen. |
| 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 37 | pub struct FollowNow { |
| 38 | pub active: bool, |
| 39 | } |
| 40 | |
| 41 | impl FollowNow { |
| 42 | pub fn request_align(&mut self) { |
| 43 | self.active = true; |
| 44 | } |
| 45 | |
| 46 | pub fn note_manual_scroll(&mut self) { |
| 47 | self.active = false; |
| 48 | } |
| 49 | |
| 50 | pub fn should_follow(self, today_visible: bool) -> bool { |
| 51 | self.active && today_visible |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn offset_differs(actual: f32, expected: f32, epsilon: f32) -> bool { |
| 56 | (actual - expected).abs() > epsilon |
| 57 | } |
| 58 | |
| 59 | #[cfg(test)] |
| 60 | mod tests { |
| 61 | use super::*; |
| 62 | |
| 63 | #[test] |
| 64 | fn noon_centers_near_bias_without_clamp() { |
| 65 | let hour_height = 120.; |
| 66 | let viewport = 480.; |
| 67 | let offset = now_scroll_offset_y(12, 0, hour_height, viewport, NOW_BIAS_FRACTION); |
| 68 | let now_y = 12. * hour_height; |
| 69 | let bias = viewport * NOW_BIAS_FRACTION; |
| 70 | assert!((offset - (bias - now_y)).abs() < 0.01); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn midnight_stays_at_top() { |
| 75 | let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION); |
| 76 | assert_eq!(offset, 0.); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn late_evening_clamps_to_day_end() { |
| 81 | let hour_height = 120.; |
| 82 | let viewport = 480.; |
| 83 | let day_height = 24. * hour_height; |
| 84 | let min_offset = -(day_height - viewport); |
| 85 | let offset = now_scroll_offset_y(23, 50, hour_height, viewport, NOW_BIAS_FRACTION); |
| 86 | assert!((offset - min_offset).abs() < 0.01); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn month_has_no_hour_grid() { |
| 91 | assert!(!shows_hour_grid(CalendarSpan::Month)); |
| 92 | assert!(shows_hour_grid(CalendarSpan::Day)); |
| 93 | assert!(shows_hour_grid(CalendarSpan::WorkWeek)); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn follow_turns_off_on_manual_scroll_and_back_on_align() { |
| 98 | let mut follow = FollowNow { active: true }; |
| 99 | assert!(follow.should_follow(true)); |
| 100 | follow.note_manual_scroll(); |
| 101 | assert!(!follow.should_follow(true)); |
| 102 | follow.request_align(); |
| 103 | assert!(follow.should_follow(true)); |
| 104 | assert!(!follow.should_follow(false)); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn offset_diff_uses_epsilon() { |
| 109 | assert!(!offset_differs(-100., -100.5, MANUAL_SCROLL_EPSILON)); |
| 110 | assert!(offset_differs(-100., -110., MANUAL_SCROLL_EPSILON)); |
| 111 | } |
| 112 | } |