Sketch tools come back in the variant used last (#4308)

* Sketch tools come back in the variant used last, and it is kept in preferences

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Scenario: a new design keeps the remembered sketch variant

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bherbruck
2026-10-21 11:22:04 -04:00
committed by GitHub
parent 54072b7014
commit 4b37c046e4
7 changed files with 60 additions and 17 deletions
-4
View File
@@ -32,7 +32,6 @@ pub struct DocSlot {
#[derive(Clone, Debug, Default)]
struct DocView {
render: crate::render_workspace::ViewState,
sketch_choices: crate::sketch_families::Choices,
hidden_profiles: Vec<u64>,
shown_dims: Vec<u64>,
locked_bodies: Vec<String>,
@@ -87,7 +86,6 @@ fn take_view(app: &mut SolveApp) -> DocView {
let u = &mut app.ui;
DocView {
render: std::mem::take(&mut app.render_workspace.view),
sketch_choices: std::mem::take(&mut app.sketch_choices),
hidden_profiles: std::mem::take(&mut u.hidden_profiles),
shown_dims: std::mem::take(&mut u.shown_dims),
locked_bodies: std::mem::take(&mut u.locked_bodies),
@@ -98,7 +96,6 @@ fn put_view(app: &mut SolveApp, v: DocView) {
app.render_workspace.view = v.render;
crate::render_workspace::invalidate_scene(app);
crate::render_workspace::sync_backend(app);
app.sketch_choices = v.sketch_choices;
let u = &mut app.ui;
u.hidden_profiles = v.hidden_profiles;
u.shown_dims = v.shown_dims;
@@ -200,7 +197,6 @@ pub fn adopt(app: &mut SolveApp, session: Session) {
crate::sketch_attachment_ui::clear(app);
app.session.replace_design(session);
crate::render_workspace::reset_document(app);
app.sketch_choices = crate::sketch_families::Choices::default();
app.session.invalidate_view(rev);
app.cam = Camera::default();
app.fit_view();
+2 -2
View File
@@ -116,11 +116,11 @@ pub fn command_for(app: &SolveApp, key: &str) -> Option<&'static str> {
/// starts in the new sketch.
pub fn press(app: &mut SolveApp, key: &str) {
if let Some(id) = command_for(app, key) {
app.start(id);
app.start(app.preferences.sketch_variants.resolve(id));
} else if let Some(id) = sketch_tool_for(app, key) {
app.start("sketch.create");
if let Some(d) = app.dialog.as_mut().filter(|d| matches!(d.kind, crate::dialogs::Kind::Sketch)) {
d.then_start = Some(id);
d.then_start = Some(app.preferences.sketch_variants.resolve(id));
}
}
}
+1 -4
View File
@@ -401,7 +401,6 @@ pub struct SolveApp {
pub install_steps: Option<install::Steps>,
/// Toolbar buttons the user pinned, removed or reordered.
pub toolbar_custom: toolbar_custom::ToolbarCustom,
pub sketch_choices: sketch_families::Choices,
/// Preferences kept between runs, and their window.
pub preferences: prefs::Prefs,
pub prefs_window: prefs::PrefsWindow,
@@ -549,7 +548,6 @@ impl SolveApp {
updater: update::Updater::default(),
install_steps: None,
toolbar_custom: toolbar_custom::ToolbarCustom::default(),
sketch_choices: sketch_families::Choices::default(),
preferences: prefs::Prefs::default(),
prefs_window: prefs::PrefsWindow::default(),
slicer_menu: dialogs_save_mesh::SlicerMenu::default(),
@@ -795,7 +793,6 @@ impl SolveApp {
}
if matches!(id, "file.new" | "doc.open" | "doc.recover") {
render_workspace::reset_document(self);
self.sketch_choices = sketch_families::Choices::default();
dim_view::cancel();
self.menu.props = None;
sketch_attachment_ui::clear(self);
@@ -933,7 +930,7 @@ impl SolveApp {
return;
}
if solidcraft_engine::find_command(id).is_some_and(|c| c.info(&self.session).enabled) {
self.sketch_choices.remember(id);
self.preferences.sketch_variants.remember(id);
}
if id == "parameters.change" {
params_dialog::open(self);
+3
View File
@@ -447,6 +447,9 @@ preference_settings! {
update: crate::update::Memo = crate::update::Memo::default();
/// Save as mesh: the last format (`stl` or `3mf`), whether it was sent to a slicer, which one
/// (an installed slicer's id or a program path), and the programs the user added (newest first).
/// The variant last started in each split tool family (Rectangle, Circle, Arc, Slot, Polygon, Line,
/// Spline): the family's button and shortcut start it again (`sketch_families`).
sketch_variants: crate::sketch_families::Choices = crate::sketch_families::Choices::default();
save_mesh_format: String = "stl".into();
/// Hidden edges in the current-view DXF export (false: omit, true: dashed).
view_dxf_dashed: bool = false;
+52 -5
View File
@@ -44,21 +44,34 @@ pub fn family(id: &str) -> Option<&'static Family> {
FAMILIES.iter().find(|f| f.commands.contains(&id))
}
/// UI state only, swapped with document tabs; never dirties the model or its undo history.
#[derive(Clone, Debug, Default)]
pub struct Choices(BTreeMap<&'static str, &'static str>);
/// The variant last started in each family. A user preference, so it survives new and opened
/// designs and restarts; it never dirties the model or its undo history. Keys are family labels,
/// values command ids; anything that is not a variant of its family is ignored when read.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct Choices(BTreeMap<String, String>);
impl Choices {
pub fn remember(&mut self, id: &str) {
if let Some(f) = family(id)
&& let Some(id) = f.commands.iter().find(|c| **c == id)
{
self.0.insert(f.label, id);
self.0.insert(f.label.to_string(), (*id).to_string());
}
}
pub fn selected(&self, f: &Family) -> Option<&'static CommandSpec> {
self.0.get(f.label).copied().or_else(|| f.commands.first().copied()).and_then(solidcraft_engine::find_command)
let saved = self.0.get(f.label).and_then(|s| f.commands.iter().find(|c| **c == s.as_str()));
saved.or_else(|| f.commands.first()).copied().and_then(solidcraft_engine::find_command)
}
/// What a family's main button or shortcut starts: its remembered variant when `id` is the
/// family's default command (the one carrying the shortcut), otherwise `id` itself.
pub fn resolve(&self, id: &'static str) -> &'static str {
match family(id) {
Some(f) if f.commands.first().copied() == Some(id) => self.selected(f).map_or(id, |c| c.id),
_ => id,
}
}
}
@@ -93,4 +106,38 @@ mod tests {
assert_eq!(choices.selected(family(id).unwrap()).unwrap().id, expected);
}
}
#[test]
fn rectangle_comes_back_in_the_variant_last_used_and_it_survives_a_prefs_reload() {
let mut h = crate::scenario::Harness::new();
h.app.run("sketch.create", serde_json::json!({"plane":"XY"})).unwrap();
h.app.start("sketch.rectangle.three_point");
h.settle();
assert_eq!(h.app.tool.as_ref().map(|t| t.cmd), Some("sketch.rectangle.three_point"));
h.app.tool = None;
// The main button and the shortcut both start the family's default command.
let rect = family("sketch.rectangle.two_point").unwrap();
assert_eq!(h.app.preferences.sketch_variants.selected(rect).unwrap().id, "sketch.rectangle.three_point");
assert_eq!(h.app.preferences.sketch_variants.resolve("sketch.rectangle.two_point"), "sketch.rectangle.three_point");
h.app.start(h.app.preferences.sketch_variants.resolve("sketch.rectangle.two_point"));
assert_eq!(h.app.tool.as_ref().map(|t| t.cmd), Some("sketch.rectangle.three_point"));
// Another family is untouched, and a non-default variant is never rewritten.
assert_eq!(h.app.preferences.sketch_variants.resolve("sketch.circle.center"), "sketch.circle.center");
assert_eq!(h.app.preferences.sketch_variants.resolve("sketch.rectangle.center"), "sketch.rectangle.center");
// A new design keeps it; so does a restart.
let mut restored = crate::SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
assert_eq!(restored.preferences.sketch_variants.resolve("sketch.rectangle.two_point"), "sketch.rectangle.three_point");
}
#[test]
fn old_or_damaged_prefs_fall_back_to_each_familys_first_variant() {
let mut app = crate::SolveApp::new(Default::default(), Default::default());
app.load_prefs(r#"{"preferences":{"save_mesh_format":"3mf"}}"#);
assert_eq!(app.preferences.sketch_variants.resolve("sketch.rectangle.two_point"), "sketch.rectangle.two_point");
app.load_prefs(r#"{"preferences":{"sketch_variants":{"Rectangle":"sketch.circle.center","Gone":"x"}}}"#);
assert_eq!(app.preferences.sketch_variants.resolve("sketch.rectangle.two_point"), "sketch.rectangle.two_point");
app.load_prefs(r#"{"preferences":{"sketch_variants":7}}"#);
assert_eq!(app.preferences.sketch_variants.resolve("sketch.polygon.inscribed"), "sketch.polygon.inscribed");
}
}
+1 -1
View File
@@ -1080,7 +1080,7 @@ pub fn toolbar(app: &mut SolveApp, ui: &mut egui::Ui) {
// Only the family's default pin follows the part's choice. Explicitly pinned
// variants remain exact commands, including in customised toolbars.
let family = crate::sketch_families::family(c.id).filter(|f| sketch_create && f.commands.first().copied() == Some(c.id));
let selected = family.and_then(|f| app.sketch_choices.selected(f)).unwrap_or(c);
let selected = family.and_then(|f| app.preferences.sketch_variants.selected(f)).unwrap_or(c);
let whole = Rect::from_min_size(pos2(button_x, top + 2.0), vec2(button_width, BUTTON_HEIGHT));
button_x += button_width + BUTTON_GAP;
let mut br = whole;
@@ -19,7 +19,7 @@
{"call":"ui.documents","params":{"action":"new"}},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"click":{"handle":"button:sketch.rectangle.two_point"}},
{"expect":{"tool":"sketch.rectangle.two_point"}},
{"expect":{"tool":"sketch.rectangle.center"}},
{"key":"Escape"},
{"call":"ui.documents","params":{"action":"switch","index":0}},
{"click":{"handle":"button:sketch.rectangle.two_point"}},