Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sliders: keep the parent material clear beneath the track (#10375)

Nathan Dane9060abc83eec
diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs
index dbb670d2e7..90be756237 100644
--- a/crates/ui-egui/src/ui_kit/fields.rs
+++ b/crates/ui-egui/src/ui_kit/fields.rs
@@ -1247,7 +1247,7 @@ impl Default for Slider {
/// The only slider element, including stepped preferences and editor-free timeline controls.
/// Native input, keyboard routing, enabled state and accessibility; one token-painted track/thumb.
-/// Tokens: control_height, space_24/8/4/2, dialog_body/rule, control_fill, form_primary.
+/// Tokens: control_height, space_24/8/4/2, dialog_rule, control_fill, form_primary.
pub(crate) fn slider<T: egui::emath::Numeric>(ui: &mut Ui, value: &mut T, range: std::ops::RangeInclusive<T>, options: Slider) -> Response {
if crate::prefs_search::suppress_chrome(ui) {
return crate::prefs_search::hidden_response(ui);
@@ -1272,7 +1272,8 @@ pub(crate) fn slider<T: egui::emath::Numeric>(ui: &mut Ui, value: &mut T, range:
t.space_4
};
ui.spacing_mut().slider_width = dimension(ui.available_width() - reserved);
- let mut widget = egui::Slider::new(value, range).show_value(options.show_value).handle_shape(egui::style::HandleShape::Circle);
+ let mut widget =
+ egui::Slider::new(value, range).show_value(options.show_value).handle_shape(egui::style::HandleShape::Circle).trailing_fill(false);
if options.off_endpoint {
// Blur's step is an editing affordance, not a persisted/API quantisation rule.
widget = widget.clamping(egui::SliderClamping::Edits);
@@ -1286,20 +1287,59 @@ pub(crate) fn slider<T: egui::emath::Numeric>(ui: &mut Ui, value: &mut T, range:
if options.integer {
widget = widget.integer();
}
+ let layer = ui.layer_id();
+ let paint_start = ui.ctx().graphics(|graphics| graphics.get(layer).map_or(egui::layers::ShapeIdx(0), |list| list.next_idx()));
let response = ui.add(widget);
+ let slider = egui::Rect::from_min_size(response.rect.min, vec2(ui.spacing().slider_width, response.rect.height()));
+ // egui 0.36.2 paints the rail, then the circular handle, then the value editor.
+ // Suppress only a positively identified pair added by this widget. A backing
+ // rectangle would flatten translucent/blurred parents. Unexpected painting
+ // (including an invisible or clipped widget) keeps the native shapes intact.
+ ui.ctx().graphics_mut(|graphics| {
+ let Some(list) = graphics.get_mut(layer) else { return };
+ let paint_end = list.next_idx();
+ if paint_end.0.saturating_sub(paint_start.0) < 2 {
+ return;
+ }
+ let handle_idx = egui::layers::ShapeIdx(paint_start.0 + 1);
+ let rail_height = ui.spacing().slider_rail_height.max(0.0);
+ let native_radius = slider.height() / 2.5;
+ let native_range = slider.x_range().shrink(native_radius);
+ let mut rail_matches = false;
+ let mut handle_matches = false;
+ // Indexed reads via mutate_shape are bounded and allocate nothing; do not
+ // scan the layer's earlier parent/sibling paints looking for a match.
+ list.mutate_shape(paint_start, |entry| {
+ if let egui::Shape::Rect(rail) = &entry.shape {
+ let expected = egui::Rect::from_center_size(slider.center(), vec2(slider.width(), rail_height));
+ rail_matches = entry.clip_rect == ui.clip_rect()
+ && (rail.rect.min - expected.min).length_sq() < 0.0001
+ && (rail.rect.max - expected.max).length_sq() < 0.0001
+ && rail.stroke == egui::Stroke::NONE;
+ }
+ });
+ list.mutate_shape(handle_idx, |entry| {
+ if let egui::Shape::Circle(handle) = &entry.shape {
+ handle_matches = entry.clip_rect == ui.clip_rect()
+ && (handle.center.y - slider.center().y).abs() < 0.01
+ // egui's handle range reverses when the control is narrower
+ // than its diameter; either endpoint still belongs to it.
+ && handle.center.x >= native_range.min.min(native_range.max) - 0.01
+ && handle.center.x <= native_range.min.max(native_range.max) + 0.01
+ && (handle.radius - native_radius).abs() < 0.01;
+ }
+ });
+ if rail_matches && handle_matches {
+ list.mutate_shape(paint_start, |entry| entry.shape = egui::Shape::Noop);
+ list.mutate_shape(handle_idx, |entry| entry.shape = egui::Shape::Noop);
+ }
+ });
if ui.ctx().memory(|memory| memory.focused()).is_some_and(|id| {
ui.ctx().read_response(id).is_some_and(|focused| response.rect.contains(focused.rect.center()))
|| egui::TextEdit::load_state(ui.ctx(), id).is_some()
}) {
crate::prefs_search::note_key_sink(ui.ctx());
}
- let slider = egui::Rect::from_min_size(response.rect.min, vec2(ui.spacing().slider_width, response.rect.height()));
- let mut background = ui.painter().clone();
- background.set_opacity(if ui.is_visible() { 1.0 } else { 0.0 });
- // At the top of the range egui's own handle reaches `slider.right()` exactly, and a fill
- // that stops there leaves its last column showing beside the editor (#5708). The gap
- // before the editor is `item_spacing.x`, so a pixel past the edge covers nothing else.
- background.rect_filled(slider.with_max_x(slider.right() + 1.0), 0.0, t.dialog_body);
// Match egui's native input range (handle_radius = thickness / 2.5), so the
// painted thumb stays under the pointer even at the two ends of the range.
let input_radius = ui.text_style_height(&egui::TextStyle::Body).max(ui.spacing().interact_size.y) / 2.5;
diff --git a/crates/ui-egui/src/ui_kit/slider_tests.rs b/crates/ui-egui/src/ui_kit/slider_tests.rs
index 8452ab2084..71b31b961e 100644
--- a/crates/ui-egui/src/ui_kit/slider_tests.rs
+++ b/crates/ui-egui/src/ui_kit/slider_tests.rs
@@ -112,7 +112,7 @@ fn blur_meter_boundaries_and_off_thumb() {
}
for dark in [false, true] {
crate::theme::set_dark(dark);
- for radius in [0.0, 18.25] {
+ for radius in [0.0, 18.25, 40.0] {
let ctx = crate::fonts::test_context();
let t = Tokens::get();
let mut value = radius;
@@ -122,6 +122,13 @@ fn blur_meter_boundaries_and_off_thumb() {
});
});
assert_eq!(value, radius, "drawing cannot round persisted fractional values");
+ assert_eq!(out.shapes.iter().filter(|s| matches!(s.shape, egui::Shape::Circle(_))).count(), 1, "off/solid thumb has no native duplicate");
+ let hit = scenario::rect_of("dialog:slider_track:").expect("blur slider track");
+ assert_eq!(
+ out.shapes.iter().filter(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect.left() < hit.right())).count(),
+ 2,
+ "off/solid track has no native rail or backing rectangle"
+ );
let thumb = out
.shapes
.iter()
@@ -291,41 +298,303 @@ fn a_sliders_value_box_takes_typed_numbers_and_clamps_them_to_its_range() {
}
}
-/// At the top of its range egui's own handle reaches the right edge of the rectangle the kit
-/// paints over it, so the mask has to pass that edge; flush with it, the handle's last column
-/// survives as a light sliver beside the value box (#5708).
+/// At either endpoint there must be just the token thumb and track: a native handle's
+/// last column used to survive beside the value box at the maximum (#5708).
#[test]
-fn the_mask_passes_the_edge_of_egui_s_own_handle_at_the_top_of_the_range() {
- let ctx = crate::fonts::test_context();
- let t = Tokens::get();
- let mut value: f64 = 2.0;
- let mut output =
- ctx.run_ui(egui::RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, vec2(400.0, 200.0))), ..Default::default() }, |ui| {
- scope(ui, |ui| {
- slider(ui, &mut value, 0.0..=2.0, Slider { step: Some(0.05), ..Default::default() });
- });
- });
- output.textures_delta.clear();
- let handle = output
- .shapes
- .iter()
- .find_map(|s| match &s.shape {
- egui::Shape::Circle(c) if c.stroke.color != t.form_primary => Some(*c),
- _ => None,
- })
- .expect("egui's own slider handle");
- let mask = output
- .shapes
+fn slider_endpoints_have_no_duplicate_native_handle_or_rail_and_keep_the_editor() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ for show_value in [false, true] {
+ for endpoint in [0.0, 2.0] {
+ let ctx = crate::fonts::test_context();
+ let mut value = endpoint;
+ let output = slider_test_frame(&ctx, vec![], |ui| {
+ slider(ui, &mut value, 0.0..=2.0, Slider { step: Some(0.05), show_value, ..Default::default() });
+ });
+ let hit = scenario::rect_of("dialog:slider_track:").expect("slider track");
+ assert_slider_paint(&output.shapes, hit, Tokens::get(), endpoint, show_value, 1.0);
+ assert_eq!(value, endpoint);
+ }
+ }
+ }
+ crate::theme::set_dark(previous);
+}
+
+#[test]
+fn narrow_sliders_do_not_retain_native_chrome_when_the_handle_range_reverses() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ for width in [24.0, 28.0] {
+ for saved in [0.0, 0.73123456789, 2.0] {
+ let ctx = crate::fonts::test_context();
+ let mut value = saved;
+ let output = slider_test_frame(&ctx, vec![], |ui| {
+ ui.set_width(width);
+ slider(ui, &mut value, 0.0..=2.0, Slider { show_value: false, ..Default::default() });
+ });
+ let hit = scenario::rect_of("dialog:slider_track:").expect("narrow slider track");
+ assert!(hit.width() < 28.0, "the fixture must constrain the native track");
+ assert_slider_paint(&output.shapes, hit, Tokens::get(), saved, false, 1.0);
+ assert_eq!(value, saved);
+ }
+ }
+ }
+ crate::theme::set_dark(previous);
+}
+
+fn slider_test_frame(ctx: &egui::Context, events: Vec<egui::Event>, mut add: impl FnMut(&mut Ui)) -> egui::FullOutput {
+ scenario::clear_handles();
+ crate::test_support::frame(
+ ctx,
+ egui::RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, vec2(400.0, 200.0))), events, focused: true, ..Default::default() },
+ |ui| {
+ ui.set_width(280.0);
+ scope(ui, &mut add);
+ },
+ )
+}
+
+/// Assert observable paint geometry, including absence of a surface-coloured backing.
+/// `shapes` is only the slider's paint span; parent/sibling paints are checked separately.
+fn assert_slider_paint(shapes: &[egui::epaint::ClippedShape], hit: Rect, t: Tokens, value: f64, show_value: bool, opacity: f32) {
+ let track = Rect::from_center_size(hit.center(), vec2((hit.width() - 2.0 * hit.height() / 2.5).max(0.0), t.space_4));
+ let center = pos2(egui::lerp(track.x_range(), (value / 2.0) as f32), track.center().y);
+ let circles: Vec<_> = shapes.iter().filter_map(|s| if let egui::Shape::Circle(c) = &s.shape { Some(c) } else { None }).collect();
+ assert_eq!(circles.len(), 1, "no duplicate native handle, even at the very edge");
+ let thumb = circles[0];
+ assert_eq!(thumb.center, center, "the custom thumb reaches the native input endpoints");
+ assert_eq!(thumb.radius, t.space_8);
+ assert_eq!(thumb.fill, t.control_fill.gamma_multiply(opacity));
+ assert_eq!(thumb.stroke, egui::Stroke::new(t.space_2, t.form_primary.gamma_multiply(opacity)));
+ let rails: Vec<_> = shapes
.iter()
- .find_map(|s| match &s.shape {
- egui::Shape::Rect(r) if r.fill == t.dialog_body => Some(r.rect),
+ .filter_map(|s| match &s.shape {
+ egui::Shape::Rect(r) if r.rect.left() < hit.right() => Some(r),
_ => None,
})
- .expect("the rectangle the kit paints over egui's slider");
- assert!(
- mask.right() > handle.center.x + handle.radius,
- "the mask ends at {} and egui's handle at {}: its last column would show",
- mask.right(),
- handle.center.x + handle.radius
- );
+ .collect();
+ assert_eq!(rails.len(), 2, "only the token track and filled portion; no native rail or backing rectangle");
+ assert_eq!(rails[0].rect, track);
+ assert_eq!(rails[0].fill, t.dialog_rule.gamma_multiply(opacity));
+ assert_eq!(rails[1].rect, Rect::from_min_max(track.min, pos2(center.x, track.bottom())));
+ assert_eq!(rails[1].fill, t.form_primary.gamma_multiply(opacity));
+ let editor: Vec<_> = shapes.iter().filter_map(|s| if let egui::Shape::Text(text) = &s.shape { Some(text) } else { None }).collect();
+ if show_value {
+ assert!(
+ editor.iter().any(|text| text.galley.text() == format!("{value:.2}")),
+ "the native value editor must remain painted: {:?}",
+ editor.iter().map(|text| text.galley.text()).collect::<Vec<_>>()
+ );
+ assert!(editor.iter().all(|text| text.pos.x > hit.right()), "the editor must remain beside the track");
+ assert!(shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect.left() > hit.right())), "native editor chrome survives");
+ } else {
+ assert!(editor.is_empty(), "show_value(false) still hides the editor");
+ }
+}
+
+#[test]
+fn slider_preserves_translucent_parent_paints_and_inherited_opacity_without_a_backing_rectangle() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ for translucent in [false, true] {
+ let mut t = Tokens::get();
+ if translucent {
+ t.apply_panel_opacity(40.0);
+ assert!(t.dialog_body.a() < 255);
+ }
+ let _tokens = crate::theme::scoped_tokens(t);
+ for opacity in [1.0, 0.45] {
+ for show_value in [false, true] {
+ for saved in [0.0, 0.73123456789, 2.0] {
+ let ctx = crate::fonts::test_context();
+ let mut value = saved;
+ slider_test_frame(&ctx, vec![], |ui| {
+ slider(ui, &mut value, 0.0..=2.0, Slider { show_value, ..Default::default() });
+ });
+ let hit = scenario::rect_of("dialog:slider_track:").expect("slider track");
+ slider_test_frame(&ctx, vec![], |ui| {
+ ui.multiply_opacity(opacity);
+ // A synthetic parent callback stands in for background effects;
+ // this test observes paint retention, without invoking a GPU.
+ ui.painter().add(egui::Shape::Vec(vec![
+ egui::Shape::rect_filled(hit.expand(12.0), t.control_radius, t.dialog_body),
+ egui::Shape::Callback(egui::PaintCallback { rect: hit, callback: std::sync::Arc::new(()) }),
+ ]));
+ // Earlier sibling paints deliberately have the native signature.
+ ui.painter().rect_filled(
+ Rect::from_center_size(hit.center(), vec2(hit.width(), ui.spacing().slider_rail_height)),
+ t.control_radius,
+ t.control_fill,
+ );
+ ui.painter().circle_filled(hit.center(), hit.height() / 2.5, t.control_fill);
+ let layer = ui.layer_id();
+ let parent = ui.ctx().graphics(|g| g.get(layer).expect("parent paints").all_entries().cloned().collect::<Vec<_>>());
+ slider(ui, &mut value, 0.0..=2.0, Slider { show_value, ..Default::default() });
+ assert_eq!(ui.opacity(), opacity, "slider must retain the parent's painter opacity");
+ ui.ctx().graphics(|g| {
+ let shapes: Vec<_> = g.get(layer).expect("slider paints").all_entries().cloned().collect();
+ assert_eq!(&shapes[..parent.len()], &parent, "parent callback/fill and earlier siblings must survive unchanged");
+ assert_slider_paint(&shapes[parent.len()..], hit, t, saved, show_value, opacity);
+ });
+ ui.painter().circle_filled(hit.right_center(), t.space_2, t.form_dim);
+ assert_eq!(ui.opacity(), opacity, "following siblings inherit the same opacity");
+ });
+ assert_eq!(value, saved, "painting preserves fractional saved values");
+ }
+ }
+ }
+ }
+ }
+ crate::theme::set_dark(previous);
+}
+
+#[test]
+fn clipping_out_the_native_track_preserves_the_editor_when_the_paint_signature_differs() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ let ctx = crate::fonts::test_context();
+ let mut value = 1.0;
+ slider_test_frame(&ctx, vec![], |ui| {
+ slider(ui, &mut value, 0.0..=2.0, Default::default());
+ });
+ let hit = scenario::rect_of("dialog:slider_track:").expect("slider track");
+ let output = slider_test_frame(&ctx, vec![], |ui| {
+ // egui omits its rail/handle when only the editor intersects the clip.
+ // The first native shapes are now editor paints and must survive.
+ ui.set_clip_rect(ui.clip_rect().with_min_x(hit.right() + 1.0));
+ slider(ui, &mut value, 0.0..=2.0, Default::default());
+ });
+ assert!(output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect.left() > hit.right())));
+ assert!(output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Text(text) if text.galley.text() == "1.00")));
+ assert_eq!(output.shapes.iter().filter(|s| matches!(s.shape, egui::Shape::Circle(_))).count(), 1, "only the clipped custom thumb is added");
+ assert_eq!(value, 1.0);
+ }
+ crate::theme::set_dark(previous);
+}
+
+#[test]
+fn slider_keyboard_focus_and_accessibility_keep_native_steps_and_editor_focus() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ for translucent in [false, true] {
+ let mut t = Tokens::get();
+ if translucent {
+ t.apply_panel_opacity(40.0);
+ }
+ let _tokens = crate::theme::scoped_tokens(t);
+ for show_value in [false, true] {
+ let ctx = crate::fonts::test_context();
+ ctx.enable_accesskit();
+ let value = std::cell::Cell::new(1.0_f64);
+ let draw = |events| {
+ slider_test_frame(&ctx, events, |ui| {
+ let mut next = value.get();
+ slider(ui, &mut next, 0.0..=2.0, Slider { step: Some(0.05), show_value, ..Default::default() });
+ value.set(next);
+ })
+ };
+ draw(vec![]);
+ let idle = draw(vec![]);
+ let tree = idle.platform_output.accesskit_update.as_ref().expect("native accessibility tree");
+ let nodes: Vec<_> = tree.nodes.iter().filter(|(_, n)| n.role() == egui::accesskit::Role::Slider).collect();
+ assert_eq!(nodes.len(), 1);
+ let node = &nodes[0].1;
+ assert_eq!(node.numeric_value(), Some(1.0));
+ assert_eq!(node.min_numeric_value(), Some(0.0));
+ assert_eq!(node.max_numeric_value(), Some(2.0));
+ assert_eq!(node.numeric_value_step(), Some(0.05));
+ assert!(!node.is_disabled());
+ draw(test_support::key(egui::Key::Tab));
+ draw(vec![]);
+ let focus = ctx.memory(|m| m.focused()).expect("Tab focuses the slider");
+ let focused = ctx.read_response(focus).expect("focused slider response");
+ assert!(focused.has_focus());
+ let hit = scenario::rect_of("dialog:slider_track:").expect("slider track");
+ assert!(hit.contains(focused.rect.center()));
+ let changed = draw(test_support::key(egui::Key::ArrowRight));
+ assert!((value.get() - 1.05).abs() < 1e-8);
+ assert_eq!(ctx.memory(|m| m.focused()), Some(focus));
+ assert_slider_paint(&changed.shapes, hit, Tokens::get(), value.get(), show_value, 1.0);
+ draw(test_support::key(egui::Key::ArrowLeft));
+ assert!((value.get() - 1.0).abs() < 1e-8);
+ if show_value {
+ draw(test_support::key(egui::Key::Tab));
+ let editing = draw(vec![]);
+ let editor_id = ctx.memory(|m| m.focused()).expect("Tab reaches the native editor");
+ assert_ne!(editor_id, focus);
+ assert!(egui::TextEdit::load_state(&ctx, editor_id).is_some());
+ assert!(editing.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Text(text) if text.pos.x > hit.right())));
+ draw(vec![egui::Event::Text("1.75".into())]);
+ draw(test_support::key(egui::Key::Enter));
+ draw(vec![]);
+ assert!((value.get() - 1.75).abs() < 1e-8);
+ }
+ // The same native hit range still accepts pointer input after keyboard/editing.
+ for (at, expected) in [(hit.left_center(), 0.0), (hit.center(), 1.0), (hit.right_center(), 2.0)] {
+ draw(test_support::pointer(at, true));
+ let released = draw(test_support::pointer(at, false));
+ assert!((value.get() - expected).abs() < 1e-8);
+ assert_slider_paint(&released.shapes, hit, Tokens::get(), expected, show_value, 1.0);
+ }
+ }
+ }
+ }
+ crate::theme::set_dark(previous);
+}
+
+#[test]
+fn disabled_and_invisible_sliders_keep_native_input_and_paint_suppression() {
+ let previous = crate::theme::is_dark();
+ for dark in [false, true] {
+ crate::theme::set_dark(dark);
+ for show_value in [false, true] {
+ for visible in [false, true] {
+ let ctx = crate::fonts::test_context();
+ ctx.enable_accesskit();
+ let value = std::cell::Cell::new(0.73123456789);
+ let draw = |events| {
+ slider_test_frame(&ctx, events, |ui| {
+ ui.add_enabled_ui(false, |ui| {
+ if !visible {
+ ui.set_invisible();
+ }
+ let mut next = value.get();
+ let response = slider(ui, &mut next, 0.0..=2.0, Slider { show_value, ..Default::default() });
+ assert!(!response.enabled());
+ assert!(!response.changed());
+ value.set(next);
+ });
+ })
+ };
+ draw(vec![]);
+ let idle = draw(vec![]);
+ let hit = scenario::rect_of("dialog:slider_track:").expect("hidden slider still allocates its track");
+ if visible {
+ let tree = idle.platform_output.accesskit_update.as_ref().expect("disabled accessibility tree");
+ assert!(tree.nodes.iter().any(|(_, n)| n.role() == egui::accesskit::Role::Slider && n.is_disabled()));
+ let circles: Vec<_> =
+ idle.shapes.iter().filter_map(|s| if let egui::Shape::Circle(c) = &s.shape { Some(c) } else { None }).collect();
+ assert_eq!(circles.len(), 1, "disabled slider still has only the token thumb");
+ assert_eq!(circles[0].radius, Tokens::get().space_8);
+ assert_ne!(circles[0].fill, Tokens::get().control_fill, "native disabled fade remains active");
+ } else {
+ assert!(idle.shapes.iter().all(|s| matches!(s.shape, egui::Shape::Noop)), "invisible slider paints no chrome or editor");
+ }
+ draw(test_support::pointer(hit.right_center(), true));
+ draw(test_support::pointer(hit.right_center(), false));
+ draw(test_support::key(egui::Key::Tab));
+ draw(test_support::key(egui::Key::ArrowRight));
+ draw(vec![egui::Event::Text("2".into())]);
+ draw(test_support::key(egui::Key::Enter));
+ assert_eq!(value.get(), 0.73123456789);
+ assert!(ctx.memory(|m| m.focused()).is_none(), "disabled/invisible slider cannot take focus");
+ }
+ }
+ }
+ crate::theme::set_dark(previous);
}