GitCorner

Code

ossartcraftphotocraft🌐
856f865cf98d
//! Where a new layer goes, as in Photoshop. Above the active layer, or as the top layer inside the
//! active group when that group is open (an artboard, open or closed, when the layer's content is
//! on the board); with nothing active, at the top of the stack. A layer that lands inside a clipping mask joins it: it is clipped when the layer
//! just above its slot is clipped, the rule Photoshop follows on every insert (#2203), so a new
//! layer (or group) between a base and its clipped layers never becomes their (empty) base.
//! Artboards are never nested or clipped: a new one goes to the top level.

use photocraft_doc::{Document, Layer, LayerContent, LayerId, LayerPath, MAX_GROUP_DEPTH};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Place {
    /// New Layer, New Group, new adjustment and fill layers: inside the anchor when it is an open
    /// group (or an artboard the content is on), else directly above it. A locked group takes the
    /// layer too, as in Photoshop (checked with Lock All): the layer is locked through it.
    Auto,
    /// [`Place::Auto`] for a layer that is edited right after it is made (Type, Shape, Place's
    /// transform box, Paste, the Gradient tool's live gradient, video layers, Render and Vanishing
    /// Point drawing, a new frame): never inside a group with a lock of its own (all, pixels,
    /// transparency or position) that would refuse that edit; the layer goes above the group.
    /// Never on an artboard its content is centred off, even beside a layer on that board: it goes
    /// above the board.
    AutoEditable,
    /// Directly above the anchor, never inside it: a layer made from another layer, or the second
    /// and later of several new layers.
    Above,
    /// Directly below the anchor (⌘-click on the Layers panel's New Layer or New Group button). Never
    /// under the Background, where it goes above instead.
    Below,
}

/// The slot `layer` would take: its parent's path (empty for the root) and its index among the
/// parent's children (bottom to top). A missing or stale `anchor` means the top of the stack.
pub fn insertion_point(doc: &Document, anchor: Option<LayerId>, layer: &Layer, place: Place) -> (LayerPath, usize) {
    let root_top = (Vec::new(), doc.layers.len());
    // Beside a frame's image means beside the frame: the image slot stays whole.
    let Some(path) = anchor.and_then(|a| doc.insertion_path(a)) else { return root_top };
    // An artboard stays at the top level: beside the anchor's top-level layer.
    let path = if layer.artboard().is_some() { path.get(..1).map(<[usize]>::to_vec).unwrap_or_default() } else { path };
    let (Some((&k, parent)), Some(target)) = (path.split_last(), doc.layer_at(&path)) else { return root_top };
    // Beside a layer on an artboard, content placed off that board (Paste, Place, a Type or Shape
    // click: centred off it) goes above the board at the top level, as it does with the board
    // itself as the anchor ([`takes_new_layers`]): inside, the board would clip it away (a layer
    // copied off every board, pasted with a layer on a board selected). The other new layers
    // ([`Place::Auto`]) are empty or cover the canvas (a neutral fill), and stay on the board.
    let editable_new = place == Place::AutoEditable;
    if editable_new && let Some(board) = off_board(doc, &path, layer) {
        return (Vec::new(), board + 1);
    }
    let locks = &target.locks;
    let editable = !(locks.all || locks.pixels || locks.transparency || locks.position);
    match place {
        Place::Auto | Place::AutoEditable if takes_new_layers(target, path.len(), layer) && (place == Place::Auto || editable) => {
            (path.clone(), target.children().map_or(0, <[Layer]>::len))
        }
        Place::Below if crate::background_cmds::background_id(doc) != Some(target.id) => (parent.to_vec(), k),
        _ => (parent.to_vec(), k + 1),
    }
}

/// Inserts `layer` at its [`insertion_point`], clipped when it lands inside a clipping mask (a
/// flag it already has is kept unless nothing is below it to clip to; an artboard is never
/// clipped); returns its id. Never fails: a slot that can't be reached puts
/// the layer on top of the stack.
pub fn insert_new(doc: &mut Document, anchor: Option<LayerId>, mut layer: Layer, place: Place) -> LayerId {
    let id = layer.id;
    let (parent, index) = insertion_point(doc, anchor, &layer, place);
    let sib = if parent.is_empty() { Some(&mut doc.layers) } else { doc.layer_at_mut(&parent).and_then(Layer::children_mut) };
    if let Some(sib) = sib {
        let mut index = index.min(sib.len());
        if layer.artboard().is_some() {
            // Past the clipped layers above the slot, so their clipping mask stays whole.
            while sib.get(index).is_some_and(|above| above.clipped) {
                index += 1;
            }
            layer.clipped = false;
        } else if place == Place::AutoEditable && index > 0 && sib.get(index - 1).is_some_and(|below| below.artboard().is_some()) {
            // New content placed directly above an artboard (pasted off its board, or beside a
            // board that doesn't take it): never clipped to it, where the board would clip it
            // away, and past the clipped layers above the slot so their clipping mask stays
            // whole, as a new artboard goes. A layer put [`Place::Above`] a board keeps its flag:
            // the layers after a pasted board, a duplicate.
            while sib.get(index).is_some_and(|above| above.clipped) {
                index += 1;
            }
            layer.clipped = false;
        } else if sib.get(index).is_some_and(|above| above.clipped) {
            layer.clipped = true;
        } else if index == 0 {
            // Nothing below to clip to (a copied clipped layer pasted into an empty group).
            layer.clipped = false;
        }
        sib.insert(index, layer);
    } else {
        doc.layers.push(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
/// takes a layer with content only when that content is centred on the board, as callers place
/// it for the canvas (Place, Paste, a Type or Shape click): elsewhere the board would clip it.
fn takes_new_layers(target: &Layer, depth: usize, layer: &Layer) -> bool {
    let LayerContent::Group(g) = &target.content else { return false };
    let open = match &g.artboard {
        Some(board) => on_board(layer, board.rect),
        None => g.expanded,
    };
    // A frame holds its image only (`layer.frame.set`/`setContent`), never new layers.
    open && target.frame.is_none() && layer.artboard().is_none() && depth + subtree_depth(layer) <= MAX_GROUP_DEPTH
}

/// Is `layer`'s content centred on the board at `rect`? A layer with no content is (it can go
/// anywhere).
fn on_board(layer: &Layer, rect: photocraft_geom::Rect) -> bool {
    crate::layer_multi_cmds::layer_bounds(layer).is_none_or(|b| {
        let (cx, cy) = centre(b);
        rect.contains(cx, cy)
    })
}

/// The top-level index of the artboard holding the layer at `path` (inside it, not the board
/// itself), when `layer`'s content is centred off that board.
fn off_board(doc: &Document, path: &[usize], layer: &Layer) -> Option<usize> {
    let (&top, inside) = path.split_first()?;
    let board = doc.layers.get(top)?.artboard()?;
    (!inside.is_empty() && !on_board(layer, board.rect)).then_some(top)
}

/// Group nesting inside `layer` itself: 0 for a layer or an empty group, as
/// [`Document::max_group_depth`] counts it. Explicit stack: pasted groups can be deep.
pub(crate) fn subtree_depth(layer: &Layer) -> usize {
    let mut max = 0;
    let mut stack: Vec<(&Layer, usize)> = vec![(layer, 0)];
    while let Some((l, depth)) = stack.pop() {
        if let Some(children) = l.children().filter(|c| !c.is_empty()) {
            max = max.max(depth + 1);
            stack.extend(children.iter().map(|c| (c, depth + 1)));
        }
    }
    max
}

#[cfg(test)]
#[path = "layer_place_tests.rs"]
mod tests;