From 826bc898efdcbe604158725880c1ef9385a618e3 Mon Sep 17 00:00:00 2001 From: Harrison MG Date: Tue, 7 Feb 2023 15:04:30 -0500 Subject: [PATCH 1/5] test --- crates/egui/src/widgets/drag_value.rs | 32 +++++++++------------------ 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 59507be2690..8fa65ba99f4 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -398,22 +398,8 @@ impl<'a> Widget for DragValue<'a> { let change = ui.input_mut(|input| { let mut change = 0.0; - if is_kb_editing { - // This deliberately doesn't listen for left and right arrow keys, - // because when editing, these are used to move the caret. - // This behavior is consistent with other editable spinner/stepper - // implementations, such as Chromium's (for HTML5 number input). - // It is also normal for such controls to go directly into edit mode - // when they receive keyboard focus, and some screen readers - // assume this behavior, so having a separate mode for incrementing - // and decrementing, that supports all arrow keys, would be - // problematic. - change += input.count_and_consume_key(Modifiers::NONE, Key::ArrowUp) as f64 - - input.count_and_consume_key(Modifiers::NONE, Key::ArrowDown) as f64; - } - #[cfg(feature = "accesskit")] - { + if !is_kb_editing { use accesskit::Action; change += input.num_accesskit_action_requests(id, Action::Increment) as f64 - input.num_accesskit_action_requests(id, Action::Decrement) as f64; @@ -475,13 +461,15 @@ impl<'a> Widget for DragValue<'a> { .desired_width(ui.spacing().interact_size.x) .font(text_style), ); - let parsed_value = match custom_parser { - Some(parser) => parser(&value_text), - None => value_text.parse().ok(), - }; - if let Some(parsed_value) = parsed_value { - let parsed_value = clamp_to_range(parsed_value, clamp_range.clone()); - set(&mut get_set_value, parsed_value); + if response.lost_focus() { + let parsed_value = match custom_parser { + Some(parser) => parser(&value_text), + None => value_text.parse().ok(), + }; + if let Some(parsed_value) = parsed_value { + let parsed_value = clamp_to_range(parsed_value, clamp_range.clone()); + set(&mut get_set_value, parsed_value); + } } ui.memory_mut(|mem| mem.drag_value.edit_string = Some(value_text)); response From 29b6308fff655b57a294d9778f5cd72e04b0dc7c Mon Sep 17 00:00:00 2001 From: Harrison MG Date: Tue, 7 Feb 2023 15:40:11 -0500 Subject: [PATCH 2/5] moved some accesskit stuff --- crates/egui/src/widgets/drag_value.rs | 33 +++++++++++++-------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 8fa65ba99f4..1461f191bd6 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -395,22 +395,21 @@ impl<'a> Widget for DragValue<'a> { let max_decimals = max_decimals.unwrap_or(auto_decimals + 2); let auto_decimals = auto_decimals.clamp(min_decimals, max_decimals); - let change = ui.input_mut(|input| { - let mut change = 0.0; - - #[cfg(feature = "accesskit")] - if !is_kb_editing { - use accesskit::Action; - change += input.num_accesskit_action_requests(id, Action::Increment) as f64 - - input.num_accesskit_action_requests(id, Action::Decrement) as f64; - } - - change - }); - #[cfg(feature = "accesskit")] { use accesskit::{Action, ActionData}; + + let change = ui.input_mut(|input| { + let mut change = 0.0; + + if !is_kb_editing { + change += input.num_accesskit_action_requests(id, Action::Increment) as f64 + - input.num_accesskit_action_requests(id, Action::Decrement) as f64; + } + + change + }); + ui.input(|input| { for request in input.accesskit_action_requests(id, Action::SetValue) { if let Some(ActionData::NumericValue(new_value)) = request.data { @@ -418,11 +417,11 @@ impl<'a> Widget for DragValue<'a> { } } }); - } - if change != 0.0 { - value += speed * change; - value = emath::round_to_decimals(value, auto_decimals); + if change != 0.0 { + value += speed * change; + value = emath::round_to_decimals(value, auto_decimals); + } } value = clamp_to_range(value, clamp_range.clone()); From 92be531c252a6fc64499019b42a1b5e90d0efe01 Mon Sep 17 00:00:00 2001 From: Harrison MG Date: Tue, 7 Feb 2023 16:59:04 -0500 Subject: [PATCH 3/5] reverted accesskit change --- crates/egui/src/widgets/drag_value.rs | 47 ++++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 1461f191bd6..4aca282c14b 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -395,21 +395,36 @@ impl<'a> Widget for DragValue<'a> { let max_decimals = max_decimals.unwrap_or(auto_decimals + 2); let auto_decimals = auto_decimals.clamp(min_decimals, max_decimals); - #[cfg(feature = "accesskit")] - { - use accesskit::{Action, ActionData}; - - let change = ui.input_mut(|input| { - let mut change = 0.0; + let change = ui.input_mut(|input| { + let mut change = 0.0; + + if is_kb_editing { + // This deliberately doesn't listen for left and right arrow keys, + // because when editing, these are used to move the caret. + // This behavior is consistent with other editable spinner/stepper + // implementations, such as Chromium's (for HTML5 number input). + // It is also normal for such controls to go directly into edit mode + // when they receive keyboard focus, and some screen readers + // assume this behavior, so having a separate mode for incrementing + // and decrementing, that supports all arrow keys, would be + // problematic. + change += input.count_and_consume_key(Modifiers::NONE, Key::ArrowUp) as f64 + - input.count_and_consume_key(Modifiers::NONE, Key::ArrowDown) as f64; + } - if !is_kb_editing { - change += input.num_accesskit_action_requests(id, Action::Increment) as f64 - - input.num_accesskit_action_requests(id, Action::Decrement) as f64; - } + #[cfg(feature = "accesskit")] + { + use accesskit::Action; + change += input.num_accesskit_action_requests(id, Action::Increment) as f64 + - input.num_accesskit_action_requests(id, Action::Decrement) as f64; + } - change - }); + change + }); + #[cfg(feature = "accesskit")] + { + use accesskit::{Action, ActionData}; ui.input(|input| { for request in input.accesskit_action_requests(id, Action::SetValue) { if let Some(ActionData::NumericValue(new_value)) = request.data { @@ -417,11 +432,11 @@ impl<'a> Widget for DragValue<'a> { } } }); + } - if change != 0.0 { - value += speed * change; - value = emath::round_to_decimals(value, auto_decimals); - } + if change != 0.0 { + value += speed * change; + value = emath::round_to_decimals(value, auto_decimals); } value = clamp_to_range(value, clamp_range.clone()); From cce435d62c5dfcf87881725504323c249336a0ab Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 8 Feb 2023 08:11:05 +0100 Subject: [PATCH 4/5] Add explanatory comment --- crates/egui/src/widgets/drag_value.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 4aca282c14b..06f9aacadc2 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -475,6 +475,8 @@ impl<'a> Widget for DragValue<'a> { .desired_width(ui.spacing().interact_size.x) .font(text_style), ); + // Only update the value when the user presses enter, or clicks elsewhere. NOT every frame. + // See https://github.com/emilk/egui/issues/2687 if response.lost_focus() { let parsed_value = match custom_parser { Some(parser) => parser(&value_text), From 2b0b278aabf0841fc081c384b3cf9edaad7c1c8e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 8 Feb 2023 09:18:55 +0100 Subject: [PATCH 5/5] fmt --- crates/egui/src/widgets/drag_value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/drag_value.rs b/crates/egui/src/widgets/drag_value.rs index 06f9aacadc2..0d33978f2a6 100644 --- a/crates/egui/src/widgets/drag_value.rs +++ b/crates/egui/src/widgets/drag_value.rs @@ -476,7 +476,7 @@ impl<'a> Widget for DragValue<'a> { .font(text_style), ); // Only update the value when the user presses enter, or clicks elsewhere. NOT every frame. - // See https://github.com/emilk/egui/issues/2687 + // See https://github.com/emilk/egui/issues/2687 if response.lost_focus() { let parsed_value = match custom_parser { Some(parser) => parser(&value_text),