Skip to content

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

Value fields on the canvas grow with an expression typed into them (#7470)

Kai Evans329ef29f2488
diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs
index 515ce5b03a..78aff21534 100644
--- a/crates/ui-egui/src/ui_kit/fields.rs
+++ b/crates/ui-egui/src/ui_kit/fields.rs
@@ -654,7 +654,9 @@ fn length_drag<'a>(value: &'a mut f64, units: &str, decimals: u8) -> egui::DragV
/// keyboard to the next widget.
pub(crate) fn canvas_value_field(ui: &mut Ui, id: egui::Id, value: &mut String, keep_tab: bool) -> Response {
let t = Tokens::get();
- let mut edit = egui::TextEdit::singleline(value).id(id).desired_width(t.space_24 * 3.0).background_color(t.input_fill());
+ let width = grown_width(ui, value, &egui::TextStyle::Body.resolve(ui.style()), t.space_24 * 3.0);
+ // At least that wide: in an area the space left is last frame's width, which can't grow.
+ let mut edit = egui::TextEdit::singleline(value).id(id).desired_width(width).min_size(vec2(width, 0.0)).background_color(t.input_fill());
if keep_tab {
edit = edit.event_filter(egui::EventFilter { tab: true, horizontal_arrows: true, ..Default::default() });
}
@@ -665,6 +667,14 @@ pub(crate) fn canvas_value_field(ui: &mut Ui, id: egui::Id, value: &mut String,
response
}
+/// How wide a value field on the canvas is: `base` for a number, as wide as an expression typed
+/// into it (parameter names, a formula), up to three times `base`.
+fn grown_width(ui: &Ui, value: &str, font: &egui::FontId, base: f32) -> f32 {
+ let text = ui.fonts_mut(|fonts| fonts.layout_no_wrap(value.to_owned(), font.clone(), egui::Color32::PLACEHOLDER).size().x);
+ // Room for the cursor after the last character.
+ (text + Tokens::get().space_6).clamp(base, base * 3.0)
+}
+
/// Placement editor whose owner routes Enter and Tab (including rejected expressions).
/// Completion remains with the caller. Tokens: cell_editor tokens.
pub(crate) fn placement_field(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str) -> Response {
@@ -673,11 +683,12 @@ pub(crate) fn placement_field(ui: &mut Ui, id: egui::Id, value: &mut String, hin
// 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 response = ui.add(
text_input(egui::TextEdit::singleline(value))
.id(id)
- .min_size(vec2(0.0, height))
- .desired_width(t.control_height * 3.0 - t.space_6)
+ .min_size(vec2(width, height))
+ .desired_width(width)
.return_key(None)
.event_filter(egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, ..Default::default() })
.font(font)
diff --git a/crates/ui-egui/src/ui_kit/fields_tests.rs b/crates/ui-egui/src/ui_kit/fields_tests.rs
index 38c3f9d44e..4a57eb5c7c 100644
--- a/crates/ui-egui/src/ui_kit/fields_tests.rs
+++ b/crates/ui-egui/src/ui_kit/fields_tests.rs
@@ -203,6 +203,40 @@ fn growing_message_field_retains_its_id_and_does_not_consume_enter() {
assert_eq!(message, "First\nSecond\n");
}
+/// The value fields on the canvas keep their width for a number and grow with an expression typed
+/// into them (parameter names, a formula), up to three times that width, also in the area they
+/// float in on the canvas, whose space is last frame's size.
+#[test]
+fn canvas_value_fields_grow_with_an_expression_up_to_three_times_their_width() {
+ for case in ["canvas", "placement"] {
+ let ctx = crate::fonts::test_context();
+ let width = |value: &str| {
+ let mut value = value.to_owned();
+ let mut rect = egui::Rect::NOTHING;
+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
+ egui::Area::new(egui::Id::new("grow")).fixed_pos(egui::pos2(10.0, 10.0)).show(ui.ctx(), |ui| {
+ ui.horizontal(|ui| {
+ let id = ui.id().with(case);
+ rect = match case {
+ "canvas" => canvas_value_field(ui, id, &mut value, false).rect,
+ _ => placement_field(ui, id, &mut value, "").rect,
+ };
+ });
+ });
+ });
+ output.textures_delta.clear();
+ rect.width()
+ };
+ let number = width("12.5 mm");
+ assert!((width("4") - number).abs() < 0.5, "{case}: a number keeps the field's width");
+ let formula = width("CellWidth * X + 2 * Wall");
+ assert!(formula > number * 1.5, "{case}: {formula} for a formula, {number} for a number");
+ let long = width("CellWidth * X + 2 * WallThickness - Clearance / 2 + LipHeight * Y");
+ assert!(long > formula && long <= number * 3.0 + 1.0, "{case}: {long} capped at three times {number}");
+ assert!((width("4") - number).abs() < 0.5, "{case}: and back to a number's width");
+ }
+}
+
#[test]
fn placement_field_keeps_focus_for_owner_validation_on_enter() {
let ctx = crate::fonts::test_context();