Fields: range and mixed fields refuse text that reads as no number

A range, mixed or spinner field that committed text it could not read
(`abc`, `(10+5)*2`) returned nothing and did not mark the commit as
refused. Enter in such a field then confirmed an open dialog with the old
value, and in Save for Web's Optimize to File Size rows it ran the search
with the old size.

The field now refuses that text as unit fields do: it flashes, shows its
value again, and the Enter does not confirm the dialog.
This commit is contained in:
dotCooCoo
2026-11-17 22:49:09 -08:00
parent 50c7208fe3
commit c24f1fc5a5
2 changed files with 38 additions and 1 deletions
+25
View File
@@ -524,3 +524,28 @@ fn kerning_reads_parentheses_around_its_text_with_a_manual_value_too() {
assert_eq!(typed(automatic, "(1,5)"), Some(SpinPick::Value(1.5)), "a decimal comma, automatic: {automatic}");
}
}
/// Enter on text that reads as no number in a range field refuses it as a unit field does: the
/// field flashes and an open dialog is not confirmed with the old value.
#[test]
fn a_range_field_refuses_text_that_reads_as_no_number() {
let typed = |text: &str| {
let ctx = themed(Default::default());
let picked = std::cell::Cell::new(None);
let mut add = |ui: &mut Ui| {
if let Some(v) = range_field(ui, "count", 12.0, 0.0..=100.0, "", 0, 80.0) {
picked.set(Some(v));
}
};
arm_field_focus(&ctx, "count");
frame(&ctx, 0.0, vec![], &mut add);
frame(&ctx, 0.1, vec![], &mut add);
let enter = |pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
frame(&ctx, 0.2, vec![egui::Event::Text(text.into()), enter(true), enter(false)], &mut add);
let refused = ctx.data_mut(|d| d.remove_temp::<bool>(dialog_reject_commit_flag())).unwrap_or(false);
(picked.get(), refused)
};
assert_eq!(typed("40"), (Some(40.0), false), "a number commits");
assert_eq!(typed("abc"), (None, true), "text that reads as no number is refused");
assert_eq!(typed("(10+5)*2"), (None, true), "parentheses the field doesn't read are refused");
}
+13 -1
View File
@@ -917,7 +917,19 @@ fn bounded_field_reading(
let stepped = step_value(ui, &resp, &mut buf, bounds, (read, show), step, &[]).map(|v| round_to(v, decimals));
let scrubbed = scrub::field(ui, id, rect, &buf, value, decimals, bounds);
if commit && buf != shown {
read(&buf)
let typed = read(&buf);
if typed.is_none() {
// Text that reads as no number is refused as [`unit_field_from`] refuses it: the field
// flashes, shows its value again, and an open dialog is not confirmed by this Enter.
let end = ui.input(|i| i.time + 0.25);
ui.data_mut(|d| {
d.remove::<String>(id);
d.insert_temp(id.with("invalid-flash"), end);
});
crate::widgets::reject_dialog_commit(ui.ctx());
ui.ctx().request_repaint();
}
typed
} else if let Some(v) = stepped.or(scrubbed).filter(|&v| Some(v) != value) {
Some(v)
} else if live && resp.changed() {