| 1 | //! Regional format rows: region, time format and first day of week. |
| 2 | |
| 3 | use app_base::format::{DateStyle, WeekdayWidth, weekday_name}; |
| 4 | use app_base::regional::{ClockPreference, Region, RegionalPrefs, SystemFormat}; |
| 5 | use app_base::ui_locale::tr_with; |
| 6 | use chrono::{NaiveTime, Weekday}; |
| 7 | use gpui_kit::*; |
| 8 | |
| 9 | use super::rows::{Choice, control_row, select_with_width}; |
| 10 | use crate::app_state::AppState; |
| 11 | use crate::i18n::tr; |
| 12 | |
| 13 | const SELECT_WIDTH: f32 = 240.; |
| 14 | const FIRST_DAY_CHOICES: [Weekday; 3] = [Weekday::Mon, Weekday::Sun, Weekday::Sat]; |
| 15 | |
| 16 | pub fn rows(app: &Entity<AppState>, cx: &App) -> Vec<Div> { |
| 17 | let state = app.read(cx); |
| 18 | let prefs = state.regional_prefs; |
| 19 | let system = SystemFormat::read(); |
| 20 | let automatic = |
| 21 | |change: fn(RegionalPrefs) -> RegionalPrefs| state.regional_style(&change(prefs), &system); |
| 22 | let auto_clock = automatic(|prefs| RegionalPrefs { |
| 23 | clock: ClockPreference::Auto, |
| 24 | ..prefs |
| 25 | }); |
| 26 | let auto_first_day = automatic(|prefs| RegionalPrefs { |
| 27 | first_day: None, |
| 28 | ..prefs |
| 29 | }) |
| 30 | .first_day(); |
| 31 | let system_region = system |
| 32 | .region() |
| 33 | .map(Region::label) |
| 34 | .or(system.locale.clone()) |
| 35 | .unwrap_or_default(); |
| 36 | let region_label = |region: Option<Region>| match region { |
| 37 | Some(region) => region.label(), |
| 38 | None => tr_with( |
| 39 | "settings.appearance.region.follow_system", |
| 40 | &[("region", &system_region)], |
| 41 | ), |
| 42 | }; |
| 43 | let mut regions = Region::ALL.to_vec(); |
| 44 | regions.sort_by_cached_key(|region| region.label()); |
| 45 | |
| 46 | vec![ |
| 47 | control_row( |
| 48 | tr("settings.appearance.region.title"), |
| 49 | tr("settings.appearance.region.detail"), |
| 50 | select_with_width( |
| 51 | "settings-region", |
| 52 | region_label(prefs.region), |
| 53 | SELECT_WIDTH, |
| 54 | false, |
| 55 | std::iter::once(None) |
| 56 | .chain(regions.into_iter().map(Some)) |
| 57 | .map(|region| { |
| 58 | choice( |
| 59 | app, |
| 60 | region_label(region), |
| 61 | prefs.region == region, |
| 62 | RegionalPrefs { region, ..prefs }, |
| 63 | ) |
| 64 | }) |
| 65 | .collect(), |
| 66 | ), |
| 67 | ), |
| 68 | control_row( |
| 69 | tr("settings.appearance.clock.title"), |
| 70 | tr("settings.appearance.clock.detail"), |
| 71 | select_with_width( |
| 72 | "settings-clock", |
| 73 | clock_label(prefs.clock, auto_clock), |
| 74 | SELECT_WIDTH, |
| 75 | false, |
| 76 | ClockPreference::ALL |
| 77 | .into_iter() |
| 78 | .map(|clock| { |
| 79 | choice( |
| 80 | app, |
| 81 | clock_label(clock, auto_clock), |
| 82 | prefs.clock == clock, |
| 83 | RegionalPrefs { clock, ..prefs }, |
| 84 | ) |
| 85 | }) |
| 86 | .collect(), |
| 87 | ), |
| 88 | ), |
| 89 | control_row( |
| 90 | tr("settings.appearance.first_day.title"), |
| 91 | tr("settings.appearance.first_day.detail"), |
| 92 | select_with_width( |
| 93 | "settings-first-day", |
| 94 | first_day_label(prefs.first_day, auto_first_day), |
| 95 | SELECT_WIDTH, |
| 96 | false, |
| 97 | std::iter::once(None) |
| 98 | .chain(FIRST_DAY_CHOICES.into_iter().map(Some)) |
| 99 | .map(|first_day| { |
| 100 | choice( |
| 101 | app, |
| 102 | first_day_label(first_day, auto_first_day), |
| 103 | prefs.first_day == first_day, |
| 104 | RegionalPrefs { first_day, ..prefs }, |
| 105 | ) |
| 106 | }) |
| 107 | .collect(), |
| 108 | ), |
| 109 | ), |
| 110 | ] |
| 111 | } |
| 112 | |
| 113 | fn choice(app: &Entity<AppState>, label: String, checked: bool, prefs: RegionalPrefs) -> Choice { |
| 114 | let app = app.clone(); |
| 115 | Choice::new(label, checked, move |cx| { |
| 116 | app.update(cx, |state, cx| state.set_regional_prefs(prefs, cx)); |
| 117 | cx.refresh_windows(); |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | /// Each choice names its example time: "24-hour (14:30)". |
| 122 | fn clock_label(clock: ClockPreference, automatic: DateStyle) -> String { |
| 123 | let (key, style) = match clock { |
| 124 | ClockPreference::Auto => ("settings.appearance.clock.auto", automatic), |
| 125 | ClockPreference::Hours24 => ( |
| 126 | "settings.appearance.clock.hours_24", |
| 127 | automatic.with_twelve_hour(false), |
| 128 | ), |
| 129 | ClockPreference::Hours12 => ( |
| 130 | "settings.appearance.clock.hours_12", |
| 131 | automatic.with_twelve_hour(true), |
| 132 | ), |
| 133 | }; |
| 134 | let example = NaiveTime::from_hms_opt(14, 30, 0) |
| 135 | .unwrap_or_default() |
| 136 | .format(style.clock()) |
| 137 | .to_string(); |
| 138 | tr_with(key, &[("example", &example)]) |
| 139 | } |
| 140 | |
| 141 | fn first_day_label(first_day: Option<Weekday>, automatic: Weekday) -> String { |
| 142 | match first_day { |
| 143 | Some(day) => weekday_name(day, WeekdayWidth::Full), |
| 144 | None => tr_with( |
| 145 | "settings.appearance.first_day.auto", |
| 146 | &[("day", &weekday_name(automatic, WeekdayWidth::Full))], |
| 147 | ), |
| 148 | } |
| 149 | } |