Skip to content

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

Manage > Units opens the unit picker (#5907) (#5908)

raykholoab9d9ac50f0f
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs
index e0c819061f..d3bff507db 100644
--- a/crates/ui-egui/src/context_menu.rs
+++ b/crates/ui-egui/src/context_menu.rs
@@ -2076,6 +2076,52 @@ mod tests {
assert!(!items(&h.app, &Target::Viewport).iter().any(|i| i.id == "sketch.toggle_driven"));
}
+ #[test]
+ fn units_toolbar_start_offers_choices_and_one_undo_step() {
+ let mut h = crate::scenario::Harness::new();
+ h.app.home.open = false;
+ h.call("ui.start", json!({"command":"document.units"}));
+ assert!(!h.app.status.as_ref().is_some_and(|(_, _, error)| *error));
+ assert_eq!(h.app.menu.target, Target::Units);
+ assert!(h.app.viewport.context_menu.is_some());
+ let menu = describe(&h.app);
+ let choices = menu["items"].as_array().unwrap();
+ assert_eq!(choices.iter().map(|item| item["label"].as_str().unwrap()).collect::<Vec<_>>(), crate::prefs::UNITS);
+ assert!(choices.iter().all(|item| item["enabled"] == true));
+ assert!(h.app.session.undo.is_empty());
+ h.call("ui.menuPick", json!({"item":"in"}));
+ assert_eq!(h.app.session.doc.units, "in");
+ assert!(h.app.viewport.context_menu.is_none());
+ assert_eq!(h.app.session.undo.len(), 1);
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(h.app.session.doc.units, "mm");
+ h.app.run("edit.redo", json!({})).unwrap();
+ assert_eq!(h.app.session.doc.units, "in");
+ }
+
+ #[test]
+ fn units_toolbar_menu_is_anchored_and_programmatic_calls_stay_headless() {
+ let mut h = crate::scenario::Harness::new();
+ h.app.home.open = false;
+ let missing = h.call("engine.execute", json!({"command":"document.units","params":{}}));
+ assert_eq!(missing["ok"], false);
+ assert!(h.app.viewport.context_menu.is_none());
+ let changed = h.call("engine.execute", json!({"command":"document.units","params":{"units":"cm"}}));
+ assert_eq!(changed["ok"], true);
+ assert_eq!(h.app.session.doc.units, "cm");
+ assert!(h.app.viewport.context_menu.is_none());
+ h.app.status = None;
+ h.step(&json!({"click":{"handle":"tab:MANAGE"}})).unwrap();
+ let button = crate::scenario::rect_of("button:document.units").unwrap();
+ h.step(&json!({"click":{"handle":"button:document.units"}})).unwrap();
+ assert_eq!(h.app.viewport.context_menu, Some(button.left_bottom()));
+ assert_eq!(h.app.menu.target, Target::Units);
+ assert!(!h.app.status.as_ref().is_some_and(|(_, _, error)| *error));
+ assert_eq!(items(&h.app, &Target::Units).iter().find(|item| item.label == "cm").unwrap().shortcut, "current");
+ h.step(&json!({"click":{"handle":"button:document.units"}})).unwrap();
+ assert!(h.app.viewport.context_menu.is_none());
+ }
+
#[test]
fn line_offset_context_menu_breaks_association_and_allows_trim() {
use solidcraft_engine::{geom::Vec2, sketch::Shape};
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 169b7b220e..2d19edc941 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -967,19 +967,24 @@ impl SolveApp {
true
}
- /// Start a command the way a toolbar click does: interactive tools and dialogs for commands
+ /// Start a command the way a toolbar click does: interactive tools, menus and dialogs for commands
/// that have them, otherwise run it with defaults.
pub fn start(&mut self, id: &str) {
- self.start_with_variant_memory(id, true);
+ self.start_at(id, None);
+ }
+
+ /// Start an interactive command with its menu anchored below the invoking button.
+ pub(crate) fn start_at(&mut self, id: &str, opener: Option<egui::Rect>) {
+ self.start_with_variant_memory(id, true, opener);
}
/// A dedicated toolbar button starts its exact tool without replacing the sketch family's
/// main button. Choosing a variant from a menu still uses `start` and remembers that choice.
- pub(crate) fn start_pinned(&mut self, id: &str) {
- self.start_with_variant_memory(id, false);
+ pub(crate) fn start_pinned(&mut self, id: &str, opener: Option<egui::Rect>) {
+ self.start_with_variant_memory(id, false, opener);
}
- fn start_with_variant_memory(&mut self, id: &str, remember_variant: bool) {
+ fn start_with_variant_memory(&mut self, id: &str, remember_variant: bool, opener: Option<egui::Rect>) {
if id == "ui.palette" {
shortcut_box::open(self, None);
return;
@@ -1105,6 +1110,15 @@ impl SolveApp {
if remember_variant && solidcraft_engine::find_command(id).is_some_and(|c| c.info(&self.session).enabled) {
self.preferences.sketch_variants.remember(id);
}
+ if id == "document.units" {
+ if let Some(rect) = opener {
+ context_menu::toggle_for(self, rect, rect.left_bottom(), context_menu::Target::Units);
+ } else {
+ let at = self.viewport.rect.map(|rect| rect.center()).unwrap_or(egui::pos2(300.0, 200.0));
+ context_menu::open_for(self, at, context_menu::Target::Units);
+ }
+ return;
+ }
if id == "parameters.change" {
params_dialog::open(self);
return;
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index 8477cf0d9d..475b2f0b7a 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -1318,7 +1318,7 @@ pub fn toolbar(app: &mut SolveApp, ui: &mut egui::Ui) {
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled() && info.enabled, &tr_label));
if ribbon_tooltip(app, resp, selected, info.disabled_reason.as_deref()).clicked() && info.enabled {
let id = selected.id;
- app.start_pinned(id);
+ app.start_pinned(id, Some(whole));
}
if let Some(f) = family {
let ar = if labels {
@@ -1410,7 +1410,7 @@ pub fn toolbar(app: &mut SolveApp, ui: &mut egui::Ui) {
menu = Some((resp.interact_pointer_pos().unwrap_or(cell.left_bottom()), c.id));
}
if ribbon_tooltip(app, resp, c, info.disabled_reason.as_deref()).clicked() && info.enabled {
- app.start(c.id);
+ app.start_at(c.id, Some(cell));
}
}
}
@@ -1531,8 +1531,8 @@ pub fn toolbar(app: &mut SolveApp, ui: &mut egui::Ui) {
RowAction::None => {}
RowAction::Menu(at) => menu = Some((at, c.id)),
RowAction::TogglePin => toggle = Some(c.id),
- RowAction::Start => {
- app.start(c.id);
+ RowAction::Start(rect) => {
+ app.start_at(c.id, Some(rect));
ui.ctx().data_mut(|d| d.remove::<egui::Id>(egui::Id::new("sc_panel_open")));
}
}
@@ -1579,7 +1579,7 @@ pub fn toolbar(app: &mut SolveApp, ui: &mut egui::Ui) {
);
crate::scenario::publish_handle(&format!("toolbar:{}", c.id), response.rect.center());
if response.clicked() {
- app.start(c.id);
+ app.start_at(c.id, Some(response.rect));
ui.close();
}
}
@@ -1662,7 +1662,7 @@ fn assistant_button(app: &mut SolveApp, ui: &mut egui::Ui, at: Rect) {
/// What a click on a panel drop-down row asks for.
enum RowAction {
None,
- Start,
+ Start(Rect),
Menu(egui::Pos2),
TogglePin,
}
@@ -1826,7 +1826,7 @@ fn dropdown_row(app: &SolveApp, ui: &mut egui::Ui, c: &'static solidcraft_engine
return RowAction::Menu(resp.interact_pointer_pos().unwrap_or(rect.left_bottom()));
}
if resp.clicked() && info.enabled {
- return RowAction::Start;
+ return RowAction::Start(rect);
}
RowAction::None
}
@@ -1910,8 +1910,8 @@ fn sketch_menu_rows(
RowAction::None => {}
RowAction::Menu(at) => *menu = Some((at, c.id)),
RowAction::TogglePin => *toggle = Some(c.id),
- RowAction::Start => {
- app.start(c.id);
+ RowAction::Start(rect) => {
+ app.start_at(c.id, Some(rect));
ui.ctx().data_mut(|d| d.remove::<egui::Id>(egui::Id::new("sc_panel_open")));
ui.close();
}