mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
* Paste into another artboard keeps the place on the board (#8788) Copying from one artboard and pasting with another board (or a layer on it) selected put the pixels at their old canvas spot on the first board, where the second board hid them, or in the middle of the view; the new layer still went beside the selected one, inside the second board. The clipboard now remembers the artboard a Copy came from (the copied layer's board, every copied layer's when several are selected, or for Copy Merged the board under the middle of the copied area). Paste and Paste in Place into a different board of the same document move the pixels by the offset between the two boards' origins, so they land at the same spot on the new board, and the layer goes inside it as before. Pasting on the same board, outside the boards or into another document is unchanged, and Paste Into / Outside keep centring on the selection. Co-Authored-By: Claude <[email protected]> * Paste between artboards: copied layers and true middle for the source board Review follow-ups: - With a pixel selection, Copy takes the active layer only, even when several layers are selected, but the source board was worked out from every selected layer, so selected layers on another board cancelled it. It now uses the layers Copy actually takes. - Copy Merged found the board under the middle of the copied area by halving each edge first, which rounds 9..11 to 9 and could pick the wrong one of two boards that meet. The edges are now added before halving. Adds a test for each, both failing before. Co-Authored-By: Claude <[email protected]> * Paste between artboards: locked boards and the placement midpoint Review follow-ups: - With a locked board itself selected, the pasted layer goes above the board, not into it, yet it was still moved to the board's spot. The board shift is now skipped in that case; a layer inside a locked board still lands on it. - layer_place found the board a new layer lands on by halving each edge first, so a paste moved to 19..21 had its middle at 19 and was turned away from a board starting at 20. The edges are now added before halving, as for the copy's source board. Adds a test for each, both failing before. Co-Authored-By: Claude <[email protected]> * Paste between artboards: floored placement middle, pastes join smaller boards Review follow-ups (FredApps): - The placement middle truncated toward zero, so content at -4..3 (middle -0.5) counted as 0 and a board starting at x = 0 took it in. One helper, layer_place::centre, now adds the edges in i64 and floors the sum; Copy Merged's source board uses it too, and so does #10512's auto-nesting, with a byte-identical layer_place.rs so the two merge in either order. - Pasting onto a smaller board kept the offset from its origin, but the layer's middle then fell past the board's edge and it was placed outside. A paste moved onto a board now joins it; the board clips the rest. Tests: negative-boundary placement, a document without boards, a smaller board (offset kept, board clips), each failing before where it applies. Co-Authored-By: Claude <[email protected]> * Paste between artboards: keep a clipped paste's clipping on a board Review follow-ups (CodeRabbit): - A paste that joins a board always lost its clipping. It now keeps its own clipping flag (the copy's, from before placement) while the board holds a layer it can clip to, and drops it on an empty board, as a new layer does. - Documented that boards more than the 32-bit range apart can't shift a paste, which then keeps its usual placement rather than wrapping around. Adds a test (a clipped group pasted onto a board with a layer and onto an empty one), failing before. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]> Co-authored-by: FredApps <[email protected]>
This commit is contained in:
6 files changed
+422
-17
No files matched your search
@@ -85,7 +85,8 @@ pub(crate) fn copy(s: &mut Session, p: &Value) -> Result<Option<Value>> {
|
||||
if bounds.is_empty() {
|
||||
return Err(EngineError::Other("Could not copy: the selected area is empty".into()));
|
||||
}
|
||||
s.clipboard = Some(Clip { svg: None, components: None, surface, bounds, layers: None, resolution: Some(doc.resolution_dpi), source: Some(doc.id) });
|
||||
s.clipboard =
|
||||
Some(Clip { svg: None, components: None, surface, bounds, layers: None, resolution: Some(doc.resolution_dpi), source: Some(doc.id), board: None });
|
||||
Ok(Some(json!({"bounds": [bounds.x0, bounds.y0, bounds.width(), bounds.height()], "channel": k})))
|
||||
}
|
||||
|
||||
@@ -197,6 +198,7 @@ pub(crate) fn copy_colors(s: &mut Session, p: &Value, colors: crate::color_chann
|
||||
resolution: Some(st.doc.resolution_dpi),
|
||||
source: Some(st.doc.id),
|
||||
components: (colors.len() > 1).then(|| colors.indices().collect()),
|
||||
board: None,
|
||||
});
|
||||
Ok(json!({"bounds":[area.x0,area.y0,area.width(),area.height()],"colors":colors.indices().collect::<Vec<_>>()}))
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ pub struct Clip {
|
||||
/// The document the pixels were copied from: Paste keeps them in place only there, and
|
||||
/// centres them in any other (#8945). None for an image from another app.
|
||||
pub source: Option<DocId>,
|
||||
/// The artboard the pixels were copied from (its id and rect at the time), when they came
|
||||
/// from one: Paste into another board of the same document keeps their place on the board
|
||||
/// ([`board_shift`], #8788). None outside boards, across several, or for a copied board.
|
||||
pub board: Option<(LayerId, Rect)>,
|
||||
}
|
||||
|
||||
impl Clip {
|
||||
@@ -209,7 +213,7 @@ fn lift(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Result<Clip> {
|
||||
};
|
||||
let mut out = Surface::new(with_alpha);
|
||||
if area.is_empty() {
|
||||
return Ok(Clip { svg: None, components: None, surface: out, bounds: Rect::EMPTY, layers: None, resolution: None, source: None });
|
||||
return Ok(Clip { svg: None, components: None, surface: out, bounds: Rect::EMPTY, layers: None, resolution: None, source: None, board: None });
|
||||
}
|
||||
// Only the selected area is read: converting the whole layer (a 24 MP Background gaining
|
||||
// alpha) cost about a second per Layer via Copy (#668).
|
||||
@@ -231,7 +235,7 @@ fn lift(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Result<Clip> {
|
||||
out.put_tiles(shared);
|
||||
out.prune();
|
||||
let bounds = out.content_bounds();
|
||||
Ok(Clip { svg: None, components: None, surface: out, bounds, layers: None, resolution: None, source: None })
|
||||
Ok(Clip { svg: None, components: None, surface: out, bounds, layers: None, resolution: None, source: None, board: None })
|
||||
}
|
||||
|
||||
/// [`lift`] over one rectangle: `src`'s pixels with alpha (`n` channels), the selection
|
||||
@@ -313,11 +317,95 @@ fn copy(s: &mut Session, merged: bool) -> Result<Value> {
|
||||
return Err(EngineError::Other("Could not copy: the selected area is empty".into()));
|
||||
}
|
||||
let b = clip.bounds;
|
||||
let clip = Clip { resolution: Some(d.doc.resolution_dpi), source: Some(d.doc.id), ..clip };
|
||||
let board = source_board(d, merged, b);
|
||||
let clip = Clip { resolution: Some(d.doc.resolution_dpi), source: Some(d.doc.id), board, ..clip };
|
||||
s.clipboard = Some(clip);
|
||||
Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()]}))
|
||||
}
|
||||
|
||||
/// The artboard a Copy took its pixels from (#8788): the board the copied layers sit in (the
|
||||
/// layers Copy actually takes: every selected layer only when it copies them whole,
|
||||
/// [`multi_copy_ids`], else the active one), or for Copy Merged the board under the middle of the
|
||||
/// copied area. None outside boards, when the copied layers span several, or when a board itself
|
||||
/// was copied (a pasted board is placed as a board, not on another one).
|
||||
fn source_board(d: &crate::DocState, merged: bool, bounds: Rect) -> Option<(LayerId, Rect)> {
|
||||
let board = if merged {
|
||||
// The same middle a pasted layer is placed by, so a clip leaves and joins boards alike.
|
||||
let (cx, cy) = crate::layer_place::centre(bounds);
|
||||
d.doc.artboards().into_iter().rev().find(|(_, _, b)| b.rect.contains(cx, cy)).map(|(id, _, _)| id)?
|
||||
} else {
|
||||
let several = multi_copy_ids(d);
|
||||
let ids = if several.is_empty() { vec![d.active_layer?] } else { several };
|
||||
let mut boards = ids.iter().map(|id| d.doc.layer(*id).filter(|l| l.artboard().is_none()).and_then(|_| d.doc.artboard_of(*id)));
|
||||
let first = boards.next()??;
|
||||
if !boards.all(|b| b == Some(first)) {
|
||||
return None;
|
||||
}
|
||||
first
|
||||
};
|
||||
Some((board, d.doc.layer(board)?.artboard()?.rect))
|
||||
}
|
||||
|
||||
/// Paste into another artboard than the one the pixels were copied from (#8788): they keep
|
||||
/// their place on the board, moved by the offset between the two boards' origins, as a layer
|
||||
/// dragged between boards does. Copying a design from one board and pasting it with another
|
||||
/// board's layer selected puts it at the same spot there, instead of where it was on the first
|
||||
/// board (outside the second, which hid it) or in the middle of the view. Only within one
|
||||
/// document, and not for Paste Into / Outside (`"clampToCanvas": false`), which centre on the
|
||||
/// selection. `active` is the layer the paste lands beside; the board is its board. Returns the
|
||||
/// shift and that board, which the pasted layer then joins ([`join_board`]).
|
||||
pub(crate) fn board_shift(clip: &Clip, doc: &Document, active: Option<LayerId>, p: &Value) -> Option<(i32, i32, LayerId)> {
|
||||
if p.get("clampToCanvas").and_then(Value::as_bool) == Some(false) || clip.source != Some(doc.id) {
|
||||
return None;
|
||||
}
|
||||
let (from, src) = clip.board?;
|
||||
let active = active?;
|
||||
let to = doc.artboard_of(active)?;
|
||||
if to == from {
|
||||
return None;
|
||||
}
|
||||
// Pasted with a locked board itself selected, the layer goes above the board, not into it
|
||||
// (`layer_place`'s rule for layers edited right after they are made), so it keeps its usual
|
||||
// place instead of one on a board it isn't on. A layer inside a locked board is placed by its
|
||||
// own rules and still lands on the board.
|
||||
let board = doc.layer(to)?;
|
||||
if active == to && !takes_pastes(board) {
|
||||
return None;
|
||||
}
|
||||
let dst = board.artboard()?.rect;
|
||||
// Boards more than the 32-bit coordinate range apart (#10538 review): the pixels can't be
|
||||
// moved that far, so the paste keeps its usual placement (in place for Paste in Place)
|
||||
// rather than wrapping around to an unrelated spot.
|
||||
Some((dst.x0.checked_sub(src.x0)?, dst.y0.checked_sub(src.y0)?, to))
|
||||
}
|
||||
|
||||
/// Does `board` take a pasted layer into it? Not with a lock that refuses editing it
|
||||
/// (`layer_place`'s rule for layers edited right after they are made).
|
||||
fn takes_pastes(board: &Layer) -> bool {
|
||||
let k = board.locks;
|
||||
!(k.all || k.pixels || k.transparency || k.position)
|
||||
}
|
||||
|
||||
/// Puts pasted layer `id` on `board`, on top of its stack, when placement left it outside. A paste
|
||||
/// moved onto a board belongs to it even when its middle falls past a smaller board's edge; the
|
||||
/// board clips what lies beyond, as it clips anything on it (#10538 review). `clipped` is the
|
||||
/// copy's own clipping flag (the one it had before placement, which set it for the slot it left):
|
||||
/// on top of the board's stack it keeps it while a layer below can be its base, as a new layer
|
||||
/// does ([`crate::layer_place::insert_new`]), and drops it on an empty board.
|
||||
fn join_board(doc: &mut Document, id: LayerId, board: LayerId, clipped: bool) {
|
||||
if doc.artboard_of(id) == Some(board) || !doc.layer(board).is_some_and(takes_pastes) {
|
||||
return;
|
||||
}
|
||||
let Some(mut layer) = doc.remove(id) else { return };
|
||||
match doc.layer_mut(board).and_then(Layer::children_mut) {
|
||||
Some(children) => {
|
||||
layer.clipped = clipped && !children.is_empty();
|
||||
children.push(layer);
|
||||
}
|
||||
None => doc.layers.push(layer),
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy the targeted layer mask (`"target":"mask"`, #2928) or smart-filter mask
|
||||
/// (`"target":"filterMask"`): the mask's values as a
|
||||
/// grayscale clip, inside the selection or the whole canvas — an untouched mask copies at its
|
||||
@@ -360,7 +448,7 @@ fn copy_mask(s: &mut Session, filter: bool) -> Result<Value> {
|
||||
}
|
||||
let b = bounds;
|
||||
let (resolution, source) = (Some(d.doc.resolution_dpi), Some(d.doc.id));
|
||||
s.clipboard = Some(Clip { svg: None, components: None, surface, bounds: b, layers: None, resolution, source });
|
||||
s.clipboard = Some(Clip { svg: None, components: None, surface, bounds: b, layers: None, resolution, source, board: None });
|
||||
Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()], "target": if filter { "filterMask" } else { "mask" }}))
|
||||
}
|
||||
|
||||
@@ -428,11 +516,13 @@ fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result<Value> {
|
||||
}
|
||||
let canvas = d.doc.bounds();
|
||||
let fmt = d.doc.pixel_format();
|
||||
let (dx, dy) = paste_offset(&clip, d.doc.id, canvas, p, in_place);
|
||||
let shift = board_shift(&clip, &d.doc, d.active_layer, p);
|
||||
let board = shift.map(|(_, _, b)| b);
|
||||
let (dx, dy) = shift.map(|(dx, dy, _)| (dx, dy)).unwrap_or_else(|| paste_offset(&clip, d.doc.id, canvas, p, in_place));
|
||||
if mode.is_none()
|
||||
&& let Some(layers) = clip.layers.as_deref().filter(|_| crate::layer_copy_cmds::layered(d.doc.mode))
|
||||
{
|
||||
return paste_layers(s, layers, dx, dy);
|
||||
return paste_layers(s, layers, dx, dy, board);
|
||||
}
|
||||
let moved = shifted(&clip.surface, dx, dy);
|
||||
let target = PixelFormat::new(fmt.mode, fmt.sample, true);
|
||||
@@ -441,6 +531,10 @@ fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result<Value> {
|
||||
let mut l = Layer::raster(doc.next_layer_name("Layer"), target);
|
||||
*crate::pixels_mut(&mut l)? = surf;
|
||||
let id = crate::layer_place::insert_new(doc, *active, l, crate::layer_place::Place::AutoEditable);
|
||||
if let Some(b) = board {
|
||||
// Pixels pasted as a new layer start unclipped.
|
||||
join_board(doc, id, b, false);
|
||||
}
|
||||
*active = Some(id);
|
||||
doc.selection = None;
|
||||
Ok(id)
|
||||
@@ -451,8 +545,9 @@ fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result<Value> {
|
||||
|
||||
/// Paste of a whole copied layer ([`whole_layer`]): fresh copies (a group's children too),
|
||||
/// converted to the document's colour, moved as the pixels would be, above the active layer. A pasted smart object gets its
|
||||
/// own contents, independent of the copied one (as across documents).
|
||||
fn paste_layers(s: &mut Session, layers: &Document, dx: i32, dy: i32) -> Result<Value> {
|
||||
/// own contents, independent of the copied one (as across documents). Moved onto `board` (see
|
||||
/// [`board_shift`]), they join it.
|
||||
fn paste_layers(s: &mut Session, layers: &Document, dx: i32, dy: i32, board: Option<LayerId>) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let mut copies = layers.clone();
|
||||
for l in &mut copies.layers {
|
||||
@@ -484,10 +579,20 @@ fn paste_layers(s: &mut Session, layers: &Document, dx: i32, dy: i32) -> Result<
|
||||
doc.metadata.psd_global_blocks.extend(linked);
|
||||
// The first pasted layer goes where a new layer would; the rest stack above it.
|
||||
let mut place = crate::layer_place::Place::AutoEditable;
|
||||
let mut pasted = Vec::with_capacity(copies.layers.len());
|
||||
for l in copies.layers {
|
||||
*active = Some(crate::layer_place::insert_new(doc, *active, l, place));
|
||||
let clipped = l.clipped;
|
||||
let id = crate::layer_place::insert_new(doc, *active, l, place);
|
||||
pasted.push((id, clipped));
|
||||
*active = Some(id);
|
||||
place = crate::layer_place::Place::Above;
|
||||
}
|
||||
// Bottom to top, so they keep their order on the board.
|
||||
if let Some(b) = board {
|
||||
for (id, clipped) in pasted {
|
||||
join_board(doc, id, b, clipped);
|
||||
}
|
||||
}
|
||||
crate::cmd::layer_multi_cmds::check_group_depth(doc, "Paste")?;
|
||||
doc.selection = None;
|
||||
active.ok_or(EngineError::Other("nothing to paste".into()))
|
||||
|
||||
@@ -17,7 +17,16 @@ fn new_clipboard_document_rejects_an_unrepresentable_origin() {
|
||||
let mut s = Session::new();
|
||||
let mut surface = Surface::new(PixelFormat::RGBA8);
|
||||
surface.write_pixel(i32::MIN, 0, &[1.0, 0.0, 0.0, 1.0]);
|
||||
s.clipboard = Some(Clip { svg: None, components: None, surface, bounds: Rect::from_xywh(i32::MIN, 0, 1, 1), layers: None, resolution: None, source: None });
|
||||
s.clipboard = Some(Clip {
|
||||
svg: None,
|
||||
components: None,
|
||||
surface,
|
||||
bounds: Rect::from_xywh(i32::MIN, 0, 1, 1),
|
||||
layers: None,
|
||||
resolution: None,
|
||||
source: None,
|
||||
board: None,
|
||||
});
|
||||
let result = new_from_clipboard(&mut s);
|
||||
assert!(result.unwrap_err().to_string().contains("clipboard origin"));
|
||||
assert!(s.execute("file.newFromClipboard", json!({})).is_err());
|
||||
@@ -58,6 +67,7 @@ fn paste_centers_clipboard_bounds_near_coordinate_limits() {
|
||||
layers: None,
|
||||
resolution: None,
|
||||
source: None,
|
||||
board: None,
|
||||
};
|
||||
let (dx, dy) = paste_offset(&clip, DocId(1), Rect::new(0, 0, 20, 20), &json!({}), false);
|
||||
assert_eq!(i64::from(origin) + i64::from(dx), 9);
|
||||
@@ -240,7 +250,8 @@ fn external_clip_session(w: i32, h: i32, cw: i32, ch: i32) -> Session {
|
||||
s.execute("file.new", json!({"width": w, "height": h})).unwrap();
|
||||
let mut surface = Surface::new(PixelFormat::RGBA8);
|
||||
surface.fill_rect(Rect::new(0, 0, cw, ch), &[1.0, 0.0, 0.0, 1.0]);
|
||||
s.clipboard = Some(Clip { svg: None, components: None, surface, bounds: Rect::new(0, 0, cw, ch), layers: None, resolution: None, source: None });
|
||||
s.clipboard =
|
||||
Some(Clip { svg: None, components: None, surface, bounds: Rect::new(0, 0, cw, ch), layers: None, resolution: None, source: None, board: None });
|
||||
s
|
||||
}
|
||||
|
||||
@@ -304,6 +315,7 @@ fn paste_offset_clamp_survives_extreme_coordinates() {
|
||||
layers: None,
|
||||
resolution: None,
|
||||
source: None,
|
||||
board: None,
|
||||
};
|
||||
let (dx, dy) = paste_offset(&clip, DocId(1), Rect::new(i32::MIN, i32::MIN, i32::MIN + 4, i32::MIN + 4), &json!({}), false);
|
||||
assert_eq!((dx, dy), (i32::MIN, i32::MIN), "an unrepresentable shift saturates instead of wrapping");
|
||||
@@ -1453,13 +1465,13 @@ fn lift_reference(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Clip {
|
||||
};
|
||||
let mut out = Surface::new(with_alpha);
|
||||
if area.is_empty() {
|
||||
return Clip { svg: None, components: None, surface: out, bounds: Rect::EMPTY, layers: None, resolution: None, source: None };
|
||||
return Clip { svg: None, components: None, surface: out, bounds: Rect::EMPTY, layers: None, resolution: None, source: None, board: None };
|
||||
}
|
||||
let px = lift_region(src, sel, area, with_alpha.channels()).unwrap();
|
||||
out.try_write_region(area, &px).unwrap();
|
||||
out.prune();
|
||||
let bounds = out.content_bounds();
|
||||
Clip { svg: None, components: None, surface: out, bounds, layers: None, resolution: None, source: None }
|
||||
Clip { svg: None, components: None, surface: out, bounds, layers: None, resolution: None, source: None, board: None }
|
||||
}
|
||||
|
||||
fn noise(fmt: PixelFormat, r: Rect, seed: u32) -> Surface {
|
||||
@@ -1526,3 +1538,269 @@ fn copy_shares_whole_tiles_with_the_layer() {
|
||||
assert_eq!(shared, src.tile_count(), "every tile is shared");
|
||||
}
|
||||
}
|
||||
|
||||
/// Board "A" at (10, 10, 50, 50) holding a red square at (10..30)², and an empty board "B" at
|
||||
/// (200, 20, 240, 60), B active.
|
||||
fn two_boards() -> (Session, LayerId, LayerId, LayerId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 100, "height": 80})).unwrap();
|
||||
let red = s.execute("layer.new.layer", json!({"name": "Red"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.edit("paint", |doc, _| {
|
||||
doc.layer_mut(red).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let a = s.execute("layer.new.artboardFromLayers", json!({"name": "A"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.artboard.set", json!({"width": 40, "height": 40})).unwrap();
|
||||
let b = s.execute("layer.new.artboard", json!({"rect": [200, 20, 40, 40], "name": "B"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
(s, red, a, b)
|
||||
}
|
||||
|
||||
fn board_of_active(s: &Session) -> Option<LayerId> {
|
||||
let st = s.active().unwrap();
|
||||
st.doc.artboard_of(st.active_layer.unwrap())
|
||||
}
|
||||
|
||||
/// #8788: pixels copied on one artboard and pasted with another board (or a layer on it) selected
|
||||
/// land at the same spot on that board, inside it, instead of at their old spot on the first board
|
||||
/// (where the second board hid them) or in the middle of the view. Paste and Paste in Place alike.
|
||||
#[test]
|
||||
fn paste_into_another_artboard_keeps_the_place_on_the_board() {
|
||||
for (cmd, params) in [("edit.paste", json!({})), ("edit.paste", json!({"center": [500.0, 500.0]})), ("edit.pasteSpecial.pasteInPlace", json!({}))] {
|
||||
let (mut s, red, a, b) = two_boards();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": b.0})).unwrap();
|
||||
let r = s.execute(cmd, params.clone()).unwrap();
|
||||
assert_eq!(r["offset"], json!([190, 10]), "{cmd} {params}: moved by B's origin minus A's");
|
||||
assert_eq!(board_of_active(&s), Some(b), "{cmd} {params}: the pasted layer is on board B");
|
||||
assert_eq!(active_bounds(&s), Rect::new(200, 20, 220, 40), "{cmd} {params}: the same spot on B");
|
||||
// The original stays on A.
|
||||
assert_eq!(s.active().unwrap().doc.artboard_of(red), Some(a));
|
||||
}
|
||||
}
|
||||
|
||||
/// Paste within the same board, or with no board on the paste side, behaves as before; so does
|
||||
/// Paste Into, which centres on the selection.
|
||||
#[test]
|
||||
fn paste_on_the_same_artboard_or_off_the_boards_is_unchanged() {
|
||||
let (mut s, red, a, _b) = two_boards();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
// Same board: in place.
|
||||
let r = s.execute("edit.paste", json!({})).unwrap();
|
||||
assert_eq!(r["offset"], json!([0, 0]));
|
||||
assert_eq!(board_of_active(&s), Some(a));
|
||||
// The Background is on no board: the usual rules (in place, here).
|
||||
let bg = s.active().unwrap().doc.layers[0].id;
|
||||
s.execute("layer.select", json!({"layer": bg.0})).unwrap();
|
||||
assert_eq!(s.execute("edit.paste", json!({})).unwrap()["offset"], json!([0, 0]));
|
||||
// Paste Into's `clampToCanvas: false` keeps its own centring.
|
||||
let clip = s.clipboard.clone().unwrap();
|
||||
let doc = &s.active().unwrap().doc;
|
||||
let on_b = doc.artboards().iter().find(|x| x.1 == "B").map(|x| x.0);
|
||||
let inside = doc.layer(on_b.unwrap()).unwrap().id;
|
||||
assert!(board_shift(&clip, doc, Some(inside), &json!({"clampToCanvas": false})).is_none());
|
||||
assert!(board_shift(&clip, doc, Some(inside), &json!({})).is_some());
|
||||
}
|
||||
|
||||
/// A copied layer taken whole (a group here) is moved onto the other board the same way.
|
||||
#[test]
|
||||
fn a_whole_layer_pasted_into_another_artboard_keeps_its_place() {
|
||||
let (mut s, red, _a, b) = two_boards();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
let g = s.execute("layer.groupLayers", json!({"name": "G"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.select", json!({"layer": g.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert!(s.clipboard.as_ref().unwrap().layers.is_some(), "a group is copied whole");
|
||||
s.execute("layer.select", json!({"layer": b.0})).unwrap();
|
||||
s.execute("edit.paste", json!({})).unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let pasted = st.active_layer.unwrap();
|
||||
assert_eq!(st.doc.artboard_of(pasted), Some(b));
|
||||
let inner = st.doc.layer(pasted).unwrap().children().unwrap()[0].surface().unwrap().content_bounds();
|
||||
assert_eq!(inner, Rect::new(200, 20, 220, 40));
|
||||
}
|
||||
|
||||
/// Copy Merged takes the board under the middle of the copied area; a copied board itself, or a
|
||||
/// selection of layers on two boards, has no source board.
|
||||
#[test]
|
||||
fn the_source_board_of_merged_board_and_mixed_copies() {
|
||||
let (mut s, red, a, b) = two_boards();
|
||||
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
s.execute("edit.copyMerged", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board.map(|x| x.0), Some(a));
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board, None, "a copied board");
|
||||
let on_b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.moveTo", json!({"layer": on_b.0, "target": b.0, "position": "into"})).unwrap();
|
||||
s.edit("paint", |doc, _| {
|
||||
doc.layer_mut(on_b).unwrap().surface_mut().unwrap().fill_rect(Rect::new(205, 25, 210, 30), &[0.0, 0.0, 1.0, 1.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": on_b.0, "mode": "add"})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board, None, "layers on two boards");
|
||||
}
|
||||
|
||||
/// With a pixel selection, Copy takes the active layer only, even with several layers selected,
|
||||
/// so its source board is the active layer's (#10538 review): other selected layers on another
|
||||
/// board don't cancel it.
|
||||
#[test]
|
||||
fn a_selection_copy_takes_the_active_layers_board_whatever_else_is_selected() {
|
||||
let (mut s, red, a, b) = two_boards();
|
||||
let on_b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.moveTo", json!({"layer": on_b.0, "target": b.0, "position": "into"})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": on_b.0})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": red.0, "mode": "add"})).unwrap();
|
||||
assert_eq!(s.active().unwrap().active_layer, Some(red));
|
||||
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board.map(|x| x.0), Some(a), "the active layer's board");
|
||||
}
|
||||
|
||||
/// Copy Merged finds the board under the true middle of the copied area: two boards meeting at
|
||||
/// x = 10, and an area 9..11 wide whose middle (10) is on the right one (#10538 review).
|
||||
#[test]
|
||||
fn copy_merged_uses_the_true_middle_of_the_area() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 40, "height": 20})).unwrap();
|
||||
let left = s.execute("layer.new.artboard", json!({"rect": [0, 0, 10, 20], "name": "L"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
let right = s.execute("layer.new.artboard", json!({"rect": [10, 0, 10, 20], "name": "R"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
assert_ne!(left, right);
|
||||
s.execute("select.rect", json!({"x": 9, "y": 5, "width": 2, "height": 2})).unwrap();
|
||||
s.execute("edit.copyMerged", json!({})).unwrap();
|
||||
let clip = s.clipboard.as_ref().unwrap();
|
||||
assert_eq!((clip.bounds.x0, clip.bounds.x1), (9, 11));
|
||||
assert_eq!(clip.board.map(|x| x.0), Some(right), "the middle, x = 10, is on R");
|
||||
}
|
||||
|
||||
/// With a locked board itself selected, a paste goes above the board rather than into it, so it
|
||||
/// isn't moved to the board's spot either (#10538 review): it keeps the usual paste placement.
|
||||
#[test]
|
||||
fn paste_with_a_locked_artboard_selected_is_not_moved_onto_it() {
|
||||
let (mut s, red, _a, b) = two_boards();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": b.0})).unwrap();
|
||||
s.execute("layer.lockLayers", json!({"all": true})).unwrap();
|
||||
let r = s.execute("edit.paste", json!({})).unwrap();
|
||||
assert_eq!(r["offset"], json!([0, 0]), "not moved by the boards' offset");
|
||||
assert_eq!(board_of_active(&s), None, "above the locked board, not in it");
|
||||
}
|
||||
|
||||
/// A merged clip whose middle is on its source board lands inside the destination board after
|
||||
/// the shift too: both use the true middle of the area (#10538 review). Boards R (10..20) and D
|
||||
/// (30..40); the clip spans 9..11 (middle 10, on R) and moves by 20 to 29..31 (middle 30, on D).
|
||||
#[test]
|
||||
fn a_shifted_merged_clip_is_placed_inside_the_destination_board() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 50, "height": 20})).unwrap();
|
||||
s.execute("layer.new.artboard", json!({"rect": [0, 0, 10, 20], "name": "L"})).unwrap();
|
||||
let r = s.execute("layer.new.artboard", json!({"rect": [10, 0, 10, 20], "name": "R"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
let d = s.execute("layer.new.artboard", json!({"rect": [30, 0, 10, 20], "name": "D"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("select.rect", json!({"x": 9, "y": 5, "width": 2, "height": 2})).unwrap();
|
||||
s.execute("edit.copyMerged", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board.map(|x| x.0), Some(r));
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": d.0})).unwrap();
|
||||
let out = s.execute("edit.paste", json!({})).unwrap();
|
||||
assert_eq!(out["offset"], json!([20, 0]));
|
||||
assert_eq!(board_of_active(&s), Some(d), "inside board D");
|
||||
}
|
||||
|
||||
/// A pasted layer centred just left of a board stays off it (#10538 review): the placement
|
||||
/// middle rounds down, so content spanning -4..3 (middle -0.5) is left of a board starting at
|
||||
/// x = 0. Truncating toward zero put it at 0, and the board took it in and clipped it.
|
||||
#[test]
|
||||
fn content_centred_just_left_of_a_board_is_not_placed_in_it() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 40, "height": 20})).unwrap();
|
||||
let b = s.execute("layer.new.artboard", json!({"rect": [0, 0, 20, 20], "name": "B"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
let place = |s: &mut Session, r: Rect| {
|
||||
s.edit("place", |doc, active| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut l = Layer::raster("L", fmt);
|
||||
l.surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]);
|
||||
let id = crate::layer_place::insert_new(doc, Some(b), l, crate::layer_place::Place::Auto);
|
||||
*active = Some(id);
|
||||
Ok(doc.artboard_of(id))
|
||||
})
|
||||
.unwrap()
|
||||
};
|
||||
assert_eq!(place(&mut s, Rect::new(-4, 5, 3, 10)), None, "middle -0.5: left of B");
|
||||
assert_eq!(place(&mut s, Rect::new(-3, 5, 4, 10)), Some(b), "middle 0.5: on B");
|
||||
assert_eq!(crate::layer_place::centre(Rect::new(-4, -4, 3, 3)), (-1, -1));
|
||||
assert_eq!(crate::layer_place::centre(Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX)), (-1, -1));
|
||||
}
|
||||
|
||||
/// In a document without artboards nothing changes (#10538 review): Paste in the same document
|
||||
/// keeps the canvas position, and the clip has no source board.
|
||||
#[test]
|
||||
fn paste_in_a_document_without_artboards_keeps_the_canvas_position() {
|
||||
let mut s = session();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.as_ref().unwrap().board, None);
|
||||
let r = s.execute("edit.paste", json!({})).unwrap();
|
||||
assert_eq!(r["offset"], json!([0, 0]));
|
||||
assert_eq!(active_bounds(&s), Rect::new(10, 10, 50, 30));
|
||||
}
|
||||
|
||||
/// Pasting onto a smaller board keeps the same offset from the board's origin; what falls past
|
||||
/// the smaller board's edge is clipped by it, as anything on a board is (#10538 review).
|
||||
#[test]
|
||||
fn paste_onto_a_smaller_artboard_keeps_the_offset_and_the_board_clips_the_rest() {
|
||||
let (mut s, red, _a, _b) = two_boards();
|
||||
// Board S at (300, 20), 10×10: smaller than the 20×20 red square.
|
||||
let small = s.execute("layer.new.artboard", json!({"rect": [300, 20, 10, 10], "name": "S"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": small.0})).unwrap();
|
||||
let r = s.execute("edit.paste", json!({})).unwrap();
|
||||
assert_eq!(r["offset"], json!([290, 10]), "S's origin minus A's");
|
||||
assert_eq!(board_of_active(&s), Some(small));
|
||||
assert_eq!(active_bounds(&s), Rect::new(300, 20, 320, 40), "the layer keeps all its pixels");
|
||||
let px = |x: i32, y: i32| photocraft_compose::render(&s.active().unwrap().doc, Rect::new(x, y, x + 1, y + 1)).px[0];
|
||||
assert!(px(305, 25)[0] > 0.99 && px(305, 25)[1] < 0.01, "shown inside S");
|
||||
assert!(!(px(315, 35)[0] > 0.99 && px(315, 35)[1] < 0.01), "clipped past S's edge");
|
||||
}
|
||||
|
||||
/// A pasted layer that joins a board keeps its own clipping while the board already holds a layer
|
||||
/// it can clip to, and drops it on an empty board (#10538 review): a clipped group copied whole
|
||||
/// from board A and pasted onto smaller boards S (holding a layer) and E (empty).
|
||||
#[test]
|
||||
fn a_clipped_paste_joining_a_board_stays_clipped_only_over_a_layer() {
|
||||
let (mut s, red, a, _b) = two_boards();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
let g = s.execute("layer.groupLayers", json!({"name": "G"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
let base = s.execute("layer.new.layer", json!({"name": "Base"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.moveTo", json!({"layer": base.0, "target": g.0, "position": "below"})).unwrap();
|
||||
s.edit("clip", |doc, _| {
|
||||
doc.layer_mut(g).unwrap().clipped = true;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(s.active().unwrap().doc.artboard_of(g), Some(a));
|
||||
let small = |s: &mut Session, x: i32, name: &str| {
|
||||
s.execute("layer.new.artboard", json!({"rect": [x, 20, 10, 10], "name": name})).unwrap()["layer"].as_u64().map(LayerId).unwrap()
|
||||
};
|
||||
let (sb, eb) = (small(&mut s, 300, "S"), small(&mut s, 400, "E"));
|
||||
let held = s.execute("layer.new.layer", json!({"name": "Held"})).unwrap()["layer"].as_u64().map(LayerId).unwrap();
|
||||
s.execute("layer.moveTo", json!({"layer": held.0, "target": sb.0, "position": "into"})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": g.0})).unwrap();
|
||||
s.execute("edit.copy", json!({})).unwrap();
|
||||
assert!(s.clipboard.as_ref().unwrap().layers.is_some(), "the group is copied whole");
|
||||
let paste_on = |s: &mut Session, board: LayerId| {
|
||||
s.execute("layer.select", json!({"layer": board.0})).unwrap();
|
||||
s.execute("edit.paste", json!({})).unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let id = st.active_layer.unwrap();
|
||||
(st.doc.artboard_of(id), st.doc.layer(id).unwrap().clipped)
|
||||
};
|
||||
assert_eq!(paste_on(&mut s, sb), (Some(sb), true), "S holds a layer to clip to");
|
||||
assert_eq!(paste_on(&mut s, eb), (Some(eb), false), "E is empty");
|
||||
}
|
||||
@@ -66,6 +66,8 @@ pub(super) fn from_svg(bytes: &[u8]) -> Result<Clip> {
|
||||
layers: None,
|
||||
resolution: None,
|
||||
source: None,
|
||||
// An image from another app came from no artboard.
|
||||
board: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,16 @@ pub fn insert_new(doc: &mut Document, anchor: Option<LayerId>, mut layer: Layer,
|
||||
id
|
||||
}
|
||||
|
||||
/// The pixel holding the middle of `bounds`, which decides the board content is on: a new or
|
||||
/// pasted layer, or one the Move tool drops (auto-nesting). The edges are added before halving
|
||||
/// (halving each first put 9..11 at 9) and the sum is floored, not truncated toward zero (which
|
||||
/// put -4..3, whose middle is -0.5, at 0, inside a board starting there); in i64, so it can't
|
||||
/// overflow. The middle of two i32 edges is itself an i32.
|
||||
pub(crate) fn centre(bounds: photocraft_geom::Rect) -> (i32, i32) {
|
||||
let mid = |a: i32, b: i32| i32::try_from((i64::from(a) + i64::from(b)).div_euclid(2)).unwrap_or(a);
|
||||
(mid(bounds.x0, bounds.x1), mid(bounds.y0, bounds.y1))
|
||||
}
|
||||
|
||||
/// Does the group `target` at depth `depth` (its path length) take a new `layer` as its top
|
||||
/// child? An open group or an artboard does, locked or not (Photoshop puts a new layer inside a
|
||||
/// group locked with Lock All), unless nesting would pass [`MAX_GROUP_DEPTH`]. An artboard
|
||||
@@ -83,7 +93,7 @@ fn takes_new_layers(target: &Layer, depth: usize, layer: &Layer) -> bool {
|
||||
let LayerContent::Group(g) = &target.content else { return false };
|
||||
let on_board = |rect: photocraft_geom::Rect| {
|
||||
crate::layer_multi_cmds::layer_bounds(layer).is_none_or(|b| {
|
||||
let (cx, cy) = (b.x0 / 2 + b.x1 / 2, b.y0 / 2 + b.y1 / 2);
|
||||
let (cx, cy) = centre(b);
|
||||
rect.contains(cx, cy)
|
||||
})
|
||||
};
|
||||
|
||||
@@ -155,8 +155,16 @@ impl PhotocraftApp {
|
||||
let r = photocraft_geom::Rect::new(0, 0, w as i32, h as i32);
|
||||
let mut surface = photocraft_raster::Surface::from_interleaved(photocraft_color::PixelFormat::RGBA8, r, &bytes);
|
||||
surface.prune();
|
||||
self.session.clipboard =
|
||||
Some(photocraft_engine::edit_cmds::Clip { components: None, surface, bounds: r, layers: None, resolution: None, source: None, svg: None });
|
||||
self.session.clipboard = Some(photocraft_engine::edit_cmds::Clip {
|
||||
components: None,
|
||||
surface,
|
||||
bounds: r,
|
||||
layers: None,
|
||||
resolution: None,
|
||||
source: None,
|
||||
svg: None,
|
||||
board: None,
|
||||
});
|
||||
// The SVG that came with this image (a vector app puts both): a later internal copy is
|
||||
// newer than it, so Paste As must not bring it back while it is unchanged.
|
||||
self.os_clip_svg = self.os_svg_now().flatten();
|
||||
|
||||
Reference in new issue
Block a user