mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 15:07:42 +00:00
Calendar: Sunday's weekday head is no longer blank (table asked for 2026-10-32), two-letter heads (NameLength::Short), Today centred and Clear only with a value (trials)
This commit is contained in:
+26
-7
@@ -250,6 +250,9 @@ pub enum NameLength {
|
||||
Narrow,
|
||||
/// `Sat`, `Oct`.
|
||||
Abbreviated,
|
||||
/// `Sa`: a weekday's two-letter name, calendar column heads where there is room for two letters. A month takes
|
||||
/// its abbreviation.
|
||||
Short,
|
||||
/// `Saturday`, `October`.
|
||||
Wide,
|
||||
}
|
||||
@@ -314,12 +317,12 @@ impl Formatter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Standalone month and weekday names, indexed by [`NameLength`] (narrow, abbreviated, wide)
|
||||
/// Standalone month and weekday names, indexed by [`NameLength`] (narrow, abbreviated, short, wide)
|
||||
/// and then by month (January first) or weekday (Monday first).
|
||||
#[derive(Debug)]
|
||||
struct Names {
|
||||
months: [[String; 12]; 3],
|
||||
weekdays: [[String; 7]; 3],
|
||||
months: [[String; 12]; 4],
|
||||
weekdays: [[String; 7]; 4],
|
||||
}
|
||||
|
||||
/// Which field comes first, second and third in a numeric date.
|
||||
@@ -509,12 +512,13 @@ impl DateFormat {
|
||||
let formatted = names.load_for_pattern(&provider, &pattern).ok()?.format(&at);
|
||||
Some(formatted.try_write_to_string().map_or_else(|(_, text)| text.into_owned(), |text| text.into_owned()))
|
||||
};
|
||||
let months = ["LLLLL", "LLL", "LLLL"].map(|pattern| {
|
||||
let months = ["LLLLL", "LLL", "LLL", "LLLL"].map(|pattern| {
|
||||
std::array::from_fn(|i| name(pattern, CalendarDate { year: 2026, month: i as u8 + 1, day: 1 }).unwrap_or_default())
|
||||
});
|
||||
// 2026-10-26 is a Monday.
|
||||
let weekdays = ["ccccc", "ccc", "cccc"].map(|pattern| {
|
||||
std::array::from_fn(|i| name(pattern, CalendarDate { year: 2026, month: 10, day: 26 + i as u8 }).unwrap_or_default())
|
||||
// 2026-10-26 is a Monday; the week runs on into November (October has 31 days).
|
||||
let monday = CalendarDate { year: 2026, month: 10, day: 26 };
|
||||
let weekdays = ["ccccc", "ccc", "cccccc", "cccc"].map(|pattern| {
|
||||
std::array::from_fn(|i| name(pattern, monday.add_days(i as i64)).unwrap_or_default())
|
||||
});
|
||||
Some(Names { months, weekdays })
|
||||
})
|
||||
@@ -777,4 +781,19 @@ mod tests {
|
||||
assert_eq!(jan31.add_months(-2), CalendarDate::new(2027, 11, 30).unwrap());
|
||||
assert_eq!(saturday.add_days(1), CalendarDate::new(2026, 11, 1).unwrap());
|
||||
}
|
||||
|
||||
// Sunday's name was blank (the table asked for 2026-10-32): every weekday has a name at every length, and the short
|
||||
// length is two letters in English.
|
||||
#[test]
|
||||
fn every_weekday_has_a_name_at_every_length() {
|
||||
let dates = DateFormat::from_tag("en-US");
|
||||
for day in Weekday::ALL {
|
||||
for length in [NameLength::Narrow, NameLength::Abbreviated, NameLength::Short, NameLength::Wide] {
|
||||
assert!(!dates.weekday_name(day, length).is_empty(), "{day:?} {length:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(dates.weekday_name(Weekday::Sunday, NameLength::Short), "Su");
|
||||
assert_eq!(dates.weekday_name(Weekday::Monday, NameLength::Short), "Mo");
|
||||
assert_eq!(dates.month_name(11, NameLength::Short), "Nov");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -835,7 +835,7 @@ impl Component for Calendar {
|
||||
if let Some(row) = geometry.weekdays {
|
||||
let first = dates.first_weekday() as usize;
|
||||
for column in 0..DAY_COLUMNS {
|
||||
let name = dates.weekday_name(Weekday::ALL[(first + column) % 7], NameLength::Narrow);
|
||||
let name = dates.weekday_name(Weekday::ALL[(first + column) % 7], NameLength::Short);
|
||||
let x = geometry.column(column);
|
||||
centred(Rect::from_min_max(egui::pos2(x.min.x, row.min.y), egui::pos2(x.max.x, row.max.y)), &name, &style.weekday, self.lines[1]);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ use super::super::words::PickerWords;
|
||||
use super::{Calendar, CalendarView, DatePrecision, PickedDate, offered};
|
||||
use crate::buttons::Link;
|
||||
use crate::choice::Segmented;
|
||||
use crate::layout_components::Row;
|
||||
use crate::layout::RowAlign;
|
||||
use crate::contract::{
|
||||
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, Entry, HeaderRegion, Mount, Overlay, POPUP_PAD, Props as _,
|
||||
Remember as _, Role, Show as _, Trigger, WordSetCx as _, clicked,
|
||||
Remember as _, Role, Show as _, ShowContainer as _, Trigger, WordSetCx as _, clicked,
|
||||
};
|
||||
use crate::popover::PopoverOutput;
|
||||
use crate::runtime::env::Label;
|
||||
@@ -104,7 +106,8 @@ pub(super) fn calendar_body(ui: &mut Ui, body: &mut BodyCx<'_>, at: CalendarAt<'
|
||||
view = next;
|
||||
}
|
||||
let mut pick = page.picked.map(Some);
|
||||
ui.horizontal(|ui| {
|
||||
// Today and Clear are centred under the page, as production's; Clear shows once there is a value to clear.
|
||||
Row::new().fill_width().align(RowAlign::Center).show(ui, |ui, _| {
|
||||
let at_precision = today.map(|today| match view.precision {
|
||||
DatePrecision::Day => today,
|
||||
DatePrecision::Month => PickedDate::month(today.year, today.month.unwrap_or(1)),
|
||||
@@ -116,7 +119,7 @@ pub(super) fn calendar_body(ui: &mut Ui, body: &mut BodyCx<'_>, at: CalendarAt<'
|
||||
pick = Some(Some(today));
|
||||
}
|
||||
}
|
||||
if clicked(&Link::new(words.date_clear.clone()).enabled(current.is_some()).show(ui)) {
|
||||
if current.is_some() && clicked(&Link::new(words.date_clear.clone()).show(ui)) {
|
||||
pick = Some(None);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -141,3 +141,26 @@ fn a_calendar_page_starts_its_weeks_on_the_day_it_is_given() {
|
||||
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})");
|
||||
}
|
||||
|
||||
/// Today is centred under the page, Clear is offered only while the field holds a date, and the weekday heads are the
|
||||
/// two-letter names starting with Sunday's.
|
||||
#[craft_ui::testing::trial]
|
||||
fn today_is_centred_and_clear_waits_for_a_value() {
|
||||
let mut picked: Option<PickedDate> = None;
|
||||
let mut open = true;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let opener = Button::new("Pick").show(ui);
|
||||
CalendarPopover::new("Due date", Bind::bound(&mut open), Bind::bound(&mut picked))
|
||||
.opened_by(&opener)
|
||||
.precisions(&[DatePrecision::Day])
|
||||
.first_weekday(Weekday::Sunday)
|
||||
.today(PickedDate::day(2026, 2, 10))
|
||||
.show(ui, |_, _| {});
|
||||
});
|
||||
fixture.room(egui::vec2(600.0, 500.0)).settle();
|
||||
let texts = fixture.texts();
|
||||
let at = |word: &str| texts.iter().find(|run| run.text == word).unwrap_or_else(|| panic!("{word} is painted")).rect.center().x;
|
||||
assert!(texts.iter().all(|run| run.text != "Clear"), "no value, no Clear");
|
||||
let middle = (at("Su") + at("Sa")) / 2.0;
|
||||
assert!((at("Today") - middle).abs() < 6.0, "Today is centred: {} against {middle}", at("Today"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user