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