|
3 | 3 | use std::{cmp::Ordering, ops::RangeInclusive};
|
4 | 4 |
|
5 | 5 | 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, |
7 | 7 | TextEdit, TextWrapMode, Ui, Widget, WidgetInfo, MINUS_CHAR_STR,
|
8 | 8 | };
|
9 | 9 |
|
@@ -569,6 +569,11 @@ impl Widget for DragValue<'_> {
|
569 | 569 | .font(text_style),
|
570 | 570 | );
|
571 | 571 |
|
| 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 | + |
572 | 577 | let update = if update_while_editing {
|
573 | 578 | // Update when the edit content has changed.
|
574 | 579 | response.changed()
|
@@ -623,12 +628,7 @@ impl Widget for DragValue<'_> {
|
623 | 628 | if response.clicked() {
|
624 | 629 | ui.data_mut(|data| data.remove::<String>(id));
|
625 | 630 | 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); |
632 | 632 | } else if response.dragged() {
|
633 | 633 | ui.ctx().set_cursor_icon(cursor_icon);
|
634 | 634 |
|
@@ -759,6 +759,16 @@ pub(crate) fn clamp_value_to_range(x: f64, range: RangeInclusive<f64>) -> f64 {
|
759 | 759 | }
|
760 | 760 | }
|
761 | 761 |
|
| 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 | + |
762 | 772 | #[cfg(test)]
|
763 | 773 | mod tests {
|
764 | 774 | use super::clamp_value_to_range;
|
|
0 commit comments