From 2bf98c9acd57fb3042defbfe271fa5af866537f3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:49:33 -0800 Subject: [PATCH] Gradient: an unfinished field value stays with the stop, point or gradient it was typed for The Gradient panel's number fields kept an unfinished value when the panel switched to another stop, midpoint, freeform point or object with the same value. With stop 2 selected, typing `20+` into Opacity and then selecting stop 3 left `20+` in the field, and typing `1` with Enter gave stop 3 21% instead of 1%. Location, Angle, Aspect Ratio, Repeats and a freeform point's Opacity and Spread did the same. Each of these fields now passes what it edits to `NumberField::edit_target`: the gradient behind the active proxy (`Session::gradient_owner`), the selected objects and symbol part, and the selected stop, midpoint or point. When that key changes, the field drops the unfinished text and shows the new target's value. `StopOwner` derives `Eq` and `Hash` so it can be part of the key. --- crates/engine/src/cmd/gradient.rs | 4 +- crates/ui-egui/src/panels/gradient.rs | 32 +++++- crates/ui-egui/src/panels/gradient/tests.rs | 116 ++++++++++++++++++++ 3 files changed, 146 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 01e0a5a732..2a32f3b7e6 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -88,8 +88,8 @@ fn select_stop(s: &mut Session, p: &Value) -> Result { /// paint for (the Live Paint Bucket, or the Shape Builder's Fill proxy). Selecting other art, /// picking a part of the instance, toggling the proxy, picking another item or choosing such a /// tool or leaving it leaves no stop selected. The Gradient panel keys its selected midpoint -/// diamond by it as well ([`Session::gradient_owner`]). -#[derive(Clone, Copy, Debug, PartialEq)] +/// diamond and its fields' unfinished values by it as well ([`Session::gradient_owner`]). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct StopOwner { /// The active document's uid, which stays the same when its tab moves. doc: Option, diff --git a/crates/ui-egui/src/panels/gradient.rs b/crates/ui-egui/src/panels/gradient.rs index c75d2f7c80..5feb4b231a 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -291,6 +291,15 @@ fn selected_mid(app: &VectorcraftApp, stops: usize) -> Option { (app.session.selected_stop().is_none() && i + 1 < stops && owner == app.session.gradient_owner()).then_some(i) } +/// What the panel's number fields edit: the gradient behind the active proxy +/// ([`vectorcraft_engine::Session::gradient_owner`]) on the selected objects and the selected part of a symbol +/// instance. Each field passes it to [`NumberField::edit_target`], with the stop, midpoint or point it shows, so a value +/// left unfinished in the field is dropped when another one is selected. +fn field_target(app: &VectorcraftApp) -> egui::IdSalt { + let selection = app.session.active().map(|st| (&st.selection.objects, st.selection.symbol_part.as_ref().map(|p| (p.instance, &p.path)))); + egui::IdSalt::new((app.session.gradient_owner(), selection)) +} + /// A stored fraction as the percent a field shows, without the `f32` noise (`0.3` is `30`, not `30.000001`). fn percent(v: f32) -> f64 { (f64::from(v) * 1e6).round() / 1e4 @@ -430,6 +439,7 @@ fn angle_field(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, placed: .range(-180.0..=180.0) .presets(&presets) .enabled(placed) + .edit_target(field_target(app)) .show(ui); apply_number(app, &field.edit, |app, a, phase| edit(app, json!({"angle": a}), phase)); } @@ -446,8 +456,13 @@ fn aspect_field(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, placed let mirror = g.geom.map_or(1.0, |x| if x.is_mirrored() && kind == GradientKind::Conic { -1.0 } else { 1.0 }); let asp = g.geom.map_or(100.0, |x| (x.aspect.abs() * 1000.0).round() / 10.0); let presets = number_presets(&ASPECT_PRESETS, "%"); - let field = - NumberField::new(tl!("Aspect Ratio"), Bind::given(Some(asp))).unit("%").range(0.5..=f64::MAX).presets(&presets).enabled(radial).show(ui); + let field = NumberField::new(tl!("Aspect Ratio"), Bind::given(Some(asp))) + .unit("%") + .range(0.5..=f64::MAX) + .presets(&presets) + .enabled(radial) + .edit_target(field_target(app)) + .show(ui); apply_number(app, &field.edit, |app, a, phase| edit(app, json!({"aspect": a * mirror}), phase)); }); } @@ -492,6 +507,7 @@ fn spread_select(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gr .commit_decimals(0) .presets(&presets) .enabled(is_grad && !current.is_pad()) + .edit_target(field_target(app)) .show(ui); apply_number(app, &repeats.edit, |app, r, phase| edit(app, json!({"repeats": r.round()}), phase)); }); @@ -596,11 +612,12 @@ fn freeform_rows(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint) { Text::new(p.color.to_hex().to_uppercase()).shade(Shade::Muted).show(ui); } }); + let target = (field_target(app), sel); Row::new().show(ui, |ui, _| { for (label, key, value) in [(tl!("Opacity"), "opacity", point.map(|p| p.opacity)), (tl!("Spread"), "spread", point.map(|p| p.spread))] { // Blank while no point is selected. let shown = value.map(|v| (f64::from(v) * 1000.0).round() / 10.0); - let field = NumberField::new(label, Bind::given(shown)).range(0.0..=100.0).unit("%").enabled(sel.is_some()).show(ui); + let field = NumberField::new(label, Bind::given(shown)).range(0.0..=100.0).unit("%").enabled(sel.is_some()).edit_target(target).show(ui); if let Some(Some(v)) = field.edit.events.committed() && let Some(i) = sel { @@ -696,12 +713,18 @@ fn host_fields(app: &mut VectorcraftApp, ui: &mut Ui, g: &Gradient, is_grad: boo } }); } + let target = field_target(app); // In a narrow panel the row wraps; each caption stays on the line of its field. Row::new().wrap(true).show(ui, |ui, _| { Row::new().show(ui, |ui, _| { Text::new(tl!("Opacity:")).shade(Shade::Muted).show(ui); let shown = stop.map_or(100.0, |s| f64::from(s.opacity) * 100.0); - let opacity = NumberField::new(tl!("Opacity"), Bind::given(Some(shown))).range(0.0..=100.0).unit("%").enabled(sel.is_some()).show(ui); + let opacity = NumberField::new(tl!("Opacity"), Bind::given(Some(shown))) + .range(0.0..=100.0) + .unit("%") + .enabled(sel.is_some()) + .edit_target((target, sel)) + .show(ui); apply_number(app, &opacity.edit, |app, v, phase| { let Some(i) = sel else { return }; let mut v2 = stops.clone(); @@ -722,6 +745,7 @@ fn host_fields(app: &mut VectorcraftApp, ui: &mut Ui, g: &Gradient, is_grad: boo .range(0.0..=100.0) .unit("%") .enabled(sel.is_some() || mid.is_some()) + .edit_target((target, sel, mid)) .show(ui); apply_number(app, &location.edit, |app, v, phase| { let v = (v / 100.0) as f32; diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index 00c56f82a2..3ad8f26f0b 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -192,6 +192,22 @@ impl Rig { self.fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text(text).key(Key::Enter, Modifiers::NONE).settle(); } + /// The text the field named `label` shows. + fn text(&self, label: &str) -> Option { + self.fixture.tree().value_of(self.target(label).node).map(str::to_owned) + } + + /// Type `text` over the text of the field named `label` without committing it. + fn type_unfinished(&mut self, label: &str, text: &str) { + let field = self.target(label); + self.fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text(text).settle(); + } + + /// Type `text` into the focused field and commit it with Enter. + fn finish(&mut self, text: &str) { + self.fixture.type_text(text).key(Key::Enter, Modifiers::NONE).settle(); + } + /// Drag from `from` and release at `to`. fn drag(&mut self, from: Pos2, to: Pos2) { self.fixture.drag_at(from, to - from).settle(); @@ -280,6 +296,106 @@ fn a_selected_stop_and_a_picked_diamond_stay_selected_when_the_document_tab_move assert_eq!(rig.number("Location"), Some(30.0), "Location still shows the midpoint"); } +/// An unfinished Opacity or Location value belongs to the stop or midpoint it was typed for: after another stop, a +/// midpoint or another object's stop is selected, the field shows the new target's value, even where it is the same, +/// and further typing starts a new value instead of finishing the old expression. +#[test] +fn an_unfinished_stop_value_does_not_reach_another_stop() { + let stops = json!([ + {"offset": 0, "color": "#ffffff"}, + {"offset": 0.5, "color": "#808080"}, + {"offset": 0.5, "color": "#404040"}, + {"offset": 1, "color": "#000000"} + ]); + let mut app = app(json!({"gradient": {"stops": stops.clone()}})); + let a = app.session.active().unwrap().selection.objects[0]; + let b = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap()["id"].clone(); + app.run("paint.setFill", json!({"gradient": {"stops": stops}})).unwrap(); + app.run("select.set", json!({"ids": [a.0]})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + let stops_of = |rig: &Rig, id: NodeId| match rig.app().session.active().unwrap().doc.node(id).unwrap().appearance.fill_paint() { + Paint::Gradient(g) => g.gradient.stops.iter().map(|s| (s.offset, s.opacity)).collect::>(), + p => panic!("expected a gradient fill, got {p:?}"), + }; + let mut rig = Rig::new(app); + + rig.type_unfinished("Opacity", "20+"); + rig.app().run("gradient.selectStop", json!({"index": 2})).unwrap(); + rig.fixture.settle(); + assert_eq!(rig.text("Opacity").as_deref(), Some("100 %"), "Opacity shows the other stop's value"); + rig.finish("1"); + let opacities: Vec = stops_of(&rig, a).into_iter().map(|(_, o)| o).collect(); + assert_eq!(opacities, [1.0, 1.0, 0.01, 1.0], "the typing gives the other stop 1%, not 20+1"); + + rig.type_unfinished("Location", "20+"); + rig.app().run("gradient.selectStop", json!({"index": 1})).unwrap(); + rig.fixture.settle(); + assert_eq!(rig.text("Location").as_deref(), Some("50 %"), "Location shows the other stop's location"); + rig.finish("1"); + let offsets: Vec = stops_of(&rig, a).into_iter().map(|(o, _)| o).collect(); + assert_eq!(offsets, [0.0, 0.01, 0.5, 1.0], "the other stop moves to 1%, not to 21%"); + + // Two midpoints, both at 50%. + let pick_mid = |rig: &Rig, i: usize| { + let mut app = rig.app(); + app.run("gradient.selectStop", json!({"index": null})).unwrap(); + app.ui.gradient_panel.mid_selected = Some((i, app.session.gradient_owner())); + }; + pick_mid(&rig, 0); + rig.fixture.settle(); + rig.type_unfinished("Location", "20+"); + pick_mid(&rig, 2); + rig.fixture.settle(); + assert_eq!(rig.text("Location").as_deref(), Some("50 %"), "Location shows the other midpoint"); + + // The same stop of another object's gradient. + rig.app().run("gradient.selectStop", json!({"index": 0})).unwrap(); + rig.fixture.settle(); + rig.type_unfinished("Opacity", "20+"); + rig.app().run("select.set", json!({"ids": [b]})).unwrap(); + rig.app().run("gradient.selectStop", json!({"index": 0})).unwrap(); + rig.fixture.settle(); + assert_eq!(rig.text("Opacity").as_deref(), Some("100 %"), "Opacity shows the other object's stop"); + rig.finish("1"); + let b = NodeId(b.as_u64().unwrap()); + assert_eq!((stops_of(&rig, a)[0].1, stops_of(&rig, b)[0].1), (1.0, 0.01), "the other object's stop takes 1%"); +} + +/// An unfinished value in a freeform point's Opacity or Spread field does not reach the next point selected, and one +/// in Angle does not reach another object's gradient. +#[test] +fn an_unfinished_point_or_angle_value_does_not_reach_a_new_target() { + let mut app = app(json!({"gradient": {}})); + let a = app.session.active().unwrap().selection.objects[0]; + let b = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50})).unwrap()["id"].clone(); + app.run("paint.setFill", json!({"gradient": {}})).unwrap(); + app.run("select.set", json!({"ids": [a.0]})).unwrap(); + let mut rig = Rig::new(app); + + let angle = rig.text("Angle"); + rig.type_unfinished("Angle", "20+"); + rig.app().run("select.set", json!({"ids": [b]})).unwrap(); + rig.fixture.settle(); + assert_eq!(rig.text("Angle"), angle, "Angle shows the other object's angle"); + + rig.app().run("paint.editGradient", json!({"kind": "freeform"})).unwrap(); + let points = + |rig: &Rig| fill(&rig.app()).freeform.map(|f| f.points.iter().map(|p| (p.opacity, p.spread)).collect::>()).unwrap_or_default(); + let before = points(&rig); + for (n, label) in ["Opacity", "Spread"].into_iter().enumerate() { + rig.app().run("paint.freeform.selectPoint", json!({"index": 0})).unwrap(); + rig.fixture.settle(); + rig.type_unfinished(label, "20+"); + rig.app().run("paint.freeform.selectPoint", json!({"index": 1})).unwrap(); + rig.fixture.settle(); + assert!(!rig.text(label).is_some_and(|t| t.contains('+')), "{label} shows the other point's value, not {:?}", rig.text(label)); + rig.finish("1"); + let now = points(&rig); + let value = |p: (f32, f32)| if n == 0 { p.0 } else { p.1 }; + assert_eq!((value(now[0]), value(now[1])), (value(before[0]), 0.01), "{label}: the other point takes 1%, not 20+1"); + } +} + /// A mirrored conic's Aspect Ratio field shows the extent's size, and a typed value keeps the /// sweep mirrored (the field's value times the mirror's sign). #[test]