diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 1178906459..da5bbea659 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -87,10 +87,12 @@ fn select_stop(s: &mut Session, p: &Value) -> Result { /// front, the Appearance panel's active item and the tool the proxy in front shows the default /// 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. +/// 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)] -pub(crate) struct StopOwner { - doc: Option, +pub struct StopOwner { + /// 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 +108,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()), @@ -140,6 +142,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 112c8b0c98..1fa700da83 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 b2131ae600..fbe030d45a 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -51,7 +51,8 @@ use egui::{Pos2, Ui}; use serde_json::{Value, json}; use vectorcraft_color::gradient::{MAX_REPEATS, MIN_STOPS, insert_stop, move_stop, remove_stop, set_midpoint}; 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 vectorcraft_tools::params::stops_json; // PLAN: assumed hub-w2's `panels::{proxy(app, ui), part_hidden(app, key), set_part_hidden(app, key, on), live_run, Live}` (co.A2, op.A5). @@ -92,9 +93,10 @@ const MENU_DELETE_PRESET: &str = "ui.gradient.deletePreset"; /// The panel's view state that lives only while the app runs (`app.ui.gradient_panel`). #[derive(Clone, Debug, Default)] pub struct GradientUi { - /// The selected midpoint diamond and the object it was picked on: it stands while no stop is - /// selected and that object is still the first selected. - pub(crate) mid_selected: Option<(usize, Option)>, + /// The selected midpoint diamond and the gradient it was picked on: it stands while no stop is + /// selected and the active proxy still shows that gradient + /// ([`vectorcraft_engine::Session::gradient_owner`]). + pub(crate) mid_selected: Option<(usize, StopOwner)>, /// The stops of the gradient the slider's open gesture began from, from its `Begin` until its /// `Commit` or `Cancel`. Every preview restarts from the interaction's snapshot and /// `paint.editGradient` sorts the stops, so the gradient shown mid-gesture is the last preview @@ -282,11 +284,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, stops: usize) -> Option { let (i, owner) = app.ui.gradient_panel.mid_selected?; - (app.session.selected_stop().is_none() && i + 1 < stops && owner == first_id(app)).then_some(i) + (app.session.selected_stop().is_none() && i + 1 < stops && owner == app.session.gradient_owner()).then_some(i) } /// A stored fraction as the percent a field shows, without the `f32` noise (`0.3` is `30`, not `30.000001`). @@ -294,10 +296,6 @@ fn percent(v: f32) -> f64 { (f64::from(v) * 1e6).round() / 1e4 } -fn first_id(app: &VectorcraftApp) -> Option { - app.session.active().and_then(|d| d.selection.objects.first().copied()) -} - // ---------- UI ---------- pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { @@ -829,7 +827,7 @@ fn stops_editor(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gra if out.selected != selected && (events.is_empty() || events.committed().is_some()) { match out.selected { Some(StopRef::Midpoint(_, m)) => { - app.ui.gradient_panel.mid_selected = Some((m, first_id(app))); + app.ui.gradient_panel.mid_selected = Some((m, app.session.gradient_owner())); if app.session.selected_stop().is_some() { select_stop(app, None); } diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index f4b57b8c8b..040031e59a 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -3,6 +3,7 @@ use std::rc::Rc; use craft::ui::testing::{Fixture, Target}; use egui::{Key, Modifiers, Pos2, pos2, vec2}; +use vectorcraft_doc::NodeId; use vectorcraft_engine::Session; use super::*; @@ -205,6 +206,56 @@ fn a_selected_diamond_shows_and_sets_its_midpoint_in_location() { assert_eq!(rig.number("Location"), Some(0.0), "the stop's location again"); } +/// 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 rig = Rig::new(app); + rig.click("Midpoint 1"); + let mid = |rig: &Rig| selected_mid(&rig.app(), 2); + assert_eq!(mid(&rig), Some(0), "the diamond is picked"); + { + let mut app = rig.app(); + app.select_tool("livePaintBucket"); + app.run("paint.setFill", json!({"gradient": {"kind": "radial"}})).unwrap(); + } + rig.fixture.settle(); + assert_eq!(mid(&rig), None, "the bucket's gradient"); + rig.app().select_tool("selection"); + rig.fixture.settle(); + assert_eq!(mid(&rig), Some(0), "the object's fill again"); + rig.app().session.fill_active = false; + rig.fixture.settle(); + 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]