BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / calendar_month.rs

calendar_month.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 3 weeks ago
a6592765 History
calendar_month.rs 6.4 KiB Download Raw
1use app_base::format::clock_format;
2use chatsvc::CalendarEvent;
3use chrono::{Datelike, FixedOffset, NaiveDate};
4use gpui_kit::component::{h_flex, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use crate::app_state::AppState;
9use crate::call::week_grid::{WeekDay, month_shown};
10use crate::theme;
11
12pub const MONTH_GRID_TAG: &str = "shell/islands/calendar/month";
13const WEEKDAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
14const WEEKDAY_HEADER_HEIGHT: f32 = 32.;
15const VISIBLE_EVENTS: usize = 3;
16const DAY_NUMBER_SIZE: f32 = 24.;
17const CHIP_HEIGHT: f32 = 20.;
18
19fn day_events(day: &WeekDay) -> Vec<usize> {
20 let mut timed: Vec<(i64, usize)> = day
21 .blocks
22 .iter()
23 .map(|block| (block.start_minute, block.event_index))
24 .collect();
25 timed.sort();
26 day.all_day
27 .iter()
28 .copied()
29 .chain(timed.into_iter().map(|(_, index)| index))
30 .collect()
31}
32
33fn more_label(hidden: usize) -> String {
34 format!("+{hidden} more")
35}
36
37fn event_chip(event: &CalendarEvent, offset: FixedOffset) -> Div {
38 let time = (!event.all_day).then(|| {
39 event
40 .start
41 .with_timezone(&offset)
42 .format(clock_format())
43 .to_string()
44 });
45 h_flex()
46 .h(px(CHIP_HEIGHT))
47 .flex_none()
48 .w_full()
49 .px(px(6.))
50 .gap(px(6.))
51 .items_center()
52 .rounded(px(4.))
53 .bg(theme::accent().opacity(if event.all_day { 0.28 } else { 0.14 }))
54 .text_size(px(11.5))
55 .text_color(theme::text())
56 .children(time.map(|time| div().flex_none().text_color(theme::text_soft()).child(time)))
57 .child(
58 div()
59 .flex_1()
60 .min_w_0()
61 .truncate()
62 .child(event.subject.clone()),
63 )
64}
65
66fn day_cell(
67 state: &Entity<AppState>,
68 day: &WeekDay,
69 events: &[CalendarEvent],
70 shown_month: NaiveDate,
71 today: NaiveDate,
72 offset: FixedOffset,
73) -> Stateful<Div> {
74 let date = day.date;
75 let in_month = date.month() == shown_month.month();
76 let is_today = date == today;
77 let indexes = day_events(day);
78 let hidden = indexes.len().saturating_sub(VISIBLE_EVENTS);
79 let number = if date.day() == 1 {
80 date.format("%b %-d").to_string()
81 } else {
82 date.day().to_string()
83 };
84 let open_state = state.clone();
85 v_flex()
86 .id(SharedString::from(format!("calendar-month-{date}")))
87 .flex_1()
88 .min_w_0()
89 .h_full()
90 .p(px(4.))
91 .gap(px(2.))
92 .overflow_hidden()
93 .cursor_pointer()
94 .when(!in_month, |cell| cell.bg(theme::background().opacity(0.35)))
95 .hover(|cell| cell.bg(theme::row_hover()))
96 .child(
97 h_flex().child(
98 div()
99 .h(px(DAY_NUMBER_SIZE))
100 .min_w(px(DAY_NUMBER_SIZE))
101 .px(px(6.))
102 .flex()
103 .items_center()
104 .justify_center()
105 .rounded_full()
106 .text_size(px(12.))
107 .when(is_today, |number| {
108 number
109 .bg(theme::accent())
110 .text_color(theme::on_accent())
111 .font_weight(FontWeight::SEMIBOLD)
112 })
113 .when(!is_today, |number| {
114 number.text_color(if in_month {
115 theme::text()
116 } else {
117 theme::text_faint()
118 })
119 })
120 .child(number),
121 ),
122 )
123 .children(
124 indexes
125 .iter()
126 .take(VISIBLE_EVENTS)
127 .filter_map(|index| events.get(*index))
128 .map(|event| event_chip(event, offset)),
129 )
130 .when(hidden > 0, |cell| {
131 cell.child(
132 div()
133 .px(px(6.))
134 .text_size(px(11.5))
135 .text_color(theme::text_muted())
136 .child(more_label(hidden)),
137 )
138 })
139 .on_click(move |_, _, cx| {
140 open_state.update(cx, |state, cx| state.show_calendar_day(date, cx));
141 })
142}
143
144fn horizontal_line() -> Div {
145 div().w_full().h(px(1.)).flex_none().bg(theme::border())
146}
147
148fn vertical_line() -> Div {
149 div().w(px(1.)).h_full().flex_none().bg(theme::border())
150}
151
152pub fn month_grid(
153 state: &Entity<AppState>,
154 start: NaiveDate,
155 days: &[WeekDay],
156 events: &[CalendarEvent],
157 today: NaiveDate,
158 offset: FixedOffset,
159) -> Div {
160 let shown_month = month_shown(start);
161 let mut rows = v_flex().flex_1().min_h_0().w_full();
162 for week in days.chunks(7) {
163 let mut row = h_flex().flex_1().min_h_0().w_full();
164 for (position, day) in week.iter().enumerate() {
165 if position > 0 {
166 row = row.child(vertical_line());
167 }
168 row = row.child(day_cell(state, day, events, shown_month, today, offset));
169 }
170 rows = rows.child(horizontal_line()).child(row);
171 }
172 v_flex()
173 .debug_selector(|| MONTH_GRID_TAG.into())
174 .flex_1()
175 .min_h_0()
176 .w_full()
177 .child(
178 h_flex()
179 .h(px(WEEKDAY_HEADER_HEIGHT))
180 .flex_none()
181 .w_full()
182 .children(WEEKDAY_NAMES.iter().map(|name| {
183 div()
184 .flex_1()
185 .px(px(10.))
186 .text_size(px(12.))
187 .text_color(theme::text_muted())
188 .child(*name)
189 })),
190 )
191 .child(rows)
192}
193
194#[cfg(test)]
195mod tests {
196 use super::{WeekDay, day_events, more_label};
197 use crate::call::week_grid::WeekBlock;
198 use chrono::NaiveDate;
199
200 #[test]
201 fn a_day_lists_all_day_events_first_then_by_start() {
202 let block = |event_index, start_minute| WeekBlock {
203 event_index,
204 start_minute,
205 end_minute: start_minute + 30,
206 lane: 0,
207 lanes: 1,
208 };
209 let day = WeekDay {
210 date: NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(),
211 all_day: vec![4],
212 blocks: vec![block(2, 600), block(1, 540)],
213 };
214 assert_eq!(day_events(&day), [4, 1, 2]);
215 assert_eq!(more_label(2), "+2 more");
216 }
217}