Calendar, popover and date picker take the first weekday (L-301)

This commit is contained in:
Dylan M. Taylor
2026-11-22 00:39:08 -05:00
parent 644e6ff8c3
commit 26fa96b3a0
5 changed files with 89 additions and 14 deletions
+11 -1
View File
@@ -274,6 +274,8 @@ pub struct DateFormat {
hour_cycle: Option<crate::HourCycle>,
/// 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<Weekday>,
/// Formatters and names, built on first use and shared by clones.
cache: Arc<Cache>,
}
@@ -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);
+15 -3
View File
@@ -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<PickedDate>,
/// The day the calendar's weeks start on; the locale's when not set.
#[prop]
first_weekday: Option<Weekday>,
}
impl<'a> DatePicker<'a> {
/// A date picker named `label` over `value`, at every precision.
pub fn new(label: impl Into<Label>, value: Bind<'a, Option<PickedDate>>) -> Self {
DatePicker { common: Common::new(), value, precisions: &DatePrecision::ALL, min: None, max: None, today: None }.label(label)
DatePicker { common: Common::new(), value, precisions: &DatePrecision::ALL, min: None, max: None, today: None, first_weekday: None }.label(label)
}
fn offered(&self) -> &'a [DatePrecision] {
offered(self.precisions)
}
/// `dates` with the calendar's first weekday when this picker sets one.
fn dates(&self, dates: Arc<DateFormat>) -> Arc<DateFormat> {
match self.first_weekday {
Some(day) => Arc::new((*dates).clone().with_first_weekday(day)),
None => dates,
}
}
fn codec(&self, dates: Arc<DateFormat>, mixed: bool) -> DateCodec {
DateCodec::new(dates, self.offered()).bounded(self.min, self.max).mixed(mixed)
}
@@ -296,7 +307,7 @@ impl Component for DatePicker<'_> {
fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) {
crate::menu::text_menu(ui, cx, &TextOptions::new());
let popup = cx.popup("calendar");
let dates = cx.env.dates();
let dates = self.dates(cx.env.dates());
let words = cx.words::<PickerWords>();
let current = cx.value().copied().flatten();
// The page follows the value the hover preview began from: a day this picker proposes under the pointer is
@@ -315,6 +326,7 @@ impl Component for DatePicker<'_> {
min: self.min,
max: self.max,
today,
first_weekday: self.first_weekday,
dates: &dates,
words: &words,
name: &name,
+17 -6
View File
@@ -534,6 +534,9 @@ pub struct Calendar {
/// The day that carries the ring.
#[prop]
today: Option<PickedDate>,
/// The day the weeks start on; the locale's when not set.
#[prop]
first_weekday: Option<Weekday>,
picked: Option<PickedDate>,
hovered: Option<PickedDate>,
moved: Option<CalendarView>,
@@ -544,10 +547,18 @@ pub struct Calendar {
impl Calendar {
/// The page of `view` named `label`, with the cell of `value` selected.
pub fn new(label: impl Into<crate::runtime::env::Label>, view: CalendarView, value: Option<PickedDate>) -> Self {
Calendar { common: Common::new(), view, value, min: None, max: None, today: None, picked: None, hovered: None, moved: None, lines: [0.0; 3] }
Calendar { common: Common::new(), view, value, min: None, max: None, today: None, first_weekday: None, picked: None, hovered: None, moved: None, lines: [0.0; 3] }
.label(label)
}
/// The scope's dates, with this calendar's first weekday when it sets one.
fn dates(&self, env: &EnvView<'_>) -> Arc<DateFormat> {
match self.first_weekday {
Some(day) => Arc::new((*env.dates()).clone().with_first_weekday(day)),
None => env.dates(),
}
}
/// Whether any day of the cell lies within the bounds.
fn permitted(&self, cell: &Cell) -> bool {
self.min.is_none_or(|m| cell.span.1.day_key() >= m.first_day().day_key())
@@ -580,7 +591,7 @@ impl Calendar {
/// The widest title the page shows at the current type scale: across the months of the page's year on a Day page,
/// so the header keeps its width while the month changes.
fn title_width(&self, cx: &MeasureCx<'_, Self>) -> f32 {
let dates = cx.env.dates();
let dates = self.dates(&cx.env);
let numbers = Numbers(cx.env.format());
let width = |title: String| cx.layout(&TextRun::new(title).role(cx.style.title.role)).size().x;
match self.view.precision {
@@ -714,7 +725,7 @@ impl Component for Calendar {
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
self.measure_lines(cx);
let dates = cx.env.dates();
let dates = self.dates(&cx.env);
let numbers = Numbers(cx.env.format());
let page = Page::of(&self.view, &dates, &numbers, false);
let geometry = Geometry::of(rect, cx.style, cx.flow.is_rtl(), &self.view, page.columns);
@@ -738,7 +749,7 @@ impl Component for Calendar {
/// Arrows, Home and End move focus over the cells; a move past the grid, Page Up and Page Down change the page; a
/// click, Enter or Space on a cell picks it, or on a navigation cell opens it; the buttons and the title page.
fn update(&mut self, cx: &mut Cx<'_, Self>) {
let dates = cx.env.dates();
let dates = self.dates(&cx.env);
let format = cx.env.format();
let today = self.current_day(&cx.env);
let page = Page::of(&self.view, &dates, &Numbers(format), false);
@@ -787,7 +798,7 @@ impl Component for Calendar {
/// The grid's size, its cells with their places, and the title's button.
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
let dates = cx.env.dates();
let dates = self.dates(&cx.env);
let numbers = Numbers(cx.env.format());
let page = Page::of(&self.view, &dates, &numbers, true);
access.grid(GridPlace::grid(page.rows(), page.columns));
@@ -809,7 +820,7 @@ impl Component for Calendar {
let words = cx.words::<PickerWords>();
ui.face(PREVIOUS, IconButton::new(IconId::new(PREVIOUS_ICON)).label(words.date_previous.clone()));
ui.face(NEXT, IconButton::new(IconId::new(NEXT_ICON)).label(words.date_next.clone()));
let dates: Arc<DateFormat> = cx.env.dates();
let dates: Arc<DateFormat> = self.dates(&cx.env);
let today = self.current_day(&cx.env);
let numbers = Numbers(cx.env.format());
let page = Page::of(&self.view, &dates, &numbers, false);
+16 -3
View File
@@ -13,7 +13,7 @@ use crate::runtime::env::Label;
use crate::runtime::overlay::CloseReason;
use crate::style::{ComponentStyle, StyleCx, SurfaceKind};
use craft_behavior::{EditEvent, Metrics, Mixed, Sense};
use craft_i18n::DateFormat;
use craft_i18n::{DateFormat, Weekday};
use egui::{Response, Ui};
use std::sync::Arc;
@@ -36,6 +36,8 @@ pub(super) struct CalendarAt<'a> {
pub(super) min: Option<PickedDate>,
pub(super) max: Option<PickedDate>,
pub(super) today: Option<PickedDate>,
/// The first weekday the host sets, for the page's grid.
pub(super) first_weekday: Option<Weekday>,
pub(super) dates: &'a DateFormat,
pub(super) words: &'a PickerWords,
pub(super) name: &'a Label,
@@ -58,7 +60,7 @@ struct Followed {
/// The calendar: a precision switch when more than one is offered, the page, and Today and Clear.
pub(super) fn calendar_body(ui: &mut Ui, body: &mut BodyCx<'_>, at: CalendarAt<'_>) -> Chosen {
let CalendarAt { current, settled, precisions, min, max, today, dates, words, name } = at;
let CalendarAt { current, settled, precisions, min, max, today, first_weekday, dates, words, name } = at;
let Followed { view: kept, seen } = *body.memory::<Followed>();
let mut view = match kept {
Some(view) if seen == settled => view,
@@ -94,6 +96,9 @@ pub(super) fn calendar_body(ui: &mut Ui, body: &mut BodyCx<'_>, at: CalendarAt<'
if let Some(today) = today {
calendar = calendar.today(today);
}
if let Some(day) = first_weekday {
calendar = calendar.first_weekday(day);
}
let page = calendar.show(ui);
if let Some(next) = page.moved {
view = next;
@@ -151,6 +156,9 @@ pub struct CalendarPopover<'a> {
/// (`RootEnv::today`); without either neither shows.
#[prop]
today: Option<PickedDate>,
/// The day the calendar's weeks start on; the locale's when not set.
#[prop]
first_weekday: Option<Weekday>,
}
impl<'a> CalendarPopover<'a> {
@@ -170,6 +178,7 @@ impl<'a> CalendarPopover<'a> {
min: None,
max: None,
today: None,
first_weekday: None,
}
.label(label)
}
@@ -220,7 +229,10 @@ impl Container for CalendarPopover<'_> {
/// The calendar. Its output's value is `NoValue`, so a pick goes to the binding as a set and closes the popover.
fn header(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>) {
let dates = body.env.dates();
let dates = match self.first_weekday {
Some(day) => Arc::new((*body.env.dates()).clone().with_first_weekday(day)),
None => body.env.dates(),
};
let words = body.words::<PickerWords>();
let name = body.label();
let read = self.value.reborrow().read(body);
@@ -236,6 +248,7 @@ impl Container for CalendarPopover<'_> {
min: self.min,
max: self.max,
today,
first_weekday: self.first_weekday,
dates: &dates,
words: &words,
name: &name,
+30 -1
View File
@@ -1,7 +1,7 @@
//! Date values and the calendar maths: parsing at a precision, and moves across month and year edges.
use craft_behavior::NavMove;
use craft_i18n::DateFormat;
use craft_i18n::{DateFormat, Weekday};
use craft_ui::buttons::Button;
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::pickers::{CalendarPopover, CalendarView, DatePicker, DatePrecision, PickedDate};
@@ -112,3 +112,32 @@ fn the_calendar_popover_follows_a_value_set_while_it_is_open() {
fixture.idle(4);
follows(&mut fixture, &value, &titles, "the date picker's calendar");
}
/// A date format keeps the weeks starting where the app puts them, whatever the locale says.
#[craft_ui::testing::trial]
fn a_date_format_starts_its_weeks_where_the_app_says() {
use craft_i18n::Weekday;
let europe = DateFormat::from_tag("de-DE");
assert_eq!(europe.first_weekday(), Weekday::Monday, "the locale's own week");
let sunday = europe.with_first_weekday(Weekday::Sunday);
assert_eq!(sunday.first_weekday(), Weekday::Sunday, "the app's week");
let week = CalendarView::open(Some(PickedDate::day(YEAR, FEBRUARY, 1)), None, &[DatePrecision::Day], &sunday);
assert_eq!(craft_ui::pickers::month_grid(YEAR, FEBRUARY, sunday.first_weekday()).iter().position(Option::is_some), Some(0), "{week:?}: February 2026 starts a Sunday-first week");
}
/// A calendar page starts its weeks on the day it is given: February 2026 begins on a Sunday, so its 1st is in the first
/// column of a Sunday-first week and the last of a Monday-first one.
#[craft_ui::testing::trial]
fn a_calendar_page_starts_its_weeks_on_the_day_it_is_given() {
use craft_ui::pickers::Calendar;
let first_cell = |day: Weekday| {
let view = CalendarView::open(Some(PickedDate::day(YEAR, FEBRUARY, 1)), None, &[DatePrecision::Day], &DateFormat::from_tag("en-US").with_first_weekday(day));
let mut fixture = Fixture::new(move |ui| {
Calendar::new("Due date", view, None).first_weekday(day).show(ui);
});
fixture.settle();
fixture.texts().iter().find(|run| run.text == "1").expect("the 1st is painted").rect.center().x
};
let (sunday, monday) = (first_cell(Weekday::Sunday), first_cell(Weekday::Monday));
assert!(sunday < monday, "the 1st is in the first column of a Sunday-first week ({sunday}) and the last of a Monday-first one ({monday})");
}