use std::cell::RefCell;
use std::rc::Rc;
use crate::core::context::MountContext;
use crate::core::dirty_registry;
use crate::core::element::{DirtyFlags, ElementId};
use crate::core::widget::Widget;
use crate::ecs::components;
use crate::style::styled::{StyleRefinement, Styled};
use crate::widgets::display::Text;
use crate::widgets::layout::VStack;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AutovalidateMode {
Disabled,
OnChange,
OnBlur,
OnSubmit,
}
type ValidatorFn = Box<dyn Fn(&str) -> Option<String>>;
type ValueGetter = Rc<dyn Fn() -> String>;
struct ValidationState {
element_id: ElementId,
validator: Option<ValidatorFn>,
getter: Option<ValueGetter>,
error: Option<String>,
dirty: bool,
#[allow(dead_code)] autovalidate: AutovalidateMode,
}
#[derive(Default)]
struct FormDomain {
validators: RefCell<Vec<ValidationState>>,
active_form: RefCell<Option<ElementId>>,
fields: RefCell<std::collections::HashMap<ElementId, Vec<ElementId>>>,
}
fn form_domain() -> Rc<FormDomain> {
crate::core::app_context::current_app().extension::<FormDomain>()
}
pub fn register_validator(
element_id: ElementId,
validator: Option<impl Fn(&str) -> Option<String> + 'static>,
getter: Option<impl Fn() -> String + 'static>,
autovalidate: AutovalidateMode,
) {
dirty_registry::register_teardown_hook(teardown_cleanup);
form_domain().validators.borrow_mut().push(ValidationState {
element_id,
validator: validator.map(|f| Box::new(f) as ValidatorFn),
getter: getter.map(|f| Rc::new(f) as ValueGetter),
error: None,
dirty: false,
autovalidate,
});
}
fn teardown_cleanup(id: ElementId) {
let dom = form_domain();
dom.validators.borrow_mut().retain(|s| s.element_id != id);
{
let mut f = dom.fields.borrow_mut();
f.remove(&id);
for fields in f.values_mut() {
fields.retain(|&fid| fid != id);
}
}
}
#[doc(hidden)]
pub fn debug_registry_sizes() -> (usize, usize, usize) {
let dom = form_domain();
let v = dom.validators.borrow().len();
let f = dom.fields.borrow();
(v, f.len(), f.values().map(|v| v.len()).sum())
}
pub fn unregister_validator(element_id: ElementId) {
form_domain()
.validators
.borrow_mut()
.retain(|s| s.element_id != element_id);
}
pub fn validate_field(element_id: ElementId, value: &str) -> Option<String> {
let dom = form_domain();
let mut validators = dom.validators.borrow_mut();
if let Some(state) = validators.iter_mut().find(|s| s.element_id == element_id) {
state.dirty = true;
let error = state.validator.as_ref().and_then(|f| f(value));
state.error = error.clone();
dirty_registry::mark_dirty(element_id, DirtyFlags::REPAINT);
dirty_registry::register_dirty(element_id, DirtyFlags::REPAINT);
error
} else {
None
}
}
pub fn validate_form(form_id: ElementId) -> bool {
let dom = form_domain();
let field_ids: Vec<ElementId> = dom
.fields
.borrow()
.get(&form_id)
.cloned()
.unwrap_or_default();
let mut all_valid = true;
{
let mut validators = dom.validators.borrow_mut();
for state in validators.iter_mut() {
if !field_ids.contains(&state.element_id) {
continue;
}
let value = state.getter.as_ref().map_or(String::new(), |g| g());
state.dirty = true;
let error = state.validator.as_ref().and_then(|f| f(&value));
state.error = error.clone();
if error.is_some() {
all_valid = false;
}
dirty_registry::mark_dirty(state.element_id, DirtyFlags::REPAINT);
dirty_registry::register_dirty(state.element_id, DirtyFlags::REPAINT);
}
}
all_valid
}
pub fn get_error(element_id: ElementId) -> Option<String> {
form_domain()
.validators
.borrow()
.iter()
.find(|s| s.element_id == element_id)
.and_then(|s| s.error.clone())
}
pub fn clear_error(element_id: ElementId) {
if let Some(state) = form_domain()
.validators
.borrow_mut()
.iter_mut()
.find(|s| s.element_id == element_id)
{
state.error = None;
state.dirty = false;
}
}
pub fn reset_form_validators(form_id: ElementId) {
let dom = form_domain();
let field_ids: Vec<ElementId> = dom
.fields
.borrow()
.get(&form_id)
.cloned()
.unwrap_or_default();
for state in dom.validators.borrow_mut().iter_mut() {
if field_ids.contains(&state.element_id) {
state.error = None;
state.dirty = false;
}
}
}
fn register_field(form_id: ElementId, field_id: ElementId) {
dirty_registry::register_teardown_hook(teardown_cleanup);
form_domain()
.fields
.borrow_mut()
.entry(form_id)
.or_default()
.push(field_id);
}
pub struct Field {
label: Option<String>,
description: Option<String>,
required: bool,
validator: Option<Rc<dyn Fn(&str) -> Option<String>>>,
value_getter: Option<Rc<dyn Fn() -> String>>,
autovalidate: AutovalidateMode,
child: Option<Box<dyn Widget>>,
style: StyleRefinement,
}
impl Field {
pub fn new() -> Self {
Self {
label: None,
description: None,
required: false,
validator: None,
value_getter: None,
autovalidate: AutovalidateMode::OnSubmit,
child: None,
style: StyleRefinement::default(),
}
}
pub fn label(mut self, s: impl Into<String>) -> Self {
self.label = Some(s.into());
self
}
pub fn description(mut self, s: impl Into<String>) -> Self {
self.description = Some(s.into());
self
}
pub fn required(mut self, v: bool) -> Self {
self.required = v;
self
}
pub fn validator(mut self, f: impl Fn(&str) -> Option<String> + 'static) -> Self {
self.validator = Some(Rc::new(f));
self
}
pub fn value(mut self, f: impl Fn() -> String + 'static) -> Self {
self.value_getter = Some(Rc::new(f));
self
}
pub fn autovalidate(mut self, mode: AutovalidateMode) -> Self {
self.autovalidate = mode;
self
}
pub fn child(mut self, widget: impl Widget + 'static) -> Self {
self.child = Some(Box::new(widget));
self
}
}
impl Default for Field {
fn default() -> Self {
Self::new()
}
}
impl Styled for Field {
fn style_refinement(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Widget for Field {
fn component_mask(&self) -> u64 {
components::STYLE | components::LAYOUT
}
fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
let container_id = ctx.arena.allocate();
ctx.preallocate(container_id, self.component_mask());
if let Some(form_id) = *form_domain().active_form.borrow() {
register_field(form_id, container_id);
}
if let Some(label) = self.label {
let label_text = if self.required {
format!("{} *", label)
} else {
label
};
let label_id =
Box::new(Text::new(&label_text).font_size(13.0).font_weight(600)).mount_box(ctx);
ctx.arena.add_child(container_id, label_id);
}
if let Some(child) = self.child {
let mut child_ctx = ctx.child_with_events(container_id);
let child_id = child.mount_box(&mut child_ctx);
ctx.arena.add_child(container_id, child_id);
if let Some(ref validator_fn) = self.validator {
let vfn = validator_fn.clone();
let getter = self.value_getter.clone();
register_validator(
child_id,
Some(move |value: &str| vfn(value)),
getter.map(|g| move || g()),
self.autovalidate,
);
}
}
if self.validator.is_some() {
let err_id = ctx.arena.allocate();
{
let Some(el) = ctx.arena.get_mut(err_id) else {
return container_id;
};
el.set_font_size(11.0);
el.set_foreground(crate::style::Color::rgba8(220, 38, 38, 255));
el.set_visible(false);
}
ctx.arena.add_child(container_id, err_id);
} else if let Some(desc) = self.description {
let desc_id = Box::new(Text::new(&desc).font_size(11.0)).mount_box(ctx);
ctx.arena.add_child(container_id, desc_id);
}
container_id
}
}
pub struct Form {
fields: Vec<Field>,
on_submit: Option<Rc<dyn Fn()>>,
form_id: Option<ElementId>,
style: StyleRefinement,
}
impl Form {
pub fn new() -> Self {
Self {
fields: Vec::new(),
on_submit: None,
form_id: None,
style: StyleRefinement::default(),
}
}
pub fn child(mut self, field: Field) -> Self {
self.fields.push(field);
self
}
pub fn on_submit(mut self, f: impl Fn() + 'static) -> Self {
self.on_submit = Some(Rc::new(f));
self
}
pub fn submit(&self) -> bool {
if validate_form(self.form_id.expect("Form not mounted")) {
if let Some(ref cb) = self.on_submit {
cb();
}
true
} else {
false
}
}
pub fn reset(&self) {
if let Some(fid) = self.form_id {
reset_form_validators(fid);
}
}
}
impl Default for Form {
fn default() -> Self {
Self::new()
}
}
impl Styled for Form {
fn style_refinement(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Widget for Form {
fn component_mask(&self) -> u64 {
components::STYLE | components::LAYOUT
}
fn mount_box(mut self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
let id = ctx.arena.allocate();
ctx.preallocate(id, self.component_mask());
self.form_id = Some(id);
form_domain().active_form.replace(Some(id));
let mut stack = VStack::new().gap(12.0);
for field in self.fields {
stack = stack.push(field);
}
let stack_id = Box::new(stack).mount_box(ctx);
ctx.arena.add_child(id, stack_id);
*form_domain().active_form.borrow_mut() = None;
id
}
}
impl std::fmt::Debug for Form {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Form")
.field("fields", &self.fields.len())
.finish_non_exhaustive()
}
}
impl std::fmt::Debug for Field {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Field").finish_non_exhaustive()
}
}