Sketch: keep polygon inputs clear of the preview (#10035) (#10199)

* claim: #10035

* Sketch: keep polygon inputs clear of the preview (#10035)
This commit is contained in:
Colin Faulkingham
2026-10-31 20:06:03 +00:00
committed by GitHub
parent 3b3d93f333
commit ec526d2b3d
4 changed files with 126 additions and 13 deletions
+56 -5
View File
@@ -531,11 +531,31 @@ fn boxes(app: &SolveApp, ui: &mut egui::Ui, proj: &Proj, t: &mut Tool) -> Option
let mut completed = false;
let mut any_focused = false;
let view = app.viewport.layout.as_ref().map(|l| l.drawable).filter(|r| r.is_finite() && r.is_positive());
for (i, b) in t.dims.iter_mut().enumerate() {
let Some((v, at)) = lv.get(i).copied() else { continue };
let field_frame = Container::popup_frame(ui.style()).inner_margin(egui::Margin::symmetric(tk.space_3 as i8, 1));
for (b, (v, _)) in t.dims.iter_mut().zip(&lv) {
if !b.locked {
b.value = fmt(app, b.label, b.kind, v);
b.value = fmt(app, b.label, b.kind, *v);
}
}
// Polygon values used to sit inside the radius, so both boxes covered a small polygon
// and each other. Place them together outside its projected outline, in screen space:
// their separation must not shrink with the shape or a tilted sketch plane.
let polygon_positions = if matches!(t.cmd, "sketch.polygon.inscribed" | "sketch.polygon.circumscribed") {
let corners = crate::tools::polygon_outline(t, e, locked(app, t, "Sides").unwrap_or(6.0))
.unwrap_or_else(|| t.pts.iter().map(|p| p.0).chain(std::iter::once(e)).collect());
let points: Vec<_> = corners.iter().filter_map(|p| proj.to_screen(plane.to_world(*p))).collect();
let bounds = egui::Rect::from_points(&points);
let sizes: Vec<_> = t
.dims
.iter()
.map(|b| crate::ui_kit::placement_field_content_size(ui, &b.value) + vec2(2.0 * tk.form_inset, 0.0) + field_frame.total_margin().sum())
.collect();
bounds.is_finite().then(|| polygon_input_positions(bounds, &sizes, view, tk.space_8))
} else {
None
};
for (i, b) in t.dims.iter_mut().enumerate() {
let Some((_, at)) = lv.get(i).copied() else { continue };
let Some(sp) = proj.to_screen(plane.to_world(at)) else { continue };
let id = egui::Id::new(("sc_dim", i));
let has_focus = ui.ctx().memory(|m| m.has_focus(id));
@@ -546,13 +566,16 @@ fn boxes(app: &SolveApp, ui: &mut egui::Ui, proj: &Proj, t: &mut Tool) -> Option
s.cursor.set_char_range(Some(egui::text_selection::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(n))));
s.store(ui.ctx(), id);
}
let pos = in_view(sp + vec2(-34.0, -11.0), view, ui.ctx().pointer_latest_pos());
let pos = polygon_positions
.as_ref()
.and_then(|positions| positions.get(i).copied())
.unwrap_or_else(|| in_view(sp + vec2(-34.0, -11.0), view, ui.ctx().pointer_latest_pos()));
// Background: under Preferences and Report a bug (#9016), like canvas value boxes.
Container::new(ui.ctx(), Surface::Canvas).area(egui::Id::new(("sc_dim_area", i))).fixed_pos(pos).order(egui::Order::Background).show(
ui.ctx(),
|ui| {
let fill = if b.locked { tk.accent_soft } else { tk.panel };
Container::popup_frame(ui.style()).fill(fill).inner_margin(egui::Margin::symmetric(tk.space_3 as i8, 1)).show(ui, |ui| {
field_frame.fill(fill).show(ui, |ui| {
let r = crate::ui_kit::placement_field(ui, id, &mut b.value, b.label);
// A completion gets first use of forward Tab. Shift+Tab always moves back;
// the box reached by egui's generic traversal must not complete its text.
@@ -590,6 +613,34 @@ fn boxes(app: &SolveApp, ui: &mut egui::Ui, proj: &Proj, t: &mut Tool) -> Option
(enter && (any_focused || nothing)).then_some(e)
}
/// Stack the polygon inputs beside its screen bounds. Try each side before falling back to
/// the drawing area's edge when the polygon fills the view; keep the boxes separate even then.
fn polygon_input_positions(bounds: egui::Rect, sizes: &[egui::Vec2], view: Option<egui::Rect>, gap: f32) -> Vec<Pos2> {
let size =
vec2(sizes.iter().map(|s| s.x).fold(0.0, f32::max), sizes.iter().map(|s| s.y).sum::<f32>() + gap * sizes.len().saturating_sub(1) as f32);
let candidates = [
egui::pos2(bounds.center().x - size.x * 0.5, bounds.top() - gap - size.y),
egui::pos2(bounds.center().x - size.x * 0.5, bounds.bottom() + gap),
egui::pos2(bounds.left() - gap - size.x, bounds.center().y - size.y * 0.5),
egui::pos2(bounds.right() + gap, bounds.center().y - size.y * 0.5),
];
let inside = |pos: Pos2| view.map_or(pos, |view| pos.clamp(view.min, (view.max - size).max(view.min)));
let start = candidates
.into_iter()
.map(inside)
.find(|pos| !egui::Rect::from_min_size(*pos, size).intersects(bounds.expand(gap * 0.5)))
.unwrap_or_else(|| inside(candidates[0]));
let mut y = start.y;
sizes
.iter()
.map(|size| {
let pos = egui::pos2(start.x, y);
y += size.y + gap;
pos
})
.collect()
}
/// About the size of a value box, for keeping it in view.
pub(crate) const BOX: egui::Vec2 = egui::vec2(76.0, 24.0);
+55
View File
@@ -568,6 +568,61 @@ fn value_boxes_stay_in_view_and_off_the_cursor() {
assert!(tiny.contains_rect(egui::Rect::from_min_size(in_view(egui::pos2(-200.0, -200.0), Some(tiny), Some(egui::pos2(40.0, 15.0))), BOX)));
}
#[test]
fn polygon_inputs_clear_each_other_and_the_preview() {
for command in ["sketch.polygon.inscribed", "sketch.polygon.circumscribed"] {
for (view, corner) in [("top", false), ("iso", false), ("top", true)] {
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
h.call("ui.view", json!({"view":view}));
h.step(&json!({"start":command})).unwrap();
if corner {
let view = h.app.viewport.layout.as_ref().unwrap().drawable.shrink(20.0);
let center = view.left_top();
let end = center + egui::vec2(16.0, 0.0);
h.step(&json!({"click":[center.x,center.y]})).unwrap();
h.step(&json!({"move":[end.x,end.y],"alt":true})).unwrap();
} else {
h.step(&json!({"click":{"sketch":[0,0]}})).unwrap();
h.step(&json!({"move":{"sketch":[2,0]},"alt":true})).unwrap();
}
h.frames(3);
let boxes: Vec<_> = (0..2usize)
.map(|i| {
h.ctx
.memory(|m| m.area_rect(egui::Id::new(("sc_dim_area", i))))
.unwrap_or_else(|| panic!("{command}, {view}, {corner:?}: input {i} missing"))
})
.collect();
assert!(!boxes[0].intersects(boxes[1]), "{command}: inputs overlap: {boxes:?}");
let tool = h.app.tool.as_ref().unwrap();
let end = crate::sketch_dims::shown(&h.app, tool, tool.hover.as_ref().unwrap());
let outline = crate::tools::polygon_outline(tool, end, 6.0).unwrap();
let state = h.app.session.world_state();
let plane = state.sketch(h.app.session.active_sketch.unwrap()).unwrap().plane;
let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
let points: Vec<_> = outline.iter().map(|p| proj.to_screen(plane.to_world(*p)).unwrap()).collect();
let bounds = egui::Rect::from_points(&points);
for rect in boxes {
assert!(!rect.intersects(bounds), "{command}: input {rect:?} covers polygon {bounds:?}");
assert!(h.app.viewport.layout.as_ref().unwrap().drawable.contains_rect(rect));
}
assert_focus(&h, 0);
type_value(&mut h, "2");
tab(&mut h, false);
assert_focus(&h, 1);
type_value(&mut h, "6");
h.step(&json!({"key":"Enter"})).unwrap();
let state = h.app.session.world_state();
let sketch = state.sketch(h.app.session.active_sketch.unwrap()).unwrap();
assert_eq!(sketch.sketch.curves.iter().filter(|c| !c.construction).count(), 6);
assert_eq!(sketch.profiles.len(), 1);
let expected = if command == "sketch.polygon.inscribed" { 6.0 * 3f64.sqrt() } else { 8.0 * 3f64.sqrt() };
assert!((sketch.profiles[0].area - expected).abs() < 1e-6, "{command}: {}", sketch.profiles[0].area);
}
}
}
/// `name = expression` in a value box, as in the Dimension tool's: the shape takes the
/// expression's size while it is typed and when placed, and the new parameter `name` drives its
/// dimension (#7567).
+13 -6
View File
@@ -1124,15 +1124,12 @@ fn grown_width(ui: &Ui, value: &str, font: &egui::FontId, base: f32) -> f32 {
pub(crate) fn placement_field(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str) -> Response {
let t = Tokens::get();
let font = egui::FontId::proportional(t.font_body);
// Canvas editors must retain their native hit target: enlarging it intercepts nearby
// sketch picks. Keep the former line + space_2 above/below, with shared galley alignment.
let height = ui.fonts_mut(|fonts| fonts.row_height(&font)) + ui.spacing().extra_text_line_spacing + t.space_4;
let width = grown_width(ui, value, &font, t.control_height * 3.0 - t.space_6);
let size = placement_field_content_size(ui, value);
let mut response = ui.add(
text_input(egui::TextEdit::singleline(value))
.id(id)
.min_size(vec2(width, height))
.desired_width(width)
.min_size(size)
.desired_width(size.x)
.return_key(None)
.event_filter(egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, ..Default::default() })
.font(font)
@@ -1190,3 +1187,13 @@ pub(crate) fn inline_name(ui: &mut Ui, rect: egui::Rect, id: egui::Id, value: &m
super::text_menu::menu(ui, &mut response, value, super::text_menu::Field::LINE);
response
}
/// The placement editor's content size, before its horizontal text margins and outer frame.
/// Floating owners use the same measurement to place adjacent editors without overlap.
pub(crate) fn placement_field_content_size(ui: &Ui, value: &str) -> egui::Vec2 {
let t = Tokens::get();
let font = egui::FontId::proportional(t.font_body);
// Retain the native hit target: a taller editor would intercept nearby sketch picks.
let height = ui.fonts_mut(|fonts| fonts.row_height(&font)) + ui.spacing().extra_text_line_spacing + t.space_4;
vec2(grown_width(ui, value, &font, t.control_height * 3.0 - t.space_6), height)
}
+2 -2
View File
@@ -1939,8 +1939,8 @@ fn gallery_covers_every_inline_element() {
&& matches!(&item.vis, syn::Visibility::Restricted(v) if v.path.is_ident("crate"))
{
let name = item.sig.ident.to_string();
// The inherited row label and the eye-drag state key are metadata, not renderable elements.
if name != "label" && name != "eye_drag_id" {
// Inherited labels, state keys and size measurements do not render elements.
if !matches!(name.as_str(), "eye_drag_id" | "label" | "placement_field_content_size") {
assert!(names.contains(name.as_str()), "{file}: {name} needs a gallery entry");
}
}