Fix disabled `DragValue` eating focus, causing focus to reset (#5826)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes https://github.com/emilk/egui/issues/5507
* [x] I have followed the instructions in the PR template
This commit is contained in:
Trương Hoàng Long 2025-03-20 10:45:16 +01:00 committed by GitHub
parent f408ccafbc
commit 6a8ee29a4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 4 deletions

View File

@ -451,10 +451,11 @@ impl Widget for DragValue<'_> {
// it is immediately rendered in edit mode, rather than being rendered
// in button mode for just one frame. This is important for
// screen readers.
let is_kb_editing = ui.memory_mut(|mem| {
mem.interested_in_focus(id, ui.layer_id());
mem.has_focus(id)
});
let is_kb_editing = ui.is_enabled()
&& ui.memory_mut(|mem| {
mem.interested_in_focus(id, ui.layer_id());
mem.has_focus(id)
});
if ui.memory_mut(|mem| mem.gained_focus(id)) {
ui.data_mut(|data| data.remove::<String>(id));

View File

@ -29,6 +29,31 @@ pub fn focus_should_skip_over_disabled_buttons() {
assert!(button_1.is_focused());
}
#[test]
pub fn focus_should_skip_over_disabled_drag_values() {
let mut value_1: u16 = 1;
let mut value_2: u16 = 2;
let mut value_3: u16 = 3;
let mut harness = Harness::new_ui(|ui| {
ui.add(egui::DragValue::new(&mut value_1));
ui.add_enabled(false, egui::DragValue::new(&mut value_2));
ui.add(egui::DragValue::new(&mut value_3));
});
harness.press_key(egui::Key::Tab);
harness.run();
let drag_value_1 = harness.get_by(|node| node.numeric_value() == Some(1.0));
assert!(drag_value_1.is_focused());
harness.press_key(egui::Key::Tab);
harness.run();
let drag_value_3 = harness.get_by(|node| node.numeric_value() == Some(3.0));
assert!(drag_value_3.is_focused());
}
#[test]
fn image_failed() {
let mut harness = Harness::new_ui(|ui| {