| 1 | //! Calendar list pane logic: group check state, Show all, mini month cells. |
| 2 | |
| 3 | use chrono::{Datelike, NaiveDate}; |
| 4 | |
| 5 | /// Groups with more calendars than this hide the ones that are off until Show all. |
| 6 | pub const SHOW_ALL_AFTER: usize = 5; |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub enum GroupCheck { |
| 10 | All, |
| 11 | Some, |
| 12 | None, |
| 13 | } |
| 14 | |
| 15 | pub fn group_check(selected: &[bool]) -> GroupCheck { |
| 16 | match selected.iter().filter(|on| **on).count() { |
| 17 | 0 => GroupCheck::None, |
| 18 | count if count == selected.len() => GroupCheck::All, |
| 19 | _ => GroupCheck::Some, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /// Indexes of the calendars a group lists: all of them when short or expanded, |
| 24 | /// otherwise the ones turned on plus the primary one. |
| 25 | pub fn listed_calendars(selected: &[bool], primary: &[bool], expanded: bool) -> Vec<usize> { |
| 26 | let all = 0..selected.len(); |
| 27 | if expanded || selected.len() <= SHOW_ALL_AFTER { |
| 28 | return all.collect(); |
| 29 | } |
| 30 | all.filter(|&index| selected[index] || primary.get(index).copied().unwrap_or(false)) |
| 31 | .collect() |
| 32 | } |
| 33 | |
| 34 | /// Whether a group offers Show all / Show fewer. |
| 35 | pub fn offers_show_all(count: usize) -> bool { |
| 36 | count > SHOW_ALL_AFTER |
| 37 | } |
| 38 | |
| 39 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 40 | pub struct MiniMonthCell { |
| 41 | pub date: NaiveDate, |
| 42 | pub in_month: bool, |
| 43 | /// Inside the range the grid shows. |
| 44 | pub shown: bool, |
| 45 | pub today: bool, |
| 46 | } |
| 47 | |
| 48 | pub fn mini_month_cell( |
| 49 | date: NaiveDate, |
| 50 | month: NaiveDate, |
| 51 | shown: &[NaiveDate], |
| 52 | today: NaiveDate, |
| 53 | ) -> MiniMonthCell { |
| 54 | MiniMonthCell { |
| 55 | date, |
| 56 | in_month: date.month() == month.month() && date.year() == month.year(), |
| 57 | shown: shown.contains(&date), |
| 58 | today: date == today, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[cfg(test)] |
| 63 | mod tests { |
| 64 | use super::*; |
| 65 | |
| 66 | #[test] |
| 67 | fn the_group_box_reads_all_some_or_none() { |
| 68 | assert_eq!(group_check(&[true, true]), GroupCheck::All); |
| 69 | assert_eq!(group_check(&[true, false]), GroupCheck::Some); |
| 70 | assert_eq!(group_check(&[false, false]), GroupCheck::None); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn a_long_group_lists_the_primary_and_the_ones_on_until_show_all() { |
| 75 | let mut selected = [false; 8]; |
| 76 | selected[3] = true; |
| 77 | selected[6] = true; |
| 78 | let mut primary = [false; 8]; |
| 79 | primary[0] = true; |
| 80 | assert_eq!(listed_calendars(&selected, &primary, false), [0, 3, 6]); |
| 81 | assert_eq!(listed_calendars(&selected, &primary, true).len(), 8); |
| 82 | assert!(offers_show_all(8)); |
| 83 | assert!(!offers_show_all(5)); |
| 84 | assert_eq!(listed_calendars(&[false; 4], &[false; 4], false).len(), 4); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn mini_month_marks_the_shown_range_and_today() { |
| 89 | let day = |d| NaiveDate::from_ymd_opt(2026, 11, d).unwrap(); |
| 90 | let shown: Vec<_> = (16..=20).map(day).collect(); |
| 91 | let cell = mini_month_cell(day(18), day(1), &shown, day(15)); |
| 92 | assert!(cell.in_month && cell.shown && !cell.today); |
| 93 | let today = mini_month_cell(day(15), day(1), &shown, day(15)); |
| 94 | assert!(today.today && !today.shown); |
| 95 | let outside = mini_month_cell( |
| 96 | NaiveDate::from_ymd_opt(2026, 12, 1).unwrap(), |
| 97 | day(1), |
| 98 | &shown, |
| 99 | day(15), |
| 100 | ); |
| 101 | assert!(!outside.in_month); |
| 102 | } |
| 103 | } |