From 065996cdba6277a01026d83839f1eeda8a29d9f0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:11:28 -0800 Subject: [PATCH] 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);