Fit Image and Image Size drop the selection when they resample (#13856)

Resampling to a new pixel size now drops the active selection instead of
keeping a scaled copy, as the reference application does. Undo restores
the old size and selection in one step. A Fit Image that changes nothing
and a resolution-only Image Size keep the selection. The shared
resample_doc helper (Crop tool) is unchanged.
This commit is contained in:
qvest-ssels committed 2026-11-13 17:58:06 +01:00
1 parent de93ee2d38
commit d6475da125
2 files changed
+93

No files matched your search

+10
View File
@@ -318,6 +318,12 @@ fn image_size(s: &mut Session, p: &Value) -> Result<Value> {
Method::Automatic if nw > ow || nh > oh => Resample::PreserveDetails,
Method::Automatic => Resample::BicubicSharper,
};
// Resampling to a new pixel size drops the selection rather than scaling it, as Photoshop
// does (#13856); undo brings it back with the old size. Taken before the resample so it
// isn't scaled for nothing. A resolution-only change (returned above) keeps it.
if (nw, nh) != (ow, oh) {
doc.selection = None;
}
crate::canvas_geom::keeping_count_points(doc, |doc| resample_doc_with_styles(doc, "image.imageSize", nw, nh, filter, scale_styles))
})?;
// The size the document has now: without resampling `width`/`height` are ignored (#1815).
@@ -1293,3 +1299,7 @@ pub fn specs() -> Vec<CommandSpec> {
#[cfg(test)]
mod tests;
#[cfg(test)]
#[path = "image_size_drops_selection_tests.rs"]
mod image_size_drops_selection_tests;
@@ -0,0 +1,83 @@
//! #13856: Fit Image and Image Size drop the active selection when they resample to a new pixel
//! size, as the reference application does; undo brings back the old size and selection in one
//! step. A Fit Image that changes nothing and a resolution-only Image Size keep the selection.
use photocraft_geom::Rect;
use serde_json::{Value, json};
use crate::Session;
/// The issue's document: 400 x 300 px with a 10,10 50 x 50 selection.
fn doc() -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 400, "height": 300})).unwrap();
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
s
}
fn size(s: &Session) -> (u32, u32) {
let d = &s.active().unwrap().doc;
(d.size.width, d.size.height)
}
fn selection(s: &Session) -> Option<Rect> {
s.active().unwrap().doc.selection.as_ref().map(|sel| sel.content_bounds())
}
fn steps(s: &Session) -> usize {
s.active().unwrap().history.past_len()
}
#[test]
fn fit_image_that_resizes_drops_the_selection_and_undo_restores_it() {
let mut s = doc();
let before = selection(&s);
assert_eq!(before, Some(Rect::from_xywh(10, 10, 50, 50)));
let n = steps(&s);
let r = s.execute("file.automate.fitImage", json!({"width": 200, "height": 200})).unwrap();
assert_eq!(r["changed"], Value::Bool(true));
assert_eq!(size(&s), (200, 150));
assert_eq!(selection(&s), None, "no selection after Fit Image");
assert_eq!(steps(&s), n + 1, "one history step");
s.undo();
assert_eq!(size(&s), (400, 300), "undo restores the size");
assert_eq!(selection(&s), before, "and the selection, in the same step");
}
#[test]
fn image_size_with_a_new_pixel_size_drops_the_selection_and_undo_restores_it() {
let mut s = doc();
let before = selection(&s);
let n = steps(&s);
s.execute("image.imageSize", json!({"width": 200, "height": 150, "resample": "bicubic"})).unwrap();
assert_eq!(size(&s), (200, 150));
assert_eq!(selection(&s), None, "no selection after Image Size");
assert_eq!(steps(&s), n + 1, "one history step");
s.undo();
assert_eq!(size(&s), (400, 300));
assert_eq!(selection(&s), before);
}
#[test]
fn fit_image_that_changes_nothing_keeps_the_selection_and_adds_no_step() {
for p in [json!({"width": 400, "height": 300}), json!({"width": 800, "height": 800, "dontEnlarge": true})] {
let mut s = doc();
let before = selection(&s);
let n = steps(&s);
let r = s.execute("file.automate.fitImage", p.clone()).unwrap();
assert_eq!(r["changed"], Value::Bool(false), "{p}");
assert_eq!(size(&s), (400, 300), "{p}");
assert_eq!(selection(&s), before, "{p}: selection kept");
assert_eq!(steps(&s), n, "{p}: no history step");
}
}
#[test]
fn image_size_with_only_a_new_resolution_keeps_the_selection() {
let mut s = doc();
let before = selection(&s);
s.execute("image.imageSize", json!({"resolution": 300, "resample": "none"})).unwrap();
assert_eq!(size(&s), (400, 300));
assert_eq!(s.active().unwrap().doc.resolution_dpi, 300.0);
assert_eq!(selection(&s), before, "a resolution-only change keeps the selection");
}