diff --git a/crates/ui-egui/src/canvas/eyedropper_tool.rs b/crates/ui-egui/src/canvas/eyedropper_tool.rs index 730d6afac1..bc2ba5fb5c 100644 --- a/crates/ui-egui/src/canvas/eyedropper_tool.rs +++ b/crates/ui-egui/src/canvas/eyedropper_tool.rs @@ -1,13 +1,14 @@ //! The Eyedropper tool and the Alt stand-in that samples with a painting tool: PhotoCraft's two //! uses of the framework's `EyedropperTool`. //! -//! As the Eyedropper tool, a press and each drag sample the foreground (Alt: the background). As +//! As the Eyedropper tool, a press and each drag sample the foreground (Alt: the background), and a +//! Shift press places a colour sampler instead (Shift+Alt on a sampler deletes it, #11748). As //! the Alt stand-in, every sample goes to the foreground, because Alt is the key that called the //! stand-in, and Shift+Alt adds a colour sampler at the clicked pixel instead (#11890). As the //! Triangle or Custom Shape stand-in, a sample also becomes the tool's solid Fill, and Shift+Alt //! samples as Alt does (#13364, #15856). -use craft::behavior::{CancelReason, Cursor, PointerButton}; +use craft::behavior::{CancelReason, Cursor, IconId, PaintedCursor, PointerButton}; use craft::color_ui::ui::{EyedropperTool, SampleSize, SampleSource}; use craft::keymap::Mods; use craft::theme::Srgba; @@ -29,10 +30,14 @@ mod cmyk_tests; #[path = "other_precise_cursor_tests.rs"] mod other_precise_cursor_tests; +/// The tip of the Color Sampler tool's pipette in its 24-unit icon box, as the framework places it. +const SAMPLER_HOTSPOT: [f32; 2] = [2.0, 22.0]; + /// The gesture's flags, kept in `CanvasTools::eyedropper`. #[derive(Debug, Default)] pub(crate) struct EyedropperState { - /// The running press added a colour sampler, so its drags sample nothing. + /// The running press added (or, with the Eyedropper, deleted) a colour sampler, so its drags + /// sample nothing. sampler_added: bool, } @@ -123,6 +128,19 @@ impl<'a> PcEyedropper<'a> { self.stand_in && !self.shape_fill } + /// The Eyedropper tool's Shift press places or deletes a colour sampler (#11748). + fn shift_edits_samplers(&self) -> bool { + !self.stand_in + } + + /// The Eyedropper tool's Shift press at the pointer (#11748). + fn shift_press(&mut self, pointer: &ToolPointer, cx: &ToolContext<'_>) { + let Some(space) = cx.space() else { return }; + let p = space.view.to_doc(pointer.pos); + let alt = pointer.mods.alt; + self.scope.with_app(|app| shift_click(app, p.x, p.y, alt)); + } + /// Shift+Alt: adds a colour sampler at the clicked pixel, as the Color Sampler tool places one. /// Returns whether the press did; a press on an existing point is not measured again and samples /// instead. @@ -149,6 +167,32 @@ impl<'a> PcEyedropper<'a> { } } +/// An Eyedropper Shift+click at document point (`x`, `y`) (#11748): adds a colour sampler on the +/// clicked pixel, as the Color Sampler tool places one, and leaves the foreground alone. With Alt +/// on an existing sampler it deletes that one instead. At the 10-sampler limit the Color Sampler +/// tool's alert opens and nothing is added; off the canvas nothing happens. +fn shift_click(app: &mut PhotocraftApp, x: f64, y: f64, alt: bool) { + if alt && let Some(index) = crate::analysis::sampler_at(app, [x, y]) { + if let Err(e) = app.run("view.colorSamplers.delete", json!({"index": index})) { + app.ui.status = e; + app.ui.status_error = true; + } + return; + } + let Some((size, full)) = app.session.active().map(|d| (d.doc.size, d.color_samplers.len() >= photocraft_engine::sampler_cmds::MAX_SAMPLERS)) else { + return; + }; + if !((0.0..f64::from(size.width)).contains(&x) && (0.0..f64::from(size.height)).contains(&y)) { + return; + } + if full { + crate::analysis::canvas::open_max_samplers_alert(app); + } else if let Err(e) = app.run("view.colorSamplers.add", json!({"x": x, "y": y})) { + app.ui.status = e; + app.ui.status_error = true; + } +} + /// Whether document point (x, y) is on a colour sampler, within the Color Sampler tool's reach of one. fn on_sampler(app: &PhotocraftApp, x: f64, y: f64) -> bool { let tol = app.tool_reach(); @@ -158,6 +202,11 @@ fn on_sampler(app: &PhotocraftApp, x: f64, y: f64) -> bool { impl CanvasTool for PcEyedropper<'_> { fn press(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) -> ToolAction { self.state.sampler_added = false; + if self.shift_edits_samplers() && pointer.mods.shift { + self.shift_press(pointer, cx); + self.state.sampler_added = true; + return ToolAction::Claimed; + } if self.shift_adds_samplers() && pointer.mods.shift && self.add_sampler(pointer, cx) { self.state.sampler_added = true; return ToolAction::Claimed; @@ -168,7 +217,9 @@ impl CanvasTool for PcEyedropper<'_> { } fn drag(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) { - // While Shift+Alt stay held after a press that added a sampler, moves sample nothing. + // The moves of a press that added a sampler sample nothing, Shift held or not; Shift + // pressed in the middle of a sampling drag keeps the Eyedropper sampling. While Shift+Alt + // stay held, the painting tools' stand-in samples nothing either. if self.state.sampler_added || (self.shift_adds_samplers() && pointer.mods.shift) { return; } @@ -194,6 +245,11 @@ impl CanvasTool for PcEyedropper<'_> { if self.precise_target && cx.precise() { return super::cursor_policy::sample_target(); } + // With Shift held a click places a colour sampler, so the cursor is the Color Sampler + // tool's eyedropper with the target (#11748). + if self.shift_edits_samplers() && cx.mods.shift && !cx.precise() { + return PaintedCursor::tool(IconId::new("sampler"), egui::Vec2::from(SAMPLER_HOTSPOT), None).into(); + } self.inner.cursor(cx) } diff --git a/crates/ui-egui/src/canvas/other_precise_cursor_tests.rs b/crates/ui-egui/src/canvas/other_precise_cursor_tests.rs index 7b052b6947..6f4fa330cf 100644 --- a/crates/ui-egui/src/canvas/other_precise_cursor_tests.rs +++ b/crates/ui-egui/src/canvas/other_precise_cursor_tests.rs @@ -73,3 +73,23 @@ fn standard_eyedropper_and_unaffected_tools_keep_their_cursors() { h.state_mut().ui.tool = Tool::Brush; assert_eq!(pointer(&mut h, Modifiers::ALT), Cursor::Painted(PaintedCursor::Crosshair), "the Alt stand-in keeps its cursor"); } + +/// #11748: with ⇧ held a click places a colour sampler, so the Eyedropper shows the Color Sampler +/// tool's eyedropper with the target; precise Other Cursors keeps its target. +#[test] +fn shift_shows_the_sampler_cursor() { + let mut h = harness(Tool::Eyedropper); + WindowHost::of(&h.ctx).set_caps_lock(Some(false)); + let shift = pointer(&mut h, Modifiers::SHIFT); + assert!(matches!(&shift, Cursor::Painted(PaintedCursor::Tool { icon, .. }) if *icon == IconId::new("sampler")), "got {shift:?}"); + let plain = pointer(&mut h, Modifiers::NONE); + assert!(matches!(&plain, Cursor::Painted(PaintedCursor::Tool { icon, .. }) if *icon == IconId::new("eyedropper")), "got {plain:?}"); + other_cursor(&mut h, "precise"); + assert!(matches!(pointer(&mut h, Modifiers::SHIFT), Cursor::Painted(PaintedCursor::Circle { centre: true, .. })), "precise keeps the target"); + h.state_mut().ui.tool = Tool::Brush; + other_cursor(&mut h, "standard"); + assert!( + !matches!(pointer(&mut h, Modifiers::SHIFT | Modifiers::ALT), Cursor::Painted(PaintedCursor::Tool { icon, .. }) if icon == IconId::new("sampler")), + "the Alt stand-in keeps its cursor" + ); +} diff --git a/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs new file mode 100644 index 0000000000..27ff22c1ea --- /dev/null +++ b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs @@ -0,0 +1,152 @@ +//! #11748: with the Eyedropper, ⇧-click adds a colour sampler on the clicked pixel (the foreground +//! stays), ⇧⌥-click on a sampler deletes it and off one adds one, ⌥-click without ⇧ still sets +//! the background, and at the 10-sampler limit the reference's alert opens and nothing is added. + +use egui::Modifiers; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ToolEvent; +use crate::state::Tool; + +const SHIFT: Modifiers = Modifiers::SHIFT; +const SHIFT_ALT: Modifiers = Modifiers { alt: true, shift: true, ctrl: false, mac_cmd: false, command: false }; + +/// 800 × 600, white, with a blue (30,30,220) rectangle at 600,350–700,450; the Eyedropper. +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); + app.run("select.rect", json!({"x": 600, "y": 350, "width": 100, "height": 100})).unwrap(); + app.run("edit.fill", json!({"color": "#1e1edc"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.ui.tool = Tool::Eyedropper; + app.sync_views(); + app +} + +/// The messages of the open alerts. +fn alerts(app: &PhotocraftApp) -> Vec { + app.framework + .dialogs + .open_dialogs() + .into_iter() + .filter(|d| d.key.as_deref() == Some("alert")) + .filter_map(|d| Some(d.fields.as_ref()?["message"].as_str()?.to_string())) + .collect() +} + +fn click(app: &mut PhotocraftApp, x: f64, y: f64, m: Modifiers) { + crate::canvas::tool_event(app, ToolEvent::Down { x, y, pressure: 1.0 }, m); + crate::canvas::tool_event(app, ToolEvent::Up { x, y }, m); +} + +fn samplers(app: &PhotocraftApp) -> Vec<[f64; 2]> { + app.session.active().unwrap().color_samplers.clone() +} + +fn foreground(app: &PhotocraftApp) -> [u8; 3] { + let c = app.session.tools.foreground; + [0, 1, 2].map(|i| (c[i] * 255.0).round() as u8) +} + +#[test] +fn shift_click_adds_a_sampler_and_keeps_the_foreground() { + let mut app = app(); + let fg = foreground(&app); + // Inside the pixel, not on its edge, where the screen-to-document round trip may land on + // either neighbour. + click(&mut app, 650.3, 400.3, SHIFT); + assert_eq!(samplers(&app), vec![[650.5, 400.5]], "on the clicked pixel's centre"); + assert_eq!(foreground(&app), fg, "the foreground doesn't change"); +} + +#[test] +fn shift_alt_click_deletes_a_sampler_and_adds_one_off_a_sampler() { + let mut app = app(); + click(&mut app, 650.3, 400.3, SHIFT); + click(&mut app, 650.3, 400.3, SHIFT_ALT); + assert!(samplers(&app).is_empty(), "⇧⌥ on the sampler deletes it"); + click(&mut app, 200.3, 100.3, SHIFT_ALT); + assert_eq!(samplers(&app), vec![[200.5, 100.5]], "⇧⌥ away from any sampler adds one"); +} + +#[test] +fn alt_click_on_a_sampler_still_sets_the_background() { + let mut app = app(); + click(&mut app, 650.0, 400.0, SHIFT); + click(&mut app, 650.0, 400.0, Modifiers::ALT); + assert_eq!(samplers(&app).len(), 1, "the sampler stays"); + let bg = app.session.tools.background; + assert_eq!([0, 1, 2].map(|i| (bg[i] * 255.0).round() as u8), [30, 30, 220]); +} + +#[test] +fn the_eleventh_shift_click_shows_the_alert_and_adds_nothing() { + let mut app = app(); + for k in 0..10 { + click(&mut app, 20.0 + 30.0 * f64::from(k), 50.0, SHIFT); + } + assert_eq!(samplers(&app).len(), 10); + click(&mut app, 500.0, 500.0, SHIFT); + assert_eq!(samplers(&app).len(), 10, "no 11th sampler"); + assert_eq!(alerts(&app), ["Could not create a new sampler because the maximum number of samplers is 10."]); +} + +#[test] +fn plain_click_and_drag_still_sample_the_foreground() { + let mut app = app(); + click(&mut app, 650.0, 400.0, Modifiers::NONE); + assert_eq!(foreground(&app), [30, 30, 220]); + assert!(samplers(&app).is_empty()); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 650.0, y: 400.0, pressure: 1.0 }, Modifiers::NONE); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 100.0, pressure: 1.0 }, Modifiers::NONE); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 100.0 }, Modifiers::NONE); + assert_eq!(foreground(&app), [255, 255, 255], "a drag samples where it goes"); +} + +#[test] +fn a_shift_click_off_the_canvas_adds_nothing() { + let mut app = app(); + click(&mut app, -20.0, 50.0, SHIFT); + assert!(samplers(&app).is_empty()); + assert!(alerts(&app).is_empty()); +} + +/// CodeRabbit on #14693: pressing ⇧ in the middle of a sampling drag doesn't stop the sampling +/// (drag sampling is unchanged), and a drag that started with ⇧ (it placed a sampler) samples +/// nothing, even if ⇧ is let go meanwhile. +#[test] +fn shift_pressed_mid_drag_keeps_sampling_and_a_shift_press_never_samples() { + let mut app = app(); + let blue = [30, 30, 220]; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 650.0, y: 400.0, pressure: 1.0 }, Modifiers::NONE); + assert_eq!(foreground(&app), blue); + // ⇧ goes down and the pointer moves onto the white: it samples the white. + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 200.0, y: 100.0, pressure: 1.0 }, SHIFT); + assert_eq!(foreground(&app), [255, 255, 255], "the drag keeps sampling with ⇧ held"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 200.0, y: 100.0 }, SHIFT); + assert!(samplers(&app).is_empty(), "no sampler from a drag that began without ⇧"); + + // A press with ⇧ places a sampler; the moves that follow sample nothing, ⇧ held or not. + let fg = foreground(&app); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 650.0, y: 400.0, pressure: 1.0 }, SHIFT); + for m in [SHIFT, Modifiers::NONE] { + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 660.0, y: 410.0, pressure: 1.0 }, m); + } + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 660.0, y: 410.0 }, Modifiers::NONE); + assert_eq!(samplers(&app).len(), 1, "one sampler"); + assert_eq!(foreground(&app), fg, "the drag after a ⇧ press samples nothing"); +} + +/// CodeRabbit on #14693: a release that another tool receives still ends a ⇧ press, so the next +/// plain Eyedropper press samples as before. +#[test] +fn a_release_taken_by_another_tool_ends_the_shift_press() { + let mut app = app(); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 650.0, y: 400.0, pressure: 1.0 }, SHIFT); + app.ui.tool = Tool::Hand; + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 650.0, y: 400.0 }, Modifiers::NONE); + app.ui.tool = Tool::Eyedropper; + click(&mut app, 200.0, 100.0, Modifiers::NONE); + assert_eq!(foreground(&app), [255, 255, 255], "the press samples: the ⇧ press is over"); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index c0336619b5..a7604f3e15 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -874,6 +874,8 @@ mod color_replacement_digit_keys_tests; #[cfg(test)] mod color_replacement_tests; #[cfg(test)] +mod eyedropper_shift_sampler_tests; +#[cfg(test)] mod home_recent_gate_tests; #[cfg(test)] mod layers_panel_nodes; diff --git a/crates/ui-egui/src/tools/analysis/canvas.rs b/crates/ui-egui/src/tools/analysis/canvas.rs index 6c6f2cf9e1..1c9953d725 100644 --- a/crates/ui-egui/src/tools/analysis/canvas.rs +++ b/crates/ui-egui/src/tools/analysis/canvas.rs @@ -173,8 +173,9 @@ pub(crate) fn drain(app: &mut PhotocraftApp, state: &mut AnalysisTools, _respons #[cfg(test)] pub const MAX_SAMPLERS_ALERT: &str = "Could not create a new sampler because the maximum number of samplers is 10."; -/// Open the alert dialog when the Color Sampler limit is reached (#11743). -fn open_max_samplers_alert(app: &mut PhotocraftApp) { +/// Open the alert dialog when the Color Sampler limit is reached (#11743); the Eyedropper's ⇧-click opens it too +/// (#11748). +pub(crate) fn open_max_samplers_alert(app: &mut PhotocraftApp) { app.ui.status.clear(); app.ui.status_error = false; let message = crate::i18n::fmt(