Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clamp_to_range_on_drag option to DragValue and clamp_to_range_on_value_drag option to Slider #4882

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/egui/src/widgets/drag_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ impl<'a> DragValue<'a> {
/// If set to `true`, all incoming and outgoing values will be clamped to the sliding [`Self::range`] (if any).
///
/// If set to `false`, a value outside of the range that is set programmatically or by user input will not be changed.
/// Dragging will be restricted to the range regardless of this setting.
/// Default: `true`.
#[inline]
pub fn clamp_to_range(mut self, clamp_to_range: bool) -> Self {
Expand Down Expand Up @@ -608,8 +607,11 @@ impl<'a> Widget for DragValue<'a> {
);
let rounded_new_value =
emath::round_to_decimals(rounded_new_value, auto_decimals);
// Dragging will always clamp the value to the range.
let rounded_new_value = clamp_value_to_range(rounded_new_value, range.clone());
let rounded_new_value = if clamp_to_range {
clamp_value_to_range(rounded_new_value, range.clone())
} else {
rounded_new_value
};
set(&mut get_set_value, rounded_new_value);

ui.data_mut(|data| data.insert_temp::<f64>(id, precise_value));
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/sliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl crate::View for Sliders {

ui.checkbox(clamp_to_range, "Clamp to range");
ui.label("If true, the slider will clamp incoming and outgoing values to the given range.");
ui.label("If false, the slider can shows values outside its range, and you can manually enter values outside the range.");
ui.label("If false, the slider can show values outside its range, and you can manually enter values outside the range.");
ui.add_space(8.0);

ui.checkbox(smart_aim, "Smart Aim");
Expand Down
Loading