Distinguish callout box and leader drags

This commit is contained in:
Dylan M. Taylor
2026-11-20 16:09:30 -05:00
parent 503a71c00e
commit f2b271c546
2 changed files with 42 additions and 2 deletions
+14 -2
View File
@@ -71,6 +71,11 @@ enum Change {
Move {
ids: Vec<AnnotationId>,
},
/// Moving a callout's text box leaves its arrow point in place.
MoveBox {
target: AnnotationId,
from: Rect,
},
/// `from` is the annotation as the press found it: its box and its own aspect.
Resize {
target: AnnotationId,
@@ -798,7 +803,12 @@ impl AnnotateTool {
}
let ids = self.editable();
if !ids.is_empty() {
self.gesture = Some(Drag::Edit { id, begun: false, origin: reading.at, change: Change::Move { ids }, mods: pointer.mods });
let box_move = (ids.len() == 1)
.then(|| self.annotations.iter().find(|a| a.id == target && a.kind == AnnotationKind::Callout && a.rect.contains(reading.at)))
.flatten()
.map(|a| Change::MoveBox { target, from: a.rect });
let change = box_move.unwrap_or(Change::Move { ids });
self.gesture = Some(Drag::Edit { id, begun: false, origin: reading.at, change, mods: pointer.mods });
}
}
None => match self.select_options.empty_press {
@@ -912,6 +922,7 @@ impl AnnotateTool {
fn edit_value(change: &Change, origin: Point, at: Point, mods: Mods, lock: ShiftLock) -> AnnotationEdit {
match change {
Change::Move { ids } => AnnotationEdit::Move { ids: ids.clone(), delta: at - origin },
Change::MoveBox { target, from } => AnnotationEdit::Resize { id: *target, rect: *from + (at - origin) },
Change::Resize { target, handle, from } => {
let aspect = corner_aspect(from, from.rect, mods.shift);
AnnotationEdit::Resize { id: *target, rect: resized(from.rect, *handle, at - origin, aspect) }
@@ -932,6 +943,7 @@ fn edit_value(change: &Change, origin: Point, at: Point, mods: Mods, lock: Shift
fn slipped(change: &Change, value: &AnnotationEdit) -> bool {
match (change, value) {
(Change::Move { .. }, AnnotationEdit::Move { delta, .. }) => delta.hypot() <= SLIP_MOVE,
(Change::MoveBox { from, .. }, AnnotationEdit::Resize { rect, .. }) => (rect.origin() - from.origin()).hypot() <= SLIP_MOVE,
(Change::Resize { from, .. }, AnnotationEdit::Resize { rect, .. }) => {
corners(from.rect.abs()).into_iter().zip(corners(rect.abs())).all(|(was, now)| (now - was).hypot() <= SLIP_RESIZE)
}
@@ -941,7 +953,7 @@ fn slipped(change: &Change, value: &AnnotationEdit) -> bool {
fn edit_label<'a>(change: &Change, words: &'a AnnotateWords) -> &'a str {
match change {
Change::Move { .. } => words.edit_move.as_str(),
Change::Move { .. } | Change::MoveBox { .. } => words.edit_move.as_str(),
Change::Resize { .. } => words.edit_resize.as_str(),
Change::Reshape { .. } => words.edit_reshape.as_str(),
}
+28
View File
@@ -74,6 +74,34 @@ fn near(a: Point, b: Point) -> bool {
(a - b).hypot() < 0.01
}
#[trial]
fn a_callout_box_and_leader_drag_report_distinct_edits() {
let mut callout = annotation(1, AnnotationKind::Callout, Rect::new(150.0, 100.0, 250.0, 140.0));
callout.points = vec![Point::new(60.0, 60.0), Point::new(120.0, 120.0)];
let make = || {
let mut tool = AnnotateTool::new(AnnotationTool::Select, 0);
tool.annotations = vec![callout.clone()];
tool.selected = vec![callout.id];
rig(tool)
};
let mut box_rig = make();
drag(&mut box_rig, Point::new(200.0, 120.0), Point::new(220.0, 130.0));
assert!(
values(&box_rig)
.iter()
.any(|edit| matches!(edit, AnnotationEdit::Resize { id, rect } if *id == callout.id && *rect == Rect::new(170.0, 110.0, 270.0, 150.0))),
"a box drag moves the box without its arrow"
);
let mut leader_rig = make();
drag(&mut leader_rig, Point::new(90.0, 90.0), Point::new(100.0, 90.0));
assert!(
values(&leader_rig)
.iter()
.any(|edit| matches!(edit, AnnotationEdit::Move { ids, delta } if ids == &[callout.id] && *delta == Vec2::new(10.0, 0.0))),
"a leader drag moves the whole callout"
);
}
fn button(at: Pos2, pressed: bool, modifiers: Modifiers) -> Event {
Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers }
}