Skip to content

Commit 96c3413

Browse files
Azkellasemilk
andauthored
Select all text in DragValue when gaining focus via keyboard (#7107)
Previously, the `DragValue` widget selected all text when focus was gained via a mouse click, but didn't when focus was gained via keyboard. https://github.com/user-attachments/assets/5e82ca2c-0214-4201-ad2d-056dabc05e92 This PR makes both gained focus behaving the same way by selecting the text on focus gained via keyboard. https://github.com/user-attachments/assets/f246c779-3368-428c-a6b2-cec20dbc20a6 - [x] I have followed the instructions in the PR template Co-authored-by: Emil Ernerfeldt <[email protected]>
1 parent 54fded3 commit 96c3413

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

crates/egui/src/widgets/drag_value.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{cmp::Ordering, ops::RangeInclusive};
44

55
use crate::{
6-
emath, text, Button, CursorIcon, Key, Modifiers, NumExt as _, Response, RichText, Sense,
6+
emath, text, Button, CursorIcon, Id, Key, Modifiers, NumExt as _, Response, RichText, Sense,
77
TextEdit, TextWrapMode, Ui, Widget, WidgetInfo, MINUS_CHAR_STR,
88
};
99

@@ -569,6 +569,11 @@ impl Widget for DragValue<'_> {
569569
.font(text_style),
570570
);
571571

572+
// Select all text when the edit gains focus.
573+
if ui.memory_mut(|mem| mem.gained_focus(id)) {
574+
select_all_text(ui, id, response.id, &value_text);
575+
}
576+
572577
let update = if update_while_editing {
573578
// Update when the edit content has changed.
574579
response.changed()
@@ -623,12 +628,7 @@ impl Widget for DragValue<'_> {
623628
if response.clicked() {
624629
ui.data_mut(|data| data.remove::<String>(id));
625630
ui.memory_mut(|mem| mem.request_focus(id));
626-
let mut state = TextEdit::load_state(ui.ctx(), id).unwrap_or_default();
627-
state.cursor.set_char_range(Some(text::CCursorRange::two(
628-
text::CCursor::default(),
629-
text::CCursor::new(value_text.chars().count()),
630-
)));
631-
state.store(ui.ctx(), response.id);
631+
select_all_text(ui, id, response.id, &value_text);
632632
} else if response.dragged() {
633633
ui.ctx().set_cursor_icon(cursor_icon);
634634

@@ -759,6 +759,16 @@ pub(crate) fn clamp_value_to_range(x: f64, range: RangeInclusive<f64>) -> f64 {
759759
}
760760
}
761761

762+
/// Select all text in the `DragValue` text edit widget.
763+
fn select_all_text(ui: &Ui, widget_id: Id, response_id: Id, value_text: &str) {
764+
let mut state = TextEdit::load_state(ui.ctx(), widget_id).unwrap_or_default();
765+
state.cursor.set_char_range(Some(text::CCursorRange::two(
766+
text::CCursor::default(),
767+
text::CCursor::new(value_text.chars().count()),
768+
)));
769+
state.store(ui.ctx(), response_id);
770+
}
771+
762772
#[cfg(test)]
763773
mod tests {
764774
use super::clamp_value_to_range;

0 commit comments

Comments
 (0)