BlueGit

ict rusty-teams

Files

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

calendar_month.rs

calendar_month.rs 7.8 KiB Download Raw
1use app_base::format::clock_format;
2use app_base::format::{WeekdayWidth, weekday_name};
3use chatsvc::CalendarEvent;
4use chrono::{Datelike, FixedOffset, NaiveDate, Weekday};
5use gpui_kit::component::{h_flex, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8
9use crate::app_state::AppState;
10use crate::call::week_grid::{WeekDay, is_effective_all_day, month_shown};
11use crate::theme;
12
13pub const MONTH_GRID_TAG: &str = "shell/islands/calendar/month";
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 let mut args = std::collections::HashMap::new();
35 args.insert("count".to_owned(), hidden.to_string());
36 app_base::ui_locale::tr_args("calendar.view.more", &args)
37}
38
39fn event_chip(event: &CalendarEvent, color: Hsla, offset: FixedOffset) -> Div {
40 let all_day = is_effective_all_day(event, offset);
41 let time = (!all_day).then(|| {
42 event
43 .start
44 .with_timezone(&offset)
45 .format(clock_format())
46 .to_string()
47 });
48 h_flex()
49 .h(px(CHIP_HEIGHT))
50 .flex_none()
51 .w_full()
52 .px(px(6.))
53 .gap(px(6.))
54 .items_center()
55 .rounded(px(6.))
56 .bg(color.opacity(if all_day { 0.28 } else { 0.14 }))
57 .text_size(px(11.5))
58 .text_color(theme::text())
59 .children(time.map(|time| div().flex_none().text_color(theme::text_soft()).child(time)))
60 .child(div().flex_1().min_w_0().truncate().child({
61 let mut sources = event.sources.clone();
62 if sources.is_empty() {
63 sources.push(event.source);
64 }
65 sources.sort_by_key(|source| source.label());
66 sources.dedup();
67 if sources.len() == 1 && sources[0] == chatsvc::CalendarSource::Outlook {
68 event.subject.clone()
69 } else {
70 let names = sources
71 .iter()
72 .map(|source| source.label())
73 .collect::<Vec<_>>()
74 .join("+");
75 format!("{} · {names}", event.subject)
76 }
77 }))
78}
79
80fn day_cell(
81 state: &Entity<AppState>,
82 day: &WeekDay,
83 events: &[CalendarEvent],
84 colors: &[Hsla],
85 shown_month: NaiveDate,
86 today: NaiveDate,
87 offset: FixedOffset,
88) -> Stateful<Div> {
89 let date = day.date;
90 let in_month = date.month() == shown_month.month();
91 let is_today = date == today;
92 let indexes = day_events(day);
93 let hidden = indexes.len().saturating_sub(VISIBLE_EVENTS);
94 let number = if date.day() == 1 {
95 format!(
96 "{} {}",
97 app_base::format::month_name(date)
98 .chars()
99 .take(3)
100 .collect::<String>(),
101 date.day()
102 )
103 } else {
104 date.day().to_string()
105 };
106 let open_state = state.clone();
107 v_flex()
108 .id(SharedString::from(format!("calendar-month-{date}")))
109 .flex_1()
110 .min_w_0()
111 .h_full()
112 .p(px(4.))
113 .gap(px(2.))
114 .overflow_hidden()
115 .cursor_pointer()
116 .when(!in_month, |cell| cell.bg(theme::background().opacity(0.35)))
117 .hover(|cell| cell.bg(theme::row_hover()))
118 .child(
119 h_flex().child(
120 div()
121 .h(px(DAY_NUMBER_SIZE))
122 .min_w(px(DAY_NUMBER_SIZE))
123 .px(px(6.))
124 .flex()
125 .items_center()
126 .justify_center()
127 .rounded_full()
128 .text_size(px(12.))
129 .when(is_today, |number| {
130 number
131 .bg(theme::accent())
132 .text_color(theme::on_accent())
133 .font_weight(FontWeight::SEMIBOLD)
134 })
135 .when(!is_today, |number| {
136 number.text_color(if in_month {
137 theme::text()
138 } else {
139 theme::text_faint()
140 })
141 })
142 .child(number),
143 ),
144 )
145 .children(
146 indexes
147 .iter()
148 .take(VISIBLE_EVENTS)
149 .filter_map(|index| Some((events.get(*index)?, *colors.get(*index)?)))
150 .map(|(event, color)| event_chip(event, color, offset)),
151 )
152 .when(hidden > 0, |cell| {
153 cell.child(
154 div()
155 .px(px(6.))
156 .text_size(px(11.5))
157 .text_color(theme::text_muted())
158 .child(more_label(hidden)),
159 )
160 })
161 .on_click(move |_, _, cx| {
162 open_state.update(cx, |state, cx| state.show_calendar_day(date, cx));
163 })
164}
165
166fn horizontal_line() -> Div {
167 div().w_full().h(px(1.)).flex_none().bg(theme::border())
168}
169
170fn vertical_line() -> Div {
171 div().w(px(1.)).h_full().flex_none().bg(theme::border())
172}
173
174pub fn month_grid(
175 state: &Entity<AppState>,
176 start: NaiveDate,
177 days: &[WeekDay],
178 events: &[CalendarEvent],
179 colors: &[Hsla],
180 today: NaiveDate,
181 offset: FixedOffset,
182 weekdays: [Weekday; 7],
183) -> Div {
184 let shown_month = month_shown(start);
185 let mut rows = v_flex().flex_1().min_h_0().w_full();
186 for week in days.chunks(7) {
187 let mut row = h_flex().flex_1().min_h_0().w_full();
188 for (position, day) in week.iter().enumerate() {
189 if position > 0 {
190 row = row.child(vertical_line());
191 }
192 row = row.child(day_cell(
193 state,
194 day,
195 events,
196 colors,
197 shown_month,
198 today,
199 offset,
200 ));
201 }
202 rows = rows.child(horizontal_line()).child(row);
203 }
204 v_flex()
205 .debug_selector(|| MONTH_GRID_TAG.into())
206 .flex_1()
207 .min_h_0()
208 .w_full()
209 .child(
210 h_flex()
211 .h(px(WEEKDAY_HEADER_HEIGHT))
212 .flex_none()
213 .w_full()
214 .children(weekdays.iter().map(|day| {
215 div()
216 .flex_1()
217 .px(px(10.))
218 .text_size(px(12.))
219 .text_color(theme::text_muted())
220 .child(weekday_name(*day, WeekdayWidth::Short))
221 })),
222 )
223 .child(rows)
224}
225
226#[cfg(test)]
227mod tests {
228 use super::{WeekDay, day_events, more_label};
229 use crate::call::week_grid::WeekBlock;
230 use chrono::NaiveDate;
231
232 #[test]
233 fn a_day_lists_all_day_events_first_then_by_start() {
234 let block = |event_index, start_minute| WeekBlock {
235 event_index,
236 start_minute,
237 end_minute: start_minute + 30,
238 lane: 0,
239 lanes: 1,
240 };
241 let day = WeekDay {
242 date: NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(),
243 all_day: vec![4],
244 blocks: vec![block(2, 600), block(1, 540)],
245 };
246 assert_eq!(day_events(&day), [4, 1, 2]);
247 let _guard = crate::i18n::locale_test_lock();
248 app_base::ui_locale::apply_locale("en");
249 assert_eq!(more_label(2), "+2 more");
250 app_base::ui_locale::apply_locale("de");
251 assert_eq!(more_label(2), "+2 weitere");
252 app_base::ui_locale::apply_locale("en");
253 }
254}