From 17554f2a9199311b43872bc4f858e9aa634d5666 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 14:48:15 -0800 Subject: [PATCH 1/2] Gradient panel: a picked midpoint stays with the gradient it was picked on The Gradient panel kept a selected midpoint diamond for the first selected object only. With that object still selected, choosing the Live Paint Bucket or toggling to the Stroke proxy left the diamond selected on the gradient the panel showed then, and the Location field edited that gradient's midpoint although nobody picked it. The diamond is now kept for the same owner a selected stop is kept for (document, object, proxy, Appearance row, symbol part and the bucket), through the new `Session::gradient_owner`. --- crates/engine/src/cmd/gradient.rs | 10 ++++++-- crates/engine/src/lib.rs | 1 + crates/ui-egui/src/panels/gradient.rs | 19 ++++++--------- crates/ui-egui/src/panels/gradient/tests.rs | 27 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 8cf1091492..43e9794f08 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -87,9 +87,10 @@ fn select_stop(s: &mut Session, p: &Value) -> Result { /// front, the Appearance panel's active item and whether the Live Paint Bucket is the active tool /// (its proxies show the default paint). Selecting other art, picking a part of the instance, /// toggling the proxy, picking another item or choosing the bucket or leaving it leaves no stop -/// selected. +/// selected. The Gradient panel keys its selected midpoint diamond by it as well +/// ([`Session::gradient_owner`]). #[derive(Clone, Copy, Debug, PartialEq)] -pub(crate) struct StopOwner { +pub struct StopOwner { doc: Option, object: Option, /// The object whose paint the active proxy shows: a Live Paint group without faces (or edges) @@ -134,6 +135,11 @@ impl Session { pub fn selected_stop(&self) -> Option { self.gradient_stop.filter(|(_, owner)| *owner == StopOwner::of(self)).map(|(i, _)| i) } + + /// Whose gradient the active proxy shows now, as a selected stop keeps it ([`StopOwner`]). + pub fn gradient_owner(&self) -> StopOwner { + StopOwner::of(self) + } } type Parsed = std::result::Result; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index e2e6430f1e..5db4b136fa 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -30,6 +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::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/panels/gradient.rs b/crates/ui-egui/src/panels/gradient.rs index 945c18ea0d..06ba6ca8a8 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -31,7 +31,8 @@ use vectorcraft_color::gradient::{ MAX_REPEATS, MIN_STOPS, duplicate_stop, insert_stop, midpoint_from_pos, midpoint_pos, move_stop, remove_stop, set_midpoint, swap_stop_colors, }; use vectorcraft_color::{Color, Freeform, FreeformMode, Gradient, GradientKind, GradientMethod, GradientPaint, GradientSpread, GradientStop, Paint}; -use vectorcraft_doc::{NodeId, StrokeGradientMode}; +use vectorcraft_doc::StrokeGradientMode; +use vectorcraft_engine::StopOwner; use super::{active_paint, live_run, pstate, set_pstate}; use crate::state::Dialog; @@ -231,15 +232,11 @@ fn gradient_swatches(app: &VectorcraftApp) -> Vec<(String, GradientPaint)> { .collect() } -/// The selected midpoint diamond: kept for the object it was picked on, and given up when a stop -/// is selected. +/// The selected midpoint diamond: kept for the gradient it was picked on, as a selected stop is +/// ([`vectorcraft_engine::Session::gradient_owner`]), and given up when a stop is selected. fn selected_mid(app: &VectorcraftApp, ctx: &egui::Context, stops: usize) -> Option { - let (i, owner) = pstate::)>>(ctx, MID_SELECTED)?; - (app.session.selected_stop().is_none() && i + 1 < stops && owner == first_id(app)).then_some(i) -} - -fn first_id(app: &VectorcraftApp) -> Option { - app.session.active().and_then(|d| d.selection.objects.first().copied()) + let (i, owner) = pstate::>(ctx, MID_SELECTED)?; + (app.session.selected_stop().is_none() && i + 1 < stops && owner == app.session.gradient_owner()).then_some(i) } // ---------- UI ---------- @@ -935,12 +932,12 @@ fn apply_ramp_edit( popover: Option<(usize, Pos2)>, ) { if let Some(i) = pick_mid { - set_pstate(ui.ctx(), MID_SELECTED, Some((i, first_id(app)))); + set_pstate(ui.ctx(), MID_SELECTED, Some((i, app.session.gradient_owner()))); if sel.is_some() { select_stop(app, None); } } else if select.is_some() { - set_pstate(ui.ctx(), MID_SELECTED, None::<(usize, Option)>); + set_pstate(ui.ctx(), MID_SELECTED, None::<(usize, StopOwner)>); } match changed { // Stops added, moved or dropped on the ramp carry colors the gradient or a swatch has, or diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index 1a607b8f24..931d87f4b7 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -1,4 +1,5 @@ use egui::{Event, Modifiers, PointerButton}; +use vectorcraft_doc::NodeId; use vectorcraft_engine::Session; use super::super::tests_long_popups::{PopupWindow, assert_last_row_scrolls_into_view, assert_short_list_does_not_scroll}; @@ -417,6 +418,32 @@ fn a_selected_diamond_shows_and_sets_its_midpoint_in_location() { assert!(texts.iter().any(|(t, _)| t == "0%")); } +/// A diamond picked on the selected object's gradient is not selected on another gradient the +/// panel shows with that object still selected: the Live Paint Bucket's, or the stroke's after the +/// proxy toggles. Going back to the object's fill selects it again. +#[test] +fn a_picked_diamond_stays_with_the_gradient_it_was_picked_on() { + let stops = json!([{"offset": 0, "color": "#ffffff", "midpoint": 0.3}, {"offset": 1, "color": "#000000"}]); + let mut app = app(json!({"gradient": {"stops": stops}})); + app.run("paint.setStroke", json!({"gradient": {"kind": "radial"}})).unwrap(); + app.session.fill_active = true; + let mut p = Panel::new(); + p.frame(&mut app, vec![], Modifiers::NONE); + let diamond = p.widget(("grad-mid", 0_usize)); + p.click(&mut app, diamond.center()); + assert_eq!(selected_mid(&app, &p.ctx, 2), Some(0), "the diamond is picked"); + app.select_tool("livePaintBucket"); + app.run("paint.setFill", json!({"gradient": {"kind": "radial"}})).unwrap(); + p.frame(&mut app, vec![], Modifiers::NONE); + assert_eq!(selected_mid(&app, &p.ctx, 2), None, "the bucket's gradient"); + app.select_tool("selection"); + p.frame(&mut app, vec![], Modifiers::NONE); + assert_eq!(selected_mid(&app, &p.ctx, 2), Some(0), "the object's fill again"); + app.session.fill_active = false; + p.frame(&mut app, vec![], Modifiers::NONE); + assert_eq!(selected_mid(&app, &p.ctx, 2), None, "the object's stroke"); +} + /// A mirrored conic's Aspect Ratio field shows the extent's size, and a typed value keeps the /// sweep mirrored. #[test] From 1e8b2fcb321b7f63d3e61025f675ce75cc429836 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 09:17:37 -0800 Subject: [PATCH 2/2] Gradient panel: a picked midpoint and a selected stop stay selected when the document tab moves The owner a selected stop, freeform point or midpoint diamond is kept for now records the active document's uid instead of its tab index. Dragging the document's tab along the strip (document.move) changed the index, so the selection was dropped and the Location field was disabled although the gradient had not changed. --- crates/engine/src/cmd/gradient.rs | 5 +++-- crates/ui-egui/src/panels/gradient/tests.rs | 22 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 3bc72bc816..12d6fb69c8 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -91,7 +91,8 @@ fn select_stop(s: &mut Session, p: &Value) -> Result { /// ([`Session::gradient_owner`]). #[derive(Clone, Copy, Debug, PartialEq)] pub struct StopOwner { - doc: Option, + /// The active document's uid, which stays the same when its tab moves. + doc: Option, object: Option, /// The object whose paint the active proxy shows: a Live Paint group without faces (or edges) /// first in the selection shows the next object's. @@ -106,7 +107,7 @@ impl StopOwner { pub(crate) fn of(s: &Session) -> Self { let st = s.active(); Self { - doc: s.active, + doc: st.map(|d| d.uid), object: st.and_then(|d| d.selection.objects.first().copied()), source: s.proxy_source(!s.fill_active).map(|(n, _)| n.id), part: st.is_some_and(|d| d.selection.symbol_part.is_some()), diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index 9efa27a97f..e874fbee5d 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -205,6 +205,28 @@ fn a_picked_diamond_stays_with_the_gradient_it_was_picked_on() { assert_eq!(mid(&rig), None, "the object's stroke"); } +/// Dragging the document's tab along the strip keeps its selected stop and its picked diamond +/// selected: both stay with the document, not with the tab it was in. +#[test] +fn a_selected_stop_and_a_picked_diamond_stay_selected_when_the_document_tab_moves() { + let stops = json!([{"offset": 0, "color": "#ffffff", "midpoint": 0.3}, {"offset": 1, "color": "#000000"}]); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 100, "height": 100})).unwrap(); + app.run("paint.setFill", json!({"gradient": {"stops": stops}})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + app.run("document.move", json!({"to": 0})).unwrap(); + assert_eq!(app.session.selected_stop(), Some(1), "the stop stays selected in the moved tab"); + let mut rig = Rig::new(app); + rig.click("Midpoint 1"); + assert_eq!(selected_mid(&rig.app(), 2), Some(0), "the diamond is picked"); + rig.app().run("document.move", json!({"to": 1})).unwrap(); + rig.fixture.settle(); + assert_eq!(selected_mid(&rig.app(), 2), Some(0), "the diamond stays picked in the moved tab"); + assert_eq!(rig.number("Location"), Some(30.0), "Location still shows the midpoint"); +} + /// 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]