Skip to content

Commit

Permalink
DragValue: update value on each key press by default (#2880)
Browse files Browse the repository at this point in the history
* Use `Response::changed` to fix editing issues

* Update comment

* Make update while editing an option

* improve docstring

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
Barugon and emilk authored Aug 9, 2023
1 parent abe91b0 commit f9f9abf
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions crates/egui/src/widgets/drag_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct DragValue<'a> {
max_decimals: Option<usize>,
custom_formatter: Option<NumFormatter<'a>>,
custom_parser: Option<NumParser<'a>>,
update_while_editing: bool,
}

impl<'a> DragValue<'a> {
Expand Down Expand Up @@ -94,6 +95,7 @@ impl<'a> DragValue<'a> {
max_decimals: None,
custom_formatter: None,
custom_parser: None,
update_while_editing: true,
}
}

Expand Down Expand Up @@ -352,6 +354,15 @@ impl<'a> DragValue<'a> {
}
.custom_parser(|s| i64::from_str_radix(s, 16).map(|n| n as f64).ok())
}

/// Update the value on each key press when text-editing the value.
///
/// Default: `true`.
/// If `false`, the value will only be updated when user presses enter or deselects the value.
pub fn update_while_editing(mut self, update: bool) -> Self {
self.update_while_editing = update;
self
}
}

impl<'a> Widget for DragValue<'a> {
Expand All @@ -366,6 +377,7 @@ impl<'a> Widget for DragValue<'a> {
max_decimals,
custom_formatter,
custom_parser,
update_while_editing,
} = self;

let shift = ui.input(|i| i.modifiers.shift_only());
Expand Down Expand Up @@ -475,9 +487,15 @@ 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 update = if update_while_editing {
// Update when the edit content has changed.
response.changed()
} else {
// Update only when the edit has lost focus.
response.lost_focus()
};
if update {
let parsed_value = match custom_parser {
Some(parser) => parser(&value_text),
None => value_text.parse().ok(),
Expand Down

0 comments on commit f9f9abf

Please sign in to comment.