use std::sync::Arc;
use super::{
names,
part_style::{Length, PartStyle, PartTransition},
recipe::{Case, Name, Recipe, Selector},
shared::fading,
};
use crate::{
animation::Transition,
app::{
events::TextInputEvent,
theme::{BorderSize, ControlSize, MotionDuration, OpacityLevel, PaletteColor, RadiusSize, Theme},
},
components::TextInput,
layout::text_style::TextStyle,
node::{CursorIcon, Outline, SpacingValue, TextColor, border::Border},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextBoxSize {
Sm,
#[default]
Md,
Lg,
}
names!(TextBoxSize {
Sm => "sm",
Md => "md",
Lg => "lg",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Judgment {
Invalid,
Warning,
Valid,
}
names!(Judgment {
Invalid => "invalid",
Warning => "warning",
Valid => "valid",
});
impl Judgment {
pub fn of(invalid: bool, warning: bool, valid: bool) -> Option<Self> {
if invalid {
Some(Self::Invalid)
} else if warning {
Some(Self::Warning)
} else if valid {
Some(Self::Valid)
} else {
None
}
}
pub fn color(self) -> PaletteColor {
match self {
Self::Invalid => PaletteColor::Destructive,
Self::Warning => PaletteColor::Warning,
Self::Valid => PaletteColor::Success,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TextBoxState {
Invalid,
Warning,
Valid,
ReadOnly,
Disabled,
Fluid,
}
names!(TextBoxState {
Invalid => "invalid",
Warning => "warning",
Valid => "valid",
ReadOnly => "read_only",
Disabled => "disabled",
Fluid => "fluid",
});
impl From<Judgment> for TextBoxState {
fn from(judgment: Judgment) -> Self {
match judgment {
Judgment::Invalid => Self::Invalid,
Judgment::Warning => Self::Warning,
Judgment::Valid => Self::Valid,
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct BoxStates {
pub size: Name,
pub invalid: bool,
pub warning: bool,
pub valid: bool,
pub read_only: bool,
pub disabled: bool,
pub fluid: bool,
pub label: Option<Arc<str>>,
pub role: Option<&'static str>,
}
impl Default for BoxStates {
fn default() -> Self {
Self {
size: TextBoxSize::default().into(),
invalid: false,
warning: false,
valid: false,
read_only: false,
disabled: false,
fluid: false,
label: None,
role: None,
}
}
}
impl BoxStates {
pub fn judgment(&self) -> Option<Judgment> {
Judgment::of(self.invalid, self.warning, self.valid)
}
pub fn case(&self, states: Vec<Name>) -> Case {
let judgment = self.judgment();
Case {
variant: None,
size: Some(self.size),
density: None,
breakpoint: super::current_breakpoint(),
states,
}
.state(TextBoxState::Invalid, judgment == Some(Judgment::Invalid))
.state(TextBoxState::Warning, judgment == Some(Judgment::Warning))
.state(TextBoxState::Valid, judgment == Some(Judgment::Valid))
.state(TextBoxState::ReadOnly, self.read_only)
.state(TextBoxState::Disabled, self.disabled)
.state(TextBoxState::Fluid, self.fluid)
}
pub fn in_field(&mut self, field: &super::InField) {
if self.label.is_none() && !field.label.is_empty() {
self.label = Some(field.label.clone());
}
if self.judgment().is_none() {
self.invalid = field.judgment == Some(Judgment::Invalid);
self.warning = field.judgment == Some(Judgment::Warning);
self.valid = field.judgment == Some(Judgment::Valid);
}
self.disabled |= field.disabled;
self.fluid |= field.fluid;
}
}
pub(crate) fn edge(color: PaletteColor) -> Border {
Border::inside(BorderSize::Sm, color)
}
pub(crate) fn ring(color: PaletteColor) -> Outline {
Outline::new(2.0, color).offset(-2.0)
}
pub(crate) fn box_style(style: PartStyle) -> PartStyle {
fading(
style
.width(Length::Fill)
.padding_x(12.0)
.radius(RadiusSize::Md)
.background(PaletteColor::Background)
.border(edge(PaletteColor::Input))
.cursor(CursorIcon::Text),
)
.transition(PartTransition::new(Transition::border_color(), MotionDuration::Fast))
}
pub(crate) fn box_rules(
recipe: Recipe,
whole: impl Into<Name>,
control: impl Into<Name>,
placeholder: impl Into<Name>,
) -> Recipe {
let (whole, control) = (whole.into(), control.into());
let recipe = recipe
.base(placeholder, |s| s.foreground(PaletteColor::MutedForeground))
.hovered(control, |s| s.border(edge(PaletteColor::Ring)))
.focus_visible(control, |s| s.outline(ring(PaletteColor::Ring)))
.state(TextBoxState::ReadOnly, control, |s| {
s.background(PaletteColor::Muted).border(edge(PaletteColor::Border))
})
.state(TextBoxState::Disabled, whole, |s| s.opacity(OpacityLevel::Disabled))
.state(TextBoxState::Disabled, control, |s| {
s.background(PaletteColor::Muted).cursor(CursorIcon::Default)
});
[Judgment::Invalid, Judgment::Warning, Judgment::Valid]
.into_iter()
.fold(recipe, |recipe, judgment| {
let (state, color) = (TextBoxState::from(judgment), judgment.color());
recipe
.state(state, control, |s| s.border(edge(color.clone())))
.when(Selector::new().state(state).hovered(), control, |s| {
s.border(edge(color.clone()))
})
.when(Selector::new().state(state).focus_visible(), control, |s| {
s.outline(ring(color))
})
})
}
pub(crate) const FLUID_TOP: f32 = 29.0;
pub(crate) const FLUID_PAD: f32 = 10.0;
pub(crate) const FLUID_HEIGHT: f32 = 56.0;
pub(crate) const FLUID_GAP: f32 = 4.0;
pub(crate) fn value_line(style: &PartStyle, theme: &Theme) -> f32 {
let [top, _, bottom, _] = style.paddings();
let spacing = theme.spacing();
let px = |padding: Option<SpacingValue>| padding.map_or(0.0, |padding| padding.resolve(&spacing, 0.0));
(px(top) - px(bottom)).max(0.0)
}
pub(crate) fn fluid_line(style: PartStyle, color: PaletteColor) -> PartStyle {
style
.border(Border::inside(0.0, color.clone()))
.underline(Border::inside(BorderSize::Sm, color))
}
pub(crate) fn fluid_box(style: PartStyle) -> PartStyle {
fluid_line(style, PaletteColor::Foreground)
.padding_top(FLUID_TOP)
.padding_bottom(FLUID_PAD)
.corners(RadiusSize::Md, RadiusSize::Md, 0.0, 0.0)
.background(PaletteColor::Muted)
}
pub(crate) fn fluid_rules(recipe: Recipe, whole: impl Into<Name>, control: impl Into<Name>) -> Recipe {
let (whole, control) = (whole.into(), control.into());
let fluid = || Selector::new().state(TextBoxState::Fluid);
let recipe = recipe
.state(TextBoxState::Fluid, whole, |s| s.width(Length::Fill))
.state(TextBoxState::Fluid, control, |s| {
fluid_box(s).height(Length::Auto).min_height(FLUID_HEIGHT)
})
.when(fluid().hovered(), control, |s| fluid_line(s, PaletteColor::Ring))
.when(fluid().state(TextBoxState::ReadOnly), control, |s| {
fluid_line(s, PaletteColor::Border)
})
.when(fluid().state(TextBoxState::ReadOnly).hovered(), control, |s| {
fluid_line(s, PaletteColor::Border)
});
[Judgment::Invalid, Judgment::Warning, Judgment::Valid]
.into_iter()
.fold(recipe, |recipe, judgment| {
let (state, color) = (TextBoxState::from(judgment), judgment.color());
recipe
.when(fluid().state(state), control, |s| fluid_line(s, color.clone()))
.when(fluid().state(state).hovered(), control, |s| fluid_line(s, color))
})
}
pub(crate) fn box_sizes(recipe: Recipe, part: impl Into<Name>, heights: bool) -> Recipe {
let part = part.into();
[
(TextBoxSize::Sm, ControlSize::Sm, 13.0),
(TextBoxSize::Lg, ControlSize::Lg, 15.0),
]
.into_iter()
.fold(recipe, |recipe, (size, height, font_size)| {
recipe.size(size, part, |s| {
let s = s.font_size(font_size);
if heights { s.height(height) } else { s }
})
})
}
pub(crate) fn ink(theme: &Theme, style: &PartStyle) -> TextStyle {
let mut text = style.text.resolve(theme);
match &style.foreground {
Some(TextColor::Color(color)) => text.color = *color,
Some(TextColor::Palette(role)) => text.color = theme.palette_color(role.clone()),
None => {}
}
text
}
pub(crate) type EditHandler = Arc<dyn Fn(&TextInputEvent) + Send + Sync>;
pub(crate) fn text_box(control: TextInput, states: &BoxStates, handlers: Vec<EditHandler>) -> TextInput {
text_box_as(control, states, handlers, "textbox")
}
pub(crate) fn text_box_as(
control: TextInput,
states: &BoxStates,
handlers: Vec<EditHandler>,
role: &'static str,
) -> TextInput {
let invalid = states.judgment() == Some(Judgment::Invalid);
let mut control = control
.tab_index(0)
.disabled(states.disabled)
.describe("a11y_role", states.role.unwrap_or("textbox"))
.describe("a11y_role", role)
.describe("a11y_invalid", if invalid { "true" } else { "false" });
if states.read_only {
control = control.describe("a11y_readonly", "true");
}
if let Some(label) = &states.label {
control = control.describe("a11y_name", label.clone());
}
let read_only = states.read_only;
if read_only || !handlers.is_empty() {
control = control.on_input(move |event: TextInputEvent| {
if read_only {
event.prevent_default();
return;
}
for handler in &handlers {
handler(&event);
}
});
}
control
}