Skip to content

Commit 2793de5

Browse files
mwcampbellemilk
andcommitted
Fix keyboard support in DragValue (#2342)
* Enable incrementing and decrementing `DragValue` with the keyboard * As soon as a DragValue is focused, render it in edit mode * Simpler, more reliable approach to managing the drag value's edit string * Add changelog entry * Update doc comment Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Add comment explaining why we don't listen for left/right arrow Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
1 parent e8dab33 commit 2793de5

File tree

4 files changed

+55
-33
lines changed

4 files changed

+55
-33
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
3333
* Improved text rendering ([#2071](https://github.com/emilk/egui/pull/2071)).
3434
* Less jitter when calling `Context::set_pixels_per_point` ([#2239](https://github.com/emilk/egui/pull/2239)).
3535
* Fixed popups and color edit going outside the screen.
36+
* Fixed keyboard support in `DragValue`. ([#2342](https://github.com/emilk/egui/pull/2342)).
3637

3738

3839
## 0.19.0 - 2022-08-20

crates/egui/src/input_state.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ impl InputState {
245245
self.pointer.wants_repaint() || self.scroll_delta != Vec2::ZERO || !self.events.is_empty()
246246
}
247247

248-
/// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
249-
pub fn consume_key(&mut self, modifiers: Modifiers, key: Key) -> bool {
250-
let mut match_found = false;
248+
/// Count presses of a key. If non-zero, the presses are consumed, so that this will only return non-zero once.
249+
pub fn count_and_consume_key(&mut self, modifiers: Modifiers, key: Key) -> usize {
250+
let mut count = 0usize;
251251

252252
self.events.retain(|event| {
253253
let is_match = matches!(
@@ -259,12 +259,17 @@ impl InputState {
259259
} if *ev_key == key && ev_mods.matches(modifiers)
260260
);
261261

262-
match_found |= is_match;
262+
count += is_match as usize;
263263

264264
!is_match
265265
});
266266

267-
match_found
267+
count
268+
}
269+
270+
/// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
271+
pub fn consume_key(&mut self, modifiers: Modifiers, key: Key) -> bool {
272+
self.count_and_consume_key(modifiers, key) > 0
268273
}
269274

270275
/// Check if the given shortcut has been pressed.

crates/egui/src/memory.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,13 @@ impl Memory {
400400

401401
/// Register this widget as being interested in getting keyboard focus.
402402
/// This will allow the user to select it with tab and shift-tab.
403+
/// This is normally done automatically when handling interactions,
404+
/// but it is sometimes useful to pre-register interest in focus,
405+
/// e.g. before deciding which type of underlying widget to use,
406+
/// as in the [`crate::DragValue`] widget, so a widget can be focused
407+
/// and rendered correctly in a single frame.
403408
#[inline(always)]
404-
pub(crate) fn interested_in_focus(&mut self, id: Id) {
409+
pub fn interested_in_focus(&mut self, id: Id) {
405410
self.interaction.focus.interested_in_focus(id);
406411
}
407412

crates/egui/src/widgets/drag_value.rs

+38-27
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,49 @@ impl<'a> Widget for DragValue<'a> {
371371
let shift = ui.input().modifiers.shift_only();
372372
let is_slow_speed = shift && ui.memory().is_being_dragged(ui.next_auto_id());
373373

374+
let kb_edit_id = ui.next_auto_id();
375+
// The following call ensures that when a `DragValue` receives focus,
376+
// it is immediately rendered in edit mode, rather than being rendered
377+
// in button mode for just one frame. This is important for
378+
// screen readers.
379+
ui.memory().interested_in_focus(kb_edit_id);
380+
let is_kb_editing = ui.memory().has_focus(kb_edit_id);
381+
374382
let old_value = get(&mut get_set_value);
375-
let value = clamp_to_range(old_value, clamp_range.clone());
376-
if old_value != value {
377-
set(&mut get_set_value, value);
378-
}
383+
let mut value = old_value;
379384
let aim_rad = ui.input().aim_radius() as f64;
380385

381386
let auto_decimals = (aim_rad / speed.abs()).log10().ceil().clamp(0.0, 15.0) as usize;
382387
let auto_decimals = auto_decimals + is_slow_speed as usize;
383-
384388
let max_decimals = max_decimals.unwrap_or(auto_decimals + 2);
385389
let auto_decimals = auto_decimals.clamp(min_decimals, max_decimals);
390+
391+
if is_kb_editing {
392+
let mut input = ui.input_mut();
393+
// This deliberately doesn't listen for left and right arrow keys,
394+
// because when editing, these are used to move the caret.
395+
// This behavior is consistent with other editable spinner/stepper
396+
// implementations, such as Chromium's (for HTML5 number input).
397+
// It is also normal for such controls to go directly into edit mode
398+
// when they receive keyboard focus, and some screen readers
399+
// assume this behavior, so having a separate mode for incrementing
400+
// and decrementing, that supports all arrow keys, would be
401+
// problematic.
402+
let change = input.count_and_consume_key(Modifiers::NONE, Key::ArrowUp) as f64
403+
- input.count_and_consume_key(Modifiers::NONE, Key::ArrowDown) as f64;
404+
405+
if change != 0.0 {
406+
value += speed * change;
407+
value = emath::round_to_decimals(value, auto_decimals);
408+
}
409+
}
410+
411+
value = clamp_to_range(value, clamp_range.clone());
412+
if old_value != value {
413+
set(&mut get_set_value, value);
414+
ui.memory().drag_value.edit_string = None;
415+
}
416+
386417
let value_text = match custom_formatter {
387418
Some(custom_formatter) => custom_formatter(value, auto_decimals..=max_decimals),
388419
None => {
@@ -394,9 +425,6 @@ impl<'a> Widget for DragValue<'a> {
394425
}
395426
};
396427

397-
let kb_edit_id = ui.next_auto_id();
398-
let is_kb_editing = ui.memory().has_focus(kb_edit_id);
399-
400428
let mut response = if is_kb_editing {
401429
let button_width = ui.spacing().interact_size.x;
402430
let mut value_text = ui
@@ -419,14 +447,10 @@ impl<'a> Widget for DragValue<'a> {
419447
let parsed_value = clamp_to_range(parsed_value, clamp_range);
420448
set(&mut get_set_value, parsed_value);
421449
}
422-
if ui.input().key_pressed(Key::Enter) {
423-
ui.memory().surrender_focus(kb_edit_id);
424-
ui.memory().drag_value.edit_string = None;
425-
} else {
426-
ui.memory().drag_value.edit_string = Some(value_text);
427-
}
450+
ui.memory().drag_value.edit_string = Some(value_text);
428451
response
429452
} else {
453+
ui.memory().drag_value.edit_string = None;
430454
let button = Button::new(
431455
RichText::new(format!("{}{}{}", prefix, value_text, suffix)).monospace(),
432456
)
@@ -448,7 +472,6 @@ impl<'a> Widget for DragValue<'a> {
448472

449473
if response.clicked() {
450474
ui.memory().request_focus(kb_edit_id);
451-
ui.memory().drag_value.edit_string = None; // Filled in next frame
452475
} else if response.dragged() {
453476
ui.output().cursor_icon = CursorIcon::ResizeHorizontal;
454477

@@ -483,18 +506,6 @@ impl<'a> Widget for DragValue<'a> {
483506
drag_state.last_dragged_value = Some(stored_value);
484507
ui.memory().drag_value = drag_state;
485508
}
486-
} else if response.has_focus() {
487-
let change = ui.input().num_presses(Key::ArrowUp) as f64
488-
+ ui.input().num_presses(Key::ArrowRight) as f64
489-
- ui.input().num_presses(Key::ArrowDown) as f64
490-
- ui.input().num_presses(Key::ArrowLeft) as f64;
491-
492-
if change != 0.0 {
493-
let new_value = value + speed * change;
494-
let new_value = emath::round_to_decimals(new_value, auto_decimals);
495-
let new_value = clamp_to_range(new_value, clamp_range);
496-
set(&mut get_set_value, new_value);
497-
}
498509
}
499510

500511
response

0 commit comments

Comments
 (0)