use std::{marker::PhantomData, sync::Arc};
use super::{
calendar::DateMemory,
date_field::FieldValue,
field::{FieldControl, InField},
icon::Icon,
icons,
input::{Input, InputPart, InputView},
names,
part_style::{Length, PartStyle},
popover::{Popover, PopoverPart},
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::{fading, pressable},
text_box::{BoxStates, Judgment, TextBoxSize},
theme::{Themed, current_date_locale, current_theme},
};
use crate::{
app::{
component::Component,
ctx::{CollisionStrategy, Ctx, Placement},
events::{KeyboardEvent, MouseEvent, TextInputEvent},
theme::{IconSize, PaletteColor, RadiusSize, Theme, TypographyStyle},
},
components::{self, Column, Row, ScrollVertical, Stack},
core::{DateText, DayPeriod, ElementRef, HourCycle, Signal, Time},
layout::{Alignment, StackAlignment, layout_kind::Justify, text_style::FontWeight},
node::{CursorIcon, Element, FocusGroup, HitTestBehavior, SpacingValue, dimension::Dimension},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TimePickerPart {
Control,
Trigger,
TriggerIcon,
Panel,
Column,
Item,
}
names!(TimePickerPart {
Control => "control",
Trigger => "trigger",
TriggerIcon => "trigger_icon",
Panel => "panel",
Column => "column",
Item => "item",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TimePickerState {
Open,
Invalid,
Warning,
Valid,
ReadOnly,
Disabled,
FullWidth,
Selected,
Fluid,
}
names!(TimePickerState {
Open => "open",
Invalid => "invalid",
Warning => "warning",
Valid => "valid",
ReadOnly => "read_only",
Disabled => "disabled",
FullWidth => "full_width",
Selected => "selected",
Fluid => "fluid",
});
pub type TimePickerSize = TextBoxSize;
pub type TimePickerRenderer = Arc<dyn Fn(TimePickerView) -> Element + Send + Sync>;
#[derive(Clone)]
pub struct TimePicker<V: FieldValue<Time> = Time> {
value: Signal<V>,
controlled: bool,
boxed: BoxStates,
full_width: bool,
placeholder: Option<Arc<str>>,
min: Option<Time>,
max: Option<Time>,
step: u32,
hour_cycle: Option<HourCycle>,
locale: Option<Arc<dyn DateText>>,
open: Option<Signal<bool>>,
memory: Option<DateMemory>,
on_change: Vec<Arc<dyn Fn(V) + Send + Sync>>,
on_input: Vec<Arc<dyn Fn(&TextInputEvent) + Send + Sync>>,
on_submit: Vec<Arc<dyn Fn(V) + Send + Sync>>,
on_open_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
on_dismiss: Vec<Arc<dyn Fn() + Send + Sync>>,
placement: Placement,
offset: Option<SpacingValue>,
collision: Option<CollisionStrategy>,
inline: bool,
trigger_label: Option<Arc<str>>,
panel_label: Option<Arc<str>>,
#[cfg_attr(not(feature = "form"), allow(dead_code))]
name: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl TimePicker<Time> {
pub fn new(value: Signal<Time>) -> Self {
Self::with(value, false)
}
pub fn controlled(value: Time, on_change: impl Fn(Time) + Send + Sync + 'static) -> Self {
Self::with(Signal::new(value), true).on_change(on_change)
}
}
impl TimePicker<Option<Time>> {
pub fn optional(value: Signal<Option<Time>>) -> Self {
Self::with(value, false)
}
pub fn controlled_optional(value: Option<Time>, on_change: impl Fn(Option<Time>) + Send + Sync + 'static) -> Self {
Self::with(Signal::new(value), true).on_change(on_change)
}
}
impl<V: FieldValue<Time>> TimePicker<V> {
fn with(value: Signal<V>, controlled: bool) -> Self {
Self {
value,
controlled,
boxed: BoxStates::default(),
full_width: false,
placeholder: None,
min: None,
max: None,
step: 15,
hour_cycle: None,
locale: None,
open: None,
memory: None,
on_change: Vec::new(),
on_input: Vec::new(),
on_submit: Vec::new(),
on_open_change: Vec::new(),
on_dismiss: Vec::new(),
placement: Placement::BottomStart,
offset: None,
collision: None,
inline: false,
trigger_label: None,
panel_label: None,
name: None,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = Some(Arc::from(placeholder));
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.boxed.size = size.into();
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.boxed.invalid = invalid;
self
}
pub fn warning(mut self, warning: bool) -> Self {
self.boxed.warning = warning;
self
}
pub fn valid(mut self, valid: bool) -> Self {
self.boxed.valid = valid;
self
}
pub fn read_only(mut self, read_only: bool) -> Self {
self.boxed.read_only = read_only;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.boxed.disabled = disabled;
self
}
pub fn label(mut self, label: &str) -> Self {
self.boxed.label = Some(Arc::from(label));
self
}
pub fn full_width(mut self, full_width: bool) -> Self {
self.full_width = full_width;
self
}
pub fn min(mut self, min: Time) -> Self {
self.min = Some(min);
self
}
pub fn max(mut self, max: Time) -> Self {
self.max = Some(max);
self
}
pub fn step(mut self, minutes: impl Into<f64>) -> Self {
self.step = minutes.into().round().clamp(1.0, 60.0) as u32;
self
}
pub fn hour_cycle(mut self, cycle: HourCycle) -> Self {
self.hour_cycle = Some(cycle);
self
}
pub fn locale(mut self, locale: impl DateText + 'static) -> Self {
self.locale = Some(crate::core::shared_date_text(locale));
self
}
pub fn open(mut self, open: Signal<bool>) -> Self {
self.open = Some(open);
self
}
pub fn memory(mut self, memory: &DateMemory) -> Self {
self.memory = Some(memory.clone());
self
}
pub fn on_change(mut self, handler: impl Fn(V) + Send + Sync + 'static) -> Self {
self.on_change.push(Arc::new(handler));
self
}
pub fn on_input(mut self, handler: impl Fn(&TextInputEvent) + Send + Sync + 'static) -> Self {
self.on_input.push(Arc::new(handler));
self
}
pub fn on_submit(mut self, handler: impl Fn(V) + Send + Sync + 'static) -> Self {
self.on_submit.push(Arc::new(handler));
self
}
pub fn on_open_change(mut self, handler: impl Fn(bool) + Send + Sync + 'static) -> Self {
self.on_open_change.push(Arc::new(handler));
self
}
pub fn on_dismiss(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_dismiss.push(Arc::new(handler));
self
}
pub fn placement(mut self, placement: Placement) -> Self {
self.placement = placement;
self
}
pub fn offset(mut self, offset: impl Into<SpacingValue>) -> Self {
self.offset = Some(offset.into());
self
}
pub fn collision(mut self, collision: CollisionStrategy) -> Self {
self.collision = Some(collision);
self
}
pub fn inline(mut self, inline: bool) -> Self {
self.inline = inline;
self
}
pub fn trigger_label(mut self, label: &str) -> Self {
self.trigger_label = Some(Arc::from(label));
self
}
pub fn panel_label(mut self, label: &str) -> Self {
self.panel_label = Some(Arc::from(label));
self
}
#[cfg(feature = "form")]
pub fn name(mut self, name: impl Into<Arc<str>>) -> Self {
self.name = Some(name.into());
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn build(self, theme: &Theme) -> Element {
let view = self.view(theme);
match theme.renderer::<TimePicker>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let key = self.key.clone();
let mut element = ctx.mount::<PickerHost<V>>(PickerProps { picker: self });
if let Some(key) = key {
element = element.key(key);
}
element
}
fn view(self, theme: &Theme) -> TimePickerView {
let text = self.locale.clone().unwrap_or_else(current_date_locale);
let cycle = self.hour_cycle.unwrap_or_else(|| text.hour_cycle());
let (open, anchor) = match &self.memory {
Some(memory) => (
self.open.clone().unwrap_or_else(|| memory.open.clone()),
memory.anchor.clone(),
),
None => (
self.open.clone().unwrap_or_else(|| Signal::new(false)),
ElementRef::new(),
),
};
let value = self.value.get();
let is_open = open.get();
let held = value.get();
let disabled = self.boxed.disabled;
let fluid = self.boxed.fluid;
let case = self
.boxed
.case(self.states.clone())
.state(TimePickerState::Open, is_open)
.state(TimePickerState::FullWidth, self.full_width);
let recipe = theme
.resolve::<TimePicker>(&case, Some(&self.rules))
.owned_by(self.id.as_ref());
let resolve: Resolve = {
let (theme, rules, owner) = (theme.clone(), self.rules.clone(), recipe.clone());
Arc::new(move |case: &Case| theme.resolve::<TimePicker>(case, Some(&rules)).owned_like(&owner))
};
let model = Arc::new(Model {
value: self.value.clone(),
controlled: self.controlled,
open: open.clone(),
text: text.clone(),
min: self.min,
max: self.max,
on_change: self.on_change.clone(),
on_submit: self.on_submit.clone(),
on_open_change: self.on_open_change.clone(),
on_dismiss: self.on_dismiss.clone(),
read_only: self.boxed.read_only,
disabled,
});
let shown = Signal::new(held.map(|time| text.format_time(time, false)).unwrap_or_default());
let mut input = Input::new(shown.clone())
.icon(icons::TIMER)
.full_width(self.full_width)
.force(self.forced)
.rules(&recipe.rules_for(TimePickerPart::Control, InputPart::Control))
.rules(&Recipe::new().base(InputPart::Root, |s| {
s.width(if fluid { Length::Fill } else { Length::Auto })
}));
input.boxed = self.boxed.clone();
input.boxed.role = Some("combobox");
if let Some(placeholder) = &self.placeholder {
input = input.placeholder(placeholder);
}
#[cfg(feature = "form")]
if let Some(name) = &self.name {
input = input.name(name.clone());
}
if let Some(id) = &self.id {
input = input.id(id.clone());
}
for handler in &self.on_input {
let handler = handler.clone();
input = input.on_input(move |event: &TextInputEvent| handler(event));
}
let typed = model.clone();
input = input.on_input(move |event: &TextInputEvent| {
if !event.default_prevented() {
typed.typed(event.new_value());
}
});
let hours: Vec<u32> = match cycle {
HourCycle::H23 => (0..24).collect(),
HourCycle::H12 => (1..=12).collect(),
};
let minutes: Vec<u32> = (0..60).step_by(self.step.max(1) as usize).collect();
TimePickerView {
text: shown,
value: held,
open: is_open,
cycle,
hours,
minutes,
label: self.boxed.label.clone(),
invalid: self.boxed.judgment() == Some(Judgment::Invalid),
read_only: self.boxed.read_only,
disabled,
full_width: self.full_width,
locale: text,
recipe,
case,
resolve,
input: input.view(theme),
anchor,
open_signal: open,
placement: self.placement,
offset: self.offset,
collision: self.collision,
inline: self.inline,
trigger_label: self.trigger_label,
panel_label: self.panel_label,
model,
id: self.id,
key: self.key,
}
}
}
impl<V: FieldValue<Time>> From<TimePicker<V>> for Element {
fn from(picker: TimePicker<V>) -> Self {
picker.build(¤t_theme())
}
}
impl<V: FieldValue<Time>> FieldControl for TimePicker<V> {
fn in_field(mut self, field: &InField) -> Element {
self.boxed.in_field(field);
self.into()
}
fn mount_in_field(mut self, field: &InField, ctx: &mut Ctx) -> Element {
self.boxed.in_field(field);
self.mount(ctx)
}
fn has_fluid_form(&self) -> bool {
true
}
}
type Resolve = Arc<dyn Fn(&Case) -> ResolvedRecipe + Send + Sync>;
trait TimeModel: Send + Sync {
fn typed(&self, text: &str);
fn commit(&self, text: &Signal<String>, submit: bool);
fn set_open(&self, open: bool);
fn toggle(&self);
fn dismissed(&self);
fn choose(&self, column: ColumnKind, number: u32, cycle: HourCycle);
fn allowed(&self, time: Time) -> bool;
fn held(&self) -> Option<Time>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColumnKind {
Hour,
Minute,
Period,
}
struct Model<V: FieldValue<Time>> {
value: Signal<V>,
controlled: bool,
open: Signal<bool>,
text: Arc<dyn DateText>,
min: Option<Time>,
max: Option<Time>,
on_change: Vec<Arc<dyn Fn(V) + Send + Sync>>,
on_submit: Vec<Arc<dyn Fn(V) + Send + Sync>>,
on_open_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
on_dismiss: Vec<Arc<dyn Fn() + Send + Sync>>,
read_only: bool,
disabled: bool,
}
impl<V: FieldValue<Time>> Model<V> {
fn write(&self, time: Option<Time>) {
let Some(value) = V::of(time) else {
return;
};
if self.value.get_untracked() == value {
return;
}
if !self.controlled {
self.value.set(value);
}
for handler in &self.on_change {
handler(value);
}
}
}
impl<V: FieldValue<Time>> TimeModel for Model<V> {
fn allowed(&self, time: Time) -> bool {
self.min.is_none_or(|min| time >= min) && self.max.is_none_or(|max| time <= max)
}
fn held(&self) -> Option<Time> {
self.value.get_untracked().get()
}
fn typed(&self, text: &str) {
if text.trim().is_empty() {
self.write(None);
return;
}
if let Some(time) = self.text.parse_time(text).filter(|time| self.allowed(*time)) {
self.write(Some(time));
}
}
fn commit(&self, text: &Signal<String>, submit: bool) {
let held = text.get_untracked();
if held.trim().is_empty() {
self.write(None);
} else if let Some(time) = self.text.parse_time(&held).filter(|time| self.allowed(*time)) {
self.write(Some(time));
}
let written = self
.held()
.map(|time| self.text.format_time(time, false))
.unwrap_or_default();
if held != written {
text.set(written);
}
if submit {
let value = self.value.get_untracked();
for handler in &self.on_submit {
handler(value);
}
}
}
fn set_open(&self, open: bool) {
if open && (self.read_only || self.disabled) {
return;
}
if self.open.get_untracked() == open {
return;
}
self.open.set(open);
for handler in &self.on_open_change {
handler(open);
}
}
fn toggle(&self) {
self.set_open(!self.open.get_untracked());
}
fn dismissed(&self) {
for handler in &self.on_open_change {
handler(false);
}
for handler in &self.on_dismiss {
handler();
}
}
fn choose(&self, column: ColumnKind, number: u32, cycle: HourCycle) {
let base = self.held().unwrap_or(Time::MIDNIGHT);
let time = match (column, cycle) {
(ColumnKind::Hour, HourCycle::H23) => Time::new(number as u8, base.minute(), 0),
(ColumnKind::Hour, HourCycle::H12) => Time::from_hour12(number as u8, base.period(), base.minute(), 0),
(ColumnKind::Minute, _) => Time::new(base.hour(), number as u8, 0),
(ColumnKind::Period, _) => {
let period = if number == 0 { DayPeriod::Am } else { DayPeriod::Pm };
Time::from_hour12(base.hour12().0, period, base.minute(), 0)
}
};
let Some(time) = time.filter(|time| self.allowed(*time)) else {
return;
};
if column == ColumnKind::Minute {
self.set_open(false);
}
self.write(Some(time));
}
}
pub struct TimePickerView {
pub text: Signal<String>,
pub value: Option<Time>,
pub open: bool,
pub cycle: HourCycle,
pub hours: Vec<u32>,
pub minutes: Vec<u32>,
pub label: Option<Arc<str>>,
pub invalid: bool,
pub read_only: bool,
pub disabled: bool,
pub full_width: bool,
pub locale: Arc<dyn DateText>,
pub recipe: ResolvedRecipe,
case: Case,
resolve: Resolve,
input: InputView,
anchor: ElementRef,
open_signal: Signal<bool>,
placement: Placement,
offset: Option<SpacingValue>,
collision: Option<CollisionStrategy>,
inline: bool,
trigger_label: Option<Arc<str>>,
panel_label: Option<Arc<str>>,
model: Arc<dyn TimeModel>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl TimePickerView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
fn id_of(&self, suffix: &str) -> Option<String> {
self.id.as_ref().map(|id| format!("{id}-{suffix}"))
}
fn choose_name(&self) -> Arc<str> {
Arc::from(self.locale.choose_time_label())
}
fn gap(&self, part: TimePickerPart) -> f32 {
let spacing = self.theme().spacing();
self.part(part).rest().gap.map_or(0.0, |gap| gap.resolve(&spacing, 0.0))
}
fn px(&self, length: Option<Length>) -> f32 {
match length.and_then(|length| length.resolve(self.theme())) {
Some(Dimension::Px(px)) => px,
_ => 0.0,
}
}
pub fn control(&self) -> components::TextInput {
let (text, keys, blur) = (self.text.clone(), self.model.clone(), self.model.clone());
let blur_text = self.text.clone();
let mut control = self
.input
.control()
.keep_text_while_focused(true)
.describe("a11y_haspopup", "dialog")
.describe("a11y_expanded", if self.open { "true" } else { "false" })
.on_key_down(move |event: KeyboardEvent| {
if !event.focus_within || event.ctrl || event.meta || event.composing {
return;
}
match event.key.as_str() {
"ArrowDown" => {
event.prevent_default();
keys.set_open(true);
}
"Enter" => keys.commit(&text, true),
_ => {}
}
});
if !self.read_only {
control = control.on_blur(move || blur.commit(&blur_text, false));
}
if super::touch_form() && !self.read_only && !self.disabled {
let tapped = self.model.clone();
control = control
.keyboard(components::KeyboardKind::None)
.on_click(move |_: MouseEvent| tapped.set_open(true));
}
control
}
pub fn trigger(&self) -> components::Button {
let model = self.model.clone();
let part = self.part(TimePickerPart::Trigger);
let glyph = self.part(TimePickerPart::TriggerIcon);
let side = glyph.rest().width.unwrap_or(Length::Icon(IconSize::Md));
let mut trigger = components::Button::empty()
.justify(Justify::Center)
.align_items(Alignment::Center)
.part(&part)
.focusable(false)
.disabled(self.disabled || self.read_only)
.describe(
"a11y_name",
self.trigger_label.clone().unwrap_or_else(|| self.choose_name()),
)
.describe("a11y_haspopup", "dialog")
.describe("a11y_expanded", if self.open { "true" } else { "false" })
.child(
Row::new()
.part(&glyph)
.child(Icon::new(icons::TIMER).size(side).build(self.theme())),
)
.on_click(move |_: MouseEvent| model.toggle());
if let Some(id) = self.id_of("trigger") {
trigger = trigger.id(id);
}
trigger
}
pub fn field(&self) -> Stack {
let [_, _, _, start] = self.input.part(InputPart::Control).rest().paddings();
let spacing = self.theme().spacing();
let start = start.map_or(0.0, |padding| padding.resolve(&spacing, 0.0));
let inset = ((self.px(self.part(TimePickerPart::Trigger).rest().width)
- self.px(self.part(TimePickerPart::TriggerIcon).rest().width))
/ 2.0)
.max(0.0);
let mut field = Stack::new()
.stack_align(StackAlignment::CenterStart)
.part(&self.input.part(InputPart::Root))
.ref_element(self.anchor.clone())
.child(self.control())
.child(
Row::new()
.padding_left((start - inset).max(0.0))
.padding_top(self.input.value_line())
.align_items(Alignment::Center)
.hit_test(HitTestBehavior::ContentOnly)
.child(self.trigger()),
);
if self.full_width || self.input.fluid() {
field = field.width(Dimension::Pct(100.0));
}
field
}
fn item_part(&self, selected: bool) -> ResolvedPart {
let case = self.case.clone().state(TimePickerState::Selected, selected);
(self.resolve)(&case).part(TimePickerPart::Item)
}
fn item(&self, column: ColumnKind, number: u32, label: String, selected: bool, allowed: bool) -> Element {
let part = self.item_part(selected);
let model = self.model.clone();
let cycle = self.cycle;
let suffix = match column {
ColumnKind::Hour => format!("hour-{number}"),
ColumnKind::Minute => format!("minute-{number}"),
ColumnKind::Period => format!("period-{}", if number == 0 { "am" } else { "pm" }),
};
let mut item = pressable(&part, self.disabled || !allowed)
.align_items(Alignment::Center)
.key(suffix.clone())
.focus_entry(selected)
.autofocus(selected && column == ColumnKind::Hour && !self.inline)
.describe("a11y_role", "option")
.describe("a11y_name", label.clone())
.describe("a11y_selected", if selected { "true" } else { "false" })
.child(part.text(&label).nowrap())
.on_click(move |_: MouseEvent| model.choose(column, number, cycle));
if let Some(id) = self.id_of(&suffix) {
item = item.id(id);
}
item.into()
}
fn column(&self, column: ColumnKind, items: Vec<Element>, held: Option<usize>) -> Element {
let part = self.part(TimePickerPart::Column).keyed(match column {
ColumnKind::Hour => "hour",
ColumnKind::Minute => "minute",
ColumnKind::Period => "period",
});
let label = self.locale.segment_label(match column {
ColumnKind::Hour => crate::core::DateSegment::Hour,
ColumnKind::Minute => crate::core::DateSegment::Minute,
ColumnKind::Period => crate::core::DateSegment::Period,
});
let list = Column::new()
.align_items(Alignment::Stretch)
.spacing(self.gap(TimePickerPart::Column))
.focus_group(FocusGroup::vertical().one_stop(true).wrap(false))
.describe("a11y_role", "listbox")
.describe("a11y_name", label)
.with_children(items);
let item = match self
.part(TimePickerPart::Item)
.rest()
.height
.and_then(|height| height.resolve(self.theme()))
{
Some(Dimension::Px(height)) => height,
_ => 0.0,
};
let state = crate::layout::layout_kind::ScrollState::new();
if let Some(index) = held {
let offset = index.saturating_sub(1) as f32 * (item + self.gap(TimePickerPart::Column));
state.set_scroll_pending(0.0, offset.max(0.0));
}
let mut scroll = ScrollVertical::new(list)
.with_scroll_state(state)
.scrollbar(crate::layout::scrollbar::ScrollBarStyle::on_hover());
if let Some(Dimension::Px(width)) = part.rest().width.and_then(|width| width.resolve(self.theme())) {
scroll = scroll.width(width);
}
if let Some(Dimension::Px(height)) = part.rest().height.and_then(|height| height.resolve(self.theme())) {
scroll = scroll.height(height);
}
scroll.into()
}
pub fn columns(&self) -> Row {
let held = self.value;
let model = &self.model;
let hour_of = |time: Time| match self.cycle {
HourCycle::H23 => time.hour() as u32,
HourCycle::H12 => time.hour12().0 as u32,
};
let hours = self
.hours
.iter()
.map(|hour| {
let selected = held.is_some_and(|time| hour_of(time) == *hour);
let allowed = (0..60).any(|minute| {
let base = held.unwrap_or(Time::MIDNIGHT);
let time = match self.cycle {
HourCycle::H23 => Time::new(*hour as u8, minute, 0),
HourCycle::H12 => Time::from_hour12(*hour as u8, base.period(), minute, 0),
};
time.is_some_and(|time| model.allowed(time))
});
self.item(ColumnKind::Hour, *hour, format!("{hour:02}"), selected, allowed)
})
.collect();
let minutes = self
.minutes
.iter()
.map(|minute| {
let selected = held.is_some_and(|time| time.minute() as u32 == *minute);
let base = held.unwrap_or(Time::MIDNIGHT);
let allowed = Time::new(base.hour(), *minute as u8, 0).is_some_and(|time| model.allowed(time));
self.item(ColumnKind::Minute, *minute, format!("{minute:02}"), selected, allowed)
})
.collect();
let mut row = Row::new()
.align_items(Alignment::Start)
.spacing(self.gap(TimePickerPart::Panel))
.child(self.column(
ColumnKind::Hour,
hours,
held.and_then(|time| self.hours.iter().position(|hour| *hour == hour_of(time))),
))
.child(self.column(
ColumnKind::Minute,
minutes,
held.and_then(|time| self.minutes.iter().position(|minute| *minute == time.minute() as u32)),
));
if self.cycle == HourCycle::H12 {
let periods = [DayPeriod::Am, DayPeriod::Pm]
.into_iter()
.map(|period| {
let selected = held.is_some_and(|time| time.period() == period);
self.item(
ColumnKind::Period,
period as u32,
self.locale.day_period_name(period),
selected,
true,
)
})
.collect();
row = row.child(self.column(ColumnKind::Period, periods, None));
}
row
}
pub fn panel(&self) -> Element {
let rules = self.recipe.rules_for(TimePickerPart::Panel, PopoverPart::Panel);
let name = self.panel_label.clone().unwrap_or_else(|| self.choose_name());
if self.inline {
let case = Case {
breakpoint: super::current_breakpoint(),
..Case::default()
};
let recipe = self.theme().resolve::<Popover>(&case, Some(&rules)).owned_by(None);
let mut panel = Column::new()
.align_items(Alignment::Stretch)
.part(&recipe.part(PopoverPart::Panel))
.describe("a11y_role", "dialog")
.describe("a11y_name", name)
.child(
Column::new()
.align_items(Alignment::Stretch)
.part(&recipe.part(PopoverPart::Body))
.child(self.columns()),
);
if let Some(id) = self.id_of("panel") {
panel = panel.id(id);
}
return panel.into();
}
let model = self.model.clone();
let mut popover = Popover::new(self.open_signal.clone(), self.anchor.clone())
.placement(self.placement)
.label(&name)
.rules(&rules)
.on_dismiss(move || model.dismissed())
.child(self.columns());
if let Some(offset) = self.offset {
popover = popover.offset(offset);
}
if let Some(collision) = self.collision {
popover = popover.collision(collision);
}
if let Some(id) = self.id_of("panel") {
popover = popover.id(id);
}
popover.into()
}
pub fn render(self) -> Element {
if self.inline {
let gap = self
.offset
.unwrap_or_else(|| crate::app::theme::SpacingSize::Xs.into())
.resolve(&self.theme().spacing(), 0.0);
let mut root = Column::new()
.max_width(Dimension::Pct(100.0))
.spacing(gap)
.child(self.field())
.child(self.panel());
if self.input.fluid() {
root = root.width(Dimension::Pct(100.0));
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
return root.into();
}
let mut root = Row::new()
.max_width(Dimension::Pct(100.0))
.child(self.field())
.child(self.panel());
if self.full_width || self.input.fluid() {
root = root.width(Dimension::Pct(100.0));
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root.into()
}
}
impl<V: FieldValue<Time>> crate::ui::Styled for TimePicker<V> {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl<V: FieldValue<Time>> Themed for TimePicker<V> {
const NAME: &'static str = "time_picker";
const ROOT: &'static str = "control";
type Renderer = TimePickerRenderer;
fn parts() -> crate::ui::Declared {
TimePickerPart::declared()
}
fn states() -> crate::ui::Declared {
TimePickerState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::{Foreground, Highlight, HighlightForeground, MutedForeground};
use TimePickerPart::{Column as Lane, Control, Item, Panel, Trigger, TriggerIcon};
let none = crate::node::color::Color::new(0, 0, 0, 0);
Recipe::new()
.base(Control, |s| {
s.width(160.0).typography(TypographyStyle::Mono).font_size(14.0)
})
.state(TimePickerState::FullWidth, Control, |s| s.width(Length::Fill))
.state(TimePickerState::Fluid, Control, |s| {
s.width(Length::Fill).height(super::text_box::FLUID_HEIGHT)
})
.base(Trigger, |s| {
fading(
s.square(24.0)
.radius(RadiusSize::Sm)
.foreground(MutedForeground)
.cursor(CursorIcon::Pointer),
)
})
.hovered(Trigger, |s| s.background(Highlight))
.base(TriggerIcon, |s| s.square(IconSize::Md))
.base(Panel, |s| s.width(Length::Auto).padding_x(4.0).padding_y(4.0).gap(4.0))
.base(Lane, |s| s.width(74.0).height(126.0).gap(2.0))
.base(Item, |s| {
fading(
s.height(30.0)
.radius(RadiusSize::Sm)
.typography(TypographyStyle::Mono)
.font_size(13.0)
.weight(FontWeight::Normal)
.foreground(Foreground)
.background(none)
.cursor(CursorIcon::Pointer),
)
})
.hovered(Item, |s| s.background(Highlight).foreground(HighlightForeground))
.focus_visible(Item, |s| s.focus_ring())
.state(TimePickerState::Selected, Item, |s| {
s.background(Highlight)
.foreground(HighlightForeground)
.weight(FontWeight::Medium)
})
.state(TimePickerState::Disabled, Trigger, |s| s.cursor(CursorIcon::Default))
}
}
#[derive(Clone)]
struct PickerProps<V: FieldValue<Time>> {
picker: TimePicker<V>,
}
impl<V: FieldValue<Time>> PartialEq for PickerProps<V> {
fn eq(&self, _: &Self) -> bool {
false
}
}
impl<V: FieldValue<Time>> crate::app::component::DevtoolsInspectable for PickerProps<V> {}
struct PickerHost<V: FieldValue<Time>> {
memory: DateMemory,
value: PhantomData<fn() -> V>,
}
impl<V: FieldValue<Time>> Component for PickerHost<V> {
type Props = PickerProps<V>;
fn create(_ctx: &mut Ctx) -> Self {
Self {
memory: DateMemory::new(),
value: PhantomData,
}
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let mut picker = ctx.props::<PickerProps<V>>().picker.clone();
if picker.memory.is_none() {
picker.memory = Some(self.memory.clone());
}
picker.build(¤t_theme())
}
}