Merge pull request #3359 from davidmann4/fix/lasso-cursor-arrow

Lasso cursors: an arrow on the hotspot with the tool icon beside it (photocraft#16671)
This commit is contained in:
David Mann
2026-11-24 18:27:56 +01:00
committed by GitHub
6 changed files with 113 additions and 21 deletions
+9 -2
View File
@@ -208,13 +208,15 @@ pub enum PaintedCursor {
end: bool,
},
/// A pointer with the tool's icon below and right of it: a crosshair (Perspective Crop), or an arrow with its tip
/// on the hotspot (Paint Bucket, Ruler).
/// on the hotspot (Paint Bucket, Ruler, the lassos).
#[non_exhaustive]
PointerTool {
/// The pointer the icon rides beside.
pointer: PointerGlyph,
/// The icon.
icon: IconId,
/// A mark at the icon's lower right.
badge: Option<CursorBadge>,
},
/// No cursor at all.
Hidden,
@@ -294,7 +296,12 @@ impl PaintedCursor {
/// `pointer` with the tool's icon below and right of it.
pub const fn pointer_tool(pointer: PointerGlyph, icon: IconId) -> Self {
PaintedCursor::PointerTool { pointer, icon }
PaintedCursor::PointerTool { pointer, icon, badge: None }
}
/// `pointer` with the tool's icon below and right of it, and `badge` at the icon's lower right.
pub const fn pointer_tool_badged(pointer: PointerGlyph, icon: IconId, badge: Option<CursorBadge>) -> Self {
PaintedCursor::PointerTool { pointer, icon, badge }
}
/// A system cursor with a mark.
+5 -7
View File
@@ -1,19 +1,16 @@
//! [`LassoTool`]: the freehand Lasso with Alt-latched straight segments, and the pieces the Polygonal Lasso shares.
use craft_behavior::geometry::{ConstrainStyle, DraftEvent, DraftKind, DraftPoint, DraftReach, OutlineDraft, SelectionOperation};
use craft_behavior::{CancelReason, Cursor, PaintedCursor, PointerButton};
use craft_behavior::{CancelReason, Cursor, PaintedCursor, PointerButton, PointerGlyph};
use craft_keymap::{Key, Mods};
use craft_ui::canvas::{CanvasTool, ToolAccess, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPath, ToolPointer};
use craft_ui::primitives::{Draw, PaintShape, StrokeWidth, ToolIcon};
use egui::{Pos2, Vec2};
use egui::Pos2;
use kurbo::Rect;
use super::drag::{InsideDrag, PressRule, cursor_over, drag_kind, moved_selection_snap};
use super::{DragKind, Place, Report, SelectEvent, SelectPhase, SelectToolPass, SelectWords, SelectionProbe, SelectionShape};
/// Where a lasso cursor's icon touches the document: the icon's own origin.
const ICON_HOTSPOT: Vec2 = Vec2::ZERO;
/// The reach of an outline's closing presses in this pass: a press within the slop of the first vertex or of the last
/// closes.
pub(super) fn reach(cx: &ToolContext<'_>) -> DraftReach {
@@ -54,7 +51,8 @@ pub(super) struct LassoCursor<'a> {
pub(super) rule: PressRule,
}
/// The lasso icon with the operation's badge: the latched one while an outline is open, else the live modifiers';
/// The arrow with its tip on the point the outline starts from, the lasso icon below and right of it and the
/// operation's badge beside the icon: the latched one while an outline is open, else the live modifiers';
/// Cmd is the Move tool's, so it shows none. Over the selection, the cursors of [`cursor_over`].
pub(super) fn cursor(shown: &LassoCursor<'_>, probe: &dyn SelectionProbe, cx: &ToolContext<'_>) -> Cursor {
let LassoCursor { icon, draft, operation, bar, inside, rule } = shown;
@@ -71,7 +69,7 @@ pub(super) fn cursor(shown: &LassoCursor<'_>, probe: &dyn SelectionProbe, cx: &T
} else {
SelectionOperation::resolve(*bar, cx.mods, probe.has_selection()).badge()
};
PaintedCursor::tool(icon.icon(), ICON_HOTSPOT, badge).into()
PaintedCursor::pointer_tool_badged(PointerGlyph::Arrow, icon.icon(), badge).into()
}
/// The outline as marching ants and, between straight segments, the rubber band: the one painter of an
+4 -7
View File
@@ -1,7 +1,7 @@
//! [`MagneticLassoTool`]: an outline that snaps to the edges the app's [`EdgeTracer`] finds.
use craft_behavior::geometry::SelectionOperation;
use craft_behavior::{CancelReason, Cursor, PaintedCursor};
use craft_behavior::{CancelReason, Cursor, PaintedCursor, PointerGlyph};
use craft_keymap::{Key, Mods};
use craft_ui::canvas::{CanvasTool, ToolAccess, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPath, ToolPointer};
use craft_ui::primitives::{Draw, PaintShape, StrokeWidth, ToolIcon};
@@ -12,9 +12,6 @@ use serde::{Deserialize, Serialize};
use super::lasso::{DraftKey, draft_command, outline_access};
use super::{Place, Report, SelectEvent, SelectPhase, SelectToolPass, SelectionProbe, SelectionShape};
/// Where the magnetic lasso cursor's icon touches the document: the icon's own origin.
const ICON_HOTSPOT: Vec2 = Vec2::ZERO;
/// The detection settings of one edge query.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
@@ -478,8 +475,8 @@ impl<T: EdgeTracer> CanvasTool for SelectToolPass<'_, MagneticLassoTool<T>> {
if self.tool.outline.as_ref().is_some_and(|o| !o.down) { ToolClaims::NONE.keyboard().deletes() } else { ToolClaims::NONE }
}
/// The width circle under the canvas's precise cursor (`width * zoom`, at least the slop); otherwise the lasso
/// icon with the operation's badge.
/// The width circle under the canvas's precise cursor (`width * zoom`, at least the slop); otherwise the arrow on
/// the pointer with the lasso icon beside it and the operation's badge.
fn cursor(&self, cx: &ToolContext<'_>) -> Cursor {
let tool = &*self.tool;
if cx.precise()
@@ -494,7 +491,7 @@ impl<T: EdgeTracer> CanvasTool for SelectToolPass<'_, MagneticLassoTool<T>> {
None if cx.mods.cmd => None,
None => SelectionOperation::resolve(tool.bar, cx.mods, self.probe.has_selection()).badge(),
};
PaintedCursor::tool(ToolIcon::MagneticLasso.icon(), ICON_HOTSPOT, badge).into()
PaintedCursor::pointer_tool_badged(PointerGlyph::Arrow, ToolIcon::MagneticLasso.icon(), badge).into()
}
/// The first click starts an outline; later ones close near the first point, add a straight segment with Alt (and
+42
View File
@@ -951,6 +951,48 @@ fn the_magnetic_lasso_shows_the_width_circle_under_the_precise_cursor() {
}
}
/// The cursor `rig` shows over the middle of its canvas with `mods` held.
fn lasso_cursor<T: Reports + 'static>(rig: &mut Rig<T>, mods: Modifiers) -> Option<craft_behavior::Cursor>
where
for<'a> SelectToolPass<'a, T>: CanvasTool,
{
let ctx = rig.fixture().ctx().clone();
let over = rig.canvas().center();
rig.fixture().frame(vec![pointer::moved(over)]);
hold(rig, mods);
rig.fixture().frame(Vec::new());
craft_ui::runtime::cursor::current(&ctx)
}
// The lassos show an arrow with its tip where the outline starts and the tool's icon below right of it, as the reference
// application does; the operation's badge rides at the icon's lower right. With the icon alone on the pointer, its
// empty top-left corner was the hotspot, so the outline began beside the icon (photocraft#16671).
#[trial]
fn the_lassos_point_an_arrow_at_where_the_outline_starts() {
use craft_behavior::{Cursor, CursorBadge, PaintedCursor, PointerGlyph};
use craft_ui::primitives::ToolIcon;
fn check(cursor: Option<Cursor>, icon: ToolIcon, badge: Option<CursorBadge>, what: &str) {
match cursor {
Some(Cursor::Painted(PaintedCursor::PointerTool { pointer, icon: shown, badge: b, .. })) => {
assert_eq!(pointer, PointerGlyph::Arrow, "{what}: an arrow");
assert_eq!(shown, icon.icon(), "{what}: the tool's icon");
assert_eq!(b, badge, "{what}: the badge");
}
other => panic!("{what}: an arrow with the tool's icon, got {other:?}"),
}
}
let selected = Rect::new(20.0, 20.0, 60.0, 60.0);
let shift = Modifiers::SHIFT;
let mut lasso = rig("Lasso", LassoTool::new(), Fake::selected(selected));
check(lasso_cursor(&mut lasso, Modifiers::NONE), ToolIcon::Lasso, None, "Lasso");
check(lasso_cursor(&mut lasso, shift), ToolIcon::Lasso, Some(CursorBadge::Add), "Lasso with Shift");
let mut polygon = rig("Polygonal Lasso", PolygonLassoTool::new(), Fake::selected(selected));
check(lasso_cursor(&mut polygon, Modifiers::NONE), ToolIcon::PolygonalLasso, None, "Polygonal Lasso");
check(lasso_cursor(&mut polygon, Modifiers::ALT), ToolIcon::PolygonalLasso, Some(CursorBadge::Subtract), "Polygonal Lasso with Alt");
let (mut magnetic, _) = magnetic();
check(lasso_cursor(&mut magnetic, Modifiers::NONE), ToolIcon::MagneticLasso, None, "Magnetic Lasso");
}
#[trial]
fn width_steps_by_one_and_shift_jumps_to_the_limits() {
let mut settings = MagneticSettings::default();
@@ -69,11 +69,11 @@ pub(crate) fn pointer_art(env: &Env, cursor: &PaintedCursor, closed: bool, marks
PaintedCursor::Locator => locator(&pen),
PaintedCursor::Arrow { hollow, toward, .. } => arrow(&pen, *hollow, *toward),
PaintedCursor::PathText { start, end, .. } => path_text(&pen, *start, *end),
PaintedCursor::PointerTool { pointer, icon, .. } => {
PaintedCursor::PointerTool { pointer, icon, badge, .. } => {
// The arrow's icon sits under its body, so it takes the larger size to show past the arrow.
let side = if *pointer == PointerGlyph::Arrow { env.tokens.size.icon.md } else { env.tokens.size.icon.sm };
let icon = SvgIcon::of(&env.tokens, env.direction, side, icon, Mirror::Auto);
pointer_tool(&pen, *pointer, icon, side, edge, ink);
pointer_tool(&pen, *pointer, icon, *badge, side, edge, ink);
}
// `Hidden` has no marks.
_ => {}
@@ -451,14 +451,27 @@ pub(crate) fn path_text(pen: &Pen, start: bool, end: bool) {
/// `pointer` with the tool's `icon` of `side` points below and right of it: a crosshair with arms of `side` points in a
/// line of `width` in `color`, as the plain crosshair pointer, beside the icon; or the solid arrow, as the plain arrow
/// pointer, over the icon's top-left corner. An icon the catalog lacks leaves only the pointer.
pub(crate) fn pointer_tool(pen: &Pen, pointer: PointerGlyph, icon: Option<SvgIcon>, side: f32, width: f32, color: Color32) {
/// pointer, over the icon's top-left corner; `badge` at the icon's lower right. An icon the catalog lacks leaves only the
/// pointer and the badge.
pub(crate) fn pointer_tool(
pen: &Pen,
pointer: PointerGlyph,
icon: Option<SvgIcon>,
badge: Option<CursorBadge>,
side: f32,
width: f32,
color: Color32,
) {
let corner = match pointer {
PointerGlyph::Arrow => ARROW_ICON,
_ => CROSSHAIR_ICON,
};
let rect = Rect::from_min_size(v(corner).to_pos2(), Vec2::splat(side));
if let Some(icon) = icon {
icon_on_halo(pen, Rect::from_min_size(v(corner).to_pos2(), Vec2::splat(side)), icon);
icon_on_halo(pen, rect, icon);
}
if let Some(badge) = badge {
self::badge(pen, rect.right_bottom().to_vec2(), badge);
}
match pointer {
PointerGlyph::Arrow => arrow(pen, false, None),
@@ -209,6 +209,41 @@ fn a_tool_icon_rides_below_right_of_its_pointer() {
}
}
/// An arrow with a tool icon keeps ink on its hotspot (the arrow's tip), and a badge lands below right of the icon: the
/// marks reach no farther left or up than without it, and farther right (photocraft#16671).
#[trial]
fn a_badged_tool_pointer_keeps_its_tip_on_the_hotspot_and_the_badge_below_right() {
fn reach(image: &CustomCursorImage) -> [i32; 4] {
let (side, [hx, hy]) = (i32::from(image.size[0]), image.hotspot.map(i32::from));
let mut reach = [0; 4];
for (i, px) in image.rgba.chunks(4).enumerate() {
if px[3] > 0 {
let (dx, dy) = (i as i32 % side - hx, i as i32 / side - hy);
reach = [reach[0].max(-dx), reach[1].max(-dy), reach[2].max(dx), reach[3].max(dy)];
}
}
reach
}
for scale in [1.0, 1.25, 2.0] {
let mut frames = frames(linux(), scale);
let plain = painted(&mut frames, PaintedCursor::pointer_tool(PointerGlyph::Arrow, IconId::new("lasso"))).image.unwrap();
let [hx, hy] = plain.hotspot.map(i32::from);
assert!(pixel(&plain, hx, hy)[3] > 0, "{scale}: the arrow's tip is on the hotspot");
for badge in [CursorBadge::Add, CursorBadge::Subtract, CursorBadge::Intersect] {
let badged = PaintedCursor::pointer_tool_badged(PointerGlyph::Arrow, IconId::new("lasso"), Some(badge));
let image = painted(&mut frames, badged).image.unwrap();
let [hx, hy] = image.hotspot.map(i32::from);
assert!(pixel(&image, hx, hy)[3] > 0, "{scale} {badge:?}: the arrow's tip is on the hotspot");
let (with, without) = (reach(&image), reach(&plain));
assert_eq!(with[..2], without[..2], "{scale} {badge:?}: nothing reaches farther left or up");
// A minus lies on the icon's bottom edge, so only the plus and the cross reach lower.
assert!(with[2] > without[2] && with[3] >= without[3], "{scale} {badge:?}: the badge lies at the icon's lower right");
}
let none = PaintedCursor::pointer_tool_badged(PointerGlyph::Arrow, IconId::new("lasso"), None);
assert_eq!(reach(&painted(&mut frames, none).image.unwrap()), reach(&plain), "{scale}: no badge is the plain pointer");
}
}
/// An unchanged pointer hands the window the same image, so the integration uploads it to the OS once; a new size or
/// scale makes a new one; a frame whose winner is a system cursor, or that asks for nothing, clears it (egui keeps an
/// image until it is replaced).