Merge pull request #12214 from hectorviov/fix/count-cursors

Count tool: arrow cursors with 1+, 1 over a marker and 1- with Alt (#11773)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 00:51:50 +01:00
commit 536d4e7b67
4 files changed
+176

No files matched your search

+7
View File
@@ -1901,6 +1901,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) {
+49
View File
@@ -84,6 +84,52 @@ impl egui::plugin::Plugin for CursorLifecycle {
}
}
/// 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);
// 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);
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<CursorIcon, String> {
bucket::show(painter, at)
@@ -676,3 +722,6 @@ fn rasterize(shape: &Shape, scale: f32) -> Option<CustomCursorImage> {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod count_tests;
@@ -0,0 +1,113 @@
//! #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<String> = 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:?}");
}
}
#[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:?}");
}
}
}
+7
View File
@@ -429,6 +429,13 @@ fn status_on_err(app: &mut PhotocraftApp, r: Result<Value, String>) {
}
}
/// 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)> {
nearest_marker(&app.session.active()?.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) {