From 1b309739a14dbd3bed4e33370ec24386b769262c Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 14 Nov 2026 11:34:36 -0600 Subject: [PATCH 1/3] Eyedropper: Shift+click adds a colour sampler, Shift+Alt+click deletes one (#11748) With the Eyedropper, Shift+click adds a colour sampler on the clicked pixel and leaves the foreground alone; Shift+Alt+click on a sampler deletes it and off one adds one; Alt+click without Shift still sets the background. At the 10-sampler limit the reference's alert opens and nothing is added. While Shift is held the cursor is the eyedropper with the sampler target. Tests: crates/ui-egui/src/eyedropper_shift_sampler_tests.rs. --- crates/ui-egui/src/canvas.rs | 11 +- crates/ui-egui/src/canvas/cursors.rs | 55 ++++++++++ crates/ui-egui/src/canvas/tool_input.rs | 6 +- .../src/eyedropper_shift_sampler_tests.rs | 100 ++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 + 5 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 crates/ui-egui/src/eyedropper_shift_sampler_tests.rs diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 2aaca6a456..2a6c9b76ff 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -70,7 +70,7 @@ pub(crate) use cursors::{ alt_picks, alt_samples, color_sampler_cursor, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor, precise_other_cursor, sampler_hover_cursor, }; -use cursors::{brush_tip_centre, eyedropper_ring, preview_locator_cursor, visible_crosshair}; +use cursors::{brush_tip_centre, eyedropper_cursor, preview_locator_cursor, visible_crosshair}; pub(crate) use display::GPU_OUTPUT; use display::texture_key; pub(crate) use display::{cache_key, canvas_display, display_doc, display_image}; @@ -2068,13 +2068,8 @@ fn draw_default_tool_cursor(app: &mut PhotocraftApp, ui: &mut egui::Ui, input: & // The Eyedropper: the comparison ring follows the pointer while it samples (#213); // otherwise its pipette, or the crosshair for precise Other Cursors. Tool::Eyedropper => { - if let Some(icon) = eyedropper_ring(app, painter, xf, p, response.is_pointer_button_down_on()) { - icon - } else if precise_other_cursor(app) { - egui::CursorIcon::Crosshair - } else { - pipette_cursor(ui.ctx(), p) - } + let shift = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)).shift; + eyedropper_cursor(app, painter, xf, p, response.is_pointer_button_down_on(), shift) } // The Count tool (#11773): an arrow with "1+", "1" over a marker (the click's own hit // test) or "1−" with ⌥ there. Precise Other Cursors keeps today's crosshair. diff --git a/crates/ui-egui/src/canvas/cursors.rs b/crates/ui-egui/src/canvas/cursors.rs index fd44445a2a..c41eeab0ce 100644 --- a/crates/ui-egui/src/canvas/cursors.rs +++ b/crates/ui-egui/src/canvas/cursors.rs @@ -353,6 +353,61 @@ fn sample_shape_fill(app: &mut PhotocraftApp, x: f64, y: f64) { /// Is document point (x, y) on a colour sampler, within the Color Sampler tool's reach of one (7 /// screen pixels)? +/// The Eyedropper's cursor: the comparison ring while it samples (#213); with ⇧ held, the +/// eyedropper with the sampler target, as a ⇧-click places a colour sampler (#11748); otherwise +/// its pipette, or the crosshair for precise Other Cursors. +pub(super) fn eyedropper_cursor(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, p: Pos2, down: bool, shift: bool) -> egui::CursorIcon { + if let Some(icon) = eyedropper_ring(app, painter, xf, p, down) { + icon + } else if precise_other_cursor(app) { + egui::CursorIcon::Crosshair + } else if shift { + color_sampler_cursor(painter.ctx(), p) + } else { + pipette_cursor(painter.ctx(), p) + } +} + +/// The alert a ⇧-click with the Eyedropper shows at the 10-sampler limit (#11748). +pub(crate) const SAMPLER_LIMIT_ALERT: &str = "Could not create a new sampler because the maximum number of samplers is 10."; + +/// An Eyedropper ⇧-click at document point (`x`, `y`) (#11748): adds a colour sampler on the +/// clicked pixel, as the Color Sampler tool places one, without changing the foreground. With ⌥ +/// on an existing sampler it deletes that one instead. At the 10-sampler limit the reference's +/// alert opens and nothing is added; off the canvas nothing happens. +pub(crate) fn eyedropper_shift_click(app: &mut PhotocraftApp, x: f64, y: f64, alt: bool) { + if alt && let Some(i) = crate::analysis_ui::sampler_at(app, [x, y]) { + if let Err(e) = app.run("view.colorSamplers.delete", json!({"index": i})) { + app.ui.status = e; + app.ui.status_error = true; + } + return; + } + let size = app.session.active().map(|d| d.doc.size); + let inside = size.is_some_and(|s| (0.0..f64::from(s.width)).contains(&x) && (0.0..f64::from(s.height)).contains(&y)); + if !inside { + return; + } + match app.run("view.colorSamplers.add", json!({"x": x, "y": y})) { + Ok(_) => {} + Err(e) if e == photocraft_engine::sampler_cmds::limit_message() => { + let shown = app + .ui + .dialogs + .iter() + .any(|d| d.kind == crate::state::DialogKind::Error && d.fields.get("message").and_then(|m| m.as_str()) == Some(tl!(SAMPLER_LIMIT_ALERT))); + if !shown { + let params = json!({ "message": tl!(SAMPLER_LIMIT_ALERT) }).as_object().cloned().unwrap_or_default(); + app.ui.open_dialog(crate::state::DialogKind::Error, params); + } + } + Err(e) => { + app.ui.status = e; + app.ui.status_error = true; + } + } +} + fn on_sampler(app: &PhotocraftApp, x: f64, y: f64) -> bool { let tol = 7.0 / f64::from(app.point_zoom().max(0.01)); app.session.active().is_some_and(|d| d.color_samplers.iter().any(|q| (q[0] - x).hypot(q[1] - y) <= tol)) diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 18663f2cff..a91e01802d 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -1,5 +1,5 @@ use super::cancel_stale_vector_drag; -use super::cursors::{alt_eyedropper, sample_eyedropper}; +use super::cursors::{alt_eyedropper, eyedropper_shift_click, sample_eyedropper}; use super::drag::Drag; use super::gesture::{finish_release, polygon_click, selection_mode}; use super::selection::finish_selection_drag; @@ -409,6 +409,10 @@ fn handle_eyedropper_event(app: &mut PhotocraftApp, tool: Tool, ev: ToolEvent, m return false; } match ev { + // ⇧-click places a colour sampler instead of sampling (#11748); ⇧⌥ on a sampler deletes it. + ToolEvent::Down { x, y, .. } if mods.shift => eyedropper_shift_click(app, x, y, mods.alt), + // While ⇧ stays held after that press, moves sample nothing. + ToolEvent::Move { .. } if mods.shift => {} ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, mods), ToolEvent::Up { .. } => {} } 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..140787f58d --- /dev/null +++ b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs @@ -0,0 +1,100 @@ +//! #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::{DialogKind, 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() -> PhotocraftApp { + 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 +} + +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); + click(&mut app, 650.0, 400.0, 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.0, 400.0, SHIFT); + click(&mut app, 650.0, 400.0, SHIFT_ALT); + assert!(samplers(&app).is_empty(), "⇧⌥ on the sampler deletes it"); + click(&mut app, 200.0, 100.0, 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"); + let alert = app.ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).and_then(|d| d.fields.get("message")).and_then(|m| m.as_str()).map(str::to_owned); + assert_eq!(alert.as_deref(), Some("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!(app.ui.dialogs.is_empty()); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0318581079..fa76fc251d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -854,6 +854,8 @@ mod pen_variants_tests; #[cfg(test)] mod color_replacement_tests; #[cfg(test)] +mod eyedropper_shift_sampler_tests; +#[cfg(test)] mod home_recent_gate_tests; #[cfg(test)] mod layer_shortcut_tests; From 313487ab8fd60b0c948fa6cbdd4473435ccb73f6 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 14 Nov 2026 12:39:46 -0600 Subject: [PATCH 2/3] Eyedropper: drag sampling is unchanged when Shift changes mid-drag (CodeRabbit on tool_input.rs:415) A press that began with Shift places a sampler and its moves sample nothing; any other press keeps sampling as it moves, Shift pressed or not. Test: shift_pressed_mid_drag_keeps_sampling_and_a_shift_press_never_samples --- crates/ui-egui/src/canvas/tool_input.rs | 25 ++++++++++++++---- .../src/eyedropper_shift_sampler_tests.rs | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index a91e01802d..b149bbe453 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -410,15 +410,30 @@ fn handle_eyedropper_event(app: &mut PhotocraftApp, tool: Tool, ev: ToolEvent, m } match ev { // ⇧-click places a colour sampler instead of sampling (#11748); ⇧⌥ on a sampler deletes it. - ToolEvent::Down { x, y, .. } if mods.shift => eyedropper_shift_click(app, x, y, mods.alt), - // While ⇧ stays held after that press, moves sample nothing. - ToolEvent::Move { .. } if mods.shift => {} - ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, mods), - ToolEvent::Up { .. } => {} + ToolEvent::Down { x, y, .. } if mods.shift => { + SAMPLER_PRESS.set(true); + eyedropper_shift_click(app, x, y, mods.alt); + } + // The moves of a press that began with ⇧ sample nothing, whether ⇧ stays down or not. + ToolEvent::Move { .. } if SAMPLER_PRESS.get() => {} + // Any other press samples and keeps sampling as it moves, ⇧ pressed meanwhile or not. + ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => { + if matches!(ev, ToolEvent::Down { .. }) { + SAMPLER_PRESS.set(false); + } + sample_eyedropper(app, x, y, mods); + } + ToolEvent::Up { .. } => SAMPLER_PRESS.set(false), } true } +thread_local! { + /// The Eyedropper press in progress began with ⇧ (it placed or deleted a colour sampler), so + /// the drag that follows doesn't sample (#11748). + static SAMPLER_PRESS: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + /// A Mixer Brush press the engine refused: Indexed Color and Bitmap documents answer it with the /// reference's alert (#11958); anything else goes to the status bar, as before. fn mixer_press_failed(app: &mut PhotocraftApp, message: String) { diff --git a/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs index 140787f58d..5666b5738a 100644 --- a/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs +++ b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs @@ -98,3 +98,29 @@ fn a_shift_click_off_the_canvas_adds_nothing() { assert!(samplers(&app).is_empty()); assert!(app.ui.dialogs.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"); +} From e4daba50cdd2fcc9de5df4b1811fc240e75408f9 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 14 Nov 2026 14:11:58 -0600 Subject: [PATCH 3/3] Eyedropper: any release ends the Shift press (CodeRabbit on tool_input.rs:426) Test: a_release_taken_by_another_tool_ends_the_shift_press --- crates/ui-egui/src/canvas/tool_input.rs | 4 ++++ .../ui-egui/src/eyedropper_shift_sampler_tests.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index b149bbe453..71dcd11deb 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -405,6 +405,10 @@ fn consumes_cancelled_vector_event(app: &mut PhotocraftApp, ev: ToolEvent) -> bo } fn handle_eyedropper_event(app: &mut PhotocraftApp, tool: Tool, ev: ToolEvent, mods: egui::Modifiers) -> bool { + // Whichever tool gets it, a button release ends the press the flag is about. + if matches!(ev, ToolEvent::Up { .. }) { + SAMPLER_PRESS.set(false); + } if tool != Tool::Eyedropper { return false; } diff --git a/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs index 5666b5738a..e8ae0c6b06 100644 --- a/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs +++ b/crates/ui-egui/src/eyedropper_shift_sampler_tests.rs @@ -124,3 +124,16 @@ fn shift_pressed_mid_drag_keeps_sampling_and_a_shift_press_never_samples() { 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 +/// Eyedropper press-less move 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; + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 200.0, y: 100.0, pressure: 1.0 }, Modifiers::NONE); + assert_eq!(foreground(&app), [255, 255, 255], "the move samples: the ⇧ press is over"); +}