mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Don't revert external changes to a focused DragValue (#8403)
* Closes <https://github.com/emilk/egui/issues/8339> * [x] I have followed the instructions in the PR template ## The bug While a `DragValue` has focus it is rendered as a `TextEdit`, and the text being edited is stored in `Memory::data` between frames. That is needed so that half-finished input such as `"1."` or `"-"` isn't thrown away just because it doesn't parse to the current value. The stored text was only discarded when the widget *gained* focus or when the widget itself changed the value. If something else changed the value while the `DragValue` was focused, the stored text was kept, shown to the user, and written back to the value when focus was lost — silently undoing the external change: ```rust ui.add(egui::DragValue::new(&mut self.value)); if ui.button("increment").clicked() { self.value += 1; } ``` Click into the `DragValue` so it has focus, then press "increment": the value goes up for one frame and then snaps back. `Slider` shows the same behaviour, since it uses a `DragValue` for its value field. ## The fix Store the value the text belongs to next to the text, and discard the text when the value no longer matches it. The remembered value is read back from the get/set closure *after* the widget has applied its own edits, so a change the widget made itself never looks like an external one — this matters for values that can't represent what was typed, e.g. `"12.5"` in a `DragValue<i32>`. This keeps the reason the text is stored in the first place intact: as long as nothing else touches the value, the text the user is typing is preserved verbatim. ## Tests Three tests in `crates/egui_kittest/tests/regression_tests.rs`: * `drag_value_should_not_revert_external_changes_while_focused` — the actual regression. Fails on `main`: ``` ---- drag_value_should_not_revert_external_changes_while_focused stdout ---- assertion `left == right` failed left: Some("0") right: Some("42") ``` and, with the display assertion removed so the test reaches the blur, on the value itself: ``` assertion `left == right` failed left: 0 right: 42 ``` * `drag_value_should_keep_text_while_typing` and `drag_value_should_keep_text_the_value_cannot_represent` — guards for the behaviour the stored text exists for. Both pass on `main` and after the fix, and both fail if the text is re-read from the value too eagerly. `cargo test -p egui_kittest` and `cargo test -p egui` pass, as do `cargo fmt --all --check`, `scripts/lint.py` and `cargo clippy -p egui -p egui_kittest --all-targets --all-features -- -D warnings`. ## Not changed `DragValue` still ignores the stored text when <kbd>Escape</kbd> is pressed, and `update_while_editing` still decides when typed text is applied — neither is touched here.
This commit is contained in:
@@ -814,3 +814,100 @@ pub fn textedit_hint_text_should_follow_text_alignment() {
|
||||
edit_center_x={edit_center_x}, edit_rect={edit_rect:?}",
|
||||
);
|
||||
}
|
||||
|
||||
/// A focused `DragValue` keeps the text the user is editing in memory.
|
||||
///
|
||||
/// If something else changes the value while the `DragValue` has focus,
|
||||
/// that memorized text is stale, and must not be written back to the value.
|
||||
///
|
||||
/// Regression test for <https://github.com/emilk/egui/issues/8339>.
|
||||
#[test]
|
||||
pub fn drag_value_should_not_revert_external_changes_while_focused() {
|
||||
let mut harness = Harness::new_ui_state(
|
||||
|ui, value: &mut i32| {
|
||||
ui.add(egui::DragValue::new(value));
|
||||
},
|
||||
0,
|
||||
);
|
||||
|
||||
// Focus the `DragValue`, putting it in text-edit mode.
|
||||
harness.key_press(egui::Key::Tab);
|
||||
harness.run();
|
||||
|
||||
// Something else changes the value while the `DragValue` is focused.
|
||||
*harness.state_mut() = 42;
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), &42);
|
||||
let drag_value = harness.get_by_role(accesskit::Role::SpinButton);
|
||||
assert_eq!(drag_value.value(), Some("42".to_owned()));
|
||||
|
||||
// Losing focus must not restore the value the `DragValue` had when it gained focus.
|
||||
harness.key_press(egui::Key::Tab);
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), &42);
|
||||
}
|
||||
|
||||
/// While the user is typing into a `DragValue`, the half-finished text must be kept
|
||||
/// between frames, even though it doesn't always parse back to the same text.
|
||||
#[test]
|
||||
pub fn drag_value_should_keep_text_while_typing() {
|
||||
let mut harness = Harness::new_ui_state(
|
||||
|ui, value: &mut f64| {
|
||||
ui.add(egui::DragValue::new(value));
|
||||
},
|
||||
0.0,
|
||||
);
|
||||
|
||||
// Focus the `DragValue`, putting it in text-edit mode with the old text selected.
|
||||
harness.key_press(egui::Key::Tab);
|
||||
harness.run();
|
||||
|
||||
// Type one character per frame. `"1."` parses to `1`, which is formatted as `"1"`,
|
||||
// so re-reading the text from the value would eat the decimal point.
|
||||
for character in "1.25".chars() {
|
||||
harness
|
||||
.get_by_role(accesskit::Role::SpinButton)
|
||||
.type_text(&character.to_string());
|
||||
harness.run();
|
||||
}
|
||||
|
||||
harness.key_press(egui::Key::Enter);
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), &1.25);
|
||||
}
|
||||
|
||||
/// An integer `DragValue` cannot represent everything the user types into it,
|
||||
/// but the text must still survive until the user is done typing.
|
||||
#[test]
|
||||
pub fn drag_value_should_keep_text_the_value_cannot_represent() {
|
||||
let mut harness = Harness::new_ui_state(
|
||||
|ui, value: &mut i32| {
|
||||
ui.add(egui::DragValue::new(value));
|
||||
},
|
||||
0,
|
||||
);
|
||||
|
||||
// Focus the `DragValue`, putting it in text-edit mode with the old text selected.
|
||||
harness.key_press(egui::Key::Tab);
|
||||
harness.run();
|
||||
|
||||
// `"12.5"` is stored as `12`, which is formatted as `"12"`.
|
||||
harness
|
||||
.get_by_role(accesskit::Role::SpinButton)
|
||||
.type_text("12.5");
|
||||
harness.run();
|
||||
|
||||
// If the text was re-read from the value now, this would append to `"12"`.
|
||||
harness
|
||||
.get_by_role(accesskit::Role::SpinButton)
|
||||
.type_text("9");
|
||||
harness.run();
|
||||
|
||||
harness.key_press(egui::Key::Enter);
|
||||
harness.run();
|
||||
|
||||
assert_eq!(harness.state(), &12, "The text should have been \"12.59\"");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user