mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-24 09:46:59 -06:00
Fix numeric fields reverting a typed value when clicking away instead of pressing Enter (#1637)
This commit is contained in:
@@ -408,3 +408,41 @@ fn keyboard_focus_keeps_selecting_the_entire_numeric_field() {
|
||||
assert_eq!(f.selection(), Some((0, field_text(mode).chars().count())), "mode {mode}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #1637: text typed into a field is committed when the focus leaves it by a click on empty space
|
||||
/// or by a press on a field drawn before it (another channel of the Color panel), whether the
|
||||
/// window reports keyboard focus or not, as Enter commits it. Escape still drops it.
|
||||
#[test]
|
||||
fn typed_text_is_committed_when_the_focus_moves_away() {
|
||||
for focused in [true, false] {
|
||||
for onto_field_above in [false, true] {
|
||||
let mut f = Field::new();
|
||||
f.focused = focused;
|
||||
let above = Cell::new(Rect::NOTHING);
|
||||
let draw = |ui: &mut egui::Ui| {
|
||||
widgets::range_field(ui, "above", 50.0, 0.0..=1000.0, "%", 0, 80.0);
|
||||
above.set(ui.min_rect());
|
||||
widgets::range_field(ui, "f", 100.0, 0.0..=1000.0, "%", 0, 80.0)
|
||||
};
|
||||
f.frame(vec![], &draw);
|
||||
let field = Pos2::new(above.get().center().x, (above.get().bottom() + f.rect.get().bottom()) / 2.0);
|
||||
let away = if onto_field_above { above.get().center() } else { Pos2::new(field.x, 180.0) };
|
||||
let case = format!("window focus {focused}, onto the field above {onto_field_above}");
|
||||
f.frame(vec![Event::PointerMoved(field), pointer_button(field, true)], &draw);
|
||||
f.frame(vec![pointer_button(field, false)], &draw);
|
||||
f.frame(vec![Event::Text("42".into())], &draw);
|
||||
let committed = [
|
||||
f.frame(vec![Event::PointerMoved(away), pointer_button(away, true)], &draw),
|
||||
f.frame(vec![pointer_button(away, false)], &draw),
|
||||
f.frame(vec![], &draw),
|
||||
];
|
||||
assert_eq!(committed.iter().flatten().copied().collect::<Vec<_>>(), [42.0], "{case}");
|
||||
|
||||
f.frame(vec![Event::PointerMoved(field), pointer_button(field, true)], &draw);
|
||||
f.frame(vec![pointer_button(field, false)], &draw);
|
||||
f.frame(vec![Event::Text("7".into())], &draw);
|
||||
let escaped = [f.frame(vec![key(Key::Escape, Modifiers::NONE)], &draw), f.frame(vec![], &draw)];
|
||||
assert_eq!(escaped, [None, None], "{case}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,7 +354,12 @@ fn recessed_text(ui: &mut Ui, id: egui::Id, shown: &str, width: f32, rows: usize
|
||||
let shown_id = id.with("shown");
|
||||
let followed = ui.data_mut(|d| d.get_temp::<String>(shown_id)).is_none_or(|before| before == shown);
|
||||
ui.data_mut(|d| d.insert_temp(shown_id, shown.to_string()));
|
||||
let typed = if editing && followed { ui.data_mut(|d| d.get_temp::<String>(id)) } else { None };
|
||||
// A press elsewhere can end the focus outside this field's own drawing (a field drawn before it
|
||||
// takes it, the canvas ends text input), so `editing` is already false on the frame it reports
|
||||
// `lost_focus`: the text typed is still taken then, to be committed (#1637). Escape drops it.
|
||||
let focus_id = id.with("had-focus");
|
||||
let had_focus = ui.data_mut(|d| d.get_temp::<bool>(focus_id)).unwrap_or(false) && !ui.input(|i| i.key_pressed(egui::Key::Escape));
|
||||
let typed = if (editing || had_focus) && followed { ui.data_mut(|d| d.get_temp::<String>(id)) } else { None };
|
||||
let mut buf: String = typed.unwrap_or_else(|| shown.to_string());
|
||||
// The box is exactly as tall as the other controls of a row (26 points a line), its text
|
||||
// centred in it: a frame around the text came out a point taller and stood out next to
|
||||
@@ -385,7 +390,11 @@ fn recessed_text(ui: &mut Ui, id: egui::Id, shown: &str, width: f32, rows: usize
|
||||
(rect, resp)
|
||||
})
|
||||
.inner;
|
||||
ui.data_mut(|d| d.insert_temp(id, buf.clone()));
|
||||
let focused = ui.memory(|m| m.has_focus(id));
|
||||
ui.data_mut(|d| {
|
||||
d.insert_temp(id, buf.clone());
|
||||
d.insert_temp(focus_id, focused);
|
||||
});
|
||||
(buf, resp, rect)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user