mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
Artboards: Color Sampler points follow the document when a board re-bases the canvas (#9302)
A board placed or moved left of / above the origin shifts the whole document (fit_canvas), but no artboard path moved the Color Sampler points or the painting symmetry, so they read other pixels afterwards. fit_canvas now returns its shift and every caller moves both by it, as Canvas Size's sampler map does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b278ff7245
commit
e76b8d3fd4
3 files changed
+79
-18
No files matched your search
@@ -202,8 +202,8 @@ pub(crate) fn recorded_duplicate(s: &Session, mut p: Value) -> Value {
|
||||
/// contents, guides, paths…) right/down by its overhang, then the canvas grows to show it. An
|
||||
/// axis whose boards span more than the largest canvas, or a shift that would push pixels past
|
||||
/// the 32-bit range, stays unshifted: the board stays partly off-canvas, as one past the size
|
||||
/// limit does.
|
||||
pub(crate) fn fit_canvas(doc: &mut Document) {
|
||||
/// limit does. Returns the shift, for [`follow_shift`].
|
||||
pub(crate) fn fit_canvas(doc: &mut Document) -> (i32, i32) {
|
||||
let max = i64::from(crate::document_preset_cmds::MAX_DIMENSION);
|
||||
let boards: Vec<Rect> = doc.artboards().iter().map(|b| b.2.rect).collect();
|
||||
// The canvas and every board, per axis (`lo` ≤ 0 is the canvas origin or a board past it).
|
||||
@@ -225,6 +225,20 @@ pub(crate) fn fit_canvas(doc: &mut Document) {
|
||||
// shapes against it, leaving them missing or clipped (#2389).
|
||||
crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes);
|
||||
}
|
||||
(sx, sy)
|
||||
}
|
||||
|
||||
/// Keep the Color Sampler points and the painting symmetry on their pixels after an edit shifted
|
||||
/// the document by `(dx, dy)` to show a board past the origin ([`fit_canvas`]), as Canvas Size's
|
||||
/// sampler map does (#9302). View state: no history step. The canvas only grew, so every point
|
||||
/// stays on it.
|
||||
pub(crate) fn follow_shift(s: &mut Session, (dx, dy): (i32, i32)) {
|
||||
let Some(st) = s.active_mut().filter(|_| (dx, dy) != (0, 0)) else { return };
|
||||
for p in &mut st.color_samplers {
|
||||
*p = [p[0] + f64::from(dx), p[1] + f64::from(dy)];
|
||||
}
|
||||
let a = photocraft_geom::Affine::translate(f64::from(dx), f64::from(dy));
|
||||
st.symmetry = st.symmetry.take().and_then(|sym| sym.transform(&a));
|
||||
}
|
||||
|
||||
fn next_artboard_name(doc: &Document) -> String {
|
||||
@@ -272,14 +286,15 @@ fn new_artboard(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let background = background_param(p, cmd)?.unwrap_or_default();
|
||||
let name = p.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string);
|
||||
let id = s.edit("New Artboard", |doc, active| {
|
||||
let (id, shift) = s.edit("New Artboard", |doc, active| {
|
||||
let mut g = Layer::group(name.unwrap_or_else(|| next_artboard_name(doc)), vec![]);
|
||||
set_artboard(&mut g, Artboard { rect, background, preset });
|
||||
let id = doc.insert_above(None, g);
|
||||
fit_canvas(doc);
|
||||
let shift = fit_canvas(doc);
|
||||
*active = Some(id);
|
||||
Ok(id)
|
||||
Ok((id, shift))
|
||||
})?;
|
||||
follow_shift(s, shift);
|
||||
crate::layer_multi_cmds::set_selection(s, vec![id], Some(id), Some(id))?;
|
||||
Ok(json!({"layer": id.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
@@ -289,17 +304,18 @@ fn artboard_from_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = p.get("layer").and_then(Value::as_u64).map(LayerId).or(d.active_layer).ok_or(EngineError::Other("no active layer".into()))?;
|
||||
let canvas = d.doc.bounds();
|
||||
let background = background_param(p, "layer.new.artboardFromGroup")?.unwrap_or_default();
|
||||
let rect = s.edit("Artboard from Group", |doc, active| {
|
||||
let (rect, shift) = s.edit("Artboard from Group", |doc, active| {
|
||||
if !doc.layers.iter().any(|t| t.id == id && t.is_group() && t.artboard().is_none()) {
|
||||
return Err(EngineError::Other("artboards are made from a top-level layer group".into()));
|
||||
}
|
||||
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
|
||||
let rect = crate::layer_multi_cmds::layer_bounds(l).unwrap_or(canvas);
|
||||
set_artboard(l, Artboard { rect, background, preset: String::new() });
|
||||
fit_canvas(doc);
|
||||
let shift = fit_canvas(doc);
|
||||
*active = Some(id);
|
||||
Ok(rect)
|
||||
Ok((rect, shift))
|
||||
})?;
|
||||
follow_shift(s, shift);
|
||||
Ok(json!({"layer": id.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
|
||||
@@ -309,7 +325,7 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let canvas = d.doc.bounds();
|
||||
let name = p.get("name").and_then(Value::as_str).map(str::to_string);
|
||||
let background = background_param(p, "layer.new.artboardFromLayers")?.unwrap_or_default();
|
||||
let (gid, rect) = s.edit("Artboard from Layers", |doc, active| {
|
||||
let (gid, rect, shift) = s.edit("Artboard from Layers", |doc, active| {
|
||||
let ids = crate::layer_multi_cmds::top_level(doc, &sel);
|
||||
if ids.is_empty() || ids.iter().any(|id| doc.artboard_of(*id).is_some()) {
|
||||
return Err(EngineError::Other("select layers that are not in an artboard".into()));
|
||||
@@ -323,10 +339,11 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
set_artboard(&mut g, Artboard { rect, background, preset: String::new() });
|
||||
let gid = doc.insert_above(None, g);
|
||||
crate::layer_multi_cmds::check_group_depth(doc, "Artboard from Layers")?;
|
||||
fit_canvas(doc);
|
||||
let shift = fit_canvas(doc);
|
||||
*active = Some(gid);
|
||||
Ok((gid, rect))
|
||||
Ok((gid, rect, shift))
|
||||
})?;
|
||||
follow_shift(s, shift);
|
||||
crate::layer_multi_cmds::set_selection(s, vec![gid], Some(gid), Some(gid))?;
|
||||
Ok(json!({"layer": gid.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
@@ -365,7 +382,7 @@ fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
));
|
||||
}
|
||||
};
|
||||
s.edit("Edit Artboard", |doc, _| {
|
||||
let shift = s.edit("Edit Artboard", |doc, _| {
|
||||
if move_contents && (dx, dy) != (0, 0) {
|
||||
let snapshot = doc.clone();
|
||||
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
|
||||
@@ -387,9 +404,9 @@ fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None if rect.width() != old.rect.width() || rect.height() != old.rect.height() => a.preset.clear(),
|
||||
None => {}
|
||||
}
|
||||
fit_canvas(doc);
|
||||
Ok(())
|
||||
Ok(fit_canvas(doc))
|
||||
})?;
|
||||
follow_shift(s, shift);
|
||||
Ok(json!({"layer": id.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
|
||||
@@ -441,7 +458,7 @@ fn add_beside(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let grown = |len: u32, by: i32| (i64::from(len) + i64::from(by)).clamp(1, 300_000) as u32;
|
||||
let label = if duplicate { "Duplicate Artboard" } else { "New Artboard" };
|
||||
let add_copy = crate::commands::layer_panel_option(s, "addCopyToCopiedLayersAndGroups", true);
|
||||
let (nid, rect) = s.edit(label, |doc, active| {
|
||||
let (nid, rect, (fx, fy)) = s.edit(label, |doc, active| {
|
||||
if (sx, sy) != (0, 0) {
|
||||
crate::image_cmds::translate_doc(doc, cmd, sx, sy)?;
|
||||
doc.size = Size::new(grown(doc.size.width, sx), grown(doc.size.height, sy));
|
||||
@@ -470,10 +487,11 @@ fn add_beside(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
set_artboard(&mut g, Artboard { rect, background: src.background, preset: src.preset.clone() });
|
||||
doc.insert_above(Some(id), g)
|
||||
};
|
||||
fit_canvas(doc);
|
||||
let fitted = fit_canvas(doc);
|
||||
*active = Some(nid);
|
||||
Ok((nid, rect))
|
||||
Ok((nid, rect, fitted))
|
||||
})?;
|
||||
follow_shift(s, (sx.saturating_add(fx), sy.saturating_add(fy)));
|
||||
crate::layer_multi_cmds::set_selection(s, vec![nid], Some(nid), Some(nid))?;
|
||||
Ok(json!({"layer": nid.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]}))
|
||||
}
|
||||
|
||||
@@ -613,6 +613,47 @@ fn moving_an_artboard_left_of_or_above_the_origin_grows_the_canvas_there() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_board_past_the_origin_moves_the_color_samplers_and_symmetry_with_the_document() {
|
||||
// #9302: the re-based document moved everything but the Color Sampler points and the
|
||||
// painting symmetry, which then sat on other pixels. They follow, as with Canvas Size.
|
||||
let samplers = |s: &Session| s.active().unwrap().color_samplers.clone();
|
||||
let center = |s: &Session| s.active().unwrap().symmetry.as_ref().and_then(|y| y.preset()).map(|p| p.center);
|
||||
for (cmd, p) in [("layer.translate", json!({"dx": -60, "dy": -30})), ("layer.artboard.set", json!({"x": -50, "y": -20}))] {
|
||||
let mut s = session(8);
|
||||
let gid = board_with_square(&mut s);
|
||||
s.execute("view.colorSamplers.add", json!({"x": 15, "y": 15})).unwrap();
|
||||
s.execute("paint.setSymmetry", json!({"mode": "vertical", "center": [70.0, 20.0]})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": gid.0})).unwrap();
|
||||
s.execute(cmd, p).unwrap();
|
||||
// The board lands at (0, 0) after the document shifted right 50 and down 20; the sampler
|
||||
// is pinned to the canvas, not the board, so it shifts with the document only.
|
||||
assert_eq!(board_rect(&s, gid.0), Rect::new(0, 0, 40, 40), "{cmd}");
|
||||
assert_eq!(samplers(&s), vec![[65.0, 35.0]], "{cmd}");
|
||||
assert_eq!(center(&s), Some([120.0, 40.0]), "{cmd}: the symmetry stays on its pixels");
|
||||
}
|
||||
// A new board left of the canvas shifts the photo right by its overhang.
|
||||
let mut s = session(8);
|
||||
add_square(&mut s, Rect::new(10, 10, 30, 30), [1.0, 0.0, 0.0, 1.0]);
|
||||
s.execute("view.colorSamplers.add", json!({"x": 15, "y": 15})).unwrap();
|
||||
s.execute("layer.new.artboard", json!({"rect": [-200, 0, 100, 80]})).unwrap();
|
||||
assert_eq!(samplers(&s), vec![[215.0, 15.0]]);
|
||||
assert!(red_at(&s, 215, 15));
|
||||
// The "+" button left of a board at the origin shifts by the board and the gap.
|
||||
let mut s = session(8);
|
||||
let gid = board_with_square(&mut s);
|
||||
s.execute("layer.artboard.set", json!({"x": 0, "y": 0})).unwrap();
|
||||
s.execute("view.colorSamplers.add", json!({"x": 5, "y": 5})).unwrap();
|
||||
s.execute("layer.artboard.addBeside", json!({"layer": gid.0, "side": "left"})).unwrap();
|
||||
let by = 40 + GAP;
|
||||
assert_eq!(board_rect(&s, gid.0), Rect::new(by, 0, by + 40, 40));
|
||||
assert_eq!(samplers(&s), vec![[f64::from(by) + 5.0, 5.0]]);
|
||||
assert!(red_at(&s, by + 5, 5));
|
||||
// A board right of the canvas shifts nothing.
|
||||
s.execute("layer.artboard.addBeside", json!({"layer": gid.0, "side": "right"})).unwrap();
|
||||
assert_eq!(samplers(&s), vec![[f64::from(by) + 5.0, 5.0]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_beside_places_a_board_on_each_side_and_alt_duplicates_contents() {
|
||||
// #3033: the Artboard tool's "+" buttons. Board 40×40 at (10, 10) holding a red square.
|
||||
|
||||
@@ -485,6 +485,7 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let mask_target = p.get("target").and_then(Value::as_str) == Some("mask");
|
||||
let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
|
||||
let mut shift = (0, 0);
|
||||
let ids = s.edit("Move", |doc, _| {
|
||||
let (alone, rest): (Vec<LayerId>, Vec<LayerId>) = roots.iter().partition(|id| mask_target && doc.layer(**id).is_some_and(moves_mask_alone));
|
||||
let mut ids = move_targets(doc, &rest);
|
||||
@@ -506,7 +507,7 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
// A board moved past any canvas edge grows the canvas, as a new one does (#1531, #3070).
|
||||
if ids.iter().any(|id| doc.layer(*id).is_some_and(|l| l.artboard().is_some())) {
|
||||
crate::artboard_cmds::fit_canvas(doc);
|
||||
shift = crate::artboard_cmds::fit_canvas(doc);
|
||||
}
|
||||
// The Move tool carries the selection along when it moves several layers whole, as
|
||||
// Photoshop does (#7538): one step, so Undo takes both back.
|
||||
@@ -515,6 +516,7 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
Ok(ids)
|
||||
})?;
|
||||
crate::artboard_cmds::follow_shift(s, shift);
|
||||
note_damage(s, &before, &ids);
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user