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]