Shapes: a very long side widget drag stops at 3 or 11 sides without overflowing

sides_after adds the drag steps with saturating arithmetic, so a drag to an extreme coordinate gives 3 or 11 sides instead of an overflow.
This commit is contained in:
dotCooCoo
2026-11-17 09:33:33 -08:00
parent ac2a05f358
commit 6a9106b8d7
+10 -2
View File
@@ -80,11 +80,11 @@ pub fn over_widget(cx: &ToolContext, p: Point) -> bool {
pub fn sides_after(from: u32, steps: i64) -> u32 {
let (min, max) = (i64::from(MIN_SIDES), i64::from(MAX_SIDES));
let to = if from <= MAX_SIDES {
i64::from(from) + steps
i64::from(from).saturating_add(steps)
} else if steps >= 0 {
return from;
} else {
max + 1 + steps
(max + 1).saturating_add(steps)
};
to.clamp(min, max) as u32
}
@@ -213,6 +213,14 @@ mod tests {
assert_eq!(sides_after(1000, -100), 3);
}
#[test]
fn the_longest_drags_stay_within_three_and_eleven() {
assert_eq!(sides_after(5, i64::MAX), 11);
assert_eq!(sides_after(5, i64::MIN), 3);
assert_eq!(sides_after(40, i64::MAX), 40);
assert_eq!(sides_after(40, i64::MIN), 3);
}
/// The widget sits 16 px right of the middle of the box's right side, at any zoom; only one
/// whole editable live polygon shows it.
#[test]