Skip to content

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

Command dialogs keep the value field focused after a viewport click (#4643) (#4652)

raykholod17b70241fdf
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index c77944a362..377d40c46f 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -441,6 +441,10 @@ pub struct Dialog {
pub profile_selection_changed: bool,
/// Give the on-canvas value box the keyboard (and select its text) on the next frame.
pub focus: bool,
+ /// Last focused value editor, shared by the form and its on-canvas value box.
+ value_focus: Option<egui::Id>,
+ /// A viewport press or click returns the keyboard after selection and canvas updates.
+ pub(crate) restore_value_focus: bool,
/// Folded to its header (the shared disclosure chevron).
pub collapsed: bool,
reset_scroll: bool,
@@ -639,6 +643,8 @@ impl Dialog {
editing: None,
profile_selection_changed: false,
focus: true,
+ value_focus: None,
+ restore_value_focus: false,
collapsed: false,
reset_scroll: true,
preview_enabled: true,
@@ -2552,6 +2558,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
let mut apply_again = app.applying.is_some() && d.repeat_after_apply;
let mut cancel = false;
let mut enter = false;
+ let mut value_fields = Vec::new();
let wide = matches!(&d.kind, Kind::Motion(k) if k.wide());
let raw_title = title(&d.kind);
@@ -2642,7 +2649,9 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
}
let body = |ui: &mut egui::Ui| {
d.sync_fillet_chain(&app.session);
- enter |= dense::rows(app, ctx, &mut d, ui);
+ let (accepted, fields) = crate::ui_kit::value_fields(ctx, || dense::rows(app, ctx, &mut d, ui));
+ enter |= accepted;
+ value_fields = fields;
d.sync_fillet_chain(&app.session);
if cell_enter {
ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
@@ -2686,6 +2695,31 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
crate::scenario::publish_rect("dialog:outer", r);
crate::scenario::publish_target("dialog:card", r);
}
+ if let Some(field) = crate::ui_kit::canvas_value_focus(ctx) {
+ value_fields.push(field);
+ }
+ // The editor loses the keyboard while the click is still being handled, possibly in a later
+ // pass of the same frame, so give it back once the pointer is idle again.
+ let pointer_busy = ctx.input(|i| i.pointer.any_down() || i.pointer.any_pressed() || i.pointer.any_released());
+ if d.restore_value_focus && !pointer_busy {
+ d.restore_value_focus = false;
+ let target = value_fields.iter().find(|(id, _)| Some(*id) == d.value_focus).or_else(|| value_fields.first());
+ if let Some(&(id, length)) = target {
+ d.focus = false;
+ ctx.memory_mut(|memory| memory.request_focus(id));
+ if let Some(mut state) = egui::text_edit::TextEditState::load(ctx, id) {
+ state
+ .cursor
+ .set_char_range(Some(egui::text_selection::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(length))));
+ state.store(ctx, id);
+ }
+ }
+ }
+ if d.restore_value_focus {
+ ctx.request_repaint();
+ } else {
+ d.value_focus = ctx.memory(|memory| memory.focused()).filter(|id| value_fields.iter().any(|(field, _)| field == id));
+ }
// Keyboard: Enter applies (from a value field, or with nothing focused), Esc cancels.
let (enter_free, esc) = ctx.input(|i| (i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Escape)));
let nothing_focused = ctx.memory(|m| m.focused().is_none());
diff --git a/crates/ui-egui/src/dialogs_all_tests.rs b/crates/ui-egui/src/dialogs_all_tests.rs
index 97b2981704..20f60f0468 100644
--- a/crates/ui-egui/src/dialogs_all_tests.rs
+++ b/crates/ui-egui/src/dialogs_all_tests.rs
@@ -577,3 +577,61 @@ fn coil_from_a_profile_hides_the_primitive_section() {
assert!(p.get("revolutions").is_some() && p.get("pitch").is_some(), "{p}");
assert!(p.get("operation").is_some(), "{p}");
}
+
+fn extrude_focus_fixture() -> Harness {
+ let mut h = Harness::new();
+ h.app.home.open = false;
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.rectangle.two_point", json!({"p0":[0,0],"p1":[50,30]})).unwrap();
+ h.app.run("sketch.finish", json!({})).unwrap();
+ h.call("ui.view", json!({"view":"top"}));
+ h.call("ui.view", json!({"view":"fit"}));
+ h.step(&json!({"start":"solid.extrude"})).unwrap();
+ h.frames(8);
+ h
+}
+
+#[test]
+fn viewport_profile_click_returns_extrude_value_focus() {
+ let mut h = extrude_focus_fixture();
+ h.step(&json!({"click":{"world":[15,10,0]}})).unwrap();
+ h.step(&json!({"text":"30"})).unwrap();
+ assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Extrude { distance, .. } if distance == "30"));
+ // Clicking an already selected profile removes it; pick it again before building.
+ h.step(&json!({"click":{"world":[15,10,0]}})).unwrap();
+ h.step(&json!({"text":"30"})).unwrap();
+ h.step(&json!({"key":"Enter"})).unwrap();
+ h.settle();
+ assert!(h.app.dialog.is_none());
+ let bounds = h.app.session.world_state().bodies.first().unwrap().mesh().bounds();
+ assert!((bounds.max.z - bounds.min.z - 30.0).abs() < 1e-7);
+}
+
+#[test]
+fn viewport_empty_click_falls_back_to_first_numeric_dialog_field() {
+ let mut h = extrude_focus_fixture();
+ if let Some(id) = h.ctx.memory(|m| m.focused()) {
+ h.ctx.memory_mut(|m| m.surrender_focus(id));
+ }
+ h.frames(2);
+ let viewport = h.app.viewport.rect.unwrap();
+ h.step(&json!({"click":[viewport.left()+80.0, viewport.bottom()-80.0]})).unwrap();
+ h.step(&json!({"text":"30"})).unwrap();
+ assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Extrude { distance, .. } if distance == "30"));
+}
+
+#[test]
+fn viewport_click_preserves_active_secondary_dialog_value() {
+ let mut h = extrude_focus_fixture();
+ if let Kind::Extrude { direction, .. } = &mut h.app.dialog.as_mut().unwrap().kind {
+ *direction = crate::dialogs::TWO_SIDES;
+ }
+ h.frames(3);
+ crate::test_support::click(&mut h, "dialog:Distance 2");
+ h.step(&json!({"key":"A","cmd":true})).unwrap();
+ h.step(&json!({"text":"7"})).unwrap();
+ assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Extrude { distance2, .. } if distance2 == "7"));
+ h.step(&json!({"click":{"world":[15,10,0]}})).unwrap();
+ h.step(&json!({"text":"30"})).unwrap();
+ assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Extrude { distance, distance2, .. } if distance == "10 mm" && distance2 == "30"));
+}
diff --git a/crates/ui-egui/src/hole_place_ui_tests.rs b/crates/ui-egui/src/hole_place_ui_tests.rs
index bdeec1f299..edf780914a 100644
--- a/crates/ui-egui/src/hole_place_ui_tests.rs
+++ b/crates/ui-egui/src/hole_place_ui_tests.rs
@@ -133,6 +133,8 @@ fn a_pick_on_a_round_edge_goes_to_its_centre() {
*cb_diameter = "16 mm".into();
*cb_depth = "3 mm".into();
}
+ // Changing the type adds fields; draw them before sending Enter to the focused editor.
+ h.frames(3);
h.step(&json!({"key": "Enter"})).unwrap();
assert!(h.app.dialog.is_none(), "{:?}", h.app.dialog.as_ref().map(|d| &d.error));
let (id, position) = last_hole(&h);
diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs
index 3b660a8fa6..3560349458 100644
--- a/crates/ui-egui/src/ui_kit/fields.rs
+++ b/crates/ui-egui/src/ui_kit/fields.rs
@@ -236,6 +236,14 @@ fn editor_reserving(ui: &mut Ui, value: &mut String, hint: &str, completion: boo
edit = edit.margin(egui::Margin { left: inset, right: inset.saturating_add(reserve.round() as i8), top: 0, bottom: 0 });
}
let response = ui.add_sized(vec2(width, row_height()), edit);
+ if completion && ui.is_enabled() {
+ ui.ctx().data_mut(|data| {
+ let id = egui::Id::new("sc_value_fields");
+ if data.get_temp::<Vec<(egui::Id, usize)>>(id).is_some() {
+ data.get_temp_mut_or_default::<Vec<(egui::Id, usize)>>(id).push((response.id, value.chars().count()));
+ }
+ });
+ }
crate::theme::TabAccent::number_focus(ui, &response);
t.input_bevel(ui, &response);
let label = crate::i18n::tr_label(label(ui));
@@ -636,6 +644,8 @@ pub(crate) fn canvas_value_field(ui: &mut Ui, id: egui::Id, value: &mut String,
}
let response = ui.add(edit);
t.input_bevel(ui, &response);
+ let field = (ui.ctx().cumulative_pass_nr(), response.id, value.chars().count());
+ ui.ctx().data_mut(|data| data.insert_temp(egui::Id::new("sc_canvas_value_focus"), field));
response
}
diff --git a/crates/ui-egui/src/ui_kit/mod.rs b/crates/ui-egui/src/ui_kit/mod.rs
index 02b892bc37..808fb015cf 100644
--- a/crates/ui-egui/src/ui_kit/mod.rs
+++ b/crates/ui-egui/src/ui_kit/mod.rs
@@ -29,6 +29,7 @@ mod shortcuts;
mod status;
mod table;
mod text;
+mod value_focus;
pub(crate) use actions::*;
pub use actions::{Button, button};
@@ -61,6 +62,7 @@ pub(crate) use search::{CommandSearch, SearchBadge, SearchFieldGeometry};
pub(crate) use shortcuts::*;
pub(crate) use status::*;
pub(crate) use table::*;
+pub(crate) use value_focus::*;
#[cfg(test)]
mod gallery;
diff --git a/crates/ui-egui/src/ui_kit/value_focus.rs b/crates/ui-egui/src/ui_kit/value_focus.rs
new file mode 100644
index 0000000000..1d5754ed3a
--- /dev/null
+++ b/crates/ui-egui/src/ui_kit/value_focus.rs
@@ -0,0 +1,18 @@
+//! Keyboard focus bookkeeping for a dialog's expression editors. Not a drawn element, so it
+//! lives outside the element files the gallery guard checks.
+
+/// Collect the expression editors drawn by a form, in display order.
+pub(crate) fn value_fields(ctx: &egui::Context, draw: impl FnOnce() -> bool) -> (bool, Vec<(egui::Id, usize)>) {
+ let id = egui::Id::new("sc_value_fields");
+ ctx.data_mut(|data| data.insert_temp(id, Vec::<(egui::Id, usize)>::new()));
+ let enter = draw();
+ let fields = ctx.data_mut(|data| data.remove_temp::<Vec<(egui::Id, usize)>>(id)).unwrap_or_default();
+ (enter, fields)
+}
+
+/// Only a value box drawn this pass can receive the keyboard after a viewport pick.
+pub(crate) fn canvas_value_focus(ctx: &egui::Context) -> Option<(egui::Id, usize)> {
+ ctx.data(|data| data.get_temp::<(u64, egui::Id, usize)>(egui::Id::new("sc_canvas_value_focus")))
+ .filter(|(pass, _, _)| *pass == ctx.cumulative_pass_nr())
+ .map(|(_, id, length)| (id, length))
+}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index 0e338af82c..627e69c3b5 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -3399,6 +3399,15 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
&& !held
&& app.dialog.is_none()
&& crate::dim_view::pick_reference(ui.ctx(), p, resp.double_clicked());
+ // Capture the press too: text editors lose focus before the click is released. A
+ // running tool keeps the keyboard for its own typed values.
+ if !held
+ && app.tool.is_none()
+ && (resp.clicked() || (resp.contains_pointer() && ui.input(|i| i.pointer.primary_pressed())))
+ && let Some(dialog) = app.dialog.as_mut()
+ {
+ dialog.restore_value_focus = true;
+ }
if resp.clicked() && !held && !reference_pick {
if app.tool.is_some() {
crate::tools::on_click(app, &proj, p, mods.shift);