BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / calendar.rs

calendar.rs

calendar.rs 21.5 KiB Download Raw
1use gpui_kit::ClipboardItem;
2use gpui_kit::component::v_flex;
3use gpui_kit::*;
4use store::{CalendarProvider, CalendarRef};
5
6use std::collections::HashMap;
7
8use app_base::format::{WeekdayWidth, clock_format, weekday_name};
9use chrono::{NaiveTime, Weekday};
10use gpui_kit::component::h_flex;
11use gpui_kit::prelude::FluentBuilder as _;
12
13use super::rows::{
14 Choice, caption, control_row, select, select_with_width, text_button, toggle_row,
15};
16use crate::app_state::AppState;
17use crate::call::calendar_prefs::{CalendarPrefs, DefaultView, TimeScale, WeekendWorkWeek};
18use crate::i18n::tr;
19use crate::theme;
20
21pub fn render_view(app: &Entity<AppState>, cx: &App) -> Div {
22 let prefs = app.read(cx).calendar_prefs.clone();
23 v_flex()
24 .w_full()
25 .child(caption(tr("settings.calendar.section_view")))
26 .children(view_rows(
27 app,
28 &prefs,
29 app.read(cx).calendar_layout().first_day,
30 ))
31}
32
33pub fn render_accounts(
34 app: &Entity<AppState>,
35 google_client: Option<&Entity<super::google_client_form::GoogleClientForm>>,
36 caldav: Option<&Entity<super::caldav_form::CalDavForm>>,
37 ics: Option<&Entity<super::caldav_form::IcsForm>>,
38 cx: &App,
39) -> Div {
40 let accounts = app.read(cx).calendar_accounts();
41 let pending = app.read(cx).google_connect_pending().is_some();
42 let mut body = v_flex()
43 .w_full()
44 .child(caption(tr("settings.calendar.section_google")));
45 if let Some(form) = google_client {
46 body = body.child(form.clone());
47 }
48 body = body.child(connect_row(app, accounts.has_google(), pending));
49 for account in &accounts.google_accounts {
50 body = body.child(account_block(app, account, &accounts));
51 }
52 body = body.child(caption(tr("settings.calendar.section_caldav")));
53 for account in &accounts.caldav_accounts {
54 body = body.child(caldav_account_block(app, account, &accounts));
55 }
56 if let Some(form) = caldav {
57 body = body.child(form.clone());
58 }
59 body = body.child(caption(tr("settings.calendar.section_ics")));
60 for subscription in &accounts.ics_subscriptions {
61 body = body.child(ics_block(app, subscription));
62 }
63 if let Some(form) = ics {
64 body = body.child(form.clone());
65 }
66 body = body
67 .child(caption(tr("settings.calendar.section_home")))
68 .child(home_choices(app, &accounts))
69 .child(caption(tr("settings.calendar.section_write")));
70 if accounts.home.provider != CalendarProvider::Outlook {
71 let write_app = app.clone();
72 let target = CalendarRef::outlook();
73 let selected = accounts
74 .write_targets
75 .iter()
76 .any(|existing| existing.key() == target.key());
77 body = body.child(
78 toggle_row(
79 "settings-write-outlook",
80 tr("settings.calendar.outlook_mailbox"),
81 tr("settings.calendar.dual_write_when_google_home"),
82 theme::text_muted(),
83 selected,
84 )
85 .on_click(move |_, _, cx| {
86 write_app.update(cx, |state, cx| {
87 state.toggle_write_target(target.clone(), cx)
88 })
89 }),
90 );
91 } else if accounts.google_accounts.is_empty() && accounts.caldav_accounts.is_empty() {
92 body = body.child(control_row(
93 tr("settings.calendar.no_extra_targets"),
94 tr("settings.calendar.connect_then_dual_write"),
95 div(),
96 ));
97 }
98 body
99}
100
101fn connect_row(app: &Entity<AppState>, connected: bool, pending: bool) -> Div {
102 let app = app.clone();
103 if connected {
104 control_row(
105 tr("settings.calendar.google_title"),
106 tr("settings.calendar.connected_detail"),
107 div(),
108 )
109 } else if pending {
110 let reopen = app.clone();
111 let copy = app.clone();
112 let cancel = app.clone();
113 control_row(
114 tr("settings.calendar.google_title"),
115 tr("settings.calendar.finish_in_browser"),
116 div()
117 .flex()
118 .gap_2()
119 .child(
120 text_button(
121 "settings-google-reopen",
122 tr("settings.calendar.open_browser_again"),
123 )
124 .on_click(move |_, _, cx| {
125 reopen.update(cx, |state, cx| state.reopen_google_connect(cx))
126 }),
127 )
128 .child(
129 text_button("settings-google-copy", tr("settings.calendar.copy_link"))
130 .on_click(move |_, _, cx| {
131 if let Some(url) =
132 copy.read(cx).google_connect_pending().map(str::to_owned)
133 {
134 cx.write_to_clipboard(ClipboardItem::new_string(url));
135 }
136 }),
137 )
138 .child(
139 text_button("settings-google-cancel", tr("common.cancel")).on_click(
140 move |_, _, cx| {
141 cancel.update(cx, |state, cx| state.cancel_google_connect(cx))
142 },
143 ),
144 ),
145 )
146 } else {
147 let connect = app.clone();
148 control_row(
149 tr("settings.calendar.google_title"),
150 tr("settings.calendar.connect_hint"),
151 text_button(
152 "settings-google-connect",
153 tr("settings.calendar.connect_button"),
154 )
155 .on_click(move |_, _, cx| {
156 connect.update(cx, |state, cx| state.connect_google_calendar(cx))
157 }),
158 )
159 }
160}
161
162fn account_block(
163 app: &Entity<AppState>,
164 account: &store::GoogleAccount,
165 accounts: &store::CalendarAccounts,
166) -> Div {
167 let disconnect_app = app.clone();
168 let account_id = account.id.clone();
169 let mut block = v_flex().w_full().child(control_row(
170 account.email.clone(),
171 tr("settings.calendar.google_account"),
172 text_button(
173 format!("settings-google-disconnect-{}", account.id),
174 tr("settings.calendar.disconnect"),
175 )
176 .on_click(move |_, _, cx| {
177 disconnect_app.update(cx, |state, cx| {
178 state.disconnect_google_account(&account_id, cx)
179 })
180 }),
181 ));
182 for calendar in &account.calendars {
183 let toggle_app = app.clone();
184 let account_id = account.id.clone();
185 let calendar_id = calendar.id.clone();
186 let write_ref = CalendarRef::google(&account.id, &calendar.id);
187 let is_home = accounts.home.key() == write_ref.key();
188 let is_write = accounts
189 .write_targets
190 .iter()
191 .any(|target| target.key() == write_ref.key());
192 block = block.child(
193 toggle_row(
194 format!("settings-google-cal-{}", calendar.id),
195 calendar.summary.clone(),
196 if calendar.primary {
197 tr("settings.calendar.primary")
198 } else {
199 tr("settings.calendar.show_in_grid")
200 },
201 theme::text_muted(),
202 calendar.selected,
203 )
204 .on_click(move |_, _, cx| {
205 toggle_app.update(cx, |state, cx| {
206 state.toggle_google_calendar_selected(&account_id, &calendar_id, cx)
207 })
208 }),
209 );
210 if !is_home {
211 let write_app = app.clone();
212 let target = write_ref;
213 block = block.child(
214 toggle_row(
215 format!("settings-google-write-{}", calendar.id),
216 tr_args_write(&calendar.summary),
217 "",
218 theme::text_muted(),
219 is_write,
220 )
221 .on_click(move |_, _, cx| {
222 write_app.update(cx, |state, cx| {
223 state.toggle_write_target(target.clone(), cx)
224 })
225 }),
226 );
227 }
228 }
229 block
230}
231
232fn tr_args_write(summary: &str) -> String {
233 let mut args = std::collections::HashMap::new();
234 args.insert("name".to_owned(), summary.to_owned());
235 crate::i18n::tr_args("settings.calendar.also_write_to", &args)
236}
237
238fn caldav_account_block(
239 app: &Entity<AppState>,
240 account: &store::CalDavAccount,
241 accounts: &store::CalendarAccounts,
242) -> Div {
243 let disconnect_app = app.clone();
244 let account_id = account.id.clone();
245 let mut block = v_flex().w_full().child(control_row(
246 format!("{} · {}", account.display_name, account.url),
247 tr("settings.calendar.caldav_title"),
248 text_button(
249 format!("settings-caldav-disconnect-{}", account.id),
250 tr("settings.calendar.disconnect"),
251 )
252 .on_click(move |_, _, cx| {
253 disconnect_app.update(cx, |state, cx| {
254 state.disconnect_caldav_account(&account_id, cx)
255 })
256 }),
257 ));
258 for calendar in &account.calendars {
259 let toggle_app = app.clone();
260 let account_id = account.id.clone();
261 let calendar_id = calendar.id.clone();
262 let write_ref = CalendarRef::caldav(&account.id, &calendar.id);
263 let is_home = accounts.home.key() == write_ref.key();
264 let is_write = accounts
265 .write_targets
266 .iter()
267 .any(|target| target.key() == write_ref.key());
268 let detail = if calendar.writable {
269 tr("settings.calendar.show_in_grid")
270 } else {
271 tr("settings.calendar.caldav_readonly")
272 };
273 block = block.child(
274 toggle_row(
275 format!("settings-caldav-cal-{}", calendar.id),
276 calendar.summary.clone(),
277 detail,
278 theme::text_muted(),
279 calendar.selected,
280 )
281 .on_click(move |_, _, cx| {
282 toggle_app.update(cx, |state, cx| {
283 state.toggle_caldav_calendar_selected(&account_id, &calendar_id, cx)
284 })
285 }),
286 );
287 if calendar.writable && !is_home {
288 let write_app = app.clone();
289 let target = write_ref;
290 block = block.child(
291 toggle_row(
292 format!("settings-caldav-write-{}", calendar.id),
293 tr_args_write(&calendar.summary),
294 "",
295 theme::text_muted(),
296 is_write,
297 )
298 .on_click(move |_, _, cx| {
299 write_app.update(cx, |state, cx| {
300 state.toggle_write_target(target.clone(), cx)
301 })
302 }),
303 );
304 }
305 }
306 block
307}
308
309fn ics_block(app: &Entity<AppState>, subscription: &store::IcsSubscription) -> Div {
310 let disconnect = app.clone();
311 let toggle = app.clone();
312 let id = subscription.id.clone();
313 let id_toggle = subscription.id.clone();
314 v_flex()
315 .w_full()
316 .child(control_row(
317 subscription.summary.clone(),
318 subscription.url.clone(),
319 text_button(
320 format!("settings-ics-disconnect-{}", subscription.id),
321 tr("settings.calendar.disconnect"),
322 )
323 .on_click(move |_, _, cx| {
324 disconnect.update(cx, |state, cx| state.disconnect_ics_subscription(&id, cx))
325 }),
326 ))
327 .child(
328 toggle_row(
329 format!("settings-ics-selected-{}", subscription.id),
330 tr("settings.calendar.show_in_grid"),
331 tr("settings.calendar.caldav_readonly"),
332 theme::text_muted(),
333 subscription.selected,
334 )
335 .on_click(move |_, _, cx| {
336 toggle.update(cx, |state, cx| {
337 state.toggle_ics_subscription_selected(&id_toggle, cx)
338 })
339 }),
340 )
341}
342
343fn home_choices(app: &Entity<AppState>, accounts: &store::CalendarAccounts) -> Div {
344 let mut choices = v_flex().w_full();
345 let outlook_app = app.clone();
346 let outlook_home = accounts.home.provider == CalendarProvider::Outlook;
347 choices = choices.child(
348 toggle_row(
349 "settings-home-outlook",
350 tr("settings.calendar.outlook_mailbox"),
351 tr("settings.calendar.outlook_home_detail"),
352 theme::text_muted(),
353 outlook_home,
354 )
355 .on_click(move |_, _, cx| {
356 outlook_app.update(cx, |state, cx| {
357 state.set_home_calendar(CalendarRef::outlook(), cx)
358 })
359 }),
360 );
361 for account in &accounts.google_accounts {
362 for calendar in &account.calendars {
363 let home_app = app.clone();
364 let target = CalendarRef::google(&account.id, &calendar.id);
365 let selected = accounts.home.key() == target.key();
366 choices = choices.child(
367 toggle_row(
368 format!("settings-home-google-{}", calendar.id),
369 format!(
370 "{} · {}",
371 tr("settings.calendar.google_label"),
372 calendar.summary
373 ),
374 account.email.clone(),
375 theme::text_muted(),
376 selected,
377 )
378 .on_click(move |_, _, cx| {
379 home_app.update(cx, |state, cx| state.set_home_calendar(target.clone(), cx))
380 }),
381 );
382 }
383 }
384 for account in &accounts.caldav_accounts {
385 for calendar in &account.calendars {
386 if !calendar.writable {
387 continue;
388 }
389 let home_app = app.clone();
390 let target = CalendarRef::caldav(&account.id, &calendar.id);
391 let selected = accounts.home.key() == target.key();
392 choices = choices.child(
393 toggle_row(
394 format!("settings-home-caldav-{}", calendar.id),
395 format!("CalDAV · {}", calendar.summary),
396 account.username.clone(),
397 theme::text_muted(),
398 selected,
399 )
400 .on_click(move |_, _, cx| {
401 home_app.update(cx, |state, cx| state.set_home_calendar(target.clone(), cx))
402 }),
403 );
404 }
405 }
406 choices
407}
408
409const CHIP_SIZE: f32 = 32.;
410const TIME_SELECT_WIDTH: f32 = 104.;
411
412/// Default view, work days and hours, time scale, weekend Work week.
413fn view_rows(app: &Entity<AppState>, prefs: &CalendarPrefs, first_day: Weekday) -> Vec<Div> {
414 vec![
415 control_row(
416 tr("settings.calendar.default_view"),
417 tr("settings.calendar.default_view_detail"),
418 select(
419 "settings-calendar-default-view",
420 default_view_label(prefs.default_view),
421 false,
422 DefaultView::ALL
423 .into_iter()
424 .map(|view| {
425 prefs_choice(
426 app,
427 default_view_label(view),
428 prefs.default_view == view,
429 move |prefs| prefs.default_view = view,
430 )
431 })
432 .collect(),
433 ),
434 ),
435 control_row(
436 tr("settings.calendar.work_days"),
437 tr("settings.calendar.mailbox_detail"),
438 work_day_chips(app, prefs, first_day),
439 ),
440 control_row(
441 tr("settings.calendar.work_hours"),
442 tr("settings.calendar.mailbox_detail"),
443 h_flex()
444 .gap(px(8.))
445 .child(work_time_select(app, prefs, true))
446 .child(work_time_select(app, prefs, false)),
447 ),
448 control_row(
449 tr("settings.calendar.time_scale"),
450 tr("settings.calendar.time_scale_detail"),
451 select(
452 "settings-calendar-time-scale",
453 time_scale_label(prefs.time_scale),
454 false,
455 TimeScale::ALL
456 .into_iter()
457 .map(|scale| {
458 prefs_choice(
459 app,
460 time_scale_label(scale),
461 prefs.time_scale == scale,
462 move |prefs| prefs.time_scale = scale,
463 )
464 })
465 .collect(),
466 ),
467 ),
468 control_row(
469 tr("settings.calendar.weekend"),
470 tr("settings.calendar.weekend_detail"),
471 select(
472 "settings-calendar-weekend",
473 weekend_label(prefs.weekend_work_week),
474 false,
475 [WeekendWorkWeek::NextWeek, WeekendWorkWeek::ThisWeek]
476 .into_iter()
477 .map(|rule| {
478 prefs_choice(
479 app,
480 weekend_label(rule),
481 prefs.weekend_work_week == rule,
482 move |prefs| prefs.weekend_work_week = rule,
483 )
484 })
485 .collect(),
486 ),
487 ),
488 ]
489}
490
491fn prefs_choice(
492 app: &Entity<AppState>,
493 label: String,
494 checked: bool,
495 change: impl Fn(&mut CalendarPrefs) + 'static,
496) -> Choice {
497 let app = app.clone();
498 let change = std::rc::Rc::new(change);
499 Choice::new(label, checked, move |cx| {
500 let change = change.clone();
501 app.update(cx, |state, cx| {
502 state.update_calendar_prefs(|prefs| change(prefs), cx)
503 });
504 })
505}
506
507fn default_view_label(view: DefaultView) -> String {
508 view.span()
509 .map_or_else(|| tr("settings.calendar.last_used"), |span| span.label())
510}
511
512fn time_scale_label(scale: TimeScale) -> String {
513 let mut args = HashMap::new();
514 args.insert("count".to_owned(), scale.minutes().to_string());
515 crate::i18n::tr_args("settings.calendar.minutes", &args)
516}
517
518fn weekend_label(rule: WeekendWorkWeek) -> String {
519 match rule {
520 WeekendWorkWeek::NextWeek => tr("settings.calendar.weekend_next"),
521 WeekendWorkWeek::ThisWeek => tr("settings.calendar.weekend_this"),
522 }
523}
524
525/// Seven day chips from the first day of week; the last work day cannot be turned off.
526fn work_day_chips(app: &Entity<AppState>, prefs: &CalendarPrefs, first_day: Weekday) -> Div {
527 let layout = prefs.layout(first_day);
528 h_flex()
529 .gap(px(4.))
530 .children(layout.weekdays().into_iter().map(|day| {
531 let on = prefs.work_hours.is_work_day(day);
532 let last = on && prefs.work_hours.days.len() == 1;
533 let app = app.clone();
534 let hours = prefs.work_hours.clone();
535 div()
536 .id(SharedString::from(format!(
537 "settings-calendar-work-day-{day}"
538 )))
539 .size(px(CHIP_SIZE))
540 .flex()
541 .items_center()
542 .justify_center()
543 .rounded(px(6.))
544 .text_size(px(12.))
545 .font_weight(FontWeight::SEMIBOLD)
546 .map(|chip| {
547 if on {
548 chip.bg(theme::accent()).text_color(theme::on_accent())
549 } else {
550 chip.bg(theme::surface_raised())
551 .text_color(theme::text_soft())
552 .hover(|chip| chip.bg(theme::popup_hover()))
553 }
554 })
555 .when(!last, |chip| {
556 chip.cursor_pointer().on_click(move |_, _, cx| {
557 let mut hours = hours.clone();
558 if on {
559 hours.days.retain(|existing| *existing != day);
560 } else {
561 hours.days.push(day);
562 hours.days.sort_by_key(Weekday::num_days_from_monday);
563 }
564 app.update(cx, |state, cx| state.set_work_hours(hours, cx));
565 })
566 })
567 .child(weekday_name(day, WeekdayWidth::Short))
568 }))
569}
570
571/// Start or end of work in 30-minute steps; end choices stay after the start.
572fn work_time_select(
573 app: &Entity<AppState>,
574 prefs: &CalendarPrefs,
575 start: bool,
576) -> impl IntoElement {
577 let hours = prefs.work_hours.clone();
578 let current = if start { hours.start } else { hours.end };
579 let choices = (0..48)
580 .filter_map(|step| NaiveTime::from_hms_opt(step / 2, (step % 2) * 30, 0))
581 .filter(|time| {
582 if start {
583 *time < hours.end
584 } else {
585 *time > hours.start
586 }
587 })
588 .map(|time| {
589 let app = app.clone();
590 let mut wanted = hours.clone();
591 if start {
592 wanted.start = time;
593 } else {
594 wanted.end = time;
595 }
596 Choice::new(
597 time.format(clock_format()).to_string(),
598 time == current,
599 move |cx| {
600 let wanted = wanted.clone();
601 app.update(cx, |state, cx| state.set_work_hours(wanted, cx));
602 },
603 )
604 })
605 .collect();
606 select_with_width(
607 if start {
608 "settings-calendar-work-start"
609 } else {
610 "settings-calendar-work-end"
611 },
612 current.format(clock_format()).to_string(),
613 TIME_SELECT_WIDTH,
614 false,
615 choices,
616 )
617}