From 9995ffc36c7dc4beab3e10e6e8cc2ceb7d28056c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 09:53:40 -0800 Subject: [PATCH] Image Trace: drop an unfinished slider value when the selection changes A value typed into an Image Trace slider row or the Divider and not yet committed stayed in the field after another object was selected, so pressing Enter applied it to the newly selected object. The settings sliders (Threshold, Colors, Paths, Corners, Noise, Stroke) and the Divider are now keyed on the selection, so another selection starts a fresh field. This stands in for SliderField::edit_target (storytold/craft-libs#5024) until the craft-libs pin includes it; the pin bump replaces each id_salt with edit_target. --- crates/ui-egui/src/panels/image_trace.rs | 13 ++++- crates/ui-egui/src/panels/image_trace/rows.rs | 29 +++++++--- .../ui-egui/src/panels/image_trace/tests.rs | 58 +++++++++++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/panels/image_trace.rs b/crates/ui-egui/src/panels/image_trace.rs index 372d3117aa..fb30859192 100644 --- a/crates/ui-egui/src/panels/image_trace.rs +++ b/crates/ui-egui/src/panels/image_trace.rs @@ -299,9 +299,18 @@ pub(crate) fn split_row(app: &mut VectorcraftApp, ui: &mut Ui) { let Some(split) = selected_split(app) else { return }; // The canvas sets the divider to a ten-thousandth: a hundredth of a percent. let pct = (split * 10_000.0).round() / 100.0; + // Workaround until the craft-libs pin includes craft-libs#5024: keying the field on the selection drops a value + // typed for one object when another is selected. The pin bump replaces this with `edit_target`. + let target = crate::panels::selection_target(app); let edit = Field::new(tl!("Divider")) .show(ui, |ui, _| { - SliderField::new(tl!("Divider"), Bind::given(pct), 0.0..=100.0).layout(SliderFieldLayout::Inline).step(1.0).unit("%").show(ui).edit + SliderField::new(tl!("Divider"), Bind::given(pct), 0.0..=100.0) + .layout(SliderFieldLayout::Inline) + .step(1.0) + .unit("%") + .id_salt(("divider", &target)) + .show(ui) + .edit }) .inner; let Some(edit) = edit else { return }; @@ -1031,7 +1040,7 @@ fn show_settings(app: &mut VectorcraftApp, ui: &mut Ui, st: &mut TraceUi, list: } split_row(app, ui); - let data = RowsData::of(app); + let data = RowsData::of_selection(app); let sampler = Sampler { enabled: t.is_trace || t.is_image, armed: app.ui.trace_ignore_sampler.is_some() }; let (main, advanced) = rows::settings_group(ui, |ui| { let main = rows::main_rows(ui, &mut st.params, &data, RowsHost::Panel); diff --git a/crates/ui-egui/src/panels/image_trace/rows.rs b/crates/ui-egui/src/panels/image_trace/rows.rs index 2c87a42d50..cbcecc07bc 100644 --- a/crates/ui-egui/src/panels/image_trace/rows.rs +++ b/crates/ui-egui/src/panels/image_trace/rows.rs @@ -22,7 +22,7 @@ use vectorcraft_engine::cmd::swatchlib::DOCUMENT_SWATCHES; use super::{MODES, PALETTES, ignore_color, palette_label}; use crate::VectorcraftApp; -use crate::panels::swatches; +use crate::panels::{SelectionTarget, selection_target, swatches}; /// Where the rows are shown. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -40,13 +40,21 @@ pub(crate) struct RowsData { pub(crate) libraries: Vec>, /// The active document's colour group names. pub(crate) groups: Vec, + /// The selection whose settings the rows show, for a host that follows the selection (the panel). A slider's + /// unfinished value is dropped when it changes. + pub(crate) target: Option, } impl RowsData { /// The lists for the active document. pub(crate) fn of(app: &VectorcraftApp) -> Self { // PLAN: assumed `swatches::library_entries(app: &VectorcraftApp) -> Vec>` (color-w10). - RowsData { libraries: swatches::library_entries(app), groups: swatch_groups(app) } + RowsData { libraries: swatches::library_entries(app), groups: swatch_groups(app), target: None } + } + + /// The lists for the active document, for rows that show the selection's settings. + pub(crate) fn of_selection(app: &VectorcraftApp) -> Self { + RowsData { target: Some(selection_target(app)), ..Self::of(app) } } } @@ -151,13 +159,18 @@ fn slider(key: &'static str, label: &'static str, range: RangeInclusive, un Slider { key, label, range, unit, default: 0.0 } } -fn slider_row(ui: &mut Ui, params: &mut Value, spec: Slider, enabled: bool, out: &mut RowsOutput) { +fn slider_row(ui: &mut Ui, params: &mut Value, data: &RowsData, spec: Slider, enabled: bool, out: &mut RowsOutput) { let Slider { key, label, range, unit, default } = spec; let value = params[key].as_f64().unwrap_or(default); let events = Field::new(label) .enabled(enabled) .show(ui, |ui, _| { let mut field = SliderField::new(label, Bind::given(value), range.clone()).layout(SliderFieldLayout::Inline).step(1.0).enabled(enabled); + // Workaround until the craft-libs pin includes craft-libs#5024: keying the field on the selection drops a + // value typed for one object when another is selected. The pin bump replaces this with `edit_target`. + if let Some(target) = &data.target { + field = field.id_salt((key, target)); + } if let Some(unit) = unit { field = field.unit(unit); } @@ -234,14 +247,14 @@ pub(crate) fn main_rows(ui: &mut Ui, params: &mut Value, data: &RowsData, host: _ if matches!(palette.as_str(), "fullTone" | "automatic") => slider("colorDetail", tl!("Colors"), 0.0..=100.0, Some("%")), _ => slider("colors", tl!("Colors"), 2.0..=256.0, None), }; - slider_row(ui, params, count, true, &mut out); + slider_row(ui, params, data, count, true, &mut out); out } /// The Advanced settings: fidelity, method, Create, snapping, shapes, grouping, gradients and Ignore Color, with an -/// eyedropper for Ignore Color when the host passes a `sampler`. `_data` and `_host` are the arguments of +/// eyedropper for Ignore Color when the host passes a `sampler`. `data` and `_host` are the arguments of /// [`main_rows`], so a host passes the same set to both. -pub(crate) fn advanced_rows(ui: &mut Ui, params: &mut Value, _data: &RowsData, _host: RowsHost, sampler: Option) -> RowsOutput { +pub(crate) fn advanced_rows(ui: &mut Ui, params: &mut Value, data: &RowsData, _host: RowsHost, sampler: Option) -> RowsOutput { let mut out = RowsOutput::default(); let mode = text(params, "mode", "blackAndWhite"); for spec in [ @@ -249,7 +262,7 @@ pub(crate) fn advanced_rows(ui: &mut Ui, params: &mut Value, _data: &RowsData, _ slider("corners", tl!("Corners"), 0.0..=100.0, Some("%")), slider("noise", tl!("Noise"), 1.0..=100.0, Some("px")), ] { - slider_row(ui, params, spec, true, &mut out); + slider_row(ui, params, data, spec, true, &mut out); } // Flat Logo always paints biggest shapes first with a hair of overlap, so there is no choice. if mode != "logo" { @@ -282,7 +295,7 @@ pub(crate) fn advanced_rows(ui: &mut Ui, params: &mut Value, _data: &RowsData, _ params[key] = json!(!create(params, key)); out.picked(); } - slider_row(ui, params, Slider { default: 10.0, ..slider("strokeWidth", tl!("Stroke"), 1.0..=100.0, Some("px")) }, strokes, &mut out); + slider_row(ui, params, data, Slider { default: 10.0, ..slider("strokeWidth", tl!("Stroke"), 1.0..=100.0, Some("px")) }, strokes, &mut out); } // (Black and White has one colour and Flat Logo flat ones: no gradients to find.) let gradients = mode == "grayscale" || mode == "color"; diff --git a/crates/ui-egui/src/panels/image_trace/tests.rs b/crates/ui-egui/src/panels/image_trace/tests.rs index 00516ffd34..c97e3e1c27 100644 --- a/crates/ui-egui/src/panels/image_trace/tests.rs +++ b/crates/ui-egui/src/panels/image_trace/tests.rs @@ -599,6 +599,64 @@ fn the_divider_slider_keeps_an_exact_divider_and_a_drag_is_one_undo_step() { assert_eq!(undo(&panel.app()), steps + 1, "one undo step for the edit"); } +/// Two traced objects with the same settings, the first one selected: the app, the first and the second object. +fn two_traced_objects(app: VectorcraftApp) -> (VectorcraftApp, NodeId, NodeId) { + let mut app = app; + let a = first_selected(&app).unwrap().id; + app.run("edit.duplicate", json!({})).unwrap(); + let b = first_selected(&app).unwrap().id; + assert_ne!(a, b); + app.run("select.set", json!({"ids": [a.0]})).unwrap(); + (app, a, b) +} + +/// Types `text` into the number box of the slider row `name` without finishing the edit. +fn type_unfinished(panel: &mut Panel, name: &str, text: &str) { + panel.click_role(name, craft::ui::contract::Role::SPIN_BUTTON); + panel.key(egui::Key::A, egui::Modifiers::COMMAND); + panel.type_text(text); +} + +/// A value typed into a settings slider for one Image Trace object, and not finished, is dropped when another +/// object is selected: Enter afterwards changes neither object. +#[test] +fn an_unfinished_slider_value_is_dropped_when_another_object_is_selected() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let (app, a, b) = two_traced_objects(traced_app()); + let mut panel = panel_over(app); + panel.frame(); + let threshold = param(&panel.app(), "threshold"); + let steps = undo(&panel.app()); + type_unfinished(&mut panel, "Threshold", "3"); + select(&mut panel, b); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(param(&panel.app(), "threshold"), threshold, "B keeps its threshold"); + assert_eq!(undo(&panel.app()), steps, "nothing was traced"); + select(&mut panel, a); + assert_eq!(param(&panel.app(), "threshold"), threshold, "A keeps its threshold"); +} + +/// A value typed into the Divider for one Image Trace object, and not finished, is dropped when another object is +/// selected: Enter afterwards moves neither divider. +#[test] +fn an_unfinished_divider_value_is_dropped_when_another_object_is_selected() { + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = traced_app(); + app.run("imageTrace.setView", json!({"view": "split", "split": 0.4})).unwrap(); + let (app, a, b) = two_traced_objects(app); + let mut panel = panel_over(app); + panel.frame(); + let steps = undo(&panel.app()); + type_unfinished(&mut panel, "Divider", "8"); + select(&mut panel, b); + assert_eq!(first_selected(&panel.app()).unwrap().trace_split(), 0.4, "B has the same divider"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(first_selected(&panel.app()).unwrap().trace_split(), 0.4, "B keeps its divider"); + assert_eq!(undo(&panel.app()), steps, "no undo step"); + select(&mut panel, a); + assert_eq!(first_selected(&panel.app()).unwrap().trace_split(), 0.4, "A keeps its divider"); +} + /// Properties shows the Divider slider of the selected Image Trace object in the Split view only, /// with its name and value for screen readers. #[test]