BlueGit

ict rusty-teams

Files

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

calendar.rs

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