diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 31cfe000de..eea2fc6238 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -97,6 +97,8 @@ pub struct Session { /// ([`Session::painting_tool`]); None for a stop of the selection's paint. A pick armed for /// such a stop samples only into a stop selected on that gradient, and otherwise ends. pub(crate) stop_pick: Option, + /// The point and document revision the Eyedropper was armed for. + freeform_point_pick: Option, /// Pointer and key input that came while the tool computed an edit on another thread, each with /// its number and view, made in order once the edit is applied. held_input: std::collections::VecDeque<(u64, tooling::HeldInput, ViewInfo)>, diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 367334a008..031d4567d3 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -63,6 +63,7 @@ impl Session { lender: None, back_to_painter: None, stop_pick: None, + freeform_point_pick: None, held_input: Default::default(), input_count: 0, edit_started_by: None, diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index 05dde4c156..486564df80 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -10,6 +10,11 @@ use vectorcraft_tools::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind use crate::{EngineError, Prefs, Result, Session}; +pub(crate) struct FreeformPointPick { + document: (u64, u64, u64), + index: usize, +} + /// What a hover's lend decision holds for ([`Session::hover_tool_at`]). #[derive(Clone, Copy, PartialEq)] pub(crate) struct HoverKey { @@ -228,6 +233,7 @@ impl Session { self.tool = self.make_tool(id); // A pick is armed for a stop after its tool is chosen (`note_stop_pick`). self.stop_pick = None; + self.freeform_point_pick = None; if let Some(selection) = text_selection { self.tool.set_option("textSelection", &selection); } @@ -628,7 +634,7 @@ impl Session { appearance_item: self.appearance_item(), constrain_angle: self.prefs.constrain_angle, keyboard_increment: self.prefs.keyboard_increment, - freeform_point: self.selected_freeform_point(), + freeform_point: if self.tool.id() == "eyedropper" { self.pick_freeform_point() } else { self.selected_freeform_point() }, raster_sample: self.prefs.eyedropper.sample_size, preview_bounds: self.prefs.use_preview_bounds, scale_strokes: self.prefs.scale_strokes, @@ -1543,6 +1549,9 @@ impl Session { let sets_pressed = self.press_taken_by(self.tool.id()); self.tool.set_option(key, v); self.note_stop_pick(key, self.tool.id() == "eyedropper"); + if key == "freeformPoint" && self.tool.id() == "eyedropper" { + self.note_freeform_point_pick(v); + } self.tool_set_since_reset = true; self.keep_tool_settings(); self.keep_press_set(before_press, |k| sets_pressed && k == key, sets_pressed); @@ -1639,9 +1648,14 @@ impl Session { }; self.keep_press_set(before_press, |k| values.contains_key(k) && (sets_pressed || shared.contains(&k)), sets_pressed); // The Eyedropper waiting for a Cmd-drag it lent a selection tool to is armed as the active one. - if (active || lender) && values.contains_key("stop") { + if active || lender { let eyedropper = id.unwrap_or(self.tool.id()) == "eyedropper"; - self.note_stop_pick("stop", eyedropper); + if values.contains_key("stop") { + self.note_stop_pick("stop", eyedropper); + } + if eyedropper && let Some(v) = values.get("freeformPoint") { + self.note_freeform_point_pick(v); + } } out } @@ -1667,6 +1681,24 @@ impl Session { } } + fn note_freeform_point_pick(&mut self, option: &Value) { + self.freeform_point_pick = None; + if option.is_string() + && let Some(doc) = self.active() + && let Some(index) = self.selected_freeform_point() + && self.proxy_freeform(!self.fill_active).is_some_and(|(f, _)| index < f.points.len()) + { + self.freeform_point_pick = Some(FreeformPointPick { document: (doc.uid, doc.revision, doc.reverts), index }); + } + } + + /// An index can outlive its point after a deletion or another paint is applied. + fn pick_freeform_point(&self) -> Option { + let pick = self.freeform_point_pick.as_ref()?; + let doc = self.active()?; + self.selected_freeform_point().filter(|&index| index == pick.index && pick.document == (doc.uid, doc.revision, doc.reverts)) + } + /// The tool that took the press under way: the active tool, or the tool that lent a selection /// tool for it (a Cmd-drag), which the dropped press gives its options back to. fn pressed_tool(&self) -> Option<&dyn Tool> { diff --git a/crates/tools/src/xform/eyedropper.rs b/crates/tools/src/xform/eyedropper.rs index 6ef02bfd11..528578d8b7 100644 --- a/crates/tools/src/xform/eyedropper.rs +++ b/crates/tools/src/xform/eyedropper.rs @@ -56,6 +56,8 @@ use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, Tool pub struct EyedropperTool { /// Sampling for the selected gradient stop: the tool to switch back to afterwards. stop: Option, + /// Sampling for the selected freeform point: the tool to switch back to afterwards. + point: Option, /// The Type tool's highlighted range while sampling, including its selection overlay. text_selection: crate::text::TypeTool, /// Brush Options › Key Color (Hue Shift): sample into the dialog, then [`Self::stop`]. @@ -567,8 +569,14 @@ impl Tool for EyedropperTool { } Click::Back(back) => { self.stop = None; + self.point = None; vec![Action::SwitchTool(back)] } + Click::SamplePoint(color, _, index, back) => { + self.point = None; + let params = json!({ "index": index, "stroke": !cx.fill_active, "color": color_json(&color), "keepModel": true }); + vec![Action::Exec("paint.freeform.setPoint".into(), params), Action::SwitchTool(back)] + } Click::MeshPoints(id, points, color) => { let params = json!({ "id": id.0, "points": points, "color": color_json(&color), "keepModel": true }); vec![Action::Exec("object.mesh.setPointColor".into(), params)] @@ -605,6 +613,9 @@ impl Tool for EyedropperTool { let mut options = if selected.get("editing").is_some_and(|v| !v.is_null()) { selected } else { json!({}) }; if let Some(o) = options.as_object_mut() { o.insert("stop".into(), json!(self.stop)); + if let Some(back) = &self.point { + o.insert("freeformPoint".into(), json!(back)); + } o.insert("brushKeyColor".into(), json!(self.brush_key_color)); if let Some(binding) = &self.image_trace_ignore_color { o.insert("imageTraceIgnoreColor".into(), binding.clone()); @@ -621,7 +632,18 @@ impl Tool for EyedropperTool { /// Type tool or a tool that picks mesh points. fn set_option(&mut self, key: &str, value: &Value) { match key { - "stop" => self.stop = value.as_str().map(str::to_string), + "stop" => { + self.stop = value.as_str().map(str::to_string); + if self.stop.is_some() { + self.point = None; + } + } + "freeformPoint" => { + self.point = value.as_str().map(str::to_string); + if self.point.is_some() { + self.stop = None; + } + } "brushKeyColor" => self.brush_key_color = value.as_bool().unwrap_or(false), "imageTraceIgnoreColor" => self.image_trace_ignore_color = (!value.is_null()).then(|| value.clone()), "meshPoints" => self.mesh_points = crate::meshedit::points_of(value), @@ -633,7 +655,7 @@ impl Tool for EyedropperTool { fn check_option(&self, key: &str, value: &Value) -> Result<(), String> { use crate::option_check as c; match key { - "stop" => c::string_or_null(key, value), + "stop" | "freeformPoint" => c::string_or_null(key, value), "brushKeyColor" => c::boolean(key, value), "textSelection" => self.text_selection.check_option(key, value), _ => Err(c::unknown(key)), @@ -663,6 +685,9 @@ impl Tool for EyedropperTool { if key != ToolKey::Escape || !self.picking() { return vec![]; } + if let Some(back) = self.point.take() { + return vec![Action::SwitchTool(back)]; + } if let Some(binding) = self.image_trace_ignore_color.take() { let back = self.stop.take().unwrap_or_else(|| "selection".to_string()); return vec![Action::Dialog("imageTraceIgnoreColor".into(), json!({ "binding": binding, "cancel": true })), Action::SwitchTool(back)]; @@ -678,7 +703,7 @@ impl Tool for EyedropperTool { } fn returns_to(&self) -> Option<&str> { - self.stop.as_deref().filter(|_| !self.brush_key_color && self.image_trace_ignore_color.is_none()) + self.point.as_deref().or_else(|| self.stop.as_deref().filter(|_| !self.brush_key_color && self.image_trace_ignore_color.is_none())) } /// Escape is the tool's while a pick is armed, so it switches back and ends nothing else. @@ -697,9 +722,10 @@ enum Click { ImageTrace(Value), /// Brush Options › Key Color: this color, sampled from this object. BrushKeyColor(Color, NodeId), - /// A pick for a stop of the gradient a tool paints with (the Live Paint Bucket's, the Shape - /// Builder's fill) that is no longer selected switches back to that tool unsampled. + /// The pick's target is no longer valid. Return to the previous tool without sampling. Back(String), + /// This color, sampled from this object, goes to the armed freeform point. + SamplePoint(Color, NodeId, usize, String), /// The fill color goes to these points of this selected gradient mesh. MeshPoints(NodeId, Vec, Color), /// `appearance.copyFrom` this source, with `reverse` or `append` or neither. @@ -715,19 +741,28 @@ impl Click { fn object(&self) -> Option { match self { Click::ImageTrace(_) | Click::Back(_) => None, - Click::BrushKeyColor(_, id) | Click::MeshPoints(id, ..) | Click::CopyFrom(id, _) | Click::Sample { source: id, .. } => Some(*id), + Click::BrushKeyColor(_, id) + | Click::SamplePoint(_, id, ..) + | Click::MeshPoints(id, ..) + | Click::CopyFrom(id, _) + | Click::Sample { source: id, .. } => Some(*id), } } } impl EyedropperTool { - /// Is a gradient stop, Brush Key Color or Image Trace Ignore Color pick armed? + /// Is a gradient stop, freeform point, Brush Key Color or Image Trace Ignore Color pick armed? fn picking(&self) -> bool { - self.stop.is_some() || self.brush_key_color || self.image_trace_ignore_color.is_some() + self.stop.is_some() || self.point.is_some() || self.brush_key_color || self.image_trace_ignore_color.is_some() } /// What a click at `p` with `mods` does, or None when it does nothing. fn click(&self, cx: &ToolContext, p: Point, mods: Mods) -> Option { + if let Some(back) = &self.point { + let Some(index) = cx.freeform_point else { return Some(Click::Back(back.clone())) }; + let (color, source) = sample_at(cx, p)?; + return Some(Click::SamplePoint(color, source, index, back.clone())); + } if let Some(binding) = &self.image_trace_ignore_color { return Some(Click::ImageTrace(binding.clone())); } diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 2ed369d2da..80476fcd1a 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -510,11 +510,12 @@ fn body(ui: &mut Ui, feed: &Feed, tab: &mut Tab, panel: &mut ColorPanelState, fi Entry::new(Tab::Swatches, tl!("Swatches").to_owned()).icon(IconId::new("swatch-book")), ]; Segmented::new("View", Bind::bound(&mut *tab), &tabs).show(ui); - // A stop's colour can come from the art, as from the Gradient panel's eyedropper. - if !feed.point { - let tip = tl!("Eyedropper: click the art to sample the stop's colour").to_owned(); - filed.eyedropper |= DropperButton::new(tip, Bind::given(false)).show(ui).edit.value() == Some(&true); - } + let tip = if feed.point { + tl!("Eyedropper: click the art to sample the point's colour") + } else { + tl!("Eyedropper: click the art to sample the stop's colour") + }; + filed.eyedropper |= DropperButton::new(tip.to_owned(), Bind::given(false)).show(ui).edit.value() == Some(&true); if *tab == Tab::Color { let (closed, picked) = mode_menu(ui, &feed.shown, panel.mode); outside |= closed; @@ -834,7 +835,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { } if filed.eyedropper { dialogs.with(|host| host.cancel_by_id(ctx, id)); - crate::panels::gradient::stop_eyedropper(app); + match target { + Target::Stop(..) => crate::panels::gradient::stop_eyedropper(app), + Target::Point(..) => crate::panels::gradient::point_eyedropper(app), + } return; } let updated = selected(app).filter(|target| owner.key.matches(app, target)).zip(app.session.gradient_edit_paints(!owner.key.fill).ok()); @@ -1722,6 +1726,91 @@ mod tests { assert!(!is_open(&app)); } + fn point_sampling_panel() -> (crate::tests::panel_harness::Panel, NodeId, NodeId, usize) { + let mut app = app(); + let target = app.session.active().unwrap().selection.objects[0]; + app.run("file.documentColorMode", json!({"mode": "cmyk"})).unwrap(); + app.run("paint.editGradient", json!({"kind": "freeform"})).unwrap(); + app.run("paint.freeform.addPoint", json!({"at": [150, 150], "color": "#0000ff"})).unwrap(); + let index = app.session.selected_freeform_point().unwrap(); + let source = app.run("shape.rectangle", json!({"x": 230, "y": 80, "width": 60, "height": 80})).unwrap()["id"].as_u64().unwrap(); + app.run("paint.setFill", json!({"color": "#00aa5a", "keepModel": true})).unwrap(); + app.run("select.set", json!({"ids": [target.0]})).unwrap(); + app.run("paint.freeform.selectPoint", json!({"index": index})).unwrap(); + let panel = crate::tests::panel_harness::Panel::new(app, |app, ui| { + app.dialogs().unwrap().with(|host| host.run(ui)); + host::pump(app, ui.ctx()); + }); + (panel, target, NodeId(source), index) + } + + fn arm_point_sample(panel: &mut crate::tests::panel_harness::Panel, index: usize) { + open_at(&mut panel.app(), index, Anchor::Screen(egui::pos2(200.0, 200.0))); + panel.frame(); + panel.click("Eyedropper: click the art to sample the point's colour"); + assert!(!is_open(&panel.app())); + assert_eq!(panel.app().session.tool_id(), "eyedropper"); + } + + fn click_sample_source(app: &mut VectorcraftApp) { + use vectorcraft_tools::{PointerEvent, PointerKind}; + let view = app.view_info(); + for kind in [PointerKind::Down, PointerKind::Up] { + let requests = app.session.pointer(&PointerEvent::new(kind, 250.0, 115.0), view).unwrap(); + crate::canvas::apply_requests(app, Ok(requests)); + } + } + + fn node_fill(app: &VectorcraftApp, id: NodeId) -> Paint { + app.session.active().unwrap().doc.node(id).unwrap().appearance.fill_paint() + } + + #[test] + fn freeform_point_eyedropper_preserves_the_sample_model_and_undoes_once() { + let (mut panel, target, source, index) = point_sampling_panel(); + let initial = node_fill(&panel.app(), target); + let source_fill = node_fill(&panel.app(), source); + let color = source_fill.color().unwrap(); + assert!(matches!(color, Color::Rgb { .. }), "the source stores RGB in a CMYK document"); + let points = |app: &VectorcraftApp| app.session.proxy_freeform(false).unwrap().0.points; + let mut expected = points(&panel.app()); + assert_ne!(expected[index].color, color); + + arm_point_sample(&mut panel, index); + click_sample_source(&mut panel.app()); + assert_eq!(panel.app().session.tool_id(), "gradient"); + expected[index].color = color; + assert_eq!(points(&panel.app()), expected, "only the point's color changes"); + assert_eq!(node_fill(&panel.app(), source), source_fill); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(node_fill(&panel.app(), target), initial); + } + + #[test] + fn freeform_point_eyedropper_cancels_and_skips_a_replaced_point() { + let (mut panel, target, source, index) = point_sampling_panel(); + let initial = node_fill(&panel.app(), target); + let source_fill = node_fill(&panel.app(), source); + + arm_point_sample(&mut panel, index); + let escape = egui::Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }; + let ctx = ctx(&panel.app()); + crate::tests::synthetic::key_frame(&mut panel.app(), &ctx, vec![escape]); + assert_eq!(panel.app().session.tool_id(), "gradient"); + assert_eq!(node_fill(&panel.app(), target), initial); + + arm_point_sample(&mut panel, index); + panel.app().run("paint.freeform.deletePoint", json!({"index": index})).unwrap(); + let replacement = panel.app().run("paint.freeform.addPoint", json!({"at": [155, 150], "color": "#ffa000"})).unwrap(); + assert_eq!(replacement["index"].as_u64(), Some(index as u64), "the replacement uses the old index"); + let before_pick = node_fill(&panel.app(), target); + assert_eq!(panel.app().session.tool_id(), "eyedropper"); + click_sample_source(&mut panel.app()); + assert_eq!(panel.app().session.tool_id(), "gradient"); + assert_eq!(node_fill(&panel.app(), target), before_pick); + assert_eq!(node_fill(&panel.app(), source), source_fill); + } + #[test] fn the_popover_closes_when_its_stop_goes_away() { let mut app = app(); diff --git a/crates/ui-egui/src/panels/gradient.rs b/crates/ui-egui/src/panels/gradient.rs index 9d6088cbd3..08025b0731 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -915,10 +915,18 @@ fn stops_editor(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gra /// selected stop, then hands back to the current tool (or, while a pick is already armed, to the /// tool that armed it). pub(crate) fn stop_eyedropper(app: &mut VectorcraftApp) { + gradient_eyedropper(app, "stop"); +} + +pub(crate) fn point_eyedropper(app: &mut VectorcraftApp) { + gradient_eyedropper(app, "freeformPoint"); +} + +fn gradient_eyedropper(app: &mut VectorcraftApp, option: &str) { let tool = app.session.tool_id(); let back = (tool == "eyedropper").then(|| app.session.tool_returns_to()).flatten().unwrap_or_else(|| tool.to_string()); app.select_tool("eyedropper"); - app.session.set_tool_option("stop", &json!(back)); + app.session.set_tool_option(option, &json!(back)); } /// Open the stop popover for stop `i` at screen position `at`, the stop selected first. diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 71e3d0fae9..5fe0d3ab01 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -187,7 +187,9 @@ or point selected, it paints the inactive Fill or Stroke. Freeform gradients: double-clicking a point with the Gradient tool opens the same `gradientStop` popover on the selected point (`paint.freeform.selectPoint`); its fields are `color` (hex), `opacity` and `spread` (percentages), applied by -`ui.dialog.confirm` through `paint.freeform.setPoint`. `ui.key` Escape ends the line Lines mode is drawing. +`ui.dialog.confirm` through `paint.freeform.setPoint`. Its eyedropper chooses the Eyedropper tool and sets the +`freeformPoint` option to the tool to return to. The next click on art samples its color into the same point if it +is still current. Escape cancels a pick and switches back. When drawing a line, `ui.key` Escape ends that line. Tool options: double-clicking a tool button runs `tool.options {tool}`. For `gradient` it opens the Gradient panel; for `eyedropper` it opens Eyedropper Options, an `eyedropperOptions` dialog (fields `sampleSize` 1/3/5, `pickUp` and