1// Dates and times (decisions/F11.md, F11d): Calendar,2// DatePicker and TimePicker over std::time's Date and TimeOfDay, written3// in the Ui's locale (F12e): "15 October 2026", "October 2026", "Mo Tu We"4// in en-GB, "15 octobre 2026", "lu ma me" in fr.56import std::string;7import std::vector;8import std::option;9import std::math;10import std::time;11import std::locale;12import pkg::alloy::window;13import pkg::alloy::gpu;14import pkg::alloy::draw;15import pkg::alloy::text as text;1617import pkg::alloy::ui;18import pkg::alloy::ui::store;19import pkg::alloy::ui::tree;20import pkg::alloy::ui::layout;21import pkg::alloy::ui::painting;22import pkg::alloy::ui::events;23import pkg::alloy::ui::widgets;24import pkg::alloy::ui::fields;25import pkg::alloy::ui::scrolling;26import pkg::alloy::ui::running;27import pkg::alloy::ui::timers;28import pkg::alloy::ui::layers;29import pkg::alloy::ui::motion;30import pkg::alloy::ui::transitions;31import pkg::alloy::ui::languages;32import pkg::alloy::ui::extensions;33import pkg::alloy::ui::views;34import ui_app;35import pkg::alloy::ui_controls;36import pkg::alloy::ui_controls::choices;3738// The day calendars take as today when set, and the app's locale, in a39// signal of the Ui's own made with it: what dates keep, an extension40// installed as either is first asked for.41pub type Dates : Extension = struct {42 pub today: Option<Date> = ::None,43 pub locale: Signal<Option<Locale>>,44};4546pub fn dates(self ui: &var Ui) -> &var Dates {47 if (ui.extension<Dates>() is ::None) {48 const saved = ui.owner;49 ui.owner = 0;50 const system: Option<Locale> = ::None;51 const locale = ui.signal(system);52 ui.owner = saved;53 ui.install(new Dates { .locale = locale });54 }55 if (ui.extension_var<Dates>() is ::Some |&var d|) {56 return &var d;57 }58 panic "pkg::alloy::ui: no dates";59}6061// What a screen reader says of a day: "Thursday, 15 October 2026".62fn spoken_date(locale: &Locale, d: &Date) -> String {63 var out = String::empty();64 locale.format_date(&d, &DateFormat { .weekday = Width::Wide }, &var out);65 return move out;66}6768// The month and year: "October 2026".69fn month_title(locale: &Locale, d: &Date) -> String {70 var out = String::empty();71 locale.format_date(&d, &DateFormat { .day = false }, &var out);72 return move out;73}7475// The day as a DatePicker shows it unless told, the locale's medium date:76// "15 Oct 2026", "Oct 15, 2026", "15 oct. 2026".77fn medium_date(locale: &Locale, d: &Date) -> String {78 var out = String::empty();79 locale.format_date(&d, &DateFormat { .month = Width::Short }, &var out);80 return move out;81}8283/* --- Calendar --- */8485const PADDING: f32 = 8.0;86const CELL: f32 = 36.0; // a day's square87const DOT: f32 = 32.0; // the circle a day is filled or ringed in88const HEADER: f32 = 36.0; // the month's title and its buttons89const ARROW: f32 = 14.0; // the buttons' chevrons90const NAMES: f32 = 24.0; // the weekdays' row91const WEEKS: u64 = 6;9293// What the pointer is over.94pub type Spot = enum {95 Day: Date,96 Previous,97 Next,98};99100// A calendar's own state: the day the keys move and the first of the101// month shown, which follows it; today, the locale, its direction and the102// week's first day as last laid out; the title, the weekdays' names and the days'103// numbers laid out in them; what the pointer is over.104pub type CalendarState = struct {105 pub focus: Option<Date> = ::None,106 pub month: Option<Date> = ::None,107 pub today: Date = Date { .year = 1970, .month = Month::January, .day = 1 },108 pub locale: Option<Locale> = ::None,109 pub first: Weekday = Weekday::Monday,110 // it reads right to left: the week from the right, the111 // month before on the right112 pub mirrored: bool = false,113 pub fonts: u32 = 0, // the Ui's fonts_changed114 pub title: Option<*text::Layout> = ::None,115 pub names: Vector<text::Layout> = Vector<text::Layout>::empty(), // from `first`116 pub numbers: Vector<text::Layout> = Vector<text::Layout>::empty(), // 1 to 31117 pub hovered: Option<Spot> = ::None,118};119120// Whether the day may be chosen: within the range and available.121fn usable(self c: &Calendar, d: &Date) -> bool => c.enabled && c.in_range(&d) && c.available(&d);122123fn in_range(self c: &Calendar, d: &Date) -> bool {124 if (c.earliest is ::Some |&e| && d.before(&e)) {125 return false;126 }127 if (c.latest is ::Some |&l| && d.after(&l)) {128 return false;129 }130 return true;131}132133// The day kept within the range.134fn clamped(self c: &Calendar, d: Date) -> Date {135 if (c.earliest is ::Some |e| && d.before(&e)) {136 return e;137 }138 if (c.latest is ::Some |l| && d.after(&l)) {139 return l;140 }141 return d;142}143144fn first_of(d: &Date) -> Date => Date { .year = d.year, .month = d.month, .day = 1 };145146// The day the calendar starts on: the chosen one, else today, within the147// range.148fn start(self c: &Calendar) -> Date {149 if (c.date is ::Some |d|) {150 return d;151 }152 return c.clamped(c.state.today);153}154155// The first of the month shown.156pub fn shown_month(self c: &Calendar) -> Date {157 if (c.state.month is ::Some |m|) {158 return m;159 }160 return first_of(&c.start());161}162163// Where the month's first day goes in its first week.164fn lead(self c: &Calendar, month: &Date) -> u64 {165 const from = c.state.first to u8 to i64;166 return ((month.weekday() to u8 to i64 - from + 7) % 7) to u64;167}168169// The day's row and column in the month shown.170fn cell_of(self c: &Calendar, month: &Date, day: u8) -> [u32 : 2] {171 const k = c.lead(&month) + day to u64 - 1;172 return [(k / 7) to u32, (k % 7) to u32];173}174175fn grid_top() -> f32 => PADDING + HEADER + NAMES;176177// Where a column of the week shows: from the right when mirrored.178fn shown_column(179 self c: &Calendar,180 column: u32181) -> u32 => if (c.state.mirrored) 6 - column else column;182183fn cell_area(self c: &Calendar, at: [u32 : 2]) -> Rect {184 const x = PADDING + c.shown_column(at[1]) to f32 * CELL;185 return Rect::at(x, grid_top() + at[0] to f32 * CELL, CELL, CELL);186}187188// The month before's button on the leading side, the month after's on189// the trailing side.190fn button_area(self c: &Calendar, spot: Spot) -> Rect {191 const leading = (spot is ::Previous) != c.state.mirrored;192 const x = if (leading) PADDING + 2.0 else PADDING + 7.0 * CELL - HEADER + 2.0;193 return Rect::at(x, PADDING + 2.0, HEADER - 4.0, HEADER - 4.0);194}195196// Whether the button goes to a month with a day in the range.197fn can_go(self c: &Calendar, months: i64) -> bool {198 const month = c.shown_month().add_months(months);199 if (months < 0 && c.earliest is ::Some |&e|) {200 return !month.add_months(1).add_days(-1).before(&e);201 }202 if (months > 0 && c.latest is ::Some |&l|) {203 return !month.after(&l);204 }205 return true;206}207208fn spot_at(self c: &Calendar, at: Point) -> Option<Spot> {209 if (holds(&c.button_area(Spot::Previous), at)) {210 return if (c.can_go(-1)) ::Some(Spot::Previous) else ::None;211 }212 if (holds(&c.button_area(Spot::Next), at)) {213 return if (c.can_go(1)) ::Some(Spot::Next) else ::None;214 }215 const x = at.x - PADDING;216 const y = at.y - grid_top();217 if (x < 0.0 || y < 0.0 || x >= 7.0 * CELL || y >= WEEKS to f32 * CELL) {218 return ::None;219 }220 const month = c.shown_month();221 const column = c.shown_column(floor(x / CELL) to u32);222 const k = floor(y / CELL) to i64 * 7 + column to i64 - c.lead(&month) to i64;223 if (k < 0 || k >= days_in_month(month.year, month.month) to i64) {224 return ::None;225 }226 return ::Some(Spot::Day(month.add_days(k)));227}228229pub fn layout(self c: &var Calendar, arrange: &var Arrange, limits: Limits) -> Extent {230 if (arrange.ui.marks.previous is ::None) {231 if (arrange.ui.scene is ::Some |&var scene|) {232 arrange.ui.marks.previous = ::Some(previous_mark(233 &var scene.canvas,234 &var scene.device()235 ));236 arrange.ui.marks.next = ::Some(next_mark(&var scene.canvas, &var scene.device()));237 }238 }239 const locale = arrange.ui.kept_locale();240 const first = if (c.first_weekday is ::Some |day|) day else locale.first_weekday();241 const state = &var c.state;242 state.today = arrange.ui.today();243 const size = arrange.theme().text_size;244 const fonts = arrange.ui.fonts_changed;245 const relaid = state.locale != ::Some(locale) || state.fonts != fonts;246 if (relaid || state.numbers.length() == 0) {247 state.numbers.clear();248 for ([1..32 to u64]) |n| {249 var digits = String::empty();250 locale.format_number(n to f64, 0, &var digits);251 state.numbers.push(text_layout(&var arrange, &digits.view(), size, REGULAR, UNBOUNDED));252 }253 }254 if (relaid || state.first != first || state.names.length() == 0) {255 // the short names, else the narrow ones where some are wider than256 // a day (Arabic's)257 const widths: [Width : 2] = [Width::Short, Width::Narrow];258 for (widths) |width| {259 state.names.clear();260 var fits = true;261 for ([..7 to i64]) |k| {262 var name = text_layout(263 &var arrange,264 &locale.weekday_name(first.add(k), width),265 size * 0.85,266 REGULAR,267 UNBOUNDED268 );269 fits = fits && name.width <= CELL - 4.0;270 state.names.push(move name);271 }272 if (fits) {273 break;274 }275 }276 }277 state.locale = ::Some(locale);278 state.mirrored = arrange.direction() is ::RightToLeft;279 state.first = first;280 state.fonts = fonts;281 const month = c.shown_month();282 var title = text_layout(283 &var arrange,284 &month_title(&locale, &month).view(),285 size,286 SEMIBOLD,287 UNBOUNDED288 );289 c.state.title = ::Some(new title);290 return limits.constrain(Extent {291 .width = 7.0 * CELL + 2.0 * PADDING,292 .height = grid_top() + WEEKS to f32 * CELL + PADDING293 });294}295296pub fn paint(self c: &Calendar, look: &Look, paint: &var Paint) {297 const theme = &look.theme;298 const state = &c.state;299 const fade = |&c| (color: Color, usable: bool) -> Color {300 var faded = color;301 if (!c.enabled || !usable) {302 faded.a = faded.a * 0.45;303 }304 return faded;305 };306 // the title between its buttons307 if (state.title is ::Some |&title|) {308 paint.text(309 &title,310 Point {311 .x = PADDING + (7.0 * CELL - title.width) / 2.0,312 .y = PADDING + (HEADER - title.height) / 2.0313 },314 fade(theme.text, true)315 );316 }317 const arrows: [Spot : 2] = [Spot::Previous, Spot::Next];318 for (arrows) |spot| {319 const area = c.button_area(spot);320 const usable = c.can_go(if (spot is ::Previous) -1 else 1);321 if (usable && c.enabled && look.hovered && state.hovered == ::Some(spot)) {322 paint.rounded_rect(323 area,324 Radii::all(theme.radius),325 Fill::Color(shade(theme.text, theme.hover, true))326 );327 }328 // each chevron points away from the month shown329 const mark = if (330 c.button_area(spot).x < PADDING + CELL331 ) look.marks.previous else look.marks.next;332 if (mark is ::Some |m|) {333 paint.mask(334 Rect::at(335 area.x + (area.width - ARROW) / 2.0,336 area.y + (area.height - ARROW) / 2.0,337 ARROW,338 ARROW339 ),340 m,341 fade(theme.muted, usable)342 );343 }344 }345 // the weekdays' names346 for (state.names.view(), [..state.names.length()]) |&name, k| {347 paint.text(348 &name,349 Point {350 .x = PADDING + c.shown_column(k to u32) to f32 * CELL + (CELL - name.width) / 2.0,351 .y = PADDING + HEADER + (NAMES - name.height) / 2.0352 },353 fade(theme.muted, true)354 );355 }356 const month = c.shown_month();357 for ([1..days_in_month(month.year, month.month) to u64 + 1]) |n| {358 const day = n to u8;359 const d = Date { .year = month.year, .month = month.month, .day = day };360 const area = c.cell_area(c.cell_of(&month, day));361 const circle = Rect::at(362 area.x + (CELL - DOT) / 2.0,363 area.y + (CELL - DOT) / 2.0,364 DOT,365 DOT366 );367 const in_range = c.in_range(&d);368 const usable = c.usable(&d);369 const chosen = c.date == ::Some(d);370 var ink = fade(theme.text, in_range);371 if (chosen) {372 paint.rounded_rect(373 circle,374 Radii::all(DOT / 2.0),375 Fill::Color(fade(theme.primary, true))376 );377 ink = theme.on_primary;378 } else if (usable && look.hovered && state.hovered == ::Some(Spot::Day(d))) {379 paint.rounded_rect(380 circle,381 Radii::all(DOT / 2.0),382 Fill::Color(shade(theme.text, theme.hover, true))383 );384 }385 if (d == state.today && !chosen) {386 paint.border(circle, Radii::all(DOT / 2.0), 1.5, fade(theme.primary, in_range));387 }388 if (look.focused && look.focus_ring && state.focus == ::Some(d)) {389 // a rounded square, unlike today's ring390 focus_ring(&look, &var paint, area.inflated(-4.0), look.theme.radius);391 }392 if (state.numbers.try_get(n - 1) is ::Some |&number|) {393 const origin = Point {394 .x = area.x + (CELL - number.width) / 2.0,395 .y = area.y + (CELL - number.height) / 2.0396 };397 if (in_range && !usable && c.enabled) {398 // refused by `available`: muted and struck through399 ink = fade(theme.muted, true);400 paint.rect(401 Rect::at(402 origin.x - 2.0,403 area.y + CELL / 2.0 - 0.5,404 number.width + 4.0,405 1.5406 ),407 Fill::Color(ink)408 );409 }410 paint.text(&number, origin, ink);411 }412 }413}414415pub fn event(self c: &var Calendar, ui: &var Ui, input: &Input) -> bool {416 if (!c.enabled) {417 return claims(&input);418 }419 match (input) {420 ::Hover |p| {421 const under = c.spot_at(p);422 if (under != c.state.hovered) {423 c.state.hovered = under;424 ui.refresh(Change::Paint);425 }426 }427 ::Click |p| {428 match (c.spot_at(p)) {429 ::Some |spot| {430 match (spot) {431 ::Previous {432 c.turn(&var ui, -1);433 }434 ::Next {435 c.turn(&var ui, 1);436 }437 ::Day |d| {438 if (c.usable(&d)) {439 c.choose(&var ui, d);440 }441 }442 }443 }444 ::None {}445 }446 }447 ::Focus |focused| {448 if (focused) {449 // the chosen day or today when shown, else the month's first450 const month = c.shown_month();451 const shown = |&month| (d: &Option<Date>) -> bool {452 if (d is ::Some |&day|) {453 return first_of(&day) == month;454 }455 return false;456 };457 if (!shown(&c.state.focus)) {458 c.state.focus = if (shown(&c.date)) c.date else if (459 shown(&::Some(c.state.today))460 ) ::Some(c.state.today) else ::Some(c.clamped(month));461 }462 }463 ui.refresh(Change::Paint);464 return false;465 }466 ::Key |&k| {467 const from = if (c.state.focus is ::Some |f|) f else c.start();468 const years = k.modifiers.shift;469 const back = (from.weekday() to u8 to i64 - c.state.first to u8 to i64 + 7) % 7;470 // Left and Right go the way the week shows471 const ahead: i64 = if (c.state.mirrored) -1 else 1;472 const next: Option<Date> = match (k.key) {473 ::Left ::Some(from.add_days(-ahead));474 ::Right ::Some(from.add_days(ahead));475 ::Up ::Some(from.add_days(-7));476 ::Down ::Some(from.add_days(7));477 ::PageUp ::Some(from.add_months(if (years) -12 else -1));478 ::PageDown ::Some(from.add_months(if (years) 12 else 1));479 ::Home ::Some(from.add_days(-back));480 ::End ::Some(from.add_days(6 - back));481 else ::None;482 };483 if (next is ::Some |d|) {484 if (k.pressed) {485 c.move_focus(&var ui, d);486 }487 return true;488 }489 if (k.key is ::Enter || is_space(&k)) {490 if (k.pressed && c.state.focus is ::Some |d| && c.usable(&d)) {491 c.choose(&var ui, d);492 }493 return true;494 }495 return false;496 }497 ::Part |&request| {498 const month = c.shown_month();499 const k = request.part to i64;500 if (k < days_in_month(month.year, month.month) to i64) {501 const d = month.add_days(k);502 match (request.action) {503 ::Click, ::Select {504 if (c.usable(&d)) {505 c.choose(&var ui, d);506 }507 }508 ::Focus {509 c.state.focus = ::Some(d);510 ui.refresh(Change::Paint);511 }512 else {}513 }514 }515 }516 else {517 return claims(&input);518 }519 }520 return true;521}522523// Moves the focus to the day, kept within the range; the month follows,524// announced when it changes.525fn move_focus(self c: &var Calendar, ui: &var Ui, d: Date) {526 const day = c.clamped(d);527 c.state.focus = ::Some(day);528 c.show_month(&var ui, first_of(&day));529 ui.refresh(Change::Paint);530}531532fn show_month(self c: &var Calendar, ui: &var Ui, month: Date) {533 if (c.shown_month() != month) {534 c.state.month = ::Some(month);535 ui.announce(month_title(&ui.kept_locale(), &month), false);536 ui.refresh(Change::Layout);537 }538}539540// Shows the month before or after, the focus moving with it.541fn turn(self c: &var Calendar, ui: &var Ui, months: i64) {542 const month = c.shown_month().add_months(months);543 if (c.state.focus is ::Some |f|) {544 c.state.focus = ::Some(c.clamped(f.add_months(months)));545 }546 c.show_month(&var ui, month);547}548549// Gives the program the day, when not chosen already; a DatePicker's550// calendar closes first.551fn choose(self c: &var Calendar, ui: &var Ui, d: Date) {552 c.state.focus = ::Some(d);553 ui.refresh(Change::Paint);554 const changed = c.date != ::Some(d);555 if (c.popup) {556 const told = copy c.on_change;557 close_own(&var ui);558 if (changed) {559 told(&var ui, d);560 }561 } else if (changed) {562 c.on_change(&var ui, d);563 }564}565566pub fn renew(self c: &var Calendar, next: Calendar) -> Change {567 var change = Change::Paint; // `available` may say otherwise of any day568 if (c.date != next.date && next.date is ::Some |d|) {569 // the month shows a day the program chose570 if (first_of(&d) != c.shown_month()) {571 c.state.month = ::Some(first_of(&d));572 change = Change::Layout;573 }574 if (c.state.focus is ::Some) {575 c.state.focus = ::Some(d);576 }577 }578 if (c.first_weekday != next.first_weekday || c.sizing != next.sizing) {579 change = Change::Layout;580 }581 c.date = next.date;582 c.earliest = next.earliest;583 c.latest = next.latest;584 c.available = move next.available;585 c.first_weekday = next.first_weekday;586 c.accessible = move next.accessible;587 c.on_change = move next.on_change;588 c.enabled = next.enabled;589 c.sizing = next.sizing;590 if (c.state.focus is ::Some |f|) {591 c.state.focus = ::Some(c.clamped(f));592 }593 return change;594}595596// The locale as last laid out.597fn locale(self c: &Calendar) -> Locale {598 if (c.state.locale is ::Some |locale|) {599 return locale;600 }601 return Locale::of(&"en");602}603604pub fn sizing(self c: &Calendar) -> Sizing => c.sizing;605pub fn interactive(self c: &Calendar) -> bool => true;606pub fn focusable(self c: &Calendar) -> bool => c.enabled;607pub fn wants_hover(self c: &Calendar) -> bool => true;608pub fn cursor(self c: &Calendar) -> Cursor => if (c.enabled) Cursor::Hand else Cursor::Arrow;609610pub fn semantics(self c: &Calendar) -> Semantics => Semantics {611 .role = Role::Grid,612 .label = copy c.accessible,613 .description = month_title(&c.locale(), &c.shown_month()),614 .disabled = !c.enabled,615 .focusable = c.enabled616};617618// The month's days, each a cell of the grid.619pub fn parts(self c: &Calendar) -> Vector<Part> {620 const month = c.shown_month();621 const count = days_in_month(month.year, month.month);622 var parts = Vector<Part>::with_capacity(count to u64);623 for ([1..count to u64 + 1]) |n| {624 const d = Date { .year = month.year, .month = month.month, .day = n to u8 };625 const at = c.cell_of(&month, n to u8);626 const usable = c.usable(&d);627 parts.push(Part {628 .semantics = Semantics {629 .role = Role::GridCell,630 .label = spoken_date(&c.locale(), &d),631 .selected = ::Some(c.date == ::Some(d)),632 .disabled = !usable,633 .focusable = usable,634 .current = d == c.state.today,635 .cell = ::Some(at)636 },637 .bounds = c.cell_area(at),638 .focused = c.state.focus == ::Some(d)639 });640 }641 return move parts;642}643644/* --- DatePicker --- */645646const FIELD_PADDING: Padding = Padding { .start = 10.0, .top = 7.0, .end = 10.0, .bottom = 7.0 };647const CHEVRON: f32 = 12.0;648const CHEVRON_GAP: f32 = 10.0;649650pub fn layout(self p: &var DatePicker, arrange: &var Arrange, limits: Limits) -> Extent {651 if (arrange.ui.marks.chevron is ::None) {652 var made = Option<Picture>::None;653 if (arrange.ui.scene is ::Some |&var scene|) {654 made = ::Some(chevron_mark(&var scene.canvas, &var scene.device()));655 }656 arrange.ui.marks.chevron = made;657 }658 const size = arrange.theme().text_size;659 const locale = arrange.ui.kept_locale();660 p.locale = ::Some(locale);661 var shown = if (p.date is ::Some |&d|) p.shown(&locale, &d) else copy p.placeholder;662 var laid = text_layout(&var arrange, &shown.view(), size, REGULAR, UNBOUNDED);663 // as wide as the longest month's day would be, and the placeholder664 var longest = String::empty();665 for ([1..13 to u8]) |m| {666 if (Month::from(m) is ::Some |month|) {667 var day = p.shown(&locale, &Date::of(arrange.ui.today().year, month, 28));668 if (day.length() > longest.length()) {669 longest = move day;670 }671 }672 }673 var widest = max(674 laid.width,675 text_layout(&var arrange, &longest.view(), size, REGULAR, UNBOUNDED).width676 );677 widest = max(678 widest,679 text_layout(&var arrange, &p.placeholder.view(), size, REGULAR, UNBOUNDED).width680 );681 const line = if (laid.lines.try_get(0) is ::Some |&first|) first.height else size * 1.25;682 p.laid = ::Some(new laid);683 p.widest = widest;684 return limits.constrain(Extent {685 .width = FIELD_PADDING.start + widest + CHEVRON_GAP + CHEVRON + FIELD_PADDING.end,686 .height = FIELD_PADDING.top + line + FIELD_PADDING.bottom687 });688}689690// The day as the field writes it: by `format`, else the locale's.691fn shown(self p: &DatePicker, locale: &Locale, d: &Date) -> String {692 if (p.format is ::Some |&format|) {693 return format(&d);694 }695 return medium_date(&locale, &d);696}697698pub fn paint(self p: &DatePicker, look: &Look, paint: &var Paint) {699 const theme = &look.theme;700 const area = Rect::sized(look.size.width, look.size.height);701 var surface = theme.surface;702 if (!p.enabled) {703 surface.a = 0.5;704 }705 paint.rounded_rect(area, Radii::all(theme.radius), Fill::Color(surface));706 if (p.error.length() > 0) {707 paint.border(708 area,709 Radii::all(theme.radius),710 if (look.focus_ring) 2.0 else 1.5,711 theme.danger712 );713 } else if (look.focus_ring) {714 paint.border(area, Radii::all(theme.radius), 2.0, theme.focus);715 } else {716 paint.border(717 area,718 Radii::all(theme.radius),719 1.0,720 if (p.open is ::Some) theme.primary else if (721 look.hovered && p.enabled722 ) theme.muted else theme.border723 );724 }725 var ink = if (p.date is ::Some) theme.text else theme.muted;726 var arrow = theme.muted;727 if (!p.enabled) {728 ink.a = 0.5;729 arrow.a = 0.5;730 }731 if (p.laid is ::Some |&laid|) {732 paint.text(733 &laid,734 Point {735 .x = look.from_start(FIELD_PADDING.start, laid.width),736 .y = (look.size.height - laid.height) / 2.0737 },738 ink739 );740 }741 // the chevron at the end, pointing down whichever way the field reads742 if (look.marks.chevron is ::Some |mark|) {743 paint.mask(744 Rect::at(745 look.from_start(look.size.width - FIELD_PADDING.end - CHEVRON, CHEVRON),746 (look.size.height - CHEVRON) / 2.0,747 CHEVRON,748 CHEVRON749 ),750 mark,751 arrow752 );753 }754}755756pub fn event(self p: &var DatePicker, ui: &var Ui, input: &Input) -> bool {757 if (!p.enabled) {758 return claims(&input);759 }760 match (input) {761 ::Click {762 p.open_calendar(&var ui, false);763 }764 ::Key |&k| {765 const opens = match (k.key) {766 ::Enter, ::Down true;767 else is_space(&k);768 };769 if (!opens) {770 return false;771 }772 if (k.pressed) {773 p.open_calendar(&var ui, true);774 }775 }776 ::Action |&a| {777 match (a) {778 ::Expand {779 p.open_calendar(&var ui, true);780 }781 ::Collapse {782 if (p.open is ::Some |layer|) {783 ui.close(layer);784 }785 }786 else {}787 }788 }789 else {790 return claims(&input);791 }792 }793 return true;794}795796// Opens a calendar of the picker's options below it, which takes the797// keyboard.798fn open_calendar(self p: &var DatePicker, ui: &var Ui, keyboard: bool) {799 if (p.open is ::Some) {800 return;801 }802 const picker = ui.source();803 var calendar = Calendar {804 .date = p.date,805 .earliest = p.earliest,806 .latest = p.latest,807 .available = copy p.available,808 .first_weekday = p.first_weekday,809 .accessible = copy p.accessible,810 .on_change = copy p.on_change,811 .popup = true812 };813 if (keyboard) {814 ui.focus_ring = true;815 }816 var overlay = Overlay::popup(picker);817 overlay.on_close = |picker| (ui: &var Ui) {818 if (819 ui.exists(picker)820 && ui.node_var(picker.index).widget is ::Some |&var widget|821 && widget is DatePicker |&var p|822 ) {823 p.open = ::None;824 ui.apply(picker.index, Change::Paint);825 }826 };827 const clearable = p.clearable;828 const chosen = p.date is ::Some;829 const on_clear = copy p.on_clear;830 p.open = ::Some(ui.open(move overlay, |move calendar, clearable, chosen, copy on_clear| (831 ui: &var Ui,832 layer: Layer833 ) -> Element {834 const shown = ui.add(copy calendar);835 if (!clearable) {836 return shown;837 }838 return ui.add(Column {839 .align = Alignment::Stretch,840 .children = &[841 shown,842 ui.add(Row {843 .justify = Justify::End,844 .padding = Padding { .start = 8.0, .top = 0.0, .end = 8.0, .bottom = 8.0 },845 .children = &[ui.add(Button {846 .label = String::from(&"Clear"),847 .variant = ButtonVariant::Plain,848 .enabled = chosen,849 .on_press = |copy on_clear, layer| (ui: &var Ui) {850 ui.close(layer);851 on_clear(&var ui);852 },853 })],854 }),855 ],856 });857 }));858 ui.refresh(Change::Paint);859}860861pub fn take_closing(self p: &var DatePicker) -> Option<Layer> {862 const closing = p.closing;863 p.closing = ::None;864 return closing;865}866867pub fn renew(self p: &var DatePicker, next: DatePicker) -> Change {868 const reopen = p.date != next.date || p.earliest != next.earliest || p.latest != next.latest869 || p.first_weekday != next.first_weekday870 || p.enabled != next.enabled;871 // the calendar open on other options closes872 if (reopen && p.open is ::Some |layer|) {873 p.closing = ::Some(layer);874 p.open = ::None;875 }876 p.date = next.date;877 p.earliest = next.earliest;878 p.latest = next.latest;879 p.available = move next.available;880 p.first_weekday = next.first_weekday;881 p.placeholder = move next.placeholder;882 p.accessible = move next.accessible;883 p.format = move next.format;884 p.on_change = move next.on_change;885 p.clearable = next.clearable;886 p.on_clear = move next.on_clear;887 p.enabled = next.enabled;888 p.error = move next.error;889 p.required = next.required;890 p.sizing = next.sizing;891 // what `format` writes may have changed with any binding's run892 return Change::Layout;893}894895pub fn sizing(self p: &DatePicker) -> Sizing => p.sizing;896pub fn interactive(self p: &DatePicker) -> bool => true;897pub fn focusable(self p: &DatePicker) -> bool => p.enabled;898pub fn cursor(self p: &DatePicker) -> Cursor => if (p.enabled) Cursor::Hand else Cursor::Arrow;899pub fn clips(self p: &DatePicker) -> bool => true;900901pub fn semantics(self p: &DatePicker) -> Semantics {902 var value = String::empty();903 if (p.date is ::Some |&d|) {904 value = p.shown(&if (p.locale is ::Some |l|) l else Locale::of(&"en"), &d);905 }906 return Semantics {907 .role = Role::Button,908 .label = copy p.accessible,909 .value = move value,910 .disabled = !p.enabled,911 .focusable = p.enabled,912 .expanded = ::Some(p.open is ::Some),913 .invalid = p.error.length() > 0,914 .required = p.required,915 .description = copy p.error916 };917}918919/* --- TimePicker --- */920921// The time on the locale's clock: "14:30", "2:30 PM".922pub fn time_label(locale: &Locale, time: &TimeOfDay) -> String {923 var out = String::empty();924 const minutes = time.hour to i64 * 60 + time.minute to i64;925 locale.format_moment(926 minutes * 60000000000,927 &Zone::utc(),928 &DateFormat { .day = false, .month = Width::None, .year = false, .time = true },929 &var out930 );931 return move out;932}933934// The times from earliest to latest by the step, as choices.935fn times(t: &TimePicker, locale: &Locale) -> Vector<Choice<TimeOfDay>> {936 var out = Vector<Choice<TimeOfDay>>::empty();937 const step = max(t.step_minutes, 1);938 var minutes = t.earliest.hour to u32 * 60 + t.earliest.minute to u32;939 const last = t.latest.hour to u32 * 60 + t.latest.minute to u32;940 while (minutes <= last && minutes < 24 * 60) {941 const time = TimeOfDay { .hour = (minutes / 60) to u8, .minute = (minutes % 60) to u8 };942 out.push(Choice<TimeOfDay> { .label = time_label(&locale, &time), .value = time });943 minutes += step;944 }945 return move out;946}947948// The Select the picker's options make.949fn as_select(t: &TimePicker, locale: &Locale) -> Select<TimeOfDay> => Select<TimeOfDay> {950 .choices = times(&t, &locale),951 .selected = t.time,952 .placeholder = copy t.placeholder,953 .accessible = copy t.accessible,954 .on_change = copy t.on_change,955 .enabled = t.enabled,956 .error = copy t.error,957 .required = t.required,958 .sizing = t.sizing959};960961fn select_of(self t: &var TimePicker) -> &var Select<TimeOfDay> {962 if (t.select is ::None) {963 var made = as_select(&t, &t.locale());964 t.select = ::Some(new made);965 }966 if (t.select is ::Some |&var s|) {967 return &var s;968 }969 panic "pkg::alloy::ui: a TimePicker with no Select";970}971972// The locale the times are written in, as last laid out.973fn locale(self t: &TimePicker) -> Locale {974 if (t.locale is ::Some |locale|) {975 return locale;976 }977 return Locale::of(&"en");978}979980// The picker's list closed.981fn time_closed(ui: &var Ui, picker: Element) {982 if (983 ui.exists(picker)984 && ui.node_var(picker.index).widget is ::Some |&var widget|985 && widget is TimePicker |&var t|986 && t.select is ::Some |&var s|987 ) {988 s.open = ::None;989 ui.apply(picker.index, Change::Paint);990 }991}992993pub fn layout(self t: &var TimePicker, arrange: &var Arrange, limits: Limits) -> Extent {994 const locale = arrange.ui.kept_locale();995 // the times written again in another locale996 if (t.locale != ::Some(locale)) {997 t.locale = ::Some(locale);998 if (t.select is ::Some) {999 var made = as_select(&t, &locale);1000 if (t.select is ::Some |&var s|) {1001 s.renew(move made);1002 }1003 }1004 }1005 return t.select_of().layout(&var arrange, limits);1006}10071008pub fn paint(self t: &TimePicker, look: &Look, paint: &var Paint) {1009 if (t.select is ::Some |&s|) {1010 s.paint(&look, &var paint);1011 }1012}10131014pub fn event(self t: &var TimePicker, ui: &var Ui, input: &Input) -> bool {1015 return t.select_of().handle(&var ui, &input, time_closed);1016}10171018pub fn take_closing(self t: &var TimePicker) -> Option<Layer> => t.select_of().take_closing();10191020pub fn renew(self t: &var TimePicker, next: TimePicker) -> Change {1021 var change = Change::Nothing;1022 const locale = t.locale();1023 if (t.select is ::Some |&var s|) {1024 change = s.renew(as_select(&next, &locale));1025 }1026 t.time = next.time;1027 t.earliest = next.earliest;1028 t.latest = next.latest;1029 t.step_minutes = next.step_minutes;1030 t.placeholder = move next.placeholder;1031 t.accessible = move next.accessible;1032 t.on_change = move next.on_change;1033 t.enabled = next.enabled;1034 t.error = move next.error;1035 t.required = next.required;1036 t.sizing = next.sizing;1037 return change;1038}10391040pub fn sizing(self t: &TimePicker) -> Sizing => t.sizing;1041pub fn interactive(self t: &TimePicker) -> bool => true;1042pub fn focusable(self t: &TimePicker) -> bool => t.enabled;1043pub fn cursor(self t: &TimePicker) -> Cursor => if (t.enabled) Cursor::Hand else Cursor::Arrow;1044pub fn clips(self t: &TimePicker) -> bool => true;10451046pub fn semantics(self t: &TimePicker) -> Semantics {1047 if (t.select is ::Some |&s|) {1048 return s.semantics();1049 }1050 return Semantics {1051 .role = Role::ComboBox,1052 .label = copy t.accessible,1053 .disabled = !t.enabled,1054 .focusable = t.enabled,1055 .expanded = ::Some(false),1056 .invalid = t.error.length() > 0,1057 .required = t.required,1058 .description = copy t.error1059 };1060}