GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Links panel: every image in the document, linked files with their status (missing, modified)
//! and embedded images, as rows with a thumbnail, name, page and status. Clicking a row selects
//! its image, Ctrl/Cmd-click adds or removes it, and Shift-click selects the images of the rows
//! from the last row clicked without Shift to it (with Ctrl/Cmd, adds them); the buttons Relink,
//! Go To, Update and Edit Original act on the selected rows (Relink through its dialog, with the
//! Replace choice: `ui.relinkDialog`); Link Info shows the details of the first. The flyout adds Relink to Folder, Find in Folder (Find Linked Files:
//! `ui.findLinksInFolder`), Embed / Unembed, Placement Options, Select Linked / Embedded / Missing /
//! Modified (`addon.select.byLinkStatus`, Shift adds), Show filters, Sort, Copy Info (the selected
//! rows' file names to the clipboard: `links.copyInfo`), thumbnail size, Show in Folder and Show DCS Transparency Interactions (a warning on the rows of images placed
//! from DCS files that are transparent or overlap transparent objects).
//!
//! Images and placed documents in opacity-mask art and in the art of symbols, graph designs and
//! patterns are listed after the layers' own, one row each; the canvas can't select them, so a
//! click picks such a row in the panel, and the buttons act on it as on any other.
//!
//! Everything runs through the `links.*` commands (`links.editOriginal` and `links.reveal` are UI
//! commands: they hand the file to the system).

use egui::{Sense, Ui, vec2};
use serde_json::{Value, json};
use vectorcraft_doc::NodeId;

use super::{pstate, set_pstate};
use crate::theme::{self, Tokens};
use crate::widgets::{self, menu_item};
use crate::{VectorcraftApp, icons};

pub const ID: &str = "links";
/// Seconds between two looks at the linked files (they change outside the app).
const RECHECK: f64 = 2.0;
const SHOW: [(&str, &str); 4] = [("all", "Show All"), ("missing", "Show Missing"), ("modified", "Show Modified"), ("embedded", "Show Embedded")];
const SORT: [(&str, &str); 4] = [("name", "Sort by Name"), ("kind", "Sort by Kind"), ("status", "Sort by Status"), ("size", "Sort by Size")];
const THUMBS: [(f32, &str); 4] = [(0.0, "No Thumbnails"), (18.0, "Small Thumbnails"), (28.0, "Medium Thumbnails"), (44.0, "Large Thumbnails")];

/// The panel's options (flyout).
#[derive(Clone)]
struct Options {
    show: &'static str,
    sort: Option<&'static str>,
    /// Thumbnail side (pt); 0 hides them.
    thumb: f32,
    /// The Link Info section is open.
    info: bool,
    /// Show DCS Transparency Interactions: rows of DCS images that are transparent or overlap
    /// transparent objects get a warning.
    dcs: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self { show: "all", sort: None, thumb: 28.0, info: true, dcs: false }
    }
}

/// `links.list` and the first selected image's `links.info`, with what they were fetched for.
#[derive(Clone, Default)]
pub(super) struct Cache {
    /// (document uid, revision, show, sort, Show DCS Transparency Interactions).
    key: Option<(u64, u64, &'static str, Option<&'static str>, bool)>,
    at: f64,
    list: Value,
    info: Option<(u64, Value)>,
    /// The objects outside the layer tree, by id: those of the rows, for their thumbnails, and
    /// those whose opacity masks hold a row's image, for the tooltip.
    nested: Vec<(u64, std::sync::Arc<vectorcraft_doc::Node>)>,
}

impl Cache {
    /// Object `id` outside the layer tree, as [`cached`] collected it.
    fn nested_node(&self, id: u64) -> Option<&std::sync::Arc<vectorcraft_doc::Node>> {
        self.nested.binary_search_by_key(&id, |(n, _)| *n).ok().and_then(|i| self.nested.get(i)).map(|(_, n)| n)
    }
}

fn options(ctx: &egui::Context) -> Options {
    pstate(ctx, "links-options")
}

fn set_options(ctx: &egui::Context, f: impl FnOnce(&mut Options)) {
    let mut o = options(ctx);
    f(&mut o);
    set_pstate(ctx, "links-options", o);
}

/// The rows to show (cached per document revision, the files looked at again every [`RECHECK`]
/// seconds) and the Link Info of the first selected image when the section is open. A frame that
/// fetches nothing returns the stored cache without copying it and stores nothing.
fn cached(app: &mut VectorcraftApp, ctx: &egui::Context, o: &Options) -> Option<std::sync::Arc<Cache>> {
    let st = app.session.active()?;
    let key = Some((st.uid, st.revision, o.show, o.sort, o.dcs));
    let first = selected_links(app, ctx).first().copied();
    let now = ctx.input(|i| i.time);
    let mut c: std::sync::Arc<Cache> = pstate(ctx, "links-cache");
    let stale = c.key != key || now - c.at > RECHECK;
    if stale {
        let same_doc = c.key == key;
        let list = app.session.execute("links.list", &json!({ "show": o.show, "sort": o.sort, "dcsTransparency": o.dcs })).ok()?;
        // One pass over the stored art for the rows outside the layer tree and the objects whose
        // opacity masks hold them, not one per frame. An object in the layer tree is looked up by
        // its index when drawn.
        let doc = app.session.active().map(|st| &st.doc);
        let want: std::collections::HashSet<u64> = list["links"]
            .as_array()
            .into_iter()
            .flatten()
            .filter(|r| nested_row(r))
            .flat_map(|r| [r["id"].as_u64(), r["owner"]["maskOf"].as_u64()])
            .flatten()
            .filter(|id| doc.is_some_and(|d| d.node(NodeId(*id)).is_none()))
            .collect();
        // The same revision keeps the objects collected for it, so their thumbnails are not drawn
        // again each time the files are looked at.
        let nested = if same_doc && want.iter().all(|id| c.nested_node(*id).is_some()) {
            c.nested.clone()
        } else {
            match doc {
                Some(d) if !want.is_empty() => stored_objects(d, want),
                _ => vec![],
            }
        };
        c = std::sync::Arc::new(Cache { key, at: now, list, info: None, nested });
    }
    let want = first.filter(|_| o.info);
    if stale || c.info.as_ref().map(|(id, _)| *id) != want {
        let info = want.and_then(|id| Some((id, app.session.execute("links.info", &json!({ "id": id })).ok()?)));
        std::sync::Arc::make_mut(&mut c).info = info;
        set_pstate(ctx, "links-cache", c.clone());
    }
    // Files change on disk without a frame of ours: look again later.
    ctx.request_repaint_after(std::time::Duration::from_secs_f64(RECHECK));
    Some(c)
}

/// The objects of `doc` outside the layer tree whose ids are in `want`, sorted by id. The art of
/// the stored brushes is set on `doc` only while a command uses it ([`Document::mounted_art`]), so
/// when an id is not found otherwise that art is set on a copy of `doc`, which gives its objects
/// the ids `links.list` reports for them.
///
/// [`Document::mounted_art`]: vectorcraft_doc::Document::mounted_art
pub(super) fn stored_objects(
    doc: &vectorcraft_doc::Document,
    mut want: std::collections::HashSet<u64>,
) -> Vec<(u64, std::sync::Arc<vectorcraft_doc::Node>)> {
    let mut out = vec![];
    collect_stored(doc, &mut want, &mut out);
    if !want.is_empty() && doc.mounted_art.is_empty() {
        let art = vectorcraft_brush::stored_art::StoredArt::read(doc);
        if !art.is_empty() {
            let mut d = doc.clone();
            art.mount(&mut d);
            collect_stored(&d, &mut want, &mut out);
        }
    }
    out.sort_by_key(|(id, _)| *id);
    out
}

/// Adds the objects of `d` outside the layer tree whose ids are in `want` to `out`, taking their
/// ids out of `want`.
fn collect_stored(
    d: &vectorcraft_doc::Document,
    want: &mut std::collections::HashSet<u64>,
    out: &mut Vec<(u64, std::sync::Arc<vectorcraft_doc::Node>)>,
) {
    d.visit_stored_arcs(|n, place| {
        if !place.in_layers() && want.remove(&n.id.0) {
            out.push((n.id.0, std::sync::Arc::clone(n)));
        }
    });
}

/// The ids of the selected image objects and placed documents.
fn selected_images(app: &VectorcraftApp) -> Vec<u64> {
    let Some(st) = app.session.active() else { return vec![] };
    let image = |id: &NodeId| {
        st.doc.node(*id).is_some_and(|n| matches!(n.kind, vectorcraft_doc::NodeKind::Image(_) | vectorcraft_doc::NodeKind::PlacedDocument(_)))
    };
    st.selection.objects.iter().filter(|id| image(id)).map(|id| id.0).collect()
}

/// Is row `r` of an object outside the layer tree (in opacity-mask art or the art of a symbol,
/// graph design or pattern), which the canvas can't select?
fn nested_row(r: &Value) -> bool {
    r["owner"].is_object()
}

/// The active document's uid and the ids of its selected objects.
fn canvas_selection(app: &VectorcraftApp) -> Option<(u64, Vec<u64>)> {
    app.session.active().map(|st| (st.uid, st.selection.objects.iter().map(|id| id.0).collect()))
}

/// The rows outside the layer tree picked in the panel, with the canvas selection
/// ([`canvas_selection`]) they were picked with.
type NestedPicks = Option<((u64, Vec<u64>), Vec<u64>)>;

/// The rows outside the layer tree picked in the panel, in the active document. A change of the
/// canvas selection, or another document, drops them (the panel looks every frame).
fn nested_picks(app: &VectorcraftApp, ctx: &egui::Context) -> Vec<u64> {
    let picks: NestedPicks = pstate(ctx, "links-nested");
    let Some((at, ids)) = picks else { return vec![] };
    if Some(&at) == canvas_selection(app).as_ref() {
        return ids;
    }
    set_pstate::<NestedPicks>(ctx, "links-nested", None);
    vec![]
}

/// Keep `ids` as the rows picked in the panel, with the canvas selection as it is now.
fn set_nested_picks(app: &VectorcraftApp, ctx: &egui::Context, ids: Vec<u64>) {
    set_pstate(ctx, "links-nested", canvas_selection(app).map(|at| (at, ids)));
}

/// The ids of the selected rows: the selected images and placed documents, then the rows outside
/// the layer tree picked in the panel.
fn selected_links(app: &VectorcraftApp, ctx: &egui::Context) -> Vec<u64> {
    let mut ids = selected_images(app);
    for id in nested_picks(app, ctx) {
        if !ids.contains(&id) {
            ids.push(id);
        }
    }
    ids
}

/// [`selected_links`] with the app's window context, for a command run outside the panel (the
/// selected images and placed documents alone when there is no window).
pub(crate) fn selected_rows_of_app(app: &VectorcraftApp) -> Vec<u64> {
    match app.ui_ctx.clone() {
        Some(ctx) => selected_links(app, &ctx),
        None => selected_images(app),
    }
}

/// The rows of `list` whose image is selected.
fn selected_rows<'a>(list: &'a Value, selected: &[u64]) -> Vec<&'a Value> {
    list["links"].as_array().into_iter().flatten().filter(|r| r["id"].as_u64().is_some_and(|id| selected.contains(&id))).collect()
}

/// The ids of `rows` that `keep` accepts.
fn ids(rows: &[&Value], keep: impl Fn(&Value) -> bool) -> Vec<u64> {
    rows.iter().filter(|r| keep(r)).filter_map(|r| r["id"].as_u64()).collect()
}

fn status_is(r: &Value, status: &str) -> bool {
    r["status"].as_str() == Some(status)
}

fn report(app: &mut VectorcraftApp, r: Result<Value, String>, done: impl FnOnce(&Value) -> String) {
    let msg = match r {
        Ok(v) => done(&v),
        Err(e) => e,
    };
    app.status(msg);
}

/// The status bar's line for `n` relinked images.
pub(crate) fn relinked_status(n: usize) -> String {
    crate::i18n::msg_n(n as u64, "Relinked {n} image", "Relinked {n} images", &[])
}

/// The status bar's line for `n` images read again from their updated files.
pub(crate) fn updated_status(n: usize) -> String {
    crate::i18n::msg_n(n as u64, "Updated {n} image", "Updated {n} images", &[])
}

/// The status bar's line for `n` relinked images, naming the files `missed` that weren't found.
pub(crate) fn relinked_status_missed(n: usize, missed: &[&str]) -> String {
    if missed.is_empty() {
        relinked_status(n)
    } else {
        let (one, other) = ("Relinked {n} image; not in that folder: {names}", "Relinked {n} images; not in that folder: {names}");
        crate::i18n::msg_n(n as u64, one, other, &[("names", &missed.join(", "))])
    }
}

/// The ids of the selected image objects and placed documents (Relink's dialog).
pub(crate) fn selected_ids(app: &VectorcraftApp) -> Vec<u64> {
    selected_images(app)
}

/// Replace images `ids` with a picked file (Control bar, Properties): it keeps its own aspect
/// ratio, fitted inside the old bounds (Placement Options: Fit), whatever their Placement Options
/// say.
pub(crate) fn replace(app: &mut VectorcraftApp, ids: Vec<u64>) {
    crate::picks::button(app, move |app| {
        let pick = crate::FilePick { filters: crate::FilePick::filters(vectorcraft_engine::cmd::fileio::place_filters()), ..Default::default() };
        let Some(path) = crate::picks::open(app, &pick) else { return };
        let r = app.run("links.relink", json!({ "ids": ids.clone(), "path": path, "preserve": "fit" }));
        report(app, r, |v| crate::i18n::msg_n(v["relinked"].as_array().map_or(0, Vec::len) as u64, "Replaced {n} image", "Replaced {n} images", &[]));
    });
}

/// Relink images `ids` (none: every missing one) to the files of their names in a picked folder.
fn relink_to_folder(app: &mut VectorcraftApp, ids: Vec<u64>) {
    crate::picks::button(app, move |app| relink_to_picked_folder(app, ids.clone()));
}

fn relink_to_picked_folder(app: &mut VectorcraftApp, ids: Vec<u64>) {
    let Some(folder) = crate::picks::folder(app) else { return };
    let ids = Some(ids).filter(|i| !i.is_empty());
    let r = app.run("links.relink", json!({ "ids": ids, "folder": folder }));
    report(app, r, |v| {
        let missed: Vec<&str> = v["notFound"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
        relinked_status_missed(v["relinked"].as_array().map_or(0, Vec::len), &missed)
    });
}

/// Select image `id` and scroll it into view; one outside the layer tree only says that it has no
/// place on the canvas.
pub(crate) fn go_to(app: &mut VectorcraftApp, id: u64) {
    let Ok(r) = app.run("links.goTo", json!({ "id": id })) else { return };
    if r["onCanvas"] == false {
        app.status("The link is inside a symbol, graph design, pattern or opacity mask and has no place on the canvas");
        return;
    }
    let b: Vec<f64> = r["bounds"].as_array().into_iter().flatten().filter_map(Value::as_f64).collect();
    if let (&[x0, y0, x1, y1], Some(v)) = (&b[..], app.view_mut()) {
        v.center = vectorcraft_geom::Point::new((x0 + x1) / 2.0, (y0 + y1) / 2.0);
        v.fitted = true;
    }
}

/// Write embedded image `id` to a picked file (the web downloads it) and link it there.
pub(crate) fn unembed(app: &mut VectorcraftApp, id: u64, name: &str) {
    if app.services.download.is_some() {
        if let Ok(v) = app.session.execute("links.unembed", &json!({ "id": id }))
            && let (Some(name), Some(bytes)) = (v["name"].as_str(), v["dataBase64"].as_str().and_then(vectorcraft_format::base64_decode))
        {
            // A download that can't start is said like any file that can't be written.
            let _ = crate::io::write_reporting(app, name, &bytes);
        }
        return;
    }
    let name = name.to_string();
    crate::picks::button(app, move |app| {
        let Some(path) = crate::picks::save(app, &crate::FilePick::named(&name)) else { return };
        let r = app.run("links.unembed", json!({ "id": id, "path": path }));
        report(app, r, |v| format!("Unembedded to {}", v["path"].as_str().unwrap_or_default()));
    });
}

/// Whether Unembed… can act on `embedded` selected embedded images: one goes to a picked file,
/// several to a picked folder (the web downloads each one).
pub(crate) fn can_unembed(app: &VectorcraftApp, embedded: usize) -> bool {
    match embedded {
        0 => false,
        1 => true,
        _ => app.services.download.is_some() || app.services.pick_folder.is_some(),
    }
}

/// Write embedded images `ids` to new files in a picked folder and link each one to its file, in
/// one undo step (the web downloads each one and links none).
pub(crate) fn unembed_several(app: &mut VectorcraftApp, ids: Vec<u64>) {
    if app.services.download.is_some() {
        for id in ids {
            unembed(app, id, "");
        }
        return;
    }
    crate::picks::button(app, move |app| unembed_to_picked_folder(app, ids.clone()));
}

fn unembed_to_picked_folder(app: &mut VectorcraftApp, ids: Vec<u64>) {
    let Some(folder) = crate::picks::folder(app) else { return };
    let r = app.run("links.unembed", json!({ "ids": ids, "folder": folder }));
    report(app, r, |v| {
        let n = v["unembedded"].as_array().map_or(0, Vec::len);
        crate::i18n::msg_n(n as u64, "Unembedded {n} image to {folder}", "Unembedded {n} images to {folder}", &[("folder", &folder)])
    });
}

/// The UI commands `links.editOriginal` (open the linked file in its app) and `links.reveal`
/// (show it in its folder) for image `id` (default: the selected linked image) → `{path}`.
pub(crate) fn open_file(app: &mut VectorcraftApp, p: &Value, reveal: bool) -> Result<Value, String> {
    let id = p.get("id").and_then(Value::as_u64).or_else(|| {
        let list = app.session.execute("links.list", &json!({})).ok()?;
        let sel = selected_images(app);
        ids(&selected_rows(&list, &sel), |r| r["linked"] == true).first().copied()
    });
    let info = app.session.execute("links.info", &json!({ "id": id })).map_err(|e| e.to_string())?;
    let path = info["path"].as_str().ok_or("select a linked image: an embedded one has no file")?.to_string();
    if info["status"] == "missing" {
        return Err(format!("{path} can't be found: relink it first"));
    }
    // A placed document's file opens here, in a new tab: saving it updates this document.
    if !reveal && info["document"] == true {
        let opened = app.run("document.open", json!({ "path": path }))?;
        // Its missing fonts are asked about as for a document opened with File › Open.
        crate::dialogs::missing_fonts::after_open(app, &opened);
        crate::dialogs::settle(app);
        return Ok(json!({ "path": path }));
    }
    if reveal {
        crate::io::reveal_path(app, &path)?;
    } else {
        app.refuse_automation("links.editOriginal")?;
        openable_original(&path)?;
        crate::io::open_original(app, &path)?;
    }
    Ok(json!({ "path": path }))
}

/// The UI command `links.copyInfo {ids?}` (the panel menu's Copy Info): copy the file names of
/// images and placed documents `ids` (default: the selected rows, [`selected_rows_of_app`], in the
/// order `links.list` gives them; `null` counts as left out) to the clipboard, one per line in that
/// order → `{text, names}`. An embedded image gives the name its row shows.
pub(crate) fn copy_info(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
    let key = "ids";
    let given = match p.get(key) {
        None | Some(Value::Null) => None,
        Some(Value::Array(a)) => {
            let mut out = Vec::with_capacity(a.len());
            for (index, value) in a.iter().enumerate() {
                out.push(value.as_u64().ok_or_else(|| format!("`{key}` entry {index} is {value}, not a non-negative integer object id"))?);
            }
            Some(out)
        }
        Some(_) => return Err(format!("{key} must be an array of non-negative integer object ids")),
    };
    let list = app.session.execute("links.list", &json!({})).map_err(|e| e.to_string())?;
    let rows: Vec<&Value> = list["links"].as_array().into_iter().flatten().collect();
    let wanted = match given {
        Some(wanted) => wanted,
        None => {
            let sel = selected_rows_of_app(app);
            ids(&rows, |r| r["id"].as_u64().is_some_and(|id| sel.contains(&id)))
        }
    };
    let by_id: std::collections::HashMap<u64, &str> =
        rows.iter().filter_map(|r| Some((r["id"].as_u64()?, r["name"].as_str().unwrap_or_default()))).collect();
    let mut names = Vec::with_capacity(wanted.len());
    for id in &wanted {
        let name = by_id.get(id).ok_or_else(|| format!("object {} is not an image or a linked file", *id))?;
        names.push((*name).to_string());
    }
    if names.is_empty() {
        return Err("select the images, or pass ids".into());
    }
    let text = names.join("\n");
    app.clipboard_out = Some(text.clone());
    app.status("Copied the link info");
    Ok(json!({ "text": text, "names": names }))
}

/// Whether Edit Original may hand `path` to the system's default app: a regular file (after
/// following links) whose name, and the name it links to, have the extension of a format
/// VectorCraft reads. Anything else (a folder or app bundle, a script, a shortcut) is refused.
pub(crate) fn openable_original(path: &str) -> Result<(), String> {
    let readable = |p: &std::path::Path| {
        let ext = p.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default();
        use vectorcraft_engine::cmd::fileio::{FORMATS, read_extensions};
        !ext.is_empty() && FORMATS.iter().any(|f| f.read && read_extensions(f).contains(&ext.as_str()))
    };
    let name = std::path::Path::new(path).file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().into_owned());
    let real = std::fs::canonicalize(path).map_err(|e| format!("can't open “{name}”: {e}"))?;
    if !real.is_file() {
        return Err(format!("“{name}” is not a file"));
    }
    if !readable(std::path::Path::new(path)) || !readable(&real) {
        return Err(format!("Edit Original opens image and document files only; “{name}” is not one"));
    }
    Ok(())
}

/// A status badge: missing (red, ?), modified (amber, !), embedded (the image icon).
fn badge(ui: &Ui, r: egui::Rect, status: &str) {
    let t = Tokens::get(ui.ctx());
    let c = r.center();
    let p = ui.painter();
    let font = theme::semibold(10.0);
    match status {
        "missing" => {
            p.circle_filled(c, 6.5, t.error);
            p.text(c, egui::Align2::CENTER_CENTER, "?", font, egui::Color32::WHITE);
        }
        "modified" => {
            let pts = vec![c + vec2(0.0, -7.0), c + vec2(7.0, 6.0), c + vec2(-7.0, 6.0)];
            p.add(egui::Shape::convex_polygon(pts, t.warning, egui::Stroke::NONE));
            p.text(c + vec2(0.0, 1.5), egui::Align2::CENTER_CENTER, "!", font, egui::Color32::BLACK);
        }
        "embedded" => icons::paint(ui, "image", egui::Rect::from_center_size(c, vec2(13.0, 13.0)), t.text_dim),
        _ => {}
    }
}

/// The tooltip of row `r`. A row outside the layer tree also names where it is stored
/// (`links.list`'s `owner`): the symbol, graph design, pattern or brush whose art holds it, and the
/// object whose opacity mask holds it, by `mask` (that object's name) or else by its id.
pub(super) fn status_tip(r: &Value, mask: Option<String>) -> String {
    let tip = file_tip(r);
    let owner = &r["owner"];
    let name = owner["name"].as_str().unwrap_or_default();
    let art = match owner["kind"].as_str() {
        Some("symbol") => Some(tl!("Symbols")),
        Some("graphDesign") => Some(tl!("Graph Design")),
        Some("pattern") => Some(tl!("Pattern")),
        Some("brush") => Some(tl!("Brushes")),
        _ => None,
    };
    let tip = match art {
        Some(kind) => format!("{tip}\n{kind}: {name}"),
        None => tip,
    };
    match owner["maskOf"].as_u64() {
        Some(id) => format!("{tip}\n{}: {}", tl!("Opacity Mask"), mask.unwrap_or_else(|| id.to_string())),
        None => tip,
    }
}

/// The file, status and notes part of [`status_tip`].
fn file_tip(r: &Value) -> String {
    let path = r["found"].as_str().or(r["path"].as_str()).unwrap_or_default();
    let tip = match r["status"].as_str().unwrap_or_default() {
        "missing" => crate::i18n::fmt(tl!("Missing: {path}"), &[("path", path)]),
        "modified" => crate::i18n::fmt(tl!("Modified since it was read: {path}"), &[("path", path)]),
        "embedded" => tl!("Embedded").into(),
        _ => path.to_string(),
    };
    let tip = if r["dcsTransparency"] == true {
        format!("{tip}\n{}", tl!("Transparent, or overlaps transparent objects: check its separations"))
    } else {
        tip
    };
    // An image shown at reduced size or too large to draw (`links.list`'s note).
    match r["note"].as_str() {
        Some(note) => format!("{tip}\n{}", crate::i18n::msg(note)),
        None => tip,
    }
}

/// A click on the row of image `id` among the shown `rows`: plain selects the image alone,
/// Ctrl/Cmd adds or removes it, and Shift selects the images of the rows from the last row clicked
/// without Shift in this document to it, or with Ctrl/Cmd adds them to the selection. A
/// Shift-click while that row is not shown, or after the last click was in another document, acts
/// as a click without Shift. Rows outside the layer tree are picked in the panel itself, as the
/// canvas can't select them; a click without Ctrl/Cmd replaces both kinds of selection.
fn click(app: &mut VectorcraftApp, ctx: &egui::Context, rows: &[Value], id: u64, m: egui::Modifiers) {
    let Some(uid) = app.session.active().map(|st| st.uid) else { return };
    let anchor: Option<(u64, u64)> = pstate(ctx, "links-anchor");
    let at = |id: u64| rows.iter().position(|r| r["id"].as_u64() == Some(id));
    let nested = |r: &Value| nested_row(r).then(|| r["id"].as_u64()).flatten();
    let mut picks = if m.command { nested_picks(app, ctx) } else { vec![] };
    if m.shift
        && let Some((_, a)) = anchor.filter(|(doc, _)| *doc == uid)
        && let (Some(i), Some(j)) = (at(a), at(id))
    {
        let range = rows.get(i.min(j)..=i.max(j)).unwrap_or_default();
        let canvas: Vec<u64> = range.iter().filter(|r| !nested_row(r)).filter_map(|r| r["id"].as_u64()).collect();
        picks.extend(range.iter().filter_map(nested).filter(|id| !picks.contains(id)).collect::<Vec<_>>());
        app.run(if m.command { "select.add" } else { "select.set" }, json!({ "ids": canvas })).ok();
        set_nested_picks(app, ctx, picks);
        return;
    }
    set_pstate(ctx, "links-anchor", Some((uid, id)));
    let row = rows.iter().find(|r| r["id"].as_u64() == Some(id));
    if row.is_some_and(nested_row) {
        match picks.iter().position(|p| *p == id) {
            Some(i) if m.command => {
                picks.remove(i);
            }
            _ => picks.push(id),
        }
        if !m.command {
            app.run("select.set", json!({ "ids": [] })).ok();
        }
        set_nested_picks(app, ctx, picks);
        return;
    }
    if m.command {
        app.run("select.toggle", json!({ "id": id })).ok();
    } else {
        app.run("select.set", json!({ "ids": [id] })).ok();
    }
    set_nested_picks(app, ctx, picks);
}

pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
    let t = Tokens::get(ui.ctx());
    let o = options(ui.ctx());
    super::layers::show_thumbs_of(app.session.active().map(|st| st.uid));
    let Some(cache) = cached(app, ui.ctx(), &o) else {
        widgets::dim_label(ui, tl!("No document"));
        return;
    };
    let Some(st) = app.session.active() else { return };
    let doc = st.doc.clone();
    let thumbs = super::layers::ThumbSource::of(app, st, &doc);
    let selected = selected_links(app, ui.ctx());
    let rows: &[Value] = cache.list["links"].as_array().map_or(&[], Vec::as_slice);
    let mut clicked: Option<(u64, egui::Modifiers, bool)> = None;
    widgets::list_box(ui, |ui| {
        ui.set_min_height(110.0);
        ui.set_width(ui.available_width());
        if rows.is_empty() {
            let detail = if o.show == "all" {
                tl!("File › Place… brings in an image, linked or embedded.")
            } else {
                tl!("Nothing matches the Show filter.")
            };
            super::empty_state(ui, "link", tl!("No images"), detail);
            return;
        }
        egui::ScrollArea::vertical().max_height(260.0).auto_shrink([false, true]).show(ui, |ui| {
            let h = o.thumb.max(16.0) + 6.0;
            for r in rows {
                let Some(id) = r["id"].as_u64() else { continue };
                let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click());
                if !ui.is_rect_visible(rect) {
                    continue;
                }
                if selected.contains(&id) {
                    ui.painter().rect_filled(rect, 0.0, t.row_selected);
                } else if resp.hovered() {
                    ui.painter().rect_filled(rect, 0.0, t.hover);
                }
                let mut x = rect.left() + 6.0;
                if o.thumb > 0.0 {
                    let tr = egui::Rect::from_min_size(egui::pos2(x, rect.center().y - o.thumb / 2.0), vec2(o.thumb, o.thumb));
                    ui.painter().rect_filled(tr, 0.0, egui::Color32::WHITE);
                    let node =
                        if nested_row(r) { cache.nested_node(id).map(std::sync::Arc::clone) } else { super::layers::node_arc(&doc, NodeId(id)) };
                    if let Some(n) = node {
                        super::layers::real_thumb(ui, &thumbs, &n, tr);
                    }
                    ui.painter().rect_stroke(tr, 0.0, egui::Stroke::new(1.0, t.border), egui::StrokeKind::Inside);
                    x = tr.right() + 6.0;
                }
                let right = rect.right() - 22.0;
                badge(
                    ui,
                    egui::Rect::from_center_size(egui::pos2(right + 10.0, rect.center().y), vec2(16.0, 16.0)),
                    r["status"].as_str().unwrap_or_default(),
                );
                let mut text_right = right - 4.0;
                if r["dcsTransparency"] == true {
                    badge(ui, egui::Rect::from_center_size(egui::pos2(text_right - 8.0, rect.center().y), vec2(16.0, 16.0)), "modified");
                    text_right -= 20.0;
                }
                if let Some(page) = r["page"].as_u64() {
                    let g = ui.painter().layout_no_wrap(format!("{page}"), egui::FontId::proportional(11.5), t.text_dim);
                    text_right -= g.size().x;
                    ui.painter().galley(egui::pos2(text_right, rect.center().y - g.size().y / 2.0), g, t.text_dim);
                    text_right -= 6.0;
                }
                let name = r["name"].as_str().unwrap_or_default();
                let clip = egui::Rect::from_min_max(egui::pos2(x, rect.top()), egui::pos2(text_right.max(x), rect.bottom()));
                ui.painter().with_clip_rect(clip).text(
                    egui::pos2(x, rect.center().y),
                    egui::Align2::LEFT_CENTER,
                    name,
                    egui::FontId::proportional(12.5),
                    t.text,
                );
                // The mask's object by the layer tree's index or from the cache: no walk of the
                // stored art each frame.
                let mask = r["owner"]["maskOf"]
                    .as_u64()
                    .and_then(|m| doc.node(NodeId(m)).or_else(|| cache.nested_node(m).map(|n| &**n)))
                    .map(|n| super::layers::shown_name(app, n, crate::i18n::current()));
                let resp = resp.on_hover_text(status_tip(r, mask));
                if resp.double_clicked() {
                    clicked = Some((id, egui::Modifiers::NONE, true));
                } else if resp.clicked() {
                    clicked = Some((id, ui.input(|i| i.modifiers), false));
                }
            }
        });
    });
    match clicked {
        Some((id, _, true)) => go_to(app, id),
        Some((id, m, false)) => click(app, ui.ctx(), rows, id, m),
        None => {}
    }
    if o.info {
        let units = app.session.general_unit();
        link_info(ui, cache.info.as_ref().map(|(_, i)| i), units);
    }
    let sel = selected_rows(&cache.list, &selected);
    let modified = ids(&sel, |r| status_is(r, "modified") || r["preview"] == true && !status_is(r, "missing"));
    let can_edit = sel.iter().any(|r| r["linked"] == true && !status_is(r, "missing"));
    let linked = ids(&sel, |r| r["linked"] == true);
    widgets::bottom_bar(ui, |ui| {
        let info = !o.info;
        if widgets::icon_button(ui, if o.info { "chevron-down" } else { "chevron-right" }, tl!("Show Link Info"), false, 24.0).clicked() {
            set_options(ui.ctx(), |o| o.info = info);
        }
        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
            if widgets::icon_button_enabled(ui, "pencil", tl!("Edit Original"), false, can_edit, 24.0).clicked() {
                crate::menus::invoke(app, "links.editOriginal", file_row(&sel));
            }
            if widgets::icon_button_enabled(ui, "rotate-cw", tl!("Update Link"), false, !modified.is_empty(), 24.0).clicked() {
                let r = app.run("links.update", json!({ "ids": modified }));
                report(app, r, |v| updated_status(v["updated"].as_array().map_or(0, Vec::len)));
            }
            if widgets::icon_button_enabled(ui, "zoom-in", tl!("Go To Link"), false, sel.len() == 1, 24.0).clicked()
                && let Some(id) = sel.first().and_then(|r| r["id"].as_u64())
            {
                go_to(app, id);
            }
            if widgets::icon_button_enabled(ui, "link", tl!("Relink…"), false, !sel.is_empty() && can_pick(app), 24.0).clicked() {
                crate::menus::invoke(app, "ui.relinkDialog", json!({ "ids": ids(&sel, |_| true) }));
            }
            if widgets::icon_button_enabled(ui, "link-2-off", tl!("Break Link"), false, !linked.is_empty(), 24.0).clicked() {
                break_link(app, linked.clone());
            }
        });
    });
}

/// Break Link (`links.embed`) of `ids`: images keep their files' pixels, placed documents become
/// editable copies of their art.
fn break_link(app: &mut VectorcraftApp, ids: Vec<u64>) {
    let r = app.run("links.embed", json!({ "ids": ids }));
    report(app, r, |v| {
        let missing = v["missing"].as_array().map_or(0, Vec::len);
        let n = v["embedded"].as_array().map_or(0, Vec::len);
        if missing == 0 {
            crate::i18n::msg_n(n as u64, "Broke {n} link", "Broke {n} links", &[])
        } else {
            crate::i18n::msg_n(
                n as u64,
                "Broke {n} link; {missing} can't be read: relink or update them first",
                "Broke {n} links; {missing} can't be read: relink or update them first",
                &[("missing", &missing.to_string())],
            )
        }
    });
}

/// The params of Edit Original and Show in Folder for the selected rows `sel`: the first linked
/// row whose file is there (rows outside the layer tree included), else none (the selection's).
fn file_row(sel: &[&Value]) -> Value {
    match ids(sel, |r| r["linked"] == true && !status_is(r, "missing")).first() {
        Some(id) => json!({ "id": id }),
        None => json!({}),
    }
}

/// Pick-a-file actions need a file dialog (the desktop app).
fn can_pick(app: &VectorcraftApp) -> bool {
    app.services.pick_open.is_some()
}

/// Bytes as B, KB or MB.
fn size_label(bytes: u64) -> String {
    match bytes {
        b if b < 1024 => format!("{b} B"),
        b if b < 1024 * 1024 => format!("{:.1} KB", b as f64 / 1024.0),
        b => format!("{:.1} MB", b as f64 / (1024.0 * 1024.0)),
    }
}

/// Milliseconds since the Unix epoch as a UTC date and time.
pub(crate) fn date_label(ms: u64) -> String {
    let [y, mo, d, h, mi, _] = vectorcraft_doc::metadata::civil(i64::try_from(ms / 1000).unwrap_or(i64::MAX));
    format!("{y:04}-{mo:02}-{d:02} {h:02}:{mi:02} UTC")
}

/// The Link Info section: the details of `info` (`links.info`), or a hint.
fn link_info(ui: &mut Ui, info: Option<&Value>, units: vectorcraft_doc::Unit) {
    widgets::divider(ui);
    let Some(i) = info else {
        widgets::dim_label(ui, tl!("Select an image to see its Link Info."));
        return;
    };
    let pair = |k: &str| {
        let n: Vec<f64> = i[k].as_array().into_iter().flatten().filter_map(Value::as_f64).collect();
        match n[..] {
            [x, y] if (x - y).abs() < 0.05 => format!("{x:.0}"),
            [x, y] => format!("{x:.0} × {y:.0}"),
            _ => String::new(),
        }
    };
    let row = super::doc_info::row;
    row(ui, tl!("Name"), i["fileName"].as_str().or(i["name"].as_str()).unwrap_or_default().into());
    row(ui, tl!("Format"), i["format"].as_str().unwrap_or_default().into());
    row(ui, tl!("Color Space"), i["colorMode"].as_str().unwrap_or_default().into());
    if let Some(l) = i["location"].as_str() {
        row(ui, tl!("Location"), l.into());
    }
    // A placed document has no resolution: its rows are left out.
    for (label, k) in [(tl!("PPI"), "ppi"), (tl!("Effective PPI"), "effectivePpi")] {
        if i.get(k).is_some() {
            row(ui, label, pair(k));
        }
    }
    // Pixels, or a placed document's artboard size.
    let dims = match (i["pixelWidth"].as_u64(), i["pageWidth"].as_f64(), i["pageHeight"].as_f64()) {
        (Some(w), _, _) => format!("{w} × {} px", i["pixelHeight"].as_u64().unwrap_or(0)),
        (None, Some(w), Some(h)) => format!("{} × {}", units.format(w), units.format(h)),
        _ => String::new(),
    };
    row(ui, tl!("Dimensions"), dims);
    let scale: Vec<f64> = i["scale"].as_array().into_iter().flatten().filter_map(Value::as_f64).collect();
    if let [x, y] = scale[..] {
        row(ui, tl!("Scale"), format!("H: {x:.1}%  V: {y:.1}%"));
    }
    row(ui, tl!("Rotation"), format!("{:.1}°", i["rotation"].as_f64().unwrap_or(0.0)));
    if let Some(b) = i["fileSize"].as_u64() {
        row(ui, tl!("Size"), size_label(b));
    }
    for (k, label) in [("created", tl!("Created")), ("modified", tl!("Modified"))] {
        if let Some(ms) = i[k].as_u64() {
            row(ui, label, date_label(ms));
        }
    }
    if let Some(p) = i["page"].as_u64() {
        row(ui, tl!("Page"), p.to_string());
    }
    let status = match i["status"].as_str().unwrap_or_default() {
        "ok" => tl!("Linked"),
        "missing" => tl!("Missing"),
        "modified" => tl!("Modified"),
        _ => tl!("Embedded"),
    };
    row(ui, tl!("Status"), status.into());
}

/// The panel's flyout menu: the link commands for the selected rows, Select Linked / Embedded /
/// Missing / Modified for the whole document, the Show and Sort options, Copy Info and the
/// thumbnail options.
pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
    let o = options(ui.ctx());
    // The rows the panel shows (the menu opens from it).
    let cache = pstate::<std::sync::Arc<Cache>>(ui.ctx(), "links-cache");
    let list = &cache.list;
    let selected = selected_links(app, ui.ctx());
    let sel = selected_rows(list, &selected);
    let linked = ids(&sel, |r| r["linked"] == true);
    let embedded: Vec<&Value> = sel.iter().copied().filter(|r| status_is(r, "embedded")).collect();
    let any = !sel.is_empty();
    let missing_any = list["missing"].as_u64().unwrap_or(0) > 0;
    if menu_item(ui, tl!("Relink…"), any && can_pick(app), false) {
        crate::menus::invoke(app, "ui.relinkDialog", json!({ "ids": ids(&sel, |_| true) }));
    }
    if menu_item(ui, tl!("Relink to Folder…"), (any || missing_any) && app.services.pick_folder.is_some(), false) {
        relink_to_folder(app, ids(&sel, |_| true));
    }
    if menu_item(ui, tl!("Find in Folder…"), (!linked.is_empty() || missing_any) && app.services.pick_folder.is_some(), false) {
        crate::menus::invoke(app, "ui.findLinksInFolder", json!({ "ids": linked }));
    }
    if menu_item(ui, tl!("Go To Link"), sel.len() == 1, false)
        && let Some(id) = sel.first().and_then(|r| r["id"].as_u64())
    {
        go_to(app, id);
    }
    if menu_item(ui, tl!("Update Link"), sel.iter().any(|r| status_is(r, "modified")), false) {
        let r = app.run("links.update", json!({ "ids": ids(&sel, |r| status_is(r, "modified")) }));
        report(app, r, |v| updated_status(v["updated"].as_array().map_or(0, Vec::len)));
    }
    if menu_item(ui, tl!("Edit Original"), sel.iter().any(|r| r["linked"] == true && !status_is(r, "missing")), false) {
        crate::menus::invoke(app, "links.editOriginal", file_row(&sel));
    }
    ui.separator();
    if menu_item(ui, tl!("Embed Image(s)"), !linked.is_empty(), false) {
        let r = app.run("links.embed", json!({ "ids": linked }));
        report(app, r, |v| {
            let missing = v["missing"].as_array().map_or(0, Vec::len);
            let n = v["embedded"].as_array().map_or(0, Vec::len);
            if missing == 0 {
                crate::i18n::msg_n(n as u64, "Embedded {n} image", "Embedded {n} images", &[])
            } else {
                crate::i18n::msg_n(
                    n as u64,
                    "Embedded {n} image; {missing} missing: relink them first",
                    "Embedded {n} images; {missing} missing: relink them first",
                    &[("missing", &missing.to_string())],
                )
            }
        });
    }
    if menu_item(ui, tl!("Unembed…"), can_unembed(app, embedded.len()), false) {
        match &embedded[..] {
            [r] => {
                if let Some(id) = r["id"].as_u64() {
                    let name = r["name"].as_str().unwrap_or("Image").to_string();
                    unembed(app, id, &name);
                }
            }
            rows => unembed_several(app, rows.iter().filter_map(|r| r["id"].as_u64()).collect()),
        }
    }
    if menu_item(ui, tl!("Placement Options…"), any, false) {
        crate::menus::invoke(app, "ui.placementOptionsDialog", json!({ "ids": ids(&sel, |_| true) }));
    }
    if menu_item(ui, tl!("Show in Folder"), sel.iter().any(|r| r["linked"] == true && !status_is(r, "missing")), false) {
        crate::menus::invoke(app, "links.reveal", file_row(&sel));
    }
    ui.separator();
    // Select Linked, Embedded, Missing and Modified: every such image in the document, not only
    // the rows shown; with Shift, added to the selection.
    use crate::addons::select::link_status;
    for (i, (status, label, _)) in link_status::STATUSES.iter().enumerate() {
        if menu_item(ui, tl!(label), app.ui.link_statuses.has.get(i).copied().unwrap_or(false), false) {
            let p = json!({ "status": status });
            let p = if ui.input(|i| i.modifiers.shift) { link_status::with_shift(p) } else { p };
            crate::menus::invoke(app, link_status::COMMAND, p);
        }
    }
    ui.separator();
    for (id, label) in SHOW {
        if menu_item(ui, tl!(label), true, o.show == id) {
            set_options(ui.ctx(), |o| o.show = id);
        }
    }
    ui.separator();
    for (id, label) in SORT {
        if menu_item(ui, tl!(label), true, o.sort == Some(id)) {
            set_options(ui.ctx(), |o| o.sort = if o.sort == Some(id) { None } else { Some(id) });
        }
    }
    ui.separator();
    if menu_item(ui, tl!("Copy Info"), any, false) {
        crate::menus::invoke(app, "links.copyInfo", json!({ "ids": ids(&sel, |_| true) }));
    }
    ui.separator();
    for (px, label) in THUMBS {
        if menu_item(ui, tl!(label), true, o.thumb == px) {
            set_options(ui.ctx(), |o| o.thumb = px);
        }
    }
    if menu_item(ui, tl!("Show Link Info"), true, o.info) {
        set_options(ui.ctx(), |o| o.info = !o.info);
    }
    if menu_item(ui, tl!("Show DCS Transparency Interactions"), true, o.dcs) {
        set_options(ui.ctx(), |o| o.dcs = !o.dcs);
    }
}