From 26fa96b3a00604c73a601a518feaaa646f420b4e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 00:39:08 -0500 Subject: [PATCH] Calendar, popover and date picker take the first weekday (L-301) --- crates/i18n/src/date.rs | 12 ++++++- crates/ui-widgets/src/pickers/date.rs | 18 +++++++++-- .../ui-widgets/src/pickers/date/calendar.rs | 23 ++++++++++---- crates/ui-widgets/src/pickers/date/popover.rs | 19 ++++++++++-- crates/ui-widgets/tests/it/pickers/date.rs | 31 ++++++++++++++++++- 5 files changed, 89 insertions(+), 14 deletions(-) diff --git a/crates/i18n/src/date.rs b/crates/i18n/src/date.rs index d554d66a3..a96348b45 100644 --- a/crates/i18n/src/date.rs +++ b/crates/i18n/src/date.rs @@ -274,6 +274,8 @@ pub struct DateFormat { hour_cycle: Option, /// Write `0`–`9` whatever the locale's digits are. latin_digits: bool, + /// The day a week starts on when the app sets one apart from the locale. + week_start: Option, /// Formatters and names, built on first use and shared by clones. cache: Arc, } @@ -374,7 +376,7 @@ impl PartsWrite for Spliced { impl DateFormat { /// The format for `region`'s time locale and clock. pub fn new(region: &Region) -> DateFormat { - Self { locale: region.time.clone(), hour_cycle: region.hour_cycle, latin_digits: false, cache: Arc::default() } + Self { locale: region.time.clone(), hour_cycle: region.hour_cycle, latin_digits: false, week_start: None, cache: Arc::default() } } /// This format writing `0`–`9` for [`Digits::Latin`], else the locale's own digits. Formatters @@ -383,6 +385,11 @@ impl DateFormat { DateFormat { latin_digits: digits == Digits::Latin, cache: Arc::default(), ..self } } + /// This format with weeks starting on `day` in place of the region's. + pub fn with_first_weekday(self, day: Weekday) -> DateFormat { + DateFormat { week_start: Some(day), ..self } + } + /// The format for a locale tag (`de-CH`), with the locale's clock. pub fn from_tag(tag: &str) -> DateFormat { Self::new(&Region::from_tag(tag)) @@ -559,6 +566,9 @@ impl DateFormat { /// The day a week starts on in the region: Sunday in the United States, Monday in most of /// Europe, Saturday in parts of the Middle East. pub fn first_weekday(&self) -> Weekday { + if let Some(day) = self.week_start { + return day; + } let first = self.cache.first_weekday.get_or_init(|| { let provider = data::dates_provider()?; let locale = self.icu_locale(false); diff --git a/crates/ui-widgets/src/pickers/date.rs b/crates/ui-widgets/src/pickers/date.rs index b86d25f48..3f17385e4 100644 --- a/crates/ui-widgets/src/pickers/date.rs +++ b/crates/ui-widgets/src/pickers/date.rs @@ -25,7 +25,7 @@ use crate::primitives::{IconSpec, TextKey, TextOptions, TextRun}; use crate::runtime::env::Label; use crate::text::{Adornment, FieldStyle, place, reserve}; use craft_behavior::{CancelReason, GesturePhase, HitLevel, HitPriority, IconId, Mixed, PartKey, Sense}; -use craft_i18n::{CalendarDate, DateFormat}; +use craft_i18n::{CalendarDate, DateFormat, Weekday}; use egui::{Rect, Vec2}; use popover::{CalendarAt, calendar_body}; use std::sync::Arc; @@ -213,18 +213,29 @@ pub struct DatePicker<'a> { /// (`RootEnv::today`); without either neither shows. #[prop] today: Option, + /// The day the calendar's weeks start on; the locale's when not set. + #[prop] + first_weekday: Option, } impl<'a> DatePicker<'a> { /// A date picker named `label` over `value`, at every precision. pub fn new(label: impl Into