From 009bfe5adadb4a0edea623eb4b58feadd403ffa4 Mon Sep 17 00:00:00 2001 From: Marek Bernat Date: Tue, 22 Apr 2025 11:28:34 +0200 Subject: [PATCH] Add a `Slider::update_while_editing(bool)` API (#5978) * Closes #5976 * [x] I have followed the instructions in the PR template - The `scripts/check.sh` fails in `cargo-deny` because of a security vulnerability in `crossbeam-channel` but this is also present on `master`. https://github.com/user-attachments/assets/a964c968-bb76-4e56-88e1-d1e3d51a401a --- crates/egui/src/widgets/slider.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/slider.rs b/crates/egui/src/widgets/slider.rs index e8b026ff..0f7eb049 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -117,6 +117,7 @@ pub struct Slider<'a> { custom_parser: Option>, trailing_fill: Option, handle_shape: Option, + update_while_editing: bool, } impl<'a> Slider<'a> { @@ -167,6 +168,7 @@ impl<'a> Slider<'a> { custom_parser: None, trailing_fill: None, handle_shape: None, + update_while_editing: true, } } @@ -641,6 +643,16 @@ impl<'a> Slider<'a> { let normalized = normalized_from_value(value, self.range(), &self.spec); lerp(position_range, normalized as f32) } + + /// 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. + #[inline] + pub fn update_while_editing(mut self, update: bool) -> Self { + self.update_while_editing = update; + self + } } impl Slider<'_> { @@ -900,7 +912,8 @@ impl Slider<'_> { .min_decimals(self.min_decimals) .max_decimals_opt(self.max_decimals) .suffix(self.suffix.clone()) - .prefix(self.prefix.clone()); + .prefix(self.prefix.clone()) + .update_while_editing(self.update_while_editing); match self.clamping { SliderClamping::Never => {}