From ff9bc615c34e9c618ba1061ec2615be025f56a19 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:07:17 -0800 Subject: [PATCH 1/4] Keep Gradient Stop edits with the stop that opened the popover --- crates/ui-egui/src/dialogs/gradient_stop.rs | 121 +++++++++++++++++--- 1 file changed, 106 insertions(+), 15 deletions(-) diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 3ba900ff01..2fe5dbaebc 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -31,6 +31,7 @@ use egui::{Pos2, Ui, vec2}; use serde_json::{Value, json}; use vectorcraft_color::gradient::move_stop; use vectorcraft_color::{Color, FreeformPoint, GradientStop, Paint}; +use vectorcraft_doc::NodeId; use vectorcraft_geom::{Point, Rect}; use vectorcraft_tools::params::color_json; @@ -58,6 +59,42 @@ enum Target { Point(FreeformPoint, usize), } +/// The paint target and selected stop or point shown when the popover opened. +#[derive(Clone, PartialEq, Eq)] +struct TargetKey { + document: u64, + objects: Vec, + item: Option, + fill: bool, + tool_paint: bool, + point: bool, + index: usize, +} + +impl TargetKey { + fn of(app: &VectorcraftApp, target: &Target) -> Option { + let doc = app.session.active()?; + let fill = app.session.fill_active; + let (point, index) = match target { + Target::Stop(_, index) => (false, *index), + Target::Point(_, index) => (true, *index), + }; + Some(Self { + document: doc.uid, + objects: doc.selection.objects.clone(), + item: app.session.appearance_item(), + fill, + tool_paint: app.session.tool_paints(!fill), + point, + index, + }) + } + + fn matches(&self, app: &VectorcraftApp, target: &Target) -> bool { + Self::of(app, target).as_ref() == Some(self) + } +} + impl Target { fn color(&self) -> Color { match self { @@ -127,13 +164,18 @@ impl Tab { /// The fields set from outside the popover or edited in it, as percentages where the field is one; each `None` is /// a field left alone. #[derive(Clone, Debug, Default, PartialEq)] -pub(crate) struct PopoverFields { +struct PopoverFields { color: Option, opacity: Option, location: Option, spread: Option, } +pub(crate) struct PopoverAnswer { + target: TargetKey, + fields: PopoverFields, +} + /// The target's values and what the Color panel shows, as `pump` last pushed them. #[derive(Clone)] struct Feed { @@ -163,6 +205,7 @@ struct Filed { /// The gradient stop popover. pub(crate) struct GradientStopPopover { + target: TargetKey, /// Where it opened. pub(crate) anchor: Anchor, /// The view it shows. @@ -177,8 +220,9 @@ pub(crate) struct GradientStopPopover { } impl GradientStopPopover { - fn new(anchor: Anchor) -> Self { + fn new(anchor: Anchor, target: TargetKey) -> Self { Self { + target, anchor, tab: Tab::default(), at: Pos2::ZERO, @@ -199,21 +243,21 @@ fn rejects(field: &str, value: &Value) -> String { } impl StackDialog for GradientStopPopover { - type Result = PopoverFields; + type Result = PopoverAnswer; fn id(&self) -> DialogId { dialog_id(KIND) } - fn show(&mut self, ui: &mut Ui, _stack: &DialogStack) -> Option> { - let Self { feed: Some(feed), tab, panel, filed, at, set, .. } = self else { return None }; + fn show(&mut self, ui: &mut Ui, _stack: &DialogStack) -> Option> { + let Self { feed: Some(feed), tab, panel, filed, at, .. } = self else { return None }; let mut open = true; let shown = Popover::new("Gradient stop", Bind::bound(&mut open)) .at_point(*at) .close_on_enter(true) .show(ui, |ui, _| body(ui, feed, tab, panel, filed)); match shown.output.closed { - Some(CloseReason::Enter) => Some(DialogResult::Ok(set.clone())), + Some(CloseReason::Enter) => self.confirm(ui.ctx()).map(DialogResult::Ok), Some(_) => Some(DialogResult::Cancel), None => None, } @@ -255,8 +299,8 @@ impl StackDialog for GradientStopPopover { } /// OK (agents): the fields that were set, which `pump` applies; an untouched popover confirms to no change. - fn confirm(&mut self, _ctx: &egui::Context) -> Option { - Some(self.set.clone()) + fn confirm(&mut self, _ctx: &egui::Context) -> Option { + Some(PopoverAnswer { target: self.target.clone(), fields: self.set.clone() }) } } @@ -397,8 +441,9 @@ pub(crate) fn open_at(app: &mut VectorcraftApp, index: usize, anchor: Anchor) { app.run("gradient.selectStop", json!({ "index": index })).ok(); } let Some(target) = selected(app) else { return }; + let Some(key) = TargetKey::of(app, &target) else { return }; dialogs.with(|host| { - host.open(GradientStopPopover::new(anchor)); + host.open(GradientStopPopover::new(anchor, key)); }); refresh(app, &ctx, dialog_id(KIND), &target); } @@ -409,9 +454,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { let Some(dialogs) = app.dialogs() else { return }; let id = dialog_id(KIND); match dialogs.with(|host| host.take_by_id::(id)) { - Some(DialogResult::Ok(fields)) => { + Some(DialogResult::Ok(PopoverAnswer { target: key, fields })) => { if fields != PopoverFields::default() && let Some(target) = selected(app) + && key.matches(app, &target) { apply(app, &target, &fields); } @@ -420,11 +466,8 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { Some(_) => return, None => {} } - if !dialogs.with(|host| host.is_open::()) { - return; - } - let Some(target) = selected(app) else { - // The stop or point went away (undo, another object selected): nothing left to edit. + let Some(key) = dialogs.with(|host| host.get_by_id::(id).map(|popover| popover.target.clone())) else { return }; + let Some(target) = selected(app).filter(|target| key.matches(app, target)) else { dialogs.with(|host| host.cancel_by_id(ctx, id)); return; }; @@ -432,6 +475,16 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { if filed.fields != PopoverFields::default() { apply(app, &target, &filed.fields); } + if filed.fields.location.is_some() + && let Some(index) = app.session.selected_stop() + { + // The stop can cross its neighbor when its location changes. + dialogs.with(|host| { + if let Some(popover) = host.get_mut_by_id::(id) { + popover.target.index = index; + } + }); + } if let Some((name, color)) = filed.swatch && let Some(target) = selected(app) { @@ -591,6 +644,44 @@ mod tests { assert!(set(&app, "color", json!("red")).is_err()); } + #[test] + fn the_popover_discards_a_pending_edit_after_selecting_other_art() { + let mut app = app(); + let first = app.session.doc().unwrap().selection.objects[0]; + let gradient = json!({"stops": [{"offset": 0, "color": "#000000"}, {"offset": 0.5, "color": "#0000ff"}, {"offset": 1, "color": "#ffffff"}]}); + let second = app.run("shape.rectangle", json!({"x": 230, "y": 80, "width": 50, "height": 90})).unwrap()["id"].clone(); + app.run("paint.setFill", json!({"gradient": gradient})).unwrap(); + app.run("select.set", json!({"ids": [first.0]})).unwrap(); + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + app.run("select.set", json!({"ids": [second]})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + let before = app.session.doc().unwrap().doc.clone(); + confirm(&mut app); + assert_eq!(app.session.doc().unwrap().doc, before); + assert!(!is_open(&app)); + + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + confirm(&mut app); + assert_eq!(stops(&app)[1].opacity, 0.25); + } + + #[test] + fn the_popover_does_not_send_live_edits_to_another_stop() { + let mut app = app(); + open_popover(&mut app); + let dialogs = app.dialogs().unwrap(); + let ctx = ctx(&app); + dialogs.with(|host| { + host.get_mut_by_id::(dialog_id(KIND)).unwrap().filed.fields.opacity = Some(25.0); + }); + app.run("gradient.selectStop", json!({"index": 2})).unwrap(); + host::pump(&mut app, &ctx); + assert!(!is_open(&app)); + assert_eq!(stops(&app)[2].opacity, 1.0); + } + #[test] fn on_a_freeform_gradient_it_edits_the_selected_point() { let mut app = app(); From 065996cdba6277a01026d83839f1eeda8a29d9f0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:11:28 -0800 Subject: [PATCH 2/4] Discard Gradient Stop edits after replacing the gradient --- crates/ui-egui/src/dialogs/gradient_stop.rs | 187 ++++++++++++++++---- 1 file changed, 152 insertions(+), 35 deletions(-) diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 2fe5dbaebc..622a5ba407 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -13,6 +13,8 @@ //! Fields: `tab` (`color` or `swatches`). Agents can set `color` (hex), `opacity` and `location` (a //! point: `spread`) as percentages, and confirm; confirming reports no stop index. +use std::sync::Arc; + use craft::behavior::IconId; use craft::color_ui::ui::{ColorPanel, ColorPanelLayout, ColorPanelOutput, ColorPanelShown, ColorPanelState, DropperButton, SwatchPalette}; use craft::theme::Srgba; @@ -30,7 +32,7 @@ use craft::ui::text::NumberField; use egui::{Pos2, Ui, vec2}; use serde_json::{Value, json}; use vectorcraft_color::gradient::move_stop; -use vectorcraft_color::{Color, FreeformPoint, GradientStop, Paint}; +use vectorcraft_color::{Color, FreeformPoint, GradientPaint, GradientStop, Paint}; use vectorcraft_doc::NodeId; use vectorcraft_geom::{Point, Rect}; use vectorcraft_tools::params::color_json; @@ -55,8 +57,8 @@ pub(crate) enum Anchor { /// What the popover edits: the selected stop of the gradient behind the active proxy (with all /// its stops), or the selected point of a freeform gradient. enum Target { - Stop(Vec, usize), - Point(FreeformPoint, usize), + Stop(Box, usize), + Point(Box, FreeformPoint, usize), } /// The paint target and selected stop or point shown when the popover opened. @@ -77,7 +79,7 @@ impl TargetKey { let fill = app.session.fill_active; let (point, index) = match target { Target::Stop(_, index) => (false, *index), - Target::Point(_, index) => (true, *index), + Target::Point(_, _, index) => (true, *index), }; Some(Self { document: doc.uid, @@ -95,24 +97,43 @@ impl TargetKey { } } +/// The gradient in that slot as the popover last saw it, including any live edits it applied. +#[derive(Clone)] +struct TargetOwner { + key: TargetKey, + paint: Arc, +} + +impl TargetOwner { + fn matches(&self, app: &VectorcraftApp, target: &Target) -> bool { + self.key.matches(app, target) && self.paint.as_ref() == target.paint() + } +} + impl Target { + fn paint(&self) -> &GradientPaint { + match self { + Self::Stop(paint, _) | Self::Point(paint, _, _) => paint, + } + } + fn color(&self) -> Color { match self { - Target::Stop(stops, i) => stops.get(*i).map_or(Color::BLACK, |s| s.color), - Target::Point(p, _) => p.color, + Target::Stop(paint, i) => paint.gradient.stops[*i].color, + Target::Point(_, p, _) => p.color, } } fn opacity(&self) -> f32 { match self { - Target::Stop(stops, i) => stops.get(*i).map_or(1.0, |s| s.opacity), - Target::Point(p, _) => p.opacity, + Target::Stop(paint, i) => paint.gradient.stops[*i].opacity, + Target::Point(_, p, _) => p.opacity, } } /// The location of a stop or the spread of a point, 0 to 1. fn amount(&self) -> f32 { match self { - Target::Stop(stops, i) => stops.get(*i).map_or(0.0, |s| s.offset), - Target::Point(p, _) => p.spread, + Target::Stop(paint, i) => paint.gradient.stops[*i].offset, + Target::Point(_, p, _) => p.spread, } } } @@ -122,10 +143,11 @@ fn selected(app: &VectorcraftApp) -> Option { if edits_points(app, &g) { let f = shown_points(&g); let i = app.session.selected_freeform_point().filter(|i| *i < f.points.len())?; - return Some(Target::Point(f.points[i], i)); + let point = f.points[i]; + return Some(Target::Point(g, point, i)); } let i = app.session.selected_stop().filter(|i| *i < g.gradient.stops.len())?; - Some(Target::Stop(g.gradient.stops, i)) + Some(Target::Stop(g, i)) } /// `stops` with stop `i` given `color`, linked to `link` (a global swatch and tint) or unlinked. @@ -172,7 +194,7 @@ struct PopoverFields { } pub(crate) struct PopoverAnswer { - target: TargetKey, + target: TargetOwner, fields: PopoverFields, } @@ -205,7 +227,7 @@ struct Filed { /// The gradient stop popover. pub(crate) struct GradientStopPopover { - target: TargetKey, + target: TargetOwner, /// Where it opened. pub(crate) anchor: Anchor, /// The view it shows. @@ -220,7 +242,7 @@ pub(crate) struct GradientStopPopover { } impl GradientStopPopover { - fn new(anchor: Anchor, target: TargetKey) -> Self { + fn new(anchor: Anchor, target: TargetOwner) -> Self { Self { target, anchor, @@ -443,7 +465,7 @@ pub(crate) fn open_at(app: &mut VectorcraftApp, index: usize, anchor: Anchor) { let Some(target) = selected(app) else { return }; let Some(key) = TargetKey::of(app, &target) else { return }; dialogs.with(|host| { - host.open(GradientStopPopover::new(anchor, key)); + host.open(GradientStopPopover::new(anchor, TargetOwner { key, paint: Arc::new(target.paint().clone()) })); }); refresh(app, &ctx, dialog_id(KIND), &target); } @@ -454,10 +476,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { let Some(dialogs) = app.dialogs() else { return }; let id = dialog_id(KIND); match dialogs.with(|host| host.take_by_id::(id)) { - Some(DialogResult::Ok(PopoverAnswer { target: key, fields })) => { + Some(DialogResult::Ok(PopoverAnswer { target: owner, fields })) => { if fields != PopoverFields::default() && let Some(target) = selected(app) - && key.matches(app, &target) + && owner.matches(app, &target) { apply(app, &target, &fields); } @@ -466,8 +488,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { Some(_) => return, None => {} } - let Some(key) = dialogs.with(|host| host.get_by_id::(id).map(|popover| popover.target.clone())) else { return }; - let Some(target) = selected(app).filter(|target| key.matches(app, target)) else { + let Some(mut owner) = dialogs.with(|host| host.get_by_id::(id).map(|popover| popover.target.clone())) else { + return; + }; + let Some(target) = selected(app).filter(|target| owner.matches(app, target)) else { dialogs.with(|host| host.cancel_by_id(ctx, id)); return; }; @@ -476,14 +500,11 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { apply(app, &target, &filed.fields); } if filed.fields.location.is_some() + && !owner.key.point && let Some(index) = app.session.selected_stop() { // The stop can cross its neighbor when its location changes. - dialogs.with(|host| { - if let Some(popover) = host.get_mut_by_id::(id) { - popover.target.index = index; - } - }); + owner.key.index = index; } if let Some((name, color)) = filed.swatch && let Some(target) = selected(app) @@ -499,20 +520,33 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { crate::panels::gradient::stop_eyedropper(app); return; } - if let Some(target) = selected(app) { - refresh(app, ctx, id, &target); + let Some(target) = selected(app).filter(|target| owner.key.matches(app, target)) else { + dialogs.with(|host| host.cancel_by_id(ctx, id)); + return; + }; + // The popover owns the edits applied in this pass and can continue with their resulting paint. + if owner.paint.as_ref() != target.paint() { + owner.paint = Arc::new(target.paint().clone()); } + dialogs.with(|host| { + if let Some(popover) = host.get_mut_by_id::(id) { + popover.target = owner; + } + }); + refresh(app, ctx, id, &target); } /// Gives the target the swatch `name`'s colour. A global or spot color (or a tint of one) stays linked, and the /// swatch's color keeps its model. fn pick_swatch(app: &mut VectorcraftApp, target: &Target, name: &str, color: Color) { match target { - Target::Stop(stops, i) => { + Target::Stop(paint, i) => { let link = crate::panels::gradient::dropped_link(app, &json!({ "swatch": name })); - set_stops_keeping_models(app, &recolor(stops.clone(), *i, color, link), Some(*i), Live::Released); + set_stops_keeping_models(app, &recolor(paint.gradient.stops.clone(), *i, color, link), Some(*i), Live::Released); + } + Target::Point(_, _, i) => { + edit_point(app, "paint.freeform.setPoint", json!({ "index": i, "color": color_json(&color), "keepModel": true })); } - Target::Point(_, i) => edit_point(app, "paint.freeform.setPoint", json!({ "index": i, "color": color_json(&color), "keepModel": true })), } } @@ -520,8 +554,8 @@ fn pick_swatch(app: &mut VectorcraftApp, target: &Target, name: &str, color: Col fn apply(app: &mut VectorcraftApp, target: &Target, f: &PopoverFields) { let pct = |v: Option| v.map(|v| (v / 100.0).clamp(0.0, 1.0)); match target { - Target::Stop(stops, i) => { - let (mut stops, mut i) = (stops.clone(), *i); + Target::Stop(paint, i) => { + let (mut stops, mut i) = (paint.gradient.stops.clone(), *i); if let Some(c) = f.color { stops = recolor(stops, i, c, None); } @@ -535,7 +569,7 @@ fn apply(app: &mut VectorcraftApp, target: &Target, f: &PopoverFields) { } set_stops(app, &stops, Some(i), Live::Released); } - Target::Point(_, i) => { + Target::Point(_, _, i) => { let mut p = json!({ "index": i }); if let Some(c) = f.color { p["color"] = color_json(&c); @@ -552,6 +586,8 @@ fn apply(app: &mut VectorcraftApp, target: &Target, f: &PopoverFields) { #[cfg(test)] mod tests { + use craft::behavior::{EditEvent, GestureId, Mixed}; + use craft::color_ui::ui::{PanelTarget, PickedColor}; use vectorcraft_engine::Session; use super::*; @@ -618,7 +654,7 @@ mod tests { fn stops(app: &VectorcraftApp) -> Vec { match selected(app) { - Some(Target::Stop(stops, _)) => stops, + Some(Target::Stop(paint, _)) => paint.gradient.stops, _ => panic!("no stop selected"), } } @@ -682,13 +718,94 @@ mod tests { assert_eq!(stops(&app)[2].opacity, 1.0); } + fn replace_gradient(app: &mut VectorcraftApp) { + let stops = json!([{"offset": 0, "color": "#000000"}, {"offset": 0.5, "color": "#0000ff"}, {"offset": 1, "color": "#ffffff"}]); + app.run("paint.setFill", json!({"gradient": {"stops": stops, "start": [100, 150], "end": [200, 150]}})).unwrap(); + assert_eq!(app.session.selected_stop(), Some(1)); + } + + #[test] + fn the_popover_discards_confirmed_edits_to_a_replacement_gradient() { + let mut app = app(); + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + let ctx = ctx(&app); + app.dialogs().unwrap().with(|host| host.confirm_by_id(&ctx, dialog_id(KIND))); + replace_gradient(&mut app); + host::pump(&mut app, &ctx); + assert_eq!(stops(&app)[1].opacity, 1.0, "the finished answer belongs to the original gradient"); + } + + #[test] + fn the_popover_discards_live_edits_to_a_replacement_gradient() { + let mut app = app(); + open_popover(&mut app); + app.dialogs().unwrap().with(|host| { + host.get_mut_by_id::(dialog_id(KIND)).unwrap().filed.fields.opacity = Some(25.0); + }); + replace_gradient(&mut app); + let ctx = ctx(&app); + host::pump(&mut app, &ctx); + assert_eq!(stops(&app)[1].opacity, 1.0, "the live edit belongs to the original gradient"); + assert!(!is_open(&app)); + } + + #[test] + fn its_live_color_drag_stays_open_and_commits_once() { + let mut app = app(); + open_popover(&mut app); + let undo = app.session.active().unwrap().history.undo.len(); + let ctx = ctx(&app); + let dialogs = app.dialogs().unwrap(); + let gesture = GestureId::new(1); + let pick = |hex| PickedColor::rgb(Srgba::from_hex(hex).unwrap()); + let before = pick("#00ff00"); + for (edit, expected) in + [(EditEvent::update(gesture, pick("#ff0000")), "#ff0000"), (EditEvent::commit(gesture, Mixed::Same(before), pick("#0000ff")), "#0000ff")] + { + let mut edit = Some(edit); + ctx.run_ui(Default::default(), |ui| { + let Some(edit) = edit.take() else { return }; + let mut state = ColorPanelState::default(); + let mut output = ColorPanel::new(Bind::bound(&mut state), [before.color; 2]).show(ui); + output.edits.push((PanelTarget::Foreground, edit)); + dialogs.with(|host| { + host.get_mut_by_id::(dialog_id(KIND)).unwrap().filed.panels.push(output); + }); + }) + .textures_delta + .clear(); + host::pump(&mut app, &ctx); + assert!(is_open(&app)); + assert_eq!(stops(&app)[1].color.to_hex(), expected); + } + assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1); + } + + #[test] + fn the_popover_keeps_editing_the_stop_after_moving_it() { + let mut app = app(); + open_popover(&mut app); + app.dialogs().unwrap().with(|host| { + host.get_mut_by_id::(dialog_id(KIND)).unwrap().filed.fields.location = Some(100.0); + }); + let ctx = ctx(&app); + host::pump(&mut app, &ctx); + assert!(is_open(&app)); + assert_eq!(app.session.selected_stop(), Some(2)); + set(&app, "opacity", json!(25)).unwrap(); + confirm(&mut app); + assert_eq!(stops(&app)[1].opacity, 1.0); + assert_eq!(stops(&app)[2].opacity, 0.25); + } + #[test] fn on_a_freeform_gradient_it_edits_the_selected_point() { let mut app = app(); app.run("paint.editGradient", json!({"kind": "freeform"})).unwrap(); app.run("paint.freeform.addPoint", json!({"at": [150, 150], "color": "#0000ff"})).unwrap(); let point = |app: &VectorcraftApp| match selected(app) { - Some(Target::Point(p, i)) => (p, i), + Some(Target::Point(_, p, i)) => (p, i), _ => panic!("no point selected"), }; let (p, i) = point(&app); From c1a23c4185ac39b8d0eb815690d12979b65f8e09 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:23:41 -0800 Subject: [PATCH 3/4] Track each paint affected by Gradient Stop edits --- crates/engine/src/cmd/gradient.rs | 30 +++++++++++++ crates/ui-egui/src/dialogs/gradient_stop.rs | 49 ++++++++++++++++++--- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index da5bbea659..5a36915747 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -136,6 +136,36 @@ impl Session { .is_ok_and(|item| item.of_kind(self, !stroke).targets_for(self, &p, !stroke).is_ok_and(|ids| !ids.is_empty())) } + /// Snapshot the paints a gradient edit without explicit targets would write, by object and + /// appearance item. Text without an item has one paint per run. The proxy can display just + /// the first paint when an edit goes to several objects or text runs. + pub fn gradient_edit_paints(&self, stroke: bool) -> Result, Vec)>> { + const CMD: &str = "paint.editGradient"; + let p = json!({ "stroke": stroke }); + if super::paint::for_tool(self, &p, stroke) { + return Ok(Vec::new()); + } + let doc = &self.doc()?.doc; + let item = item_target(self, &p, CMD)?.of_kind(self, !stroke); + let ids = item.targets_for(self, &p, !stroke)?; + let ids = super::appearance::drop_design_art_rows(self, ids, item, !stroke, CMD)?; + let mut out = Vec::with_capacity(ids.len()); + for (id, item) in ids.iter().copied().zip(item.per_target(self, &ids)) { + let (Some(node), Some(item)) = (doc.node(id), item) else { continue }; + let index = item.resolve(&node.appearance, !stroke, CMD)?; + let (item, paints) = if index.is_none() + && let NodeKind::Text(text) = &node.kind + { + (None, text.runs.iter().map(|run| if stroke { run.style.stroke.clone() } else { run.style.fill.clone() }).collect()) + } else { + let item = index.or_else(|| node.appearance.items.iter().rposition(|row| row.is_fill() != stroke)); + (item, node.appearance.paint_at(index, !stroke).cloned().into_iter().collect()) + }; + out.push((id, item, paints)); + } + Ok(out) + } + /// The selected gradient stop (`gradient.selectStop`), while the gradient it was selected on /// is still the one behind the active proxy. Callers check it against the stop count (an undo /// can remove stops). diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 622a5ba407..272c7bd238 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -101,12 +101,19 @@ impl TargetKey { #[derive(Clone)] struct TargetOwner { key: TargetKey, - paint: Arc, + paints: Arc, +} + +struct TargetPaints { + shown: GradientPaint, + edited: Vec<(NodeId, Option, Vec)>, } impl TargetOwner { fn matches(&self, app: &VectorcraftApp, target: &Target) -> bool { - self.key.matches(app, target) && self.paint.as_ref() == target.paint() + self.key.matches(app, target) + && self.paints.shown == *target.paint() + && app.session.gradient_edit_paints(!self.key.fill).is_ok_and(|paints| paints == self.paints.edited) } } @@ -464,8 +471,10 @@ pub(crate) fn open_at(app: &mut VectorcraftApp, index: usize, anchor: Anchor) { } let Some(target) = selected(app) else { return }; let Some(key) = TargetKey::of(app, &target) else { return }; + let Ok(edited) = app.session.gradient_edit_paints(!key.fill) else { return }; + let paints = Arc::new(TargetPaints { shown: target.paint().clone(), edited }); dialogs.with(|host| { - host.open(GradientStopPopover::new(anchor, TargetOwner { key, paint: Arc::new(target.paint().clone()) })); + host.open(GradientStopPopover::new(anchor, TargetOwner { key, paints })); }); refresh(app, &ctx, dialog_id(KIND), &target); } @@ -520,13 +529,14 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { crate::panels::gradient::stop_eyedropper(app); return; } - let Some(target) = selected(app).filter(|target| owner.key.matches(app, target)) else { + let updated = selected(app).filter(|target| owner.key.matches(app, target)).zip(app.session.gradient_edit_paints(!owner.key.fill).ok()); + let Some((target, edited)) = updated else { dialogs.with(|host| host.cancel_by_id(ctx, id)); return; }; - // The popover owns the edits applied in this pass and can continue with their resulting paint. - if owner.paint.as_ref() != target.paint() { - owner.paint = Arc::new(target.paint().clone()); + // The popover can continue with the paints resulting from its own edits in this pass. + if owner.paints.shown != *target.paint() || owner.paints.edited != edited { + owner.paints = Arc::new(TargetPaints { shown: target.paint().clone(), edited }); } dialogs.with(|host| { if let Some(popover) = host.get_mut_by_id::(id) { @@ -750,6 +760,31 @@ mod tests { assert!(!is_open(&app)); } + #[test] + fn discards_edits_when_only_a_secondary_selected_gradient_is_replaced() { + let mut app = app(); + let first = app.session.active().unwrap().selection.objects[0]; + let second = app.run("shape.rectangle", json!({"x": 230, "y": 80, "width": 50, "height": 90})).unwrap()["id"].clone(); + let initial = json!({"stops": [{"offset": 0, "color": "#ffffff"}, {"offset": 0.5, "color": "#00ff00"}, {"offset": 1, "color": "#000000"}], + "start": [100, 150], "end": [200, 150]}); + app.run("paint.setFill", json!({"id": second, "gradient": initial})).unwrap(); + app.run("select.set", json!({"ids": [first.0, second]})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + assert_eq!(app.session.active().unwrap().selection.objects[0], first); + open_popover(&mut app); + set(&app, "opacity", json!(25)).unwrap(); + let proxy = active_paint(&app); + let selected = app.session.active().unwrap().selection.objects.clone(); + let gradient = json!({"stops": [{"offset": 0, "color": "#000000"}, {"offset": 0.5, "color": "#0000ff"}, {"offset": 1, "color": "#ffffff"}]}); + app.run("paint.setFill", json!({"id": second, "gradient": gradient})).unwrap(); + assert_eq!(app.session.active().unwrap().selection.objects, selected); + assert_eq!(app.session.selected_stop(), Some(1)); + assert_eq!(active_paint(&app), proxy); + let before = app.session.active().unwrap().doc.clone(); + confirm(&mut app); + assert!(app.session.active().unwrap().doc == before, "the edit must not overwrite the secondary replacement"); + } + #[test] fn its_live_color_drag_stays_open_and_commits_once() { let mut app = app(); From c0da24c63aed77f3e45314081527a26153e1a364 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:38:27 -0800 Subject: [PATCH 4/4] Name the gradient edit paint snapshot --- crates/engine/src/cmd/gradient.rs | 19 +++++++++++++++---- crates/engine/src/lib.rs | 2 +- crates/ui-egui/src/dialogs/gradient_stop.rs | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 5a36915747..01e0a5a732 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -124,6 +124,17 @@ impl StopOwner { } } +/// The drawing paints affected by an edit from the Gradient panel. +#[derive(PartialEq)] +pub struct GradientEditPaints(Vec); + +#[derive(PartialEq)] +struct EditedPaint { + id: NodeId, + item: Option, + paints: Vec, +} + impl Session { /// Does `paint.editGradient` without `ids` edit objects on the fill (or the stroke, `stroke`)? It does not /// while the active tool paints with the chosen fill (or stroke: [`Session::tool_paints`]) or when nothing it @@ -139,11 +150,11 @@ impl Session { /// Snapshot the paints a gradient edit without explicit targets would write, by object and /// appearance item. Text without an item has one paint per run. The proxy can display just /// the first paint when an edit goes to several objects or text runs. - pub fn gradient_edit_paints(&self, stroke: bool) -> Result, Vec)>> { + pub fn gradient_edit_paints(&self, stroke: bool) -> Result { const CMD: &str = "paint.editGradient"; let p = json!({ "stroke": stroke }); if super::paint::for_tool(self, &p, stroke) { - return Ok(Vec::new()); + return Ok(GradientEditPaints(Vec::new())); } let doc = &self.doc()?.doc; let item = item_target(self, &p, CMD)?.of_kind(self, !stroke); @@ -161,9 +172,9 @@ impl Session { let item = index.or_else(|| node.appearance.items.iter().rposition(|row| row.is_fill() != stroke)); (item, node.appearance.paint_at(index, !stroke).cloned().into_iter().collect()) }; - out.push((id, item, paints)); + out.push(EditedPaint { id, item, paints }); } - Ok(out) + Ok(GradientEditPaints(out)) } /// The selected gradient stop (`gradient.selectStop`), while the gradient it was selected on diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b1a7d37277..722192a185 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -30,7 +30,7 @@ pub use cmd::clipboard::{Clipboard, SourceLayer}; pub use cmd::create::constrain_click; pub use cmd::distortcmds::{perspective_click, width_point_pair}; pub use cmd::flare::flare_settings; -pub use cmd::gradient::StopOwner; +pub use cmd::gradient::{GradientEditPaints, StopOwner}; pub use cmd::graph::{GraphTypeSubject, calculated_axes as graph_calculated_axes, graph_type_subject}; pub use cmd::params::{check_keys, check_params}; pub use cmd::pathops::auto_simplify_level; diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 272c7bd238..f2bf968f04 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -34,6 +34,7 @@ use serde_json::{Value, json}; use vectorcraft_color::gradient::move_stop; use vectorcraft_color::{Color, FreeformPoint, GradientPaint, GradientStop, Paint}; use vectorcraft_doc::NodeId; +use vectorcraft_engine::GradientEditPaints; use vectorcraft_geom::{Point, Rect}; use vectorcraft_tools::params::color_json; @@ -106,7 +107,7 @@ struct TargetOwner { struct TargetPaints { shown: GradientPaint, - edited: Vec<(NodeId, Option, Vec)>, + edited: GradientEditPaints, } impl TargetOwner {