Fit Artboard on Screen: no i32 overflow for a board with large coordinates (#1181)

* Fit Artboard on Screen: no i32 overflow for a board with large coordinates

Fixes #981. `fit_artboard` computed the view centre as `(b.x0 + b.x1) as f32`,
which overflows i32 for a board near ±2^30 on either axis, and for a board
whose far edge `Rect::from_xywh` saturated at i32::MAX. In test and debug
builds the shell view command panicked outside any dispatch guard; in release
the sum wrapped and the view was centred on the wrong point. The midpoint is
now computed in f64 and rounded to the f32 the view stores.

The same repro hit a second overflow on the engine side: `layer.new.artboard`'s
default placement added `GAP` to the rightmost board's far edge, which may
already be i32::MAX. That is now a saturating add (the saturated base then
fails rect validation with an actionable error instead of panicking).

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Drop the artboard_cmds.rs overflow hunk (#1032 fixes it in i64); keep the f64 midpoint

The engine's default-placement overflow lands with #1032, which rejects boards
outside the i32 range instead of saturating. Keep only the Fit Artboard f64
midpoint fix; its test now uses a board that stays in range under #1032.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: wstock <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-08 16:38:09 -04:00
1 parent 34225c0f4a
commit 64da6bb455
1 file changed
+23 -1
+23 -1
View File
@@ -65,7 +65,9 @@ pub fn fit_artboard(app: &mut PhotocraftApp) -> Result<Value, String> {
let v = &mut app.ui.views[i];
// Leave room for the name above the board.
v.zoom = ((area.x - 60.0) / b.width().max(1) as f32).min((area.y - 80.0) / b.height().max(1) as f32).clamp(0.01, 64.0);
v.center = [(b.x0 + b.x1) as f32 / 2.0, (b.y0 + b.y1) as f32 / 2.0];
// Widen before adding: a board near ±2^30, or one whose far edge saturated at i32::MAX,
// overflows an i32 sum (#981).
v.center = [((f64::from(b.x0) + f64::from(b.x1)) / 2.0) as f32, ((f64::from(b.y0) + f64::from(b.y1)) / 2.0) as f32];
v.fit_pending = false;
Ok(json!({"zoom": v.zoom, "artboard": id.0, "bounds": [b.x0, b.y0, b.x1, b.y1]}))
}
@@ -203,6 +205,26 @@ mod tests {
assert_eq!(app.ui.dock_tabs.history, 2);
}
#[test]
fn fit_artboard_centres_a_board_with_large_coordinates() {
// #981: `(b.x0 + b.x1) as f32` overflowed i32 for boards near ±2^30 or the i32 edges,
// panicking outside any dispatch guard.
for (params, want) in [
(json!({"x": 2147483000, "y": 0, "width": 600, "height": 1000}), [(2147483000.0 + 2147483600.0) / 2.0, 500.0]),
(json!({"x": 1073741824, "width": 50, "height": 20}), [1073741849.0, 10.0]),
(json!({"y": -1073741840, "width": 50, "height": 20}), [25.0, -1073741830.0]),
] {
let (mut app, ctx) = app();
let i = app.session.active_index().unwrap();
app.run("layer.new.artboard", params.clone()).unwrap();
app.sync_views();
menu(&mut app, &ctx, "view.fitArtboardOnScreen", json!({})).unwrap();
// The view centre is f32, so compare against the true midpoint rounded to f32.
let c = app.ui.views[i].center;
assert_eq!(c, [want[0] as f32, want[1] as f32], "{params}");
}
}
#[test]
fn comp_target_follows_selection_then_last_applied() {
let (mut app, _) = app();