Merge pull request #8544 from dotCooCoo/fix/bounded-field-rejects-invalid

Fields: range and mixed fields refuse text that reads as no number
This commit is contained in:
BFlat
2026-11-18 15:10:13 +07:00
committed by GitHub
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() {