From 526d0cd494d7a5b71c9031aea9f1a92a3dd0ed72 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 15:32:35 -0600 Subject: [PATCH 1/4] Count tool: arrow cursors with 1+, 1 over a marker and 1- with Alt (#11773) --- crates/ui-egui/src/canvas.rs | 7 ++ crates/ui-egui/src/canvas/tool_cursor.rs | 48 +++++++++++ .../src/canvas/tool_cursor/count_tests.rs | 84 +++++++++++++++++++ crates/ui-egui/src/tools/analysis_ui.rs | 8 ++ 4 files changed, 147 insertions(+) create mode 100644 crates/ui-egui/src/canvas/tool_cursor/count_tests.rs diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index ac471a9269..e4f4a80e7f 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1654,6 +1654,13 @@ fn draw_default_tool_cursor(app: &mut PhotocraftApp, ui: &mut egui::Ui, input: & pipette_cursor(ui.ctx(), p) } } + // 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. + Tool::Count if !precise_other_cursor(app) => { + let alt = crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)).alt; + let over = crate::analysis_ui::count_marker_at(app, xf.to_doc(p)).is_some(); + crate::tool_cursor::count(ui.ctx(), p, crate::tool_cursor::CountCursor::pick(over, alt)) + } // The Color Sampler: a pipette, or the crosshair for precise Other Cursors. Tool::ColorSampler => { if precise_other_cursor(app) { diff --git a/crates/ui-egui/src/canvas/tool_cursor.rs b/crates/ui-egui/src/canvas/tool_cursor.rs index 84b69b26cf..41b26afa13 100644 --- a/crates/ui-egui/src/canvas/tool_cursor.rs +++ b/crates/ui-egui/src/canvas/tool_cursor.rs @@ -85,6 +85,51 @@ impl egui::plugin::Plugin for CursorLifecycle { } /// The bucket artwork and its exact fill crosshair move as one native cursor or painted image. +/// The Count tool's cursor (#11773): an arrow with "1+" (a click adds a marker), "1" (over a +/// marker: a click or drag moves it) or "1−" (⌥ over a marker: a click deletes it). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum CountCursor { + Add, + Move, + Delete, +} + +impl CountCursor { + /// The cursor for a pointer `over` a marker or not, with ⌥ held or not. Shift changes nothing, + /// and ⌥ off a marker is still "1+" (an ⌥-click there does nothing). + pub(crate) fn pick(over: bool, alt: bool) -> Self { + match (over, alt) { + (false, _) => Self::Add, + (true, false) => Self::Move, + (true, true) => Self::Delete, + } + } + + /// The badge drawn below right of the arrow. + pub(crate) fn label(self) -> &'static str { + match self { + Self::Add => "1+", + Self::Move => "1", + Self::Delete => "1\u{2212}", + } + } +} + +/// Draw the Count cursor `kind` at `at`: the OS arrow (hotspot at its tip) with its badge below right +/// of it, dark digits on a light halo so they read on any pixel. PhotoCraft's own artwork. +pub(crate) fn count(ctx: &egui::Context, at: Pos2, kind: CountCursor) -> CursorIcon { + let pos = at + vec2(12.0, 16.0); + // Painted straight onto a tooltip-order layer, above the canvas and its overlays: an Area + // would be invisible on the frame it first appears. + let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("pc-count-cursor"))); + let font = egui::FontId::proportional(11.0); + for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0)] { + painter.text(pos + vec2(dx, dy), egui::Align2::LEFT_TOP, kind.label(), font.clone(), egui::Color32::WHITE); + } + painter.text(pos, egui::Align2::LEFT_TOP, kind.label(), font, egui::Color32::BLACK); + CursorIcon::Default +} + pub(crate) fn bucket(painter: &Painter, at: Pos2) -> Result { bucket::show(painter, at) } @@ -666,3 +711,6 @@ fn rasterize(shape: &Shape, scale: f32) -> Option { #[cfg(test)] mod tests; + +#[cfg(test)] +mod count_tests; diff --git a/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs b/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs new file mode 100644 index 0000000000..aaf49baea7 --- /dev/null +++ b/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs @@ -0,0 +1,84 @@ +//! #11773: the Count tool's cursors. An arrow with "1+" off a marker, "1" on one and "1−" with ⌥ +//! on one; Shift changes nothing, Ctrl is the Move tool's cursor. "On a marker" is the click's own +//! hit test, so the cursor never promises what the click won't do. + +use serde_json::json; + +use super::{CountCursor, count}; +use crate::PhotocraftApp; +use crate::canvas::ToolEvent; +use crate::state::Tool; + +/// A 200 × 100 document with the Count tool and one marker at 50,40. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.run("count.add", json!({"x": 50, "y": 40})).unwrap(); + app.ui.tool = Tool::Count; + app +} + +fn markers(app: &PhotocraftApp) -> Vec<[f64; 2]> { + app.session.active().unwrap().doc.measurement.count_groups.iter().flat_map(|g| g.points.clone()).collect() +} + +#[test] +fn the_cursor_kind_follows_the_marker_and_alt() { + assert_eq!(CountCursor::pick(false, false), CountCursor::Add, "empty canvas or pasteboard"); + assert_eq!(CountCursor::pick(true, false), CountCursor::Move, "over a marker"); + assert_eq!(CountCursor::pick(true, true), CountCursor::Delete, "⌥ over a marker"); + assert_eq!(CountCursor::pick(false, true), CountCursor::Add, "⌥ off a marker: still 1+"); + assert_eq!([CountCursor::Add, CountCursor::Move, CountCursor::Delete].map(CountCursor::label), ["1+", "1", "1\u{2212}"], "the badges (a real minus sign)"); +} + +#[test] +fn over_a_marker_is_the_clicks_own_hit_test() { + // Walk a click away from the marker: wherever the cursor says "1" the click grabs and moves the + // marker, and wherever it says "1+" the click adds a new one. + for dx in [0.0, 2.0, 4.0, 6.0, 6.9, 7.1, 8.0, 15.0] { + let mut app = app(); + let at = [50.0 + dx, 40.0]; + let kind = CountCursor::pick(crate::analysis_ui::count_marker_at(&app, at).is_some(), false); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: at[0], y: at[1], pressure: 1.0 }, egui::Modifiers::NONE); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 150.0, y: 80.0, pressure: 1.0 }, egui::Modifiers::NONE); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 150.0, y: 80.0 }, egui::Modifiers::NONE); + let m = markers(&app); + match kind { + CountCursor::Move => assert_eq!(m, vec![[150.0, 80.0]], "{dx} px off: \"1\" and the press moved the marker"), + CountCursor::Add => assert_eq!(m.len(), 2, "{dx} px off: \"1+\" and the click added a marker: {m:?}"), + CountCursor::Delete => assert_ne!(kind, CountCursor::Delete, "{dx} px off: no ⌥ is held"), + } + } +} + +#[test] +fn ctrl_makes_the_count_tool_the_move_tool() { + // Holding Ctrl turns the Count tool into the temporary Move tool, so the canvas shows the Move + // tool's cursor instead of these. + assert!(crate::hold_keys::cmd_moves(Tool::Count)); +} + +#[test] +fn the_cursor_is_the_os_arrow_with_its_badge() { + // The OS arrow keeps its hotspot at the tip; the badge is drawn beside it. + let ctx = egui::Context::default(); + let mut got = Vec::new(); + let mut o = ctx.run_ui(egui::RawInput::default(), |ui| { + for kind in [CountCursor::Add, CountCursor::Move, CountCursor::Delete] { + got.push(count(ui.ctx(), egui::pos2(100.0, 60.0), kind)); + } + }); + o.textures_delta.clear(); + assert_eq!(got, [egui::CursorIcon::Default; 3]); + let texts: Vec = o + .shapes + .into_iter() + .filter_map(|c| match c.shape { + egui::Shape::Text(t) => Some(t.galley.text().to_owned()), + _ => None, + }) + .collect(); + for label in ["1+", "1", "1\u{2212}"] { + assert!(texts.iter().any(|t| t == label), "badge {label} drawn: {texts:?}"); + } +} diff --git a/crates/ui-egui/src/tools/analysis_ui.rs b/crates/ui-egui/src/tools/analysis_ui.rs index ed113909a6..de02348562 100644 --- a/crates/ui-egui/src/tools/analysis_ui.rs +++ b/crates/ui-egui/src/tools/analysis_ui.rs @@ -400,6 +400,14 @@ fn status_on_err(app: &mut PhotocraftApp, r: Result) { } } +/// The count marker a Count tool click at document point `at` would grab (the click's own hit +/// test, with its tolerance), or `None`. The cursor uses it too, so it never says "1" where a click +/// adds a marker (#11773). +pub(crate) fn count_marker_at(app: &PhotocraftApp, at: [f64; 2]) -> Option<(usize, usize)> { + let doc = app.session.active().map(|d| d.doc.clone())?; + nearest_marker(&doc, at, tolerance(app)) +} + fn nearest_marker(d: &Document, at: [f64; 2], tol: f64) -> Option<(usize, usize)> { let mut best: Option<(f64, usize, usize)> = None; for (gi, g) in d.measurement.count_groups.iter().enumerate().filter(|(_, g)| g.visible) { From afb7f3ef59ec2f8f60e185094a7496f65c9ca4fc Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 18:32:10 -0600 Subject: [PATCH 2/4] Count cursor badge: halo on all eight sides (eliets2 review on crates/ui-egui/src/canvas/tool_cursor.rs, count()) Only the four cardinal copies were drawn, leaving the digits' diagonal corners without the light halo on dark or busy pixels. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/tool_cursor.rs | 3 +- .../src/canvas/tool_cursor/count_tests.rs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas/tool_cursor.rs b/crates/ui-egui/src/canvas/tool_cursor.rs index 41b26afa13..4229b23e54 100644 --- a/crates/ui-egui/src/canvas/tool_cursor.rs +++ b/crates/ui-egui/src/canvas/tool_cursor.rs @@ -123,7 +123,8 @@ pub(crate) fn count(ctx: &egui::Context, at: Pos2, kind: CountCursor) -> CursorI // would be invisible on the frame it first appears. let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("pc-count-cursor"))); let font = egui::FontId::proportional(11.0); - for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0)] { + // The halo: a white copy one point off on every side, diagonals too. + for (dx, dy) in [(-1.0, -1.0), (0.0, -1.0), (1.0, -1.0), (-1.0, 0.0), (1.0, 0.0), (-1.0, 1.0), (0.0, 1.0), (1.0, 1.0)] { painter.text(pos + vec2(dx, dy), egui::Align2::LEFT_TOP, kind.label(), font.clone(), egui::Color32::WHITE); } painter.text(pos, egui::Align2::LEFT_TOP, kind.label(), font, egui::Color32::BLACK); diff --git a/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs b/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs index aaf49baea7..1b25791144 100644 --- a/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs +++ b/crates/ui-egui/src/canvas/tool_cursor/count_tests.rs @@ -82,3 +82,32 @@ fn the_cursor_is_the_os_arrow_with_its_badge() { assert!(texts.iter().any(|t| t == label), "badge {label} drawn: {texts:?}"); } } + +#[test] +fn the_badge_halo_surrounds_the_digits_on_all_eight_sides() { + // White copies one point off in every direction, diagonals too, so the dark digits read on + // dark and busy pixels. + let ctx = egui::Context::default(); + let mut o = ctx.run_ui(egui::RawInput::default(), |ui| { + count(ui.ctx(), egui::pos2(100.0, 60.0), CountCursor::Add); + }); + o.textures_delta.clear(); + let texts: Vec<(egui::Pos2, egui::Color32)> = o + .shapes + .into_iter() + .filter_map(|c| match c.shape { + egui::Shape::Text(t) => Some((t.pos, t.fallback_color)), + _ => None, + }) + .collect(); + let digits = texts.iter().find(|(_, c)| *c == egui::Color32::BLACK).expect("the dark digits").0; + for dx in [-1.0, 0.0, 1.0] { + for dy in [-1.0, 0.0, 1.0] { + if (dx, dy) == (0.0, 0.0) { + continue; + } + let want = digits + egui::vec2(dx, dy); + assert!(texts.iter().any(|&(p, c)| c == egui::Color32::WHITE && p == want), "a halo copy at {dx},{dy}: {texts:?}"); + } + } +} From 83049a019b02a5678a74ffbfd7af7a31bfad6d42 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 18:32:16 -0600 Subject: [PATCH 3/4] Count cursor: borrow the document for the hit test, no clone per frame (eliets2 review on crates/ui-egui/src/tools/analysis_ui.rs, count_marker_at) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/tools/analysis_ui.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/ui-egui/src/tools/analysis_ui.rs b/crates/ui-egui/src/tools/analysis_ui.rs index de02348562..77dfca5aec 100644 --- a/crates/ui-egui/src/tools/analysis_ui.rs +++ b/crates/ui-egui/src/tools/analysis_ui.rs @@ -404,8 +404,7 @@ fn status_on_err(app: &mut PhotocraftApp, r: Result) { /// test, with its tolerance), or `None`. The cursor uses it too, so it never says "1" where a click /// adds a marker (#11773). pub(crate) fn count_marker_at(app: &PhotocraftApp, at: [f64; 2]) -> Option<(usize, usize)> { - let doc = app.session.active().map(|d| d.doc.clone())?; - nearest_marker(&doc, at, tolerance(app)) + nearest_marker(&app.session.active()?.doc, at, tolerance(app)) } fn nearest_marker(d: &Document, at: [f64; 2], tol: f64) -> Option<(usize, usize)> { From f72e3bdc7f908c5e0ae7ace61921fd5320366d31 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 18:32:16 -0600 Subject: [PATCH 4/4] Tool cursors: put bucket()'s doc comment back on bucket() (eliets2 review on crates/ui-egui/src/canvas/tool_cursor.rs, CountCursor) The Count cursor was inserted between bucket()'s comment and bucket(), so that line documented CountCursor instead. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/tool_cursor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas/tool_cursor.rs b/crates/ui-egui/src/canvas/tool_cursor.rs index 4229b23e54..b647bf8d57 100644 --- a/crates/ui-egui/src/canvas/tool_cursor.rs +++ b/crates/ui-egui/src/canvas/tool_cursor.rs @@ -84,7 +84,6 @@ impl egui::plugin::Plugin for CursorLifecycle { } } -/// The bucket artwork and its exact fill crosshair move as one native cursor or painted image. /// The Count tool's cursor (#11773): an arrow with "1+" (a click adds a marker), "1" (over a /// marker: a click or drag moves it) or "1−" (⌥ over a marker: a click deletes it). #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -131,6 +130,7 @@ pub(crate) fn count(ctx: &egui::Context, at: Pos2, kind: CountCursor) -> CursorI CursorIcon::Default } +/// The bucket artwork and its exact fill crosshair move as one native cursor or painted image. pub(crate) fn bucket(painter: &Painter, at: Pos2) -> Result { bucket::show(painter, at) }