BlueGit

ict rusty-teams

Files

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

calendar_picker.rs

calendar_picker.rs 15.5 KiB Download Raw
1//! Calendar list pane left of the grid, laid out like Teams: heading, mini month,
2//! Add calendar, and one collapsible group per account with show/hide boxes.
3
4use std::rc::Rc;
5
6use chrono::Local;
7use gpui_kit::assets::IconName;
8use gpui_kit::component::Sizable as _;
9use gpui_kit::component::button::{Button, ButtonVariants as _};
10use gpui_kit::component::{h_flex, v_flex};
11use gpui_kit::prelude::FluentBuilder as _;
12use gpui_kit::*;
13use store::{CalDavAccount, CalendarAccounts, GoogleAccount, IcsSubscription};
14
15use super::calendar_mini_month::mini_month_grid;
16use crate::app_state::AppState;
17use crate::call::calendar_list::{GroupCheck, group_check, listed_calendars, offers_show_all};
18use crate::call::calendar_prefs::OUTLOOK_GROUP;
19use crate::call::week_grid::google_calendar_index;
20use crate::i18n::tr;
21use crate::settings_section::SettingsSection;
22use crate::theme;
23use crate::views::widgets::icon;
24
25pub const PICKER_WIDTH: f32 = 236.;
26pub const CALENDAR_PANE_TAG: &str = "shell/islands/calendar/picker";
27pub const MINI_MONTH_TAG: &str = "shell/islands/calendar/picker/mini-month";
28const ROW_HEIGHT: f32 = 28.;
29const MINI_CELL: f32 = 30.;
30const CHECK_SIZE: f32 = 14.;
31
32pub fn render(app: &Entity<AppState>, cx: &App) -> Div {
33 let state = app.read(cx);
34 let accounts = state.calendar_accounts();
35 let prefs = &state.calendar_prefs;
36 let mut column = v_flex()
37 .debug_selector(|| CALENDAR_PANE_TAG.into())
38 .w(px(PICKER_WIDTH))
39 .flex_none()
40 .h_full()
41 .border_r_1()
42 .border_color(theme::border())
43 .bg(theme::surface())
44 .px(px(12.))
45 .py(px(12.))
46 .gap(px(4.))
47 .child(
48 div()
49 .text_size(px(16.))
50 .font_weight(FontWeight::BOLD)
51 .text_color(theme::text())
52 .pb(px(8.))
53 .child(tr("calendar.view.title")),
54 )
55 .child(mini_month(app, state))
56 .child(add_calendar_row(app))
57 .child(outlook_group(
58 app,
59 &accounts,
60 prefs.group_collapsed(OUTLOOK_GROUP),
61 ));
62 for account in &accounts.google_accounts {
63 column = column.child(google_group(
64 app,
65 &accounts,
66 account,
67 prefs.group_collapsed(&account.id),
68 state.calendar_expanded_groups.contains(&account.id),
69 ));
70 }
71 for account in &accounts.caldav_accounts {
72 column = column.child(caldav_group(
73 app,
74 &accounts,
75 account,
76 prefs.group_collapsed(&account.id),
77 ));
78 }
79 for subscription in &accounts.ics_subscriptions {
80 column = column.child(ics_row(app, subscription));
81 }
82 column
83}
84
85/// Mini month with its own month arrows; a click shows the span around that day.
86fn mini_month(app: &Entity<AppState>, state: &AppState) -> Div {
87 let month = state.calendar_mini_month_shown();
88 let (back, forward, pick) = (app.clone(), app.clone(), app.clone());
89 v_flex()
90 .debug_selector(|| MINI_MONTH_TAG.into())
91 .pb(px(8.))
92 .child(
93 h_flex()
94 .items_center()
95 .justify_between()
96 .pb(px(4.))
97 .child(
98 div()
99 .pl(px(4.))
100 .text_size(px(13.))
101 .font_weight(FontWeight::SEMIBOLD)
102 .text_color(theme::text())
103 .child(app_base::format::month_year_label(month)),
104 )
105 .child(
106 h_flex()
107 .child(
108 Button::new("calendar-mini-back")
109 .xsmall()
110 .ghost()
111 .icon(IconName::ChevronUp)
112 .tooltip(tr("calendar.view.previous_month_short"))
113 .on_click(move |_, _, cx| {
114 back.update(cx, |state, cx| {
115 state.move_calendar_mini_month(-1, cx)
116 })
117 }),
118 )
119 .child(
120 Button::new("calendar-mini-forward")
121 .xsmall()
122 .ghost()
123 .icon(IconName::ChevronDown)
124 .tooltip(tr("calendar.view.next_month_short"))
125 .on_click(move |_, _, cx| {
126 forward.update(cx, |state, cx| {
127 state.move_calendar_mini_month(1, cx)
128 })
129 }),
130 ),
131 ),
132 )
133 .child(mini_month_grid(
134 "calendar-mini",
135 month,
136 &state.calendar_dates(),
137 state.calendar_layout().first_day,
138 Local::now().date_naive(),
139 MINI_CELL,
140 Rc::new(move |date, _, cx| {
141 pick.update(cx, |state, cx| state.show_calendar_date(date, cx))
142 }),
143 ))
144}
145
146fn add_calendar_row(app: &Entity<AppState>) -> Stateful<Div> {
147 let app = app.clone();
148 h_flex()
149 .id("calendar-add-calendar")
150 .h(px(ROW_HEIGHT))
151 .flex_none()
152 .w_full()
153 .items_center()
154 .gap(px(8.))
155 .px(px(4.))
156 .rounded(px(6.))
157 .cursor_pointer()
158 .hover(|row| row.bg(theme::popup_hover()))
159 .text_size(px(13.))
160 .text_color(theme::accent_text())
161 .child(icon(IconName::Plus, 14., theme::accent_text()))
162 .child(tr("calendar.view.add_calendar"))
163 .on_click(move |_, _, cx| {
164 app.update(cx, |state, cx| {
165 state.open_settings(SettingsSection::CalendarAccounts, cx)
166 })
167 })
168}
169
170fn outlook_group(app: &Entity<AppState>, accounts: &CalendarAccounts, collapsed: bool) -> Div {
171 let on = accounts.outlook_selected;
172 let (toggle_group, toggle_row) = (app.clone(), app.clone());
173 let collapse = app.clone();
174 group(
175 format!("calendar-group-{OUTLOOK_GROUP}"),
176 tr("calendar.view.my_calendars"),
177 if on {
178 GroupCheck::All
179 } else {
180 GroupCheck::None
181 },
182 collapsed,
183 move |cx| toggle_group.update(cx, |state, cx| state.toggle_outlook_calendar_selected(cx)),
184 move |cx| {
185 collapse.update(cx, |state, cx| {
186 state.update_calendar_prefs(|prefs| prefs.toggle_group(OUTLOOK_GROUP), cx)
187 })
188 },
189 )
190 .when(!collapsed, |group| {
191 group.child(calendar_row(
192 "calendar-picker-outlook",
193 "Outlook".to_owned(),
194 on,
195 theme::calendar_color(0),
196 move |cx| toggle_row.update(cx, |state, cx| state.toggle_outlook_calendar_selected(cx)),
197 ))
198 })
199}
200
201fn google_group(
202 app: &Entity<AppState>,
203 accounts: &CalendarAccounts,
204 account: &GoogleAccount,
205 collapsed: bool,
206 expanded: bool,
207) -> Div {
208 let selected: Vec<bool> = account
209 .calendars
210 .iter()
211 .map(|calendar| calendar.selected)
212 .collect();
213 let primary: Vec<bool> = account
214 .calendars
215 .iter()
216 .map(|calendar| calendar.primary)
217 .collect();
218 let check = group_check(&selected);
219 let (toggle_group, collapse, expand) = (app.clone(), app.clone(), app.clone());
220 let (group_id, collapse_id, expand_id) =
221 (account.id.clone(), account.id.clone(), account.id.clone());
222 let mut body = group(
223 format!("calendar-group-{}", account.id),
224 account.email.clone(),
225 check,
226 collapsed,
227 move |cx| {
228 toggle_group.update(cx, |state, cx| {
229 state.set_google_account_selected(&group_id, check != GroupCheck::All, cx)
230 })
231 },
232 move |cx| {
233 collapse.update(cx, |state, cx| {
234 state.update_calendar_prefs(|prefs| prefs.toggle_group(&collapse_id), cx)
235 })
236 },
237 );
238 if collapsed {
239 return body;
240 }
241 for index in listed_calendars(&selected, &primary, expanded) {
242 let calendar = &account.calendars[index];
243 let toggle = app.clone();
244 let (account_id, calendar_id) = (account.id.clone(), calendar.id.clone());
245 let color = google_calendar_index(accounts, &account.id, &calendar.id)
246 .map_or_else(theme::accent, theme::calendar_color);
247 body = body.child(calendar_row(
248 format!("calendar-picker-{}-{}", account.id, calendar.id),
249 calendar.summary.clone(),
250 calendar.selected,
251 color,
252 move |cx| {
253 toggle.update(cx, |state, cx| {
254 state.toggle_google_calendar_selected(&account_id, &calendar_id, cx)
255 });
256 },
257 ));
258 }
259 if offers_show_all(account.calendars.len()) {
260 body = body.child(
261 div()
262 .id(SharedString::from(format!(
263 "calendar-show-all-{}",
264 account.id
265 )))
266 .pl(px(28.))
267 .h(px(ROW_HEIGHT))
268 .flex_none()
269 .flex()
270 .items_center()
271 .cursor_pointer()
272 .text_size(px(12.))
273 .text_color(theme::accent_text())
274 .child(if expanded {
275 tr("calendar.view.show_fewer")
276 } else {
277 tr("calendar.view.show_all")
278 })
279 .on_click(move |_, _, cx| {
280 expand.update(cx, |state, cx| {
281 state.toggle_calendar_group_expanded(&expand_id, cx)
282 })
283 }),
284 );
285 }
286 body
287}
288
289fn caldav_group(
290 app: &Entity<AppState>,
291 accounts: &CalendarAccounts,
292 account: &CalDavAccount,
293 collapsed: bool,
294) -> Div {
295 let selected: Vec<bool> = account
296 .calendars
297 .iter()
298 .map(|calendar| calendar.selected)
299 .collect();
300 let check = group_check(&selected);
301 let (collapse, group_id) = (app.clone(), account.id.clone());
302 let mut body = group(
303 format!("calendar-group-caldav-{}", account.id),
304 format!("CalDAV · {}", account.display_name),
305 check,
306 collapsed,
307 |_| {},
308 move |cx| {
309 collapse.update(cx, |state, cx| {
310 state.update_calendar_prefs(|prefs| prefs.toggle_group(&group_id), cx)
311 })
312 },
313 );
314 if collapsed {
315 return body;
316 }
317 for calendar in &account.calendars {
318 let toggle = app.clone();
319 let (account_id, calendar_id) = (account.id.clone(), calendar.id.clone());
320 let color_index = accounts
321 .google_accounts
322 .iter()
323 .map(|account| account.calendars.len())
324 .sum::<usize>()
325 + 1
326 + accounts
327 .caldav_accounts
328 .iter()
329 .flat_map(|account| account.calendars.iter().map(|c| c.id.as_str()))
330 .position(|id| id == calendar.id)
331 .unwrap_or(0);
332 body = body.child(calendar_row(
333 format!("calendar-picker-caldav-{}", calendar.id),
334 calendar.summary.clone(),
335 calendar.selected,
336 theme::calendar_color(color_index),
337 move |cx| {
338 toggle.update(cx, |state, cx| {
339 state.toggle_caldav_calendar_selected(&account_id, &calendar_id, cx)
340 });
341 },
342 ));
343 }
344 body
345}
346
347fn ics_row(app: &Entity<AppState>, subscription: &IcsSubscription) -> Stateful<Div> {
348 let toggle = app.clone();
349 let id = subscription.id.clone();
350 calendar_row(
351 format!("calendar-picker-ics-{}", subscription.id),
352 subscription.summary.clone(),
353 subscription.selected,
354 theme::calendar_color(8),
355 move |cx| {
356 toggle.update(cx, |state, cx| {
357 state.toggle_ics_subscription_selected(&id, cx)
358 })
359 },
360 )
361}
362
363/// Group header: chevron to collapse, a check box for the whole group, the title.
364fn group(
365 id: String,
366 title: String,
367 check: GroupCheck,
368 collapsed: bool,
369 toggle: impl Fn(&mut App) + 'static,
370 collapse: impl Fn(&mut App) + 'static,
371) -> Div {
372 v_flex().w_full().pt(px(6.)).child(
373 h_flex()
374 .h(px(ROW_HEIGHT))
375 .flex_none()
376 .w_full()
377 .items_center()
378 .gap(px(6.))
379 .child(
380 div()
381 .id(SharedString::from(format!("{id}-chevron")))
382 .flex_none()
383 .cursor_pointer()
384 .child(icon(
385 if collapsed {
386 IconName::ChevronRight
387 } else {
388 IconName::ChevronDown
389 },
390 14.,
391 theme::text_muted(),
392 ))
393 .on_click(move |_, _, cx| collapse(cx)),
394 )
395 .child(
396 div()
397 .id(SharedString::from(format!("{id}-check")))
398 .flex_none()
399 .cursor_pointer()
400 .child(check_box(check, theme::accent()))
401 .on_click(move |_, _, cx| toggle(cx)),
402 )
403 .child(
404 div()
405 .flex_1()
406 .min_w_0()
407 .truncate()
408 .text_size(px(13.))
409 .font_weight(FontWeight::SEMIBOLD)
410 .text_color(theme::text())
411 .child(title),
412 ),
413 )
414}
415
416fn calendar_row(
417 id: impl Into<ElementId>,
418 label: String,
419 on: bool,
420 color: Hsla,
421 on_click: impl Fn(&mut App) + 'static,
422) -> Stateful<Div> {
423 h_flex()
424 .id(id)
425 .h(px(ROW_HEIGHT))
426 .flex_none()
427 .w_full()
428 .items_center()
429 .gap(px(8.))
430 .pl(px(24.))
431 .pr(px(4.))
432 .rounded(px(6.))
433 .cursor_pointer()
434 .hover(|row| row.bg(theme::popup_hover()))
435 .on_click(move |_, _, cx| on_click(cx))
436 .child(check_box(
437 if on {
438 GroupCheck::All
439 } else {
440 GroupCheck::None
441 },
442 color,
443 ))
444 .child(
445 div()
446 .flex_1()
447 .min_w_0()
448 .truncate()
449 .text_size(px(13.))
450 .text_color(theme::text())
451 .child(label),
452 )
453}
454
455/// Check box in the calendar's colour: filled with a tick when on, a dash when partly on.
456fn check_box(check: GroupCheck, color: Hsla) -> Div {
457 div()
458 .size(px(CHECK_SIZE))
459 .flex_none()
460 .flex()
461 .items_center()
462 .justify_center()
463 .rounded(px(3.))
464 .map(|check_box| match check {
465 GroupCheck::All => {
466 check_box
467 .bg(color)
468 .child(icon(IconName::Check, 10., theme::on_accent()))
469 }
470 GroupCheck::Some => check_box.bg(color.opacity(0.5)).child(
471 div()
472 .w(px(8.))
473 .h(px(2.))
474 .rounded_full()
475 .bg(theme::on_accent()),
476 ),
477 // Lines stay soft: an empty box is a light fill of the calendar colour.
478 GroupCheck::None => check_box.bg(color.opacity(0.22)),
479 })
480}