Snap toolbar slider drags to round values

The main parameter slider already snaps to round values, but the toolbar slider does not, so the same drag cannot land on exact values there.

Apply the same aim-radius smart_aim calculation used by slider to bar_slider, and add a drag test through the existing slider harness that aims near a round value on a bar slider.

Test: bar_slider_drag_reaches_exact_round_values failed before the change and passes after.
This commit is contained in:
eliets2 committed 2026-11-06 19:27:34 +02:00
1 parent 047afaaa5c
commit 446cf794cf
2 files changed
+35 -3

No files matched your search

+7 -2
View File
@@ -91,8 +91,13 @@ pub fn bar_slider(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Ra
if (resp.dragged() || resp.clicked())
&& let Some(p) = resp.interact_pointer_pos()
{
let t = ((p.x - rect.left() - 4.0) / (rect.width() - 8.0).max(1.0)).clamp(0.0, 1.0);
let v = lo + (hi - lo) * t;
let value_at = |x: f32| {
let t = ((x - rect.left() - 4.0) / (rect.width() - 8.0).max(1.0)).clamp(0.0, 1.0);
f64::from(lo) + (f64::from(hi) - f64::from(lo)) * f64::from(t)
};
// Use the same round-value aiming as egui's standard sliders, within one physical pixel.
let radius = ui.input(|i| i.aim_radius());
let v = egui::emath::smart_aim::best_in_range_f64(value_at(p.x - radius), value_at(p.x + radius)) as f32;
if (v - *value).abs() > f32::EPSILON {
*value = v;
resp.mark_changed();
+28 -1
View File
@@ -5,7 +5,7 @@ use egui_kittest::{
kittest::{By, Queryable},
};
use materialcraft_engine::core::{NodeId, Value};
use materialcraft_ui_egui::{MaterialCraftApp, UiState};
use materialcraft_ui_egui::{MaterialCraftApp, UiState, widgets};
use serde_json::json;
fn harness(ppp: f32, render: bool, definition: &str) -> (Harness<'static, MaterialCraftApp>, NodeId) {
@@ -76,6 +76,33 @@ fn fractional_parameter_values_remain_reachable() {
assert_eq!(n.params.get("offset"), Some(&Value::Float2([-0.5, 0.25])));
}
#[test]
fn bar_slider_drag_reaches_exact_round_values() {
let mut h = Harness::builder().with_size(vec2(800.0, 400.0)).with_pixels_per_point(1.0).build_ui_state(
|ui, value: &mut f32| {
widgets::bar_slider(ui, "Brightness", value, 0.0..=1.0, 128.0);
},
0.0f32,
);
h.run_steps(3);
let rect = h.get(By::new().role(egui::accesskit::Role::Slider).label("Brightness")).rect();
let start = pos2(rect.left() + rect.width() * 0.2, rect.center().y);
// Aim 0.4 of a physical pixel to the right of 0.5, as the other drags do.
let target = pos2(rect.left() + 4.0 + (rect.width() - 8.0) * 0.5 + 0.4 / h.ctx.pixels_per_point(), rect.center().y);
h.event(Event::PointerMoved(start));
h.step();
h.event(Event::PointerButton { pos: start, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.step();
h.event(Event::PointerMoved(start + vec2(10.0, 0.0)));
h.step();
h.event(Event::PointerMoved(target));
h.step();
h.event(Event::PointerButton { pos: target, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
h.step();
h.run_steps(3);
assert_eq!(*h.state(), 0.5);
}
#[test]
#[ignore = "requires an offscreen GPU"]
fn exact_angle_screenshot() {