BlueGit

ict rusty-teams

Files

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

calendar_now.rs

calendar_now.rs 4.9 KiB Download Raw
1use super::calendar_prefs::TimeScale;
2use super::week_grid::CalendarSpan;
3
4/// Fraction of the hour-grid viewport used to keep «now» below the top edge.
5pub const NOW_BIAS_FRACTION: f32 = 1. / 3.;
6
7/// Fallback grid height before layout reports a real viewport.
8pub const DEFAULT_GRID_VIEWPORT: f32 = 480.;
9
10/// Pixels of scroll-offset slack before a change counts as manual scroll.
11pub const MANUAL_SCROLL_EPSILON: f32 = 2.;
12
13/// Vertical scroll offset (negative Y) so local wall time sits near `bias_fraction`
14/// down the visible hour grid. Clamped to the day extent.
15pub fn now_scroll_offset_y(
16 hour: u32,
17 minute: u32,
18 hour_height: f32,
19 viewport_height: f32,
20 bias_fraction: f32,
21) -> f32 {
22 let hour = hour.min(23);
23 let minute = minute.min(59);
24 let day_height = 24. * hour_height;
25 let now_y = (hour as f32 + minute as f32 / 60.) * hour_height;
26 let viewport = viewport_height.max(0.);
27 let bias = (viewport * bias_fraction.clamp(0., 1.)).clamp(0., viewport);
28 let min_offset = -(day_height - viewport).max(0.);
29 (bias - now_y).clamp(min_offset, 0.)
30}
31
32/// Height of one hour row for a time scale; finer scales get taller rows.
33pub fn hour_height(scale: TimeScale) -> f32 {
34 match scale {
35 TimeScale::Min15 => 120.,
36 TimeScale::Min30 => 96.,
37 TimeScale::Min60 => 64.,
38 }
39}
40
41/// Minutes shown above work start when the grid opens on a range without today.
42pub const WORK_START_LEAD_MINUTES: i64 = 30;
43
44/// Vertical scroll offset (negative Y) that puts work start, minus a short lead, at the top.
45pub fn work_start_scroll_offset_y(
46 work_start_minute: i64,
47 hour_height: f32,
48 viewport_height: f32,
49) -> f32 {
50 let day_height = 24. * hour_height;
51 let target =
52 (work_start_minute - WORK_START_LEAD_MINUTES).clamp(0, 24 * 60) as f32 / 60. * hour_height;
53 let min_offset = -(day_height - viewport_height.max(0.)).max(0.);
54 (-target).clamp(min_offset, 0.)
55}
56
57pub fn shows_hour_grid(span: CalendarSpan) -> bool {
58 !matches!(span, CalendarSpan::Month)
59}
60
61/// Whether auto-follow should keep aligning while today is on screen.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
63pub struct FollowNow {
64 pub active: bool,
65}
66
67impl FollowNow {
68 pub fn request_align(&mut self) {
69 self.active = true;
70 }
71
72 pub fn note_manual_scroll(&mut self) {
73 self.active = false;
74 }
75
76 pub fn should_follow(self, today_visible: bool) -> bool {
77 self.active && today_visible
78 }
79}
80
81pub fn offset_differs(actual: f32, expected: f32, epsilon: f32) -> bool {
82 (actual - expected).abs() > epsilon
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn noon_centers_near_bias_without_clamp() {
91 let hour_height = 120.;
92 let viewport = 480.;
93 let offset = now_scroll_offset_y(12, 0, hour_height, viewport, NOW_BIAS_FRACTION);
94 let now_y = 12. * hour_height;
95 let bias = viewport * NOW_BIAS_FRACTION;
96 assert!((offset - (bias - now_y)).abs() < 0.01);
97 }
98
99 #[test]
100 fn work_start_sits_at_the_top_with_a_short_lead() {
101 let hour = hour_height(TimeScale::Min30);
102 assert_eq!(work_start_scroll_offset_y(8 * 60, hour, 480.), -7.5 * hour);
103 assert_eq!(work_start_scroll_offset_y(0, hour, 480.), 0.);
104 // A late start never scrolls past the end of the day.
105 let late = work_start_scroll_offset_y(23 * 60 + 30, hour, 480.);
106 assert_eq!(late, -(24. * hour - 480.));
107 }
108
109 #[test]
110 fn finer_time_scales_get_taller_rows() {
111 assert!(hour_height(TimeScale::Min15) > hour_height(TimeScale::Min30));
112 assert!(hour_height(TimeScale::Min30) > hour_height(TimeScale::Min60));
113 }
114
115 #[test]
116 fn midnight_stays_at_top() {
117 let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);
118 assert_eq!(offset, 0.);
119 }
120
121 #[test]
122 fn late_evening_clamps_to_day_end() {
123 let hour_height = 120.;
124 let viewport = 480.;
125 let day_height = 24. * hour_height;
126 let min_offset = -(day_height - viewport);
127 let offset = now_scroll_offset_y(23, 50, hour_height, viewport, NOW_BIAS_FRACTION);
128 assert!((offset - min_offset).abs() < 0.01);
129 }
130
131 #[test]
132 fn month_has_no_hour_grid() {
133 assert!(!shows_hour_grid(CalendarSpan::Month));
134 assert!(shows_hour_grid(CalendarSpan::Day));
135 assert!(shows_hour_grid(CalendarSpan::WorkWeek));
136 }
137
138 #[test]
139 fn follow_turns_off_on_manual_scroll_and_back_on_align() {
140 let mut follow = FollowNow { active: true };
141 assert!(follow.should_follow(true));
142 follow.note_manual_scroll();
143 assert!(!follow.should_follow(true));
144 follow.request_align();
145 assert!(follow.should_follow(true));
146 assert!(!follow.should_follow(false));
147 }
148
149 #[test]
150 fn offset_diff_uses_epsilon() {
151 assert!(!offset_differs(-100., -100.5, MANUAL_SCROLL_EPSILON));
152 assert!(offset_differs(-100., -110., MANUAL_SCROLL_EPSILON));
153 }
154}