Skip to content

Commit

Permalink
Add clamp_to_range option to DragValue, rename clamp_range to `ra…
Browse files Browse the repository at this point in the history
…nge` (deprecating the former) (#4728)

Adds the ability to have `DragValue` not clamp values its presented with
and instead apply clamping only once there's any user input.

In action:


https://github.com/emilk/egui/assets/1220815/af42fd67-86d0-4364-8ae6-48a2ec15646a




Alternative name could be `only_clamp_on_change`, not entirely certain
which one is better 🤔
  • Loading branch information
Wumpf authored Jun 28, 2024
1 parent 3b9f964 commit 10f092d
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 117 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Widget for &mut epaint::TessellationOptions {
.on_hover_text("Apply feathering to smooth out the edges of shapes. Turn off for small performance gain.");

if *feathering {
ui.add(crate::DragValue::new(feathering_size_in_pixels).clamp_range(0.0..=10.0).speed(0.1).suffix(" px"));
ui.add(crate::DragValue::new(feathering_size_in_pixels).range(0.0..=10.0).speed(0.1).suffix(" px"));
}
});

Expand All @@ -165,7 +165,7 @@ impl Widget for &mut epaint::TessellationOptions {
ui.label("Spline tolerance");
let speed = 0.01 * *bezier_tolerance;
ui.add(
crate::DragValue::new(bezier_tolerance).clamp_range(0.0001..=10.0)
crate::DragValue::new(bezier_tolerance).range(0.0001..=10.0)
.speed(speed)
);
});
Expand Down
10 changes: 5 additions & 5 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,16 @@ impl Options {
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Line scroll speed");
ui.add(
crate::DragValue::new(line_scroll_speed).clamp_range(0.0..=f32::INFINITY),
)
.on_hover_text("How many lines to scroll with each tick of the mouse wheel");
ui.add(crate::DragValue::new(line_scroll_speed).range(0.0..=f32::INFINITY))
.on_hover_text(
"How many lines to scroll with each tick of the mouse wheel",
);
});
ui.horizontal(|ui| {
ui.label("Scroll zoom speed");
ui.add(
crate::DragValue::new(scroll_zoom_speed)
.clamp_range(0.0..=f32::INFINITY)
.range(0.0..=f32::INFINITY)
.speed(0.001),
)
.on_hover_text("How fast to zoom with ctrl/cmd + scroll");
Expand Down
84 changes: 40 additions & 44 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,26 +571,26 @@ impl ScrollStyle {
});

ui.horizontal(|ui| {
ui.add(DragValue::new(bar_width).clamp_range(0.0..=32.0));
ui.add(DragValue::new(bar_width).range(0.0..=32.0));
ui.label("Full bar width");
});
if *floating {
ui.horizontal(|ui| {
ui.add(DragValue::new(floating_width).clamp_range(0.0..=32.0));
ui.add(DragValue::new(floating_width).range(0.0..=32.0));
ui.label("Thin bar width");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(floating_allocated_width).clamp_range(0.0..=32.0));
ui.add(DragValue::new(floating_allocated_width).range(0.0..=32.0));
ui.label("Allocated width");
});
}

ui.horizontal(|ui| {
ui.add(DragValue::new(handle_min_length).clamp_range(0.0..=32.0));
ui.add(DragValue::new(handle_min_length).range(0.0..=32.0));
ui.label("Minimum handle length");
});
ui.horizontal(|ui| {
ui.add(DragValue::new(bar_outer_margin).clamp_range(0.0..=32.0));
ui.add(DragValue::new(bar_outer_margin).range(0.0..=32.0));
ui.label("Outer margin");
});

Expand All @@ -603,7 +603,7 @@ impl ScrollStyle {
if *floating {
crate::Grid::new("opacity").show(ui, |ui| {
fn opacity_ui(ui: &mut Ui, opacity: &mut f32) {
ui.add(DragValue::new(opacity).speed(0.01).clamp_range(0.0..=1.0));
ui.add(DragValue::new(opacity).speed(0.01).range(0.0..=1.0));
}

ui.label("Opacity");
Expand All @@ -626,7 +626,7 @@ impl ScrollStyle {
});
} else {
ui.horizontal(|ui| {
ui.add(DragValue::new(bar_inner_margin).clamp_range(0.0..=32.0));
ui.add(DragValue::new(bar_inner_margin).range(0.0..=32.0));
ui.label("Inner margin");
});
}
Expand Down Expand Up @@ -1418,7 +1418,7 @@ impl Style {
ui.label("Animation duration");
ui.add(
DragValue::new(animation_time)
.clamp_range(0.0..=1.0)
.range(0.0..=1.0)
.speed(0.02)
.suffix(" s"),
);
Expand Down Expand Up @@ -1515,58 +1515,58 @@ impl Spacing {
ui.end_row();

ui.label("Indent");
ui.add(DragValue::new(indent).clamp_range(0.0..=100.0));
ui.add(DragValue::new(indent).range(0.0..=100.0));
ui.end_row();

ui.label("Slider width");
ui.add(DragValue::new(slider_width).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(slider_width).range(0.0..=1000.0));
ui.end_row();

ui.label("Slider rail height");
ui.add(DragValue::new(slider_rail_height).clamp_range(0.0..=50.0));
ui.add(DragValue::new(slider_rail_height).range(0.0..=50.0));
ui.end_row();

ui.label("ComboBox width");
ui.add(DragValue::new(combo_width).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(combo_width).range(0.0..=1000.0));
ui.end_row();

ui.label("Default area size");
ui.add(two_drag_values(default_area_size, 0.0..=1000.0));
ui.end_row();

ui.label("TextEdit width");
ui.add(DragValue::new(text_edit_width).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(text_edit_width).range(0.0..=1000.0));
ui.end_row();

ui.label("Tooltip wrap width");
ui.add(DragValue::new(tooltip_width).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(tooltip_width).range(0.0..=1000.0));
ui.end_row();

ui.label("Default menu width");
ui.add(DragValue::new(menu_width).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(menu_width).range(0.0..=1000.0));
ui.end_row();

ui.label("Menu spacing")
.on_hover_text("Horizontal spacing between menus");
ui.add(DragValue::new(menu_spacing).clamp_range(0.0..=10.0));
ui.add(DragValue::new(menu_spacing).range(0.0..=10.0));
ui.end_row();

ui.label("Checkboxes etc");
ui.vertical(|ui| {
ui.add(
DragValue::new(icon_width)
.prefix("outer icon width:")
.clamp_range(0.0..=60.0),
.range(0.0..=60.0),
);
ui.add(
DragValue::new(icon_width_inner)
.prefix("inner icon width:")
.clamp_range(0.0..=60.0),
.range(0.0..=60.0),
);
ui.add(
DragValue::new(icon_spacing)
.prefix("spacing:")
.clamp_range(0.0..=10.0),
.range(0.0..=10.0),
);
});
ui.end_row();
Expand All @@ -1579,7 +1579,7 @@ impl Spacing {

ui.horizontal(|ui| {
ui.label("Max height of a combo box");
ui.add(DragValue::new(combo_height).clamp_range(0.0..=1000.0));
ui.add(DragValue::new(combo_height).range(0.0..=1000.0));
});

ui.collapsing("Scroll Area", |ui| {
Expand Down Expand Up @@ -1611,23 +1611,23 @@ impl Interaction {
.show(ui, |ui| {
ui.label("interact_radius")
.on_hover_text("Interact with the closest widget within this radius.");
ui.add(DragValue::new(interact_radius).clamp_range(0.0..=20.0));
ui.add(DragValue::new(interact_radius).range(0.0..=20.0));
ui.end_row();

ui.label("resize_grab_radius_side").on_hover_text("Radius of the interactive area of the side of a window during drag-to-resize");
ui.add(DragValue::new(resize_grab_radius_side).clamp_range(0.0..=20.0));
ui.add(DragValue::new(resize_grab_radius_side).range(0.0..=20.0));
ui.end_row();

ui.label("resize_grab_radius_corner").on_hover_text("Radius of the interactive area of the corner of a window during drag-to-resize.");
ui.add(DragValue::new(resize_grab_radius_corner).clamp_range(0.0..=20.0));
ui.add(DragValue::new(resize_grab_radius_corner).range(0.0..=20.0));
ui.end_row();

ui.label("Tooltip delay").on_hover_text(
"Delay in seconds before showing tooltips after the mouse stops moving",
);
ui.add(
DragValue::new(tooltip_delay)
.clamp_range(0.0..=1.0)
.range(0.0..=1.0)
.speed(0.05)
.suffix(" s"),
);
Expand All @@ -1638,7 +1638,7 @@ impl Interaction {
);
ui.add(
DragValue::new(tooltip_grace_time)
.clamp_range(0.0..=1.0)
.range(0.0..=1.0)
.speed(0.05)
.suffix(" s"),
);
Expand Down Expand Up @@ -1996,7 +1996,7 @@ impl TextCursorStyle {
ui.add(
DragValue::new(on_duration)
.speed(0.1)
.clamp_range(0.0..=2.0)
.range(0.0..=2.0)
.suffix(" s"),
);
ui.end_row();
Expand All @@ -2005,7 +2005,7 @@ impl TextCursorStyle {
ui.add(
DragValue::new(off_duration)
.speed(0.1)
.clamp_range(0.0..=2.0)
.range(0.0..=2.0)
.suffix(" s"),
);
ui.end_row();
Expand Down Expand Up @@ -2065,12 +2065,12 @@ fn two_drag_values(value: &mut Vec2, range: std::ops::RangeInclusive<f32>) -> im
ui.horizontal(|ui| {
ui.add(
DragValue::new(&mut value.x)
.clamp_range(range.clone())
.range(range.clone())
.prefix("x: "),
);
ui.add(
DragValue::new(&mut value.y)
.clamp_range(range.clone())
.range(range.clone())
.prefix("y: "),
);
})
Expand Down Expand Up @@ -2212,7 +2212,7 @@ impl Widget for &mut Rounding {
ui.checkbox(&mut same, "same");

let mut cr = self.nw;
ui.add(DragValue::new(&mut cr).clamp_range(0.0..=f32::INFINITY));
ui.add(DragValue::new(&mut cr).range(0.0..=f32::INFINITY));
*self = Rounding::same(cr);
})
.response
Expand All @@ -2222,19 +2222,19 @@ impl Widget for &mut Rounding {

crate::Grid::new("rounding").num_columns(2).show(ui, |ui| {
ui.label("NW");
ui.add(DragValue::new(&mut self.nw).clamp_range(0.0..=f32::INFINITY));
ui.add(DragValue::new(&mut self.nw).range(0.0..=f32::INFINITY));
ui.end_row();

ui.label("NE");
ui.add(DragValue::new(&mut self.ne).clamp_range(0.0..=f32::INFINITY));
ui.add(DragValue::new(&mut self.ne).range(0.0..=f32::INFINITY));
ui.end_row();

ui.label("SW");
ui.add(DragValue::new(&mut self.sw).clamp_range(0.0..=f32::INFINITY));
ui.add(DragValue::new(&mut self.sw).range(0.0..=f32::INFINITY));
ui.end_row();

ui.label("SE");
ui.add(DragValue::new(&mut self.se).clamp_range(0.0..=f32::INFINITY));
ui.add(DragValue::new(&mut self.se).range(0.0..=f32::INFINITY));
ui.end_row();
});
})
Expand Down Expand Up @@ -2266,28 +2266,28 @@ impl Widget for &mut Shadow {
ui.add(
DragValue::new(&mut offset.x)
.speed(1.0)
.clamp_range(-100.0..=100.0)
.range(-100.0..=100.0)
.prefix("x: "),
);
ui.add(
DragValue::new(&mut offset.y)
.speed(1.0)
.clamp_range(-100.0..=100.0)
.range(-100.0..=100.0)
.prefix("y: "),
);
ui.end_row();

ui.add(
DragValue::new(blur)
.speed(1.0)
.clamp_range(0.0..=100.0)
.range(0.0..=100.0)
.prefix("blur: "),
);

ui.add(
DragValue::new(spread)
.speed(1.0)
.clamp_range(0.0..=100.0)
.range(0.0..=100.0)
.prefix("spread: "),
);
});
Expand All @@ -2302,12 +2302,8 @@ impl Widget for &mut Stroke {
let Stroke { width, color } = self;

ui.horizontal(|ui| {
ui.add(
DragValue::new(width)
.speed(0.1)
.clamp_range(0.0..=f32::INFINITY),
)
.on_hover_text("Width");
ui.add(DragValue::new(width).speed(0.1).range(0.0..=f32::INFINITY))
.on_hover_text("Width");
ui.color_edit_button_srgba(color);

// stroke preview:
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ fn rgba_edit_ui(ui: &mut Ui, [r, g, b, a]: &mut [f32; 4], alpha: Alpha) -> bool
DragValue::new(value)
.speed(0.003)
.prefix(prefix)
.clamp_range(0.0..=1.0)
.range(0.0..=1.0)
.custom_formatter(|n, _| format!("{n:.03}"))
.ui(ui)
}
Expand Down
Loading

0 comments on commit 10f092d

Please sign in to comment.