GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use super::controls::collect_for_export_menu;
use super::model::{Place, Row, opens, painted_name};
use super::thumbs::{node_arc, real_thumb, thumb};
use super::{
    Arc, COLUMN, Color32, ColumnDrag, Document, INDENT, LayersDrag, Node, NodeId, NodeKind, Out, PanelDrag, ROW_KEYS, SQUARE_X, Sense, Stroke,
    StrokeKind, TARGET_X, Tokens, Ui, Value, View, icons, json, vec2, widgets,
};
use vectorcraft_tools::intertwine::{in_intertwine, refused_member};

/// Stable id for the layers panel's per-name UI state.
pub(super) fn key(name: &str) -> egui::Id {
    egui::Id::unique(("layers-panel", name))
}

/// Temp-memory key for the layer rename editor.
pub(super) fn rename_id() -> egui::Id {
    egui::Id::unique("layers-rename")
}

/// Whether a row's name is italic: a layer whose Layer Options › Print is off does not print, and
/// the panel shows that in its name, as the reference application does (a template layer's Print is
/// off too).
fn prints_italic(n: &Node) -> bool {
    matches!(n.kind, NodeKind::Layer { printable: false, .. })
}

/// Paints the row name `name` at `pos` (left, vertically centred), italic when `italic`, and
/// returns its rect. Row names are painted rather than widgets, so italics come from a layout job's
/// text format: egui slants the glyphs itself, no italic face is needed.
fn paint_name(painter: &egui::Painter, pos: egui::Pos2, name: &str, font: egui::FontId, color: Color32, italic: bool) -> egui::Rect {
    let mut job = egui::text::LayoutJob::default();
    job.append(name, 0.0, egui::TextFormat { font_id: font, color, italics: italic, ..Default::default() });
    let galley = painter.layout_job(job);
    let rect = egui::Align2::LEFT_CENTER.anchor_size(pos, galley.size());
    painter.galley(rect.min, galley, color);
    rect
}

/// The colour of the layer holding `id`.
fn layer_colour(doc: &Document, id: NodeId) -> Color32 {
    let [r, g, b] = doc.layer_color(id);
    Color32::from_rgb(r, g, b)
}

/// The layer colour bar of row `n` (rect `r`) at `x`: wider on layers.
fn colour_bar(ui: &Ui, n: &Node, r: egui::Rect, x: f32, colour: Color32) {
    let w = if n.is_layer() { 4.0 } else { 2.0 };
    ui.painter().rect_filled(egui::Rect::from_min_size(egui::pos2(x - 1.0, r.top()), vec2(w, r.height())), 0.0, colour);
}

/// The thumbnail of row `n` (rect `r`) at `x`; returns where it went.
fn row_thumb(ui: &Ui, view: &View, n: &Arc<Node>, r: egui::Rect, x: f32) -> egui::Rect {
    let s = (view.h - 4.0).min(40.0);
    let th = egui::Rect::from_min_size(egui::pos2(x, r.center().y - s / 2.0), vec2(s, s));
    ui.painter().rect_filled(th, 0.0, Color32::WHITE);
    ui.painter().rect_stroke(th, 0.0, Stroke::new(1.0, view.t.border), StrokeKind::Outside);
    if !real_thumb(ui, &view.thumbs, n, th) {
        thumb(ui, n, th);
    }
    th
}

/// The dragged row `src`, dimmed, following the pointer at `pos` above everything else: its
/// colour bar, thumbnail and name.
pub(super) fn drag_ghost(ui: &Ui, view: &View, src: NodeId, pos: egui::Pos2) {
    let (doc, t, h) = (view.doc, &view.t, view.h);
    let Some(n) = node_arc(doc, src) else { return };
    let n = &n;
    let (grab, width, depth) = ui.data(|d| d.get_temp::<(egui::Vec2, f32, usize)>(key("grab"))).unwrap_or((vec2(h, h / 2.0), 240.0, 0));
    let r = egui::Rect::from_min_size(pos - grab, vec2(width, h));
    widgets::drag_ghost(ui.ctx(), key("ghost"), r, |ui, r| {
        let mut x = r.left() + 2.0 * COLUMN + 2.0;
        colour_bar(ui, n, r, x, layer_colour(doc, src));
        x += 22.0 + depth as f32 * INDENT;
        if view.opts.thumb(n) {
            x = row_thumb(ui, view, n, r, x).right() + 4.0;
        }
        let label = n.identify_label(view.identify_xml_id);
        let name = if view.identify_xml_id { label } else { painted_name(n, &label, crate::i18n::current()) };
        paint_name(ui.painter(), egui::pos2(x, r.center().y), &name, egui::FontId::proportional(13.0), t.text, prints_italic(n));
    });
}

/// Resolve the row clicked this frame: plain (highlight it), Ctrl/Cmd (toggle it) or Shift
/// (highlight the rows from the last one clicked down to it). A plain click also lets go of the
/// selected ruler guides, keeping the selected art (#7340): a guide's row picks it the way a
/// highlight picks a row, so picking another row alone drops it.
pub(super) fn resolve_click(ui: &Ui, view: &View, list: &[Row], out: &mut Out) {
    let Some((id, m)) = out.click.take() else { return };
    let anchor: Option<NodeId> = ui.data(|d| d.get_temp(key("anchor")));
    crate::panels::set_row_keys(ui.ctx(), ROW_KEYS, true);
    let order: Vec<NodeId> = list.iter().map(|r| r.node.id).collect();
    let click = widgets::row_click(&order, &view.rows, anchor, id, m);
    if click.moves_anchor() {
        ui.data_mut(|d| d.insert_temp(key("anchor"), id));
    }
    let mut rows = view.rows.clone();
    widgets::apply_row_click(&mut rows, &click);
    out.rows_after_click = Some(rows);
    let ids = |rows: &[NodeId]| rows.iter().map(|r| r.0).collect::<Vec<_>>();
    if matches!(click, widgets::RowClick::Only(_)) && !view.guides.is_empty() {
        // Toggled out of the selection, so the selected art stays.
        out.actions.push(("guide.select".into(), json!({"indexes": view.guides, "toggle": true})));
    }
    out.actions.push(match click {
        widgets::RowClick::Only(id) => ("layer.setCurrent".into(), json!({"id": id.0})),
        widgets::RowClick::Toggle(id) => ("layer.highlight".into(), json!({"ids": [id.0], "mode": "toggle"})),
        widgets::RowClick::Range(rows) => ("layer.highlight".into(), json!({"ids": ids(&rows), "mode": "set"})),
        widgets::RowClick::AddRange(rows) => ("layer.highlight".into(), json!({"ids": ids(&rows), "mode": "add"})),
    });
}

/// Whether row `n` can take `drag` (the rows or objects `moving`) inside it. A group or an
/// Intertwine group takes objects; an Intertwine group, or a group in one, only objects that can
/// be intertwined.
fn takes(n: &Node, drag: &LayersDrag, moving: &[NodeId], doc: &Document) -> bool {
    match n.kind {
        NodeKind::Layer { .. } => true,
        NodeKind::Group { .. } | NodeKind::Intertwine { .. } => {
            let objects = match drag {
                LayersDrag::Rows(ids) => !ids.iter().any(|i| doc.node(*i).is_some_and(Node::is_layer)),
                LayersDrag::Art => true,
                LayersDrag::Guides(_) => false,
            };
            objects && (!in_intertwine(doc, n.id) || moving.iter().filter_map(|i| doc.node(*i)).all(|m| refused_member(m).is_none()))
        }
        _ => false,
    }
}

/// The row of ruler guide `i` on the layer of `item`: the guide's own eye and lock, a guide mark
/// and its name, the selection square when it is selected. The eye and lock hide and lock this
/// guide alone (`layer.setProps` on its node, one undo step), and take part in a drag down either
/// column, as other rows do; they work while its layer is hidden or locked, and the guide shows or
/// can be edited only when its layer allows it too. A click elsewhere selects the guide and leaves
/// no row highlighted; Shift or Cmd adds it to (or takes it from) the selected guides. Dragged onto
/// another layer's row the guide goes to that layer, and onto the trash it is deleted. A hidden or
/// locked guide, or one on a hidden or locked layer, can't be selected or dragged.
fn guide_row(ui: &mut Ui, view: &View, item: &Row, i: usize, out: &mut Out) {
    let (t, h) = (&view.t, view.h);
    let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click_and_drag());
    let Some(g) = view.ruler_guides.get(i) else { return };
    let selected = view.guides.contains(&i);
    let editable = view.doc.guide_editable(g);
    if selected {
        ui.painter().rect_filled(r, 0.0, t.row_selected);
    } else if editable && resp.hovered() {
        ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.6));
    }
    ui.painter().line_segment([r.left_bottom(), r.right_bottom()], Stroke::new(1.0, t.input_border));
    if let Some(node) = g.id.and_then(|id| view.doc.node(id)) {
        eye_and_lock(ui, view, node, r, out);
    }
    let mut x = r.left() + 2.0 * COLUMN + 2.0;
    colour_bar(ui, &item.node, r, x, item.colour);
    x += 6.0 + item.depth as f32 * INDENT + 16.0;
    // The guide mark: a short line across or down, in the guides' colour.
    let c = egui::pos2(x + 8.0, r.center().y);
    let half = if g.vertical { vec2(0.0, 6.0) } else { vec2(6.0, 0.0) };
    ui.painter().line_segment([c - half, c + half], Stroke::new(1.5, t.accent));
    x += 22.0;
    let name = format!("<{}>", if g.vertical { tl!("Vertical Guide") } else { tl!("Horizontal Guide") });
    let color = if item.dim { t.text_dim } else { t.text };
    ui.painter().text(egui::pos2(x, r.center().y), egui::Align2::LEFT_CENTER, name, egui::FontId::proportional(13.0), color);
    let col_x = r.right() - 43.5;
    ui.painter().line_segment([egui::pos2(col_x, r.top()), egui::pos2(col_x, r.bottom())], Stroke::new(1.0, t.input_border));
    if selected {
        let q = egui::Rect::from_center_size(egui::pos2(r.right() - SQUARE_X, r.center().y), vec2(7.0, 7.0));
        ui.painter().rect_filled(q, 0.0, item.colour);
        ui.painter().rect_stroke(q, 0.0, Stroke::new(1.0, t.text), StrokeKind::Inside);
    }
    if editable && resp.clicked() {
        let m = ui.input(|i| i.modifiers);
        // The highlighted rows are let go, so Delete then deletes the selected guide (Edit › Clear)
        // rather than those rows.
        if !view.rows.is_empty() {
            out.actions.push(("layer.highlight".into(), json!({"ids": [], "mode": "set"})));
            out.rows_after_click = Some(vec![]);
        }
        out.actions.push(("guide.select".into(), json!({"indexes": [i], "toggle": m.shift || m.command})));
    }
    if resp.secondary_clicked() {
        crate::panels::set_row_keys(ui.ctx(), ROW_KEYS, true);
        if editable && !selected {
            if !view.rows.is_empty() {
                out.actions.push(("layer.highlight".into(), json!({"ids": [], "mode": "set"})));
                out.rows_after_click = Some(vec![]);
            }
            out.actions.push(("guide.select".into(), json!({"indexes": [i]})));
        }
    }
    resp.context_menu(|ui| {
        guide_context_menu(ui, view, item, i, out);
    });
    // Dragged: the selected guides when this is one of them, else this one, to another layer's row.
    // Only the primary button drags a guide row, and not while a drag down a column passes it.
    if editable && resp.drag_started_by(egui::PointerButton::Primary) && view.column.is_none() {
        let guides = if selected { view.guides.clone() } else { vec![i] };
        egui::DragAndDrop::set_payload(ui.ctx(), LayersDrag::Guides(guides));
    }
    if resp.dragged_by(egui::PointerButton::Primary) {
        ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
    }
    // Guides dropped here go to this guide's layer.
    if let Some(drag) = resp.dnd_hover_payload::<LayersDrag>()
        && let LayersDrag::Guides(guides) = &*drag
    {
        guide_drop(ui, view, guides, item.node.id, r, &resp, out);
    }
}

/// The right-click context menu of ruler guide row `i` (`item`).
fn guide_context_menu(ui: &mut Ui, view: &View, item: &Row, i: usize, out: &mut Out) {
    let Some(g) = view.ruler_guides.get(i) else { return };
    let selected = view.guides.contains(&i);
    let editable = view.doc.guide_editable(g);
    let guides: Vec<usize> = if selected { view.guides.clone() } else { vec![i] };
    let node = g.id.and_then(|id| view.doc.node(id));
    let guide_nodes: Vec<u64> = if selected {
        guides.iter().filter_map(|&idx| view.ruler_guides.get(idx)?.id.map(|id| id.0)).collect()
    } else {
        node.map(|n| vec![n.id.0]).unwrap_or_default()
    };
    let visible = node.is_none_or(|n| n.visible);
    if widgets::menu_item(ui, if visible { "Hide" } else { "Show" }, !guide_nodes.is_empty(), false) {
        out.actions.push(("layer.setProps".into(), json!({ "ids": guide_nodes, "visible": !visible })));
        ui.close();
    }
    let locked = node.is_some_and(|n| n.locked);
    if widgets::menu_item(ui, if locked { "Unlock" } else { "Lock" }, !guide_nodes.is_empty(), false) {
        out.actions.push(("layer.setProps".into(), json!({ "ids": guide_nodes, "locked": !locked })));
        ui.close();
    }
    ui.separator();
    if widgets::menu_item(ui, "Release Guides", editable, false) {
        let params = if guides.len() > 1 { json!({}) } else { json!({ "index": i }) };
        out.actions.push(("view.guides.release".into(), params));
        ui.close();
    }
    if widgets::menu_item(ui, "Delete", editable, false) {
        out.actions.push(("guide.remove".into(), json!({ "indexes": guides })));
        ui.close();
    }
    ui.separator();
    let layers_only = view.opts.layers_only;
    if widgets::menu_item(ui, if layers_only { "Locate Layer" } else { "Locate Object" }, true, false) {
        out.actions.push(("layer.locate".into(), json!({ "id": item.node.id.0, "layersOnly": layers_only })));
        ui.close();
    }
}

/// The row of the spine of blend `item` (#4820): a path mark and `<Path>`, the selection square
/// while the blend is selected. A click selects the blend, whose spine Direct Selection then edits
/// on the canvas; Shift or Cmd adds it to (or takes it from) the selection. The spine of a hidden
/// or locked blend can't be selected.
fn spine_row(ui: &mut Ui, view: &View, item: &Row, out: &mut Out) {
    let (t, h) = (&view.t, view.h);
    let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click());
    let blend = item.node.id;
    let selected = view.sel.contains(&blend);
    let editable = (view.doc.editable())(blend);
    if editable && resp.hovered() {
        ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.6));
    }
    ui.painter().line_segment([r.left_bottom(), r.right_bottom()], Stroke::new(1.0, t.input_border));
    let mut x = r.left() + 2.0 * COLUMN + 2.0;
    colour_bar(ui, &item.node, r, x, item.colour);
    x += 6.0 + item.depth as f32 * INDENT + 16.0;
    // The path mark: a short diagonal between two anchor dots.
    let (a, b) = (egui::pos2(x + 2.0, r.center().y + 5.0), egui::pos2(x + 14.0, r.center().y - 5.0));
    ui.painter().line_segment([a, b], Stroke::new(1.5, t.accent));
    for p in [a, b] {
        ui.painter().rect_filled(egui::Rect::from_center_size(p, vec2(3.0, 3.0)), 0.0, t.accent);
    }
    x += 22.0;
    let name = format!("<{}>", tl!("Path"));
    let color = if item.dim { t.text_dim } else { t.text };
    ui.painter().text(egui::pos2(x, r.center().y), egui::Align2::LEFT_CENTER, name, egui::FontId::proportional(13.0), color);
    let col_x = r.right() - 43.5;
    ui.painter().line_segment([egui::pos2(col_x, r.top()), egui::pos2(col_x, r.bottom())], Stroke::new(1.0, t.input_border));
    if selected {
        let q = egui::Rect::from_center_size(egui::pos2(r.right() - SQUARE_X, r.center().y), vec2(7.0, 7.0));
        ui.painter().rect_filled(q, 0.0, item.colour);
        ui.painter().rect_stroke(q, 0.0, Stroke::new(1.0, t.text), StrokeKind::Inside);
    }
    if editable && resp.clicked() {
        let m = ui.input(|i| i.modifiers);
        let action = if m.shift || m.command { "select.toggle" } else { "select.set" };
        out.actions.push((action.into(), json!({"ids": [blend.0]})));
    }
    if resp.secondary_clicked() {
        crate::panels::set_row_keys(ui.ctx(), ROW_KEYS, true);
        if editable && !selected {
            out.actions.push(("select.set".into(), json!({"ids": [blend.0]})));
        }
    }
    resp.context_menu(|ui| {
        spine_context_menu(ui, view, item, out);
    });
}

/// The right-click context menu of blend spine row `item`.
fn spine_context_menu(ui: &mut Ui, view: &View, item: &Row, out: &mut Out) {
    let blend = item.node.id;
    let selected = view.sel.contains(&blend);
    let editable = (view.doc.editable())(blend);
    let has_spine = !matches!(&item.node.kind, NodeKind::Blend { spec, .. } if spec.no_spine);
    let ensure_selected = |out: &mut Out| {
        if !selected {
            out.actions.push(("select.set".into(), json!({ "ids": [blend.0] })));
        }
    };
    if widgets::menu_item(ui, "Select", editable, false) {
        out.actions.push(("select.set".into(), json!({ "ids": [blend.0] })));
        ui.close();
    }
    if widgets::menu_item(ui, "Blend Options…", editable, false) {
        ensure_selected(out);
        out.actions.push(("ui.blendOptions".into(), json!({})));
        ui.close();
    }
    ui.separator();
    if widgets::menu_item(ui, "Reverse Spine", editable && has_spine, false) {
        ensure_selected(out);
        out.actions.push(("object.blend.reverseSpine".into(), json!({})));
        ui.close();
    }
    if widgets::menu_item(ui, "Reverse Front to Back", editable, false) {
        ensure_selected(out);
        out.actions.push(("object.blend.reverseFrontToBack".into(), json!({})));
        ui.close();
    }
    if widgets::menu_item(ui, "Expand", editable, false) {
        ensure_selected(out);
        out.actions.push(("object.blend.expand".into(), json!({})));
        ui.close();
    }
    if widgets::menu_item(ui, "Release", editable, false) {
        ensure_selected(out);
        out.actions.push(("object.blend.release".into(), json!({})));
        ui.close();
    }
    ui.separator();
    let layers_only = view.opts.layers_only;
    if widgets::menu_item(ui, if layers_only { "Locate Layer" } else { "Locate Object" }, true, false) {
        out.actions.push(("layer.locate".into(), json!({ "id": blend.0, "layersOnly": layers_only })));
        ui.close();
    }
}

/// Guide rows `guides` dragged over row `r` (`resp`) of layer `layer`: the row lights up, and the
/// release puts them on that layer (`guide.setLayer`, one undo step).
fn guide_drop(ui: &Ui, view: &View, guides: &[usize], layer: NodeId, r: egui::Rect, resp: &egui::Response, out: &mut Out) {
    ui.painter().rect_stroke(r.shrink(1.0), 0.0, Stroke::new(2.0, view.t.accent), StrokeKind::Inside);
    if resp.dnd_release_payload::<LayersDrag>().is_some() {
        out.actions.push(("guide.setLayer".into(), json!({"indexes": guides, "layer": layer.0})));
    }
}

/// The row's fill (selected, current or hovered), its bottom edge and the current layer's marker.
fn row_background(ui: &Ui, view: &View, n: &Node, r: egui::Rect, resp: &egui::Response) {
    let t = &view.t;
    // Highlighted rows (else the current layer's).
    let highlighted = if view.rows.is_empty() { n.is_layer() && Some(n.id) == view.current } else { view.rows.contains(&n.id) };
    if highlighted {
        ui.painter().rect_filled(r, 0.0, t.row_selected);
    } else if resp.hovered() {
        ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.6));
    }
    ui.painter().line_segment([r.left_bottom(), r.right_bottom()], Stroke::new(1.0, t.input_border));
    if n.is_layer() && Some(n.id) == view.current {
        // Current-layer marker: a small triangle in the top-right corner.
        let c = r.right_top();
        ui.painter().add(egui::Shape::convex_polygon(vec![c, c + vec2(-6.0, 0.0), c + vec2(0.0, 6.0)], t.text, Stroke::NONE));
    }
}

/// The disclosure triangle of a row at `(r, x)`; Alt opens or closes everything inside too.
fn disclosure(ui: &Ui, view: &View, n: &Node, (r, x): (egui::Rect, f32), open: bool, out: &mut Out) {
    let t = &view.t;
    let dr = egui::Rect::from_min_size(egui::pos2(x, r.center().y - 8.0), vec2(14.0, 16.0));
    let dresp = ui.interact(dr, ui.scope_id().with(("disc", n.id.0)), Sense::click());
    icons::paint(ui, if open { "chevron-down" } else { "chevron-right" }, dr, t.text_dim);
    if dresp.clicked() {
        out.toggle.push((n.id, !open));
        if ui.input(|i| i.modifiers.alt) {
            n.walk(&mut |c| {
                if c.id != n.id && opens(c, &view.opts) {
                    out.toggle.push((c.id, !open));
                }
            });
        }
    }
}

/// The row's name label or inline rename field. Returns (name_end, name_rect).
fn row_name(ui: &mut Ui, view: &View, item: &Row, r: egui::Rect, x: f32, out: &mut Out) -> (f32, egui::Rect) {
    let n = &*item.node;
    let doc = view.doc;
    let t = &view.t;
    let xml_id = view.identify_xml_id;
    let name = if doc.mask_edit.is_some_and(|m| m.layer == n.id) { "<Opacity Mask>".to_string() } else { n.identify_label(xml_id) };
    let font = egui::FontId::proportional(13.0);
    let renaming: Option<(u64, String)> = ui.data(|d| d.get_temp(rename_id()));
    let name_rect = egui::Rect::from_min_max(egui::pos2(x - 2.0, r.center().y - 10.0), egui::pos2(r.right() - 44.0, r.center().y + 10.0));
    let name_end = match renaming {
        Some((rid, mut buf)) if rid == n.id.0 => {
            let field = rename_id().with("field");
            let kept = crate::widgets::own_field_shift_enter(ui, field, &mut buf, &name);
            let mut child = ui.new_child(egui::UiBuilder::new().max_rect(name_rect));
            let te = child
                .add(egui::TextEdit::singleline(&mut buf).id(field).align(egui::Align2::LEFT_CENTER).desired_width(name_rect.width()).font(font));
            // Focus the field as it opens. Asking every frame took the focus back from Enter,
            // Escape or a click elsewhere, so the rename never ended.
            if !te.has_focus() && !te.lost_focus() {
                te.request_focus();
            }
            if te.lost_focus() {
                ui.data_mut(|d| d.remove::<(u64, String)>(rename_id()));
                if child.input(|i| !i.key_pressed(egui::Key::Escape)) && buf != name {
                    out.actions.push(("layer.setProps".into(), json!({"ids": [n.id.0], "name": buf})));
                }
            } else {
                // Shift+Enter renames it and keeps the field open.
                if kept && buf != name {
                    out.actions.push(("layer.setProps".into(), json!({"ids": [n.id.0], "name": buf.clone()})));
                }
                ui.data_mut(|d| d.insert_temp(rename_id(), (n.id.0, buf)));
            }
            name_rect.right()
        }
        _ => {
            let painter = ui.painter().with_clip_rect(name_rect);
            // Generated names ("<Path>", "<Opacity Mask>") are translated where painted; the stored name stays English.
            // XML IDs stay as ids (not translated).
            let shown = if doc.mask_edit.is_some_and(|m| m.layer == n.id) {
                tl!("<Opacity Mask>").to_string()
            } else if xml_id {
                name
            } else {
                painted_name(n, &n.display_name(), crate::i18n::current())
            };
            let text = paint_name(&painter, egui::pos2(x, r.center().y), &shown, font, if item.dim { t.text_dim } else { t.text }, prints_italic(n));
            // A clipping path's name is underlined, a masked object's with a dashed line.
            if item.clip_path {
                painter.line_segment([text.left_bottom(), text.right_bottom()], Stroke::new(1.0, t.text));
            } else if n.extras().mask.is_some() {
                painter.extend(egui::Shape::dashed_line(&[text.left_bottom(), text.right_bottom()], Stroke::new(1.0, t.text), 3.0, 2.0));
            }
            text.right()
        }
    };
    (name_end, name_rect)
}

/// The widgets of row `item`.
pub(super) fn row(ui: &mut Ui, view: &View, item: &Row, out: &mut Out) {
    if let Some(i) = item.guide {
        return guide_row(ui, view, item, i, out);
    }
    if item.spine {
        return spine_row(ui, view, item, out);
    }
    let n = &*item.node;
    let (depth, has_children, open, color) = (item.depth, item.opens, item.open, item.colour);
    let t = &view.t;
    let h = view.h;
    let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click_and_drag());
    if view.reveal == Some(n.id) {
        ui.scroll_to_rect(r, None);
    }
    let is_sel = view.sel.contains(&n.id);
    let child_sel = !is_sel
        && n.children().is_some_and(|_| {
            let mut any = false;
            n.walk(&mut |c| any |= c.id != n.id && view.sel.contains(&c.id));
            any
        });
    row_background(ui, view, n, r, &resp);
    eye_and_lock(ui, view, n, r, out);
    // Layer colour bar (every row shows the colour of its layer).
    let mut x = r.left() + 2.0 * COLUMN + 2.0;
    colour_bar(ui, n, r, x, color);
    x += 6.0 + depth as f32 * INDENT;
    if has_children {
        disclosure(ui, view, n, (r, x), open, out);
    }
    x += 16.0;
    // Thumbnail.
    let thumb_rect = view.opts.thumb(n).then(|| {
        let th = row_thumb(ui, view, &item.node, r, x);
        x = th.right() + 4.0;
        th
    });
    // Name (Preferences › Units › Identify Objects By: Object Name or XML ID).
    let (name_end, name_rect) = row_name(ui, view, item, r, x, out);
    // Target circle and selection square.
    let col_x = r.right() - 43.5;
    ui.painter().line_segment([egui::pos2(col_x, r.top()), egui::pos2(col_x, r.bottom())], Stroke::new(1.0, t.input_border));
    let tc = egui::pos2(r.right() - TARGET_X, r.center().y);
    let tresp = target_circle(ui, n, tc, h, shows_targeted(view, n, is_sel), &mut out.actions, t);
    let sq = egui::pos2(r.right() - SQUARE_X, r.center().y);
    if is_sel {
        let q = egui::Rect::from_center_size(sq, vec2(7.0, 7.0));
        ui.painter().rect_filled(q, 0.0, color);
        ui.painter().rect_stroke(q, 0.0, Stroke::new(1.0, t.text), StrokeKind::Inside);
    } else if child_sel {
        ui.painter().rect_filled(egui::Rect::from_center_size(sq, vec2(4.0, 4.0)), 0.0, color);
    }
    let sq_resp = ui.interact(egui::Rect::from_center_size(sq, vec2(18.0, h)), ui.scope_id().with(("selsq", n.id.0)), Sense::click_and_drag());
    // Dragging the selected-art square carries the selection to another row.
    if (is_sel || child_sel) && sq_resp.drag_started_by(egui::PointerButton::Primary) {
        egui::DragAndDrop::set_payload(ui.ctx(), LayersDrag::Art);
    }
    let m = ui.input(|i| i.modifiers);
    let on_name = ui.input(|i| i.pointer.interact_pos()).is_some_and(|p| p.x <= name_end.max(name_rect.left() + 20.0) && p.x >= name_rect.left());
    if tresp.clicked() {
        out.actions.push(("layer.target".into(), json!({"id": n.id.0, "add": m.shift})));
    } else if sq_resp.clicked() {
        out.actions.push(("layer.selectAll".into(), json!({"id": n.id.0, "add": m.shift})));
    } else if resp.clicked() {
        out.click = Some((n.id, m));
        // Alt-click on a layer's or sublayer's name also selects its art, as its selection square does.
        // Only the drawn text counts, not the wider rename target of a short name.
        let on_text = ui.input(|i| i.pointer.interact_pos()).is_some_and(|p| p.x >= name_rect.left() && p.x <= name_end.min(name_rect.right()));
        if m.alt && !m.shift && !m.command && n.is_layer() && on_text {
            out.actions.push(("layer.selectAll".into(), json!({"id": n.id.0})));
        }
    }
    if resp.double_clicked() {
        let on_thumb = thumb_rect.is_some_and(|th| ui.input(|i| i.pointer.interact_pos()).is_some_and(|p| th.contains(p)));
        if on_name && !on_thumb {
            ui.data_mut(|d| d.insert_temp(rename_id(), (n.id.0, n.identify_label(view.identify_xml_id))));
        } else {
            out.actions.push(("ui.layerOptions".into(), json!({"ids": [n.id.0]})));
        }
    }
    // Row menu: context actions on this row (or every highlighted row when this is one of them).
    let collect_ids: Vec<u64> = if view.rows.contains(&n.id) { view.rows.iter().map(|id| id.0).collect() } else { vec![n.id.0] };
    // Right-clicking an unselected row selects it; a highlighted row keeps multiselection.
    if resp.secondary_clicked() && !view.rows.contains(&n.id) {
        out.actions.push(("layer.setCurrent".into(), json!({"id": n.id.0})));
        out.rows_after_click = Some(vec![n.id]);
    }
    if resp.secondary_clicked() {
        crate::panels::set_row_keys(ui.ctx(), ROW_KEYS, true);
    }
    resp.context_menu(|ui| {
        row_context_menu(ui, view, n, &collect_ids, out);
    });
    // Dragging rows: the highlighted rows when this is one of them, else this row. Only the primary
    // button drags; the secondary button opens the row menu, and a drag with it moves nothing.
    if resp.drag_started_by(egui::PointerButton::Primary) && view.column.is_none() {
        let ids = if view.rows.contains(&n.id) { view.rows.clone() } else { vec![n.id] };
        egui::DragAndDrop::set_payload(ui.ctx(), LayersDrag::Rows(ids));
        let grab = resp.interact_pointer_pos().map_or(vec2(h, h / 2.0), |p| p - r.min);
        ui.data_mut(|d| {
            d.insert_temp(key("grab"), (grab, r.width(), depth));
            d.insert_temp(key("grabbed"), n.id);
        });
    }
    // The cursor of a row drag is the panel's ([`super::drag_cursor`]): Alt can change it on any frame.
    drop_target(ui, view, n, r, x - 16.0, open, &resp, out);
}

/// The right-click context menu of row `n`.
fn row_context_menu(ui: &mut Ui, view: &View, n: &Node, collect_ids: &[u64], out: &mut Out) {
    let is_multi = collect_ids.len() > 1;
    if n.is_guide() {
        // "Release Guide" / "Release Guides"
        let rel_label = if is_multi { "Release Guides" } else { "Release Guide" };
        if widgets::menu_item(ui, rel_label, true, false) {
            out.actions.push(("view.guides.release".into(), json!({ "ids": collect_ids })));
            ui.close();
        }
        // "Duplicate" / "Duplicate Selection"
        let dup_label = if is_multi { "Duplicate Selection" } else { "Duplicate" };
        if widgets::menu_item(ui, dup_label, true, false) {
            out.actions.push(("layer.duplicate".into(), json!({ "ids": collect_ids })));
            ui.close();
        }
        // "Delete Guide" / "Delete Guides"
        let del_label = if is_multi { "Delete Guides" } else { "Delete Guide" };
        if widgets::menu_item(ui, del_label, true, false) {
            out.actions.push(("layer.delete".into(), json!({ "ids": collect_ids })));
            ui.close();
        }
        ui.separator();
        // Lock / Unlock
        let any_unlocked = collect_ids.iter().filter_map(|&id| view.doc.node(NodeId(id))).any(|node| !node.locked);
        let lock_label = if any_unlocked { "Lock" } else { "Unlock" };
        if widgets::menu_item(ui, lock_label, true, false) {
            out.actions.push(("layer.setProps".into(), json!({ "ids": collect_ids, "locked": any_unlocked })));
            ui.close();
        }
        // Hide / Show
        let any_visible = collect_ids.iter().filter_map(|&id| view.doc.node(NodeId(id))).any(|node| node.visible);
        let hide_label = if any_visible { "Hide" } else { "Show" };
        if widgets::menu_item(ui, hide_label, true, false) {
            out.actions.push(("layer.setProps".into(), json!({ "ids": collect_ids, "visible": !any_visible })));
            ui.close();
        }
        return;
    }
    // "Layer Options…" / "Options for Selection…" / "Options…"
    let options_label = if is_multi {
        "Options for Selection…"
    } else if n.is_layer() {
        "Layer Options…"
    } else {
        "Options…"
    };
    if widgets::menu_item(ui, options_label, true, false) {
        out.actions.push(("ui.layerOptions".into(), json!({ "ids": collect_ids })));
        ui.close();
    }
    // "Duplicate "<name>"" or "Duplicate Selection"
    let display = {
        let raw = painted_name(n, &n.display_name(), crate::i18n::current());
        if raw.chars().count() > 20 {
            let truncated: String = raw.chars().take(20).collect();
            format!("{truncated}…")
        } else {
            raw
        }
    };
    let dup_label = if is_multi { tl!("Duplicate Selection").to_string() } else { format!("{} \"{display}\"", tl!("Duplicate")) };
    // A copy of the isolated node goes beside it, outside the isolated art.
    let dup_enabled = !view.root.is_some_and(|r| collect_ids.contains(&r.0));
    if widgets::menu_item_name(ui, &dup_label, dup_enabled, false) {
        out.actions.push(("layer.duplicate".into(), json!({ "ids": collect_ids })));
        ui.close();
    }
    // "Delete "<name>"" or "Delete Selection"
    let del_label = if is_multi { tl!("Delete Selection").to_string() } else { format!("{} \"{display}\"", tl!("Delete")) };
    let del_enabled = {
        let listed: std::collections::HashSet<NodeId> = collect_ids.iter().map(|&id| NodeId(id)).collect();
        !view.doc.layers.iter().all(|l| listed.contains(&l.id))
    };
    if widgets::menu_item_name(ui, &del_label, del_enabled, false) {
        out.actions.push(("layer.delete".into(), json!({ "ids": collect_ids })));
        ui.close();
    }
    ui.separator();
    // In isolation mode the other layers are outside the isolated art, so these pairs are off.
    let isolated = view.root.is_some();
    // Lock Others / Unlock All Layers
    {
        let (lock_cmd, lock_params, lock_enabled) = multi_others_action(view.doc, collect_ids, false);
        let lock_label = if lock_cmd == "layer.lockOthers" { "Lock Others" } else { "Unlock All Layers" };
        if widgets::menu_item(ui, lock_label, lock_enabled && !isolated, false) {
            out.actions.push((lock_cmd, lock_params));
            ui.close();
        }
    }
    // Hide Others / Show All Layers
    {
        let (hide_cmd, hide_params, hide_enabled) = multi_others_action(view.doc, collect_ids, true);
        let hide_label = if hide_cmd == "layer.hideOthers" { "Hide Others" } else { "Show All Layers" };
        if widgets::menu_item(ui, hide_label, hide_enabled && !isolated, false) {
            out.actions.push((hide_cmd, hide_params));
            ui.close();
        }
    }
    ui.separator();
    // Locate Object, or Locate Layer with Show Layers Only on
    let layers_only = view.opts.layers_only;
    let locate_ok = !layers_only || locate_layer_listed(view.doc, view.root);
    if widgets::menu_item(ui, if layers_only { "Locate Layer" } else { "Locate Object" }, locate_ok, false) {
        out.actions.push(("layer.locate".into(), json!({ "id": n.id.0, "layersOnly": layers_only })));
        ui.close();
    }
    ui.separator();
    // Collect for Export
    let ids: Vec<u64> = collect_ids.to_vec();
    collect_for_export_menu(ui, true, move |multiple| {
        out.actions.push(("assets.add".into(), json!({ "ids": ids.clone(), "multiple": multiple })));
    });
}

/// Context menu Others action for `ids`: Lock Others / Unlock All Layers or Hide Others / Show All Layers.
/// When any other layer is unlocked (visible), returns `lockOthers` (`hideOthers`) preserving all layers holding
/// `ids`. When all other layers are locked (hidden) already, returns `unlockAll` (`showAll`).
pub(crate) fn multi_others_action(doc: &Document, ids: &[u64], eye: bool) -> (String, Value, bool) {
    let own_layers: std::collections::HashSet<NodeId> = ids.iter().filter_map(|&id| doc.layer_of(NodeId(id))).collect();
    let has_other_active = if eye {
        doc.layers.iter().any(|l| !own_layers.contains(&l.id) && l.visible)
    } else {
        doc.layers.iter().any(|l| !own_layers.contains(&l.id) && !l.locked)
    };
    let cmd = match (eye, has_other_active) {
        (true, true) => "layer.hideOthers",
        (true, false) => "layer.showAll",
        (false, true) => "layer.lockOthers",
        (false, false) => "layer.unlockAll",
    };
    let enabled = match (eye, has_other_active) {
        (true, true) | (false, true) => true,
        (true, false) => any_layer_state(doc, |n| !n.visible),
        (false, false) => any_layer_state(doc, |n| n.locked),
    };
    let params = if cmd.ends_with("Others") { json!({ "ids": ids }) } else { json!({}) };
    (cmd.into(), params, enabled)
}

fn any_layer_state(doc: &Document, pred: impl Fn(&Node) -> bool) -> bool {
    let mut found = false;
    doc.walk(|n| {
        if !found && n.is_layer() && pred(n) {
            found = true;
        }
    });
    found
}

/// Alt-clicking row `n`'s eye (`eye`) or lock: Hide Others or Lock Others, every top-level layer
/// but the one holding the row; when they all are hidden (locked) already, Show All Layers (Unlock
/// All Layers). Each is one undo step.
fn others_action(doc: &Document, n: &Node, eye: bool) -> (String, Value) {
    let own = doc.layer_of(n.id);
    let mut others = doc.layers.iter().filter(|l| Some(l.id) != own);
    let cmd = match (eye, if eye { others.any(|l| l.visible) } else { others.any(|l| !l.locked) }) {
        (true, true) => "layer.hideOthers",
        (true, false) => "layer.showAll",
        (false, true) => "layer.lockOthers",
        (false, false) => "layer.unlockAll",
    };
    // Show All and Unlock All take no rows; a ruler guide's row names its layer.
    let params = if cmd.ends_with("Others") { json!({ "ids": [layer_row(n, own).0] }) } else { json!({}) };
    (cmd.into(), params)
}

/// Ctrl/Cmd+Alt-clicking row `n`'s eye runs Outline Others, which puts every top-level layer but the
/// one holding the row in Outline and puts that one in Preview. When all the other layers are in
/// Outline already, it runs Preview All Layers. Each is one undo step.
fn outline_others_action(doc: &Document, n: &Node) -> (String, Value) {
    let own = doc.layer_of(n.id);
    if doc.layers.iter().any(|l| Some(l.id) != own && !matches!(l.kind, NodeKind::Layer { preview: false, .. })) {
        ("layer.outlineOthers".into(), json!({ "ids": [layer_row(n, own).0] }))
    } else {
        // Preview All takes no rows.
        ("layer.previewAll".into(), json!({}))
    }
}

/// The row the layer commands of an Alt-click on row `n`'s eye or lock name: `n` itself, or for a
/// ruler guide's row the layer `own` that holds the guide (the layer commands refuse a guide).
fn layer_row(n: &Node, own: Option<NodeId>) -> NodeId {
    if n.is_ruler_guide() { own.unwrap_or(n.id) } else { n.id }
}

/// Whether Locate Layer can highlight a row the panel lists: outside isolation mode, or with a
/// layer or sublayer isolated (`root`). An isolated group or object holds no layer row, and the
/// layer holding it is not listed.
pub(super) fn locate_layer_listed(doc: &Document, root: Option<NodeId>) -> bool {
    root.is_none_or(|r| doc.node(r).is_some_and(Node::is_layer))
}

/// The eye and lock columns of row `n` at `r`.
fn eye_and_lock(ui: &mut Ui, view: &View, n: &Node, r: egui::Rect, out: &mut Out) {
    let t = &view.t;
    let h = view.h;
    let eye = egui::Rect::from_min_size(r.min, vec2(COLUMN, h));
    let lock = egui::Rect::from_min_size(r.min + vec2(COLUMN, 0.0), vec2(COLUMN, h));
    ui.painter().line_segment([eye.right_top(), eye.right_bottom()], Stroke::new(1.0, t.input_border));
    ui.painter().line_segment([lock.right_top(), lock.right_bottom()], Stroke::new(1.0, t.input_border));
    let preview = !matches!(n.kind, NodeKind::Layer { preview: false, .. });
    let er = ui.interact(eye, ui.scope_id().with(("eye", n.id.0)), Sense::click_and_drag());
    if n.visible {
        let icon = egui::Rect::from_center_size(eye.center(), vec2(14.0, 14.0));
        if n.is_template() {
            // A template layer shows the template mark instead of the eye.
            icons::paint(ui, "shapes", icon, t.icon);
        } else if preview {
            icons::paint(ui, "eye", icon, t.icon);
        } else {
            // Outline view: a hollow eye.
            icons::paint(ui, "eye", icon, t.text_dim);
            ui.painter().circle_stroke(eye.center(), 7.5, Stroke::new(1.0, t.text_dim));
        }
    }
    let lr = ui.interact(lock, ui.scope_id().with(("lock", n.id.0)), Sense::click_and_drag());
    if n.locked {
        icons::paint(ui, "lock", egui::Rect::from_center_size(lock.center(), vec2(12.0, 12.0)), t.icon);
    } else if lr.hovered() {
        icons::paint(ui, "lock", egui::Rect::from_center_size(lock.center(), vec2(12.0, 12.0)), t.text_disabled);
    }
    let m = ui.input(|i| i.modifiers);
    // In isolation mode the other layers are outside the isolated art: Alt-clicks change nothing.
    let isolated = view.root.is_some();
    if er.clicked() {
        if m.alt && m.command {
            if !isolated {
                out.actions.push(outline_others_action(view.doc, n));
            }
        } else if m.alt {
            if !isolated {
                out.actions.push(others_action(view.doc, n, true));
            }
        } else if m.command && n.is_layer() {
            // Ctrl/Cmd-click: Preview ↔ Outline for this layer.
            out.actions.push(("layer.setProps".into(), json!({"ids": [n.id.0], "preview": !preview})));
        } else {
            out.actions.push(("layer.setProps".into(), json!({"ids": [n.id.0], "visible": !n.visible})));
        }
    }
    if lr.clicked() && m.alt {
        if !isolated {
            out.actions.push(others_action(view.doc, n, false));
        }
    } else if lr.clicked() {
        out.actions.push(("layer.setProps".into(), json!({"ids": [n.id.0], "locked": !n.locked})));
    }
    // Dragging down a column with the primary button gives every row it passes this row's new state.
    if er.drag_started_by(egui::PointerButton::Primary) {
        out.column = Some(Some(ColumnDrag { eye: true, value: !n.visible, ids: vec![n.id.0] }));
    } else if lr.drag_started_by(egui::PointerButton::Primary) {
        out.column = Some(Some(ColumnDrag { eye: false, value: !n.locked, ids: vec![n.id.0] }));
    } else if let Some(c) = out.column.clone().unwrap_or_else(|| view.column.clone())
        && !c.ids.contains(&n.id.0)
        && ui.input(|i| i.pointer.primary_down() && i.pointer.interact_pos().is_some_and(|p| p.y >= r.top() && p.y < r.bottom()))
    {
        let mut c = c;
        c.ids.push(n.id.0);
        out.column = Some(Some(c));
    }
}

/// Row `n` at `r` as a drop target for dragged rows or the selected-art square: shows where they
/// would go (a line above or below it, or a box around it for inside) and moves them on release
/// (Alt copies). `indent` is where the row's content starts.
#[allow(clippy::too_many_arguments)]
fn drop_target(ui: &Ui, view: &View, n: &Node, r: egui::Rect, indent: f32, open: bool, resp: &egui::Response, out: &mut Out) {
    let Some(drag) = resp.dnd_hover_payload::<LayersDrag>() else { return };
    let doc = view.doc;
    let moving: Vec<NodeId> = match &*drag {
        LayersDrag::Rows(ids) => ids.clone(),
        LayersDrag::Art => view.sel.iter().copied().collect(),
        LayersDrag::Guides(guides) => {
            // Guides go to the layer of the row (the row's own, for a layer); in isolation mode only
            // to a layer inside the isolated art.
            if let Some(layer) = if n.is_layer() { Some(n.id) } else { doc.layer_containing(n.id) }
                && view.root.is_none_or(|r| doc.ancestry(layer).is_some_and(|a| a.contains(&r)))
            {
                guide_drop(ui, view, guides, layer, r, resp, out);
            }
            return;
        }
    };
    // Never into itself or a row inside it. With Alt, rows held over one of their own rows copy
    // beside it, above or below, as `layer.move` places a copy there (#8237).
    let alt = ui.input(|i| i.modifiers.alt);
    let beside_itself = alt && matches!(&*drag, LayersDrag::Rows(_)) && moving.contains(&n.id);
    let around = doc.ancestry(n.id).unwrap_or_default();
    if moving.is_empty() || moving.iter().any(|m| around.contains(m) && !(beside_itself && *m == n.id)) {
        return;
    }
    let Some(p) = ui.input(|i| i.pointer.hover_pos()) else { return };
    let rel = (p.y - r.top()) / r.height().max(1.0);
    let inside = takes(n, &drag, &moving, doc);
    let place = match &*drag {
        LayersDrag::Rows(_) if beside_itself && rel < 0.5 => Place::Above,
        LayersDrag::Rows(_) if beside_itself => Place::Below,
        LayersDrag::Art if inside => Place::Inside,
        LayersDrag::Art => Place::Above,
        LayersDrag::Rows(_) if inside && rel >= 0.25 && (rel <= 0.75 || open) => Place::Inside,
        LayersDrag::Rows(_) if rel < 0.5 => Place::Above,
        LayersDrag::Rows(_) | LayersDrag::Guides(_) => Place::Below,
    };
    // In isolation mode the isolated row takes a drop only inside it, so nothing leaves the isolated
    // art or lands beside it; held over its own row, it goes nowhere.
    let place = if view.root == Some(n.id) {
        if !inside || beside_itself {
            return;
        }
        Place::Inside
    } else {
        place
    };
    // Beside a member of an Intertwine group (or a row in one) only what can be intertwined goes.
    if place != Place::Inside
        && doc.parent_of(n.id).is_some_and(|p| in_intertwine(doc, p))
        && moving.iter().filter_map(|i| doc.node(*i)).any(|m| refused_member(m).is_some())
    {
        return;
    }
    let stroke = Stroke::new(2.0, view.t.accent);
    match place {
        Place::Above => {
            ui.painter().line_segment([egui::pos2(indent, r.top()), egui::pos2(r.right(), r.top())], stroke);
        }
        Place::Below => {
            ui.painter().line_segment([egui::pos2(indent, r.bottom()), egui::pos2(r.right(), r.bottom())], stroke);
        }
        Place::Inside => {
            ui.painter().rect_stroke(r.shrink(1.0), 0.0, stroke, StrokeKind::Inside);
        }
    }
    if resp.dnd_release_payload::<LayersDrag>().is_some() {
        let copy = ui.input(|i| i.modifiers.alt);
        let ids: Vec<u64> = match &*drag {
            // The selection's top objects, as Arrange and Group take them.
            LayersDrag::Art => doc.paint_order(moving.iter().copied()).iter().map(|i| i.0).collect(),
            LayersDrag::Rows(ids) => ids.iter().map(|i| i.0).collect(),
            LayersDrag::Guides(_) => return,
        };
        out.actions.push(("layer.move".into(), json!({"ids": ids, "target": n.id.0, "place": place.id(), "copy": copy})));
    }
}

/// Whether `n`'s target circle shows as targeted: `n` is the one target, or, with nothing
/// targeted, it is selected. A layer is never selected itself (its art is), so with nothing
/// targeted a layer shows as targeted while all of its visible, unlocked art is selected, as an
/// object added to the selection from its circle does (#8646).
fn shows_targeted(view: &View, n: &Node, is_sel: bool) -> bool {
    match view.target {
        Some(id) => id == n.id,
        None if n.is_layer() => {
            if view.sel.is_empty() {
                return false;
            }
            let art = n.layer_art(true);
            !art.is_empty() && art.iter().all(|id| view.sel.contains(id))
        }
        None => is_sel,
    }
}

/// The target circle of `n`'s row at `c`: a ring, doubled while `n` is targeted, filled when `n`
/// has an appearance or transparency of its own. Its response is clicked to target `n`; dragging
/// it carries `n`'s appearance ([`PanelDrag::Appearance`]) onto another row's circle (Alt
/// copies it) or the trash.
fn target_circle(ui: &mut Ui, n: &Node, c: egui::Pos2, h: f32, targeted: bool, actions: &mut Vec<(String, Value)>, t: &Tokens) -> egui::Response {
    let resp = ui.interact(egui::Rect::from_center_size(c, vec2(16.0, h)), ui.scope_id().with(("target", n.id.0)), Sense::click_and_drag());
    // Only the primary button carries the appearance away.
    if resp.drag_started_by(egui::PointerButton::Primary) {
        egui::DragAndDrop::set_payload(ui.ctx(), PanelDrag::Appearance(n.id));
    }
    let held = resp.dnd_hover_payload::<PanelDrag>().is_some_and(|d| matches!(*d, PanelDrag::Appearance(id) if id != n.id));
    if let Some(d) = resp.dnd_release_payload::<PanelDrag>()
        && let PanelDrag::Appearance(source) = *d
        && source != n.id
    {
        let copy = ui.input(|i| i.modifiers.alt);
        actions.push(("appearance.transfer".into(), json!({"source": source.0, "target": n.id.0, "copy": copy})));
    }
    let ring = if held { t.accent } else { t.icon };
    ui.painter().circle_stroke(c, 5.0, Stroke::new(if held { 1.5 } else { 1.0 }, ring));
    if targeted {
        ui.painter().circle_stroke(c, 2.8, Stroke::new(1.0, t.icon));
    }
    if has_styled_target(n) {
        ui.painter().circle_filled(c, 3.2, t.icon);
    }
    if resp.dragged_by(egui::PointerButton::Primary) {
        ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
    }
    resp
}

/// Whether an object's target circle is filled: its appearance is not basic (a group or layer:
/// it has fills, strokes or effects of its own) or its transparency (opacity, blend mode,
/// isolation, knockout or an opacity mask) is not the default.
fn has_styled_target(n: &Node) -> bool {
    !n.appearance.is_basic()
        || (matches!(n.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) && !n.appearance.items.is_empty())
        || !n.has_default_transparency()
}