GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Navigator panel: a thumbnail of the document (rendered with `Renderer::render_region_with`,
//! cached by document revision, off the UI thread for heavy documents and not during drags: see
//! [`crate::thumb_worker`]) with the view box you drag to pan, zoom out/in buttons, a zoom slider
//! and a zoom field. Panel Options… sets the view box's color and whether the thumbnail draws
//! dashed lines as solid lines, and the panel menu's View Artboard Contents Only (on by default)
//! limits the thumbnail to the art inside the artboards ([`PanelOptions`]).

use std::sync::Arc;

use egui::{Color32, Rect, Sense, Stroke, StrokeKind, Ui, pos2, vec2};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use vectorcraft_color::Color;
use vectorcraft_doc::LAYER_COLORS;
use vectorcraft_geom::{Point, Rect as DRect};

use crate::VectorcraftApp;
use crate::theme::Tokens;
use crate::thumb_worker::{Settle, ThumbWorker};
use crate::widgets::{self, menu_item};

/// The view box's color by default (Light Red, the first of [`view_box_colors`]).
pub const VIEW_BOX_RED: [u8; 3] = [0xff, 0x30, 0x30];

/// The view box color presets of Panel Options: Light Red, the default, then the layer colors.
pub fn view_box_colors() -> impl Iterator<Item = (&'static str, [u8; 3])> {
    std::iter::once(("Light Red", VIEW_BOX_RED)).chain(LAYER_COLORS.iter().copied())
}

/// `color` (a preset's name, in any case, or `#rrggbb`) as RGB; `None` for anything else,
/// including a color with an alpha or a short `#rgb`.
pub fn resolve(color: &str) -> Option<[u8; 3]> {
    let color = color.trim();
    if let Some((_, rgb)) = view_box_colors().find(|(name, _)| name.eq_ignore_ascii_case(color)) {
        return Some(rgb);
    }
    let hex = color.strip_prefix('#').filter(|h| h.len() == 6 && h.bytes().all(|b| b.is_ascii_hexdigit()))?;
    let byte = |at: usize| u8::from_str_radix(hex.get(at..at + 2)?, 16).ok();
    Some([byte(0)?, byte(2)?, byte(4)?])
}

/// Navigator panel options (≡ › Panel Options… and ≡ › View Artboard Contents Only), kept with the
/// UI state.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PanelOptions {
    /// The view box's color, `#rrggbb`.
    pub color: String,
    /// Draw Dashed Lines as Solid Lines (off by default): the thumbnail draws dashed strokes
    /// without their dashes. The canvas, printing and exports keep the dashes.
    pub solid_dashes: bool,
    /// View Artboard Contents Only (the panel menu; on by default, and on for UI state saved
    /// without it): the thumbnail fits the artboards and draws only the art inside them, cut at
    /// each artboard's edges. Off, it also fits and draws the art on the pasteboard.
    pub view_artboard_contents_only: bool,
}

impl Default for PanelOptions {
    fn default() -> Self {
        let [r, g, b] = VIEW_BOX_RED;
        Self { color: Color::rgb8(r, g, b).to_hex(), solid_dashes: false, view_artboard_contents_only: true }
    }
}

impl PanelOptions {
    /// The color the view box is drawn in: [`Self::color`], or Light Red when it is not a color.
    pub fn view_box(&self) -> Color32 {
        let [r, g, b] = resolve(&self.color).unwrap_or(VIEW_BOX_RED);
        Color32::from_rgb(r, g, b)
    }
}

/// `ui.navigatorPanelOptions {color?, solidDashes?, viewArtboardContentsOnly?}`: with none, open
/// Panel Options on the panel's options; otherwise set the ones given and answer the options.
pub fn set_options(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
    let (color, solid, contents_only) = (p.get("color"), p.get("solidDashes"), p.get("viewArtboardContentsOnly"));
    if color.is_none() && solid.is_none() && contents_only.is_none() {
        return crate::dialogs::navigator_panel_options::open(app);
    }
    let mut o = app.ui.navigator_panel.clone();
    if let Some(c) = color {
        let [r, g, b] = c.as_str().and_then(resolve).ok_or_else(|| {
            let color = c.as_str().map_or_else(|| c.to_string(), str::to_string);
            format!("`{color}` is not a colour (#rrggbb or a preset name)")
        })?;
        o.color = Color::rgb8(r, g, b).to_hex();
    }
    if let Some(s) = solid {
        let key = "solidDashes";
        o.solid_dashes = s.as_bool().ok_or_else(|| format!("`{key}` must be true or false"))?;
    }
    if let Some(v) = contents_only {
        let key = "viewArtboardContentsOnly";
        o.view_artboard_contents_only = v.as_bool().ok_or_else(|| format!("`{key}` must be true or false"))?;
    }
    app.ui.navigator_panel = o;
    Ok(serde_json::to_value(&app.ui.navigator_panel).unwrap_or_default())
}

/// What a navigator image shows: document `uid` at `revision`, `w`×`h` pixels, the artboards only,
/// dashed lines drawn as solid lines.
#[derive(Clone, Copy, Debug, PartialEq)]
struct ImageKey {
    uid: u64,
    revision: u64,
    w: u32,
    h: u32,
    artboard_only: bool,
    solid_dashes: bool,
}

#[derive(Default)]
struct Cache {
    /// Renders heavy documents' images (None: on this thread).
    worker: Option<ThumbWorker<(), (ImageKey, DRect)>>,
    worker_started: bool,
    /// The region shown for (uid, revision, artboard only): the art bounds walk the whole document.
    region: Option<((u64, u64, bool), DRect)>,
    /// The image shown: what it shows and the document rect it covers.
    shown: Option<(ImageKey, DRect)>,
    tex: Option<egui::TextureHandle>,
    /// The image the worker is making.
    pending: Option<ImageKey>,
    /// How long the last image took: past [`HEAVY_MS`] the next goes to the worker.
    last_ms: f64,
    settle: Settle,
    /// Images made (tests count them).
    renders: u64,
}

/// An image that took longer than this (ms) makes the next one render off the UI thread.
const HEAVY_MS: f64 = 8.0;

thread_local! {
    static CACHE: crate::graphics::TexCache<Cache> = crate::graphics::TexCache::default();
}

impl Cache {
    /// Upload image `r` showing `key` over `region`.
    fn upload(&mut self, ctx: &egui::Context, key: ImageKey, region: DRect, r: &vectorcraft_render::Rendered, ms: f64) {
        let img = crate::canvas::color_image(r);
        match &mut self.tex {
            Some(tx) => tx.set(img, egui::TextureOptions::LINEAR),
            None => self.tex = Some(ctx.load_texture("navigator", img, egui::TextureOptions::LINEAR)),
        }
        self.shown = Some((key, region));
        self.last_ms = ms;
        self.renders += 1;
    }

    /// The region (artboards, and the art unless `artboard_only`, with a margin) to show
    /// `doc` (`uid` at `revision`) in. While `busy` (a drag) the last one of the document stays,
    /// rather than walking the whole document every frame.
    fn region(&mut self, doc: &vectorcraft_doc::Document, uid: u64, revision: u64, artboard_only: bool, busy: bool) -> DRect {
        let key = (uid, revision, artboard_only);
        if let Some((k, r)) = self.region
            && (k == key || (busy && k.0 == uid && k.2 == artboard_only))
        {
            return r;
        }
        let mut region = doc.artboards.iter().map(|a| a.rect).reduce(|a, b| a.union(b)).unwrap_or(DRect::new(0.0, 0.0, 612.0, 792.0));
        if !artboard_only && let Some(b) = doc.art_bounds() {
            region = region.union(b);
        }
        let region = region.inflate(region.width() * 0.04 + 4.0, region.height() * 0.04 + 4.0);
        self.region = Some((key, region));
        region
    }

    /// Bring the image up to `key` (`doc` over `region`): heavy documents render on the worker
    /// while the last image stays, and nothing renders during a drag (`busy`) or before the
    /// document has settled, unless there's no image of it yet.
    fn update(&mut self, ctx: &egui::Context, doc: &Arc<vectorcraft_doc::Document>, key: ImageKey, region: DRect, busy: bool) {
        if !self.worker_started {
            self.worker_started = true;
            self.worker = ThumbWorker::spawn(ctx, "navigator");
        }
        let done = match self.worker.as_ref().map(ThumbWorker::poll) {
            Some(Some(done)) => done,
            Some(None) => {
                // The thread is gone: render here from now on.
                (self.worker, self.pending) = (None, None);
                vec![]
            }
            None => vec![],
        };
        for d in done {
            if self.pending == Some(d.key.0) {
                self.pending = None;
            }
            if let Some(img) = d.img {
                self.upload(ctx, d.key.0, d.key.1, &img, d.ms);
            }
        }
        let shown = self.shown.map(|(k, _)| k);
        if shown == Some(key) || self.pending == Some(key) {
            return;
        }
        let have = self.tex.is_some() && shown.is_some_and(|k| k.uid == key.uid);
        let now = ctx.input(|i| i.time);
        if have && (busy || !self.settle.settled(ctx, key.uid, key.revision, now)) {
            return;
        }
        let s = (key.w as f64 / region.width()).min(key.h as f64 / region.height());
        let opts = image_options(key);
        let heavy = self.last_ms > HEAVY_MS && have;
        if heavy && let Some(w) = &self.worker {
            let (doc, opts) = (doc.clone(), opts.clone());
            let job = move |r: &mut vectorcraft_render::Renderer| {
                if opts.solid_dashes {
                    SOLID_RENDERER.with(|solid| solid.borrow_mut().render_region_with(&doc, region, s, &opts))
                } else {
                    r.render_region_with(&doc, region, s, &opts)
                }
            };
            if w.submit((), (key, region), Box::new(move |r| Some(job(r)))) {
                self.pending = Some(key);
                return;
            }
            self.worker = None;
        }
        let t0 = crate::now_ms();
        let renderer = if key.solid_dashes { &SOLID_RENDERER } else { &NAV_RENDERER };
        let img = renderer.with(|r| r.borrow_mut().render_region_with(doc, region, s, &opts));
        self.upload(ctx, key, region, &img, crate::now_ms() - t0);
    }
}

/// How the image `key` names is drawn: as exported (template layers and guides left out),
/// transparent, with images far smaller than they are sampling their mip levels, with Draw
/// Dashed Lines as Solid Lines, dashed strokes without their dashes, and with View Artboard
/// Contents Only, the art cut to the artboards (none in the gaps between them).
fn image_options(key: ImageKey) -> vectorcraft_render::RenderOptions {
    vectorcraft_render::RenderOptions {
        skip_templates: true,
        image_mips: true,
        solid_dashes: key.solid_dashes,
        trim: key.artboard_only,
        ..Default::default()
    }
}

thread_local! {
    /// Renders light documents' images on the UI thread.
    static NAV_RENDERER: std::cell::RefCell<vectorcraft_render::Renderer> = std::cell::RefCell::new(vectorcraft_render::Renderer::new());
    /// Renders the images with Draw Dashed Lines as Solid Lines on, on the thread that renders
    /// them: each renderer draws every image it makes with one setting, so the art it keeps from
    /// one image is drawn the same way in the next.
    static SOLID_RENDERER: std::cell::RefCell<vectorcraft_render::Renderer> = std::cell::RefCell::new(vectorcraft_render::Renderer::new());
}

/// Map a document point into the thumbnail rect (region → rect, aspect preserved).
pub fn doc_to_thumb(region: DRect, thumb: Rect, p: Point) -> egui::Pos2 {
    let s = (thumb.width() as f64 / region.width()).min(thumb.height() as f64 / region.height());
    let ox = thumb.left() as f64 + (thumb.width() as f64 - region.width() * s) / 2.0;
    let oy = thumb.top() as f64 + (thumb.height() as f64 - region.height() * s) / 2.0;
    pos2((ox + (p.x - region.x0) * s) as f32, (oy + (p.y - region.y0) * s) as f32)
}

/// Inverse of [`doc_to_thumb`].
pub fn thumb_to_doc(region: DRect, thumb: Rect, p: egui::Pos2) -> Point {
    let s = (thumb.width() as f64 / region.width()).min(thumb.height() as f64 / region.height());
    let ox = thumb.left() as f64 + (thumb.width() as f64 - region.width() * s) / 2.0;
    let oy = thumb.top() as f64 + (thumb.height() as f64 - region.height() * s) / 2.0;
    Point::new(region.x0 + (p.x as f64 - ox) / s, region.y0 + (p.y as f64 - oy) / s)
}

/// The view box in the thumbnail: the corners of the canvas `canvas` showing view `v` (with the
/// interface zoomed by `ui_zoom`), mapped into the document (turned with the view) and then into
/// the thumbnail, cut to the thumbnail grown by a point. Fewer than three points when the view is
/// outside the thumbnail.
fn view_box(region: DRect, thumb: Rect, canvas: Rect, v: &crate::state::View, ui_zoom: f32) -> Vec<egui::Pos2> {
    let xf = crate::canvas::Xf::at(canvas, v, ui_zoom);
    let mut pts: Vec<egui::Pos2> = [canvas.left_top(), canvas.right_top(), canvas.right_bottom(), canvas.left_bottom()]
        .iter()
        .map(|c| doc_to_thumb(region, thumb, xf.to_doc(*c)))
        .collect();
    let b = thumb.expand(1.0);
    // One edge of `b` at a time: how far a point is inside it (negative outside).
    let edges: [fn(egui::Pos2, Rect) -> f32; 4] = [|p, b| p.x - b.left(), |p, b| b.right() - p.x, |p, b| p.y - b.top(), |p, b| b.bottom() - p.y];
    for inside in edges {
        let Some(&last) = pts.last() else { break };
        let mut out = vec![];
        let mut prev = last;
        for &p in &pts {
            let (s0, s1) = (inside(prev, b), inside(p, b));
            if (s0 >= 0.0) != (s1 >= 0.0) {
                // Where the side from `prev` to `p` crosses the edge.
                out.push(prev + (p - prev) * (s0 / (s0 - s1)));
            }
            if s1 >= 0.0 {
                out.push(p);
            }
            prev = p;
        }
        pts = out;
    }
    pts
}

/// Slider position (0..1) ↔ zoom percent on a log range, 3.13 %–64 000 %.
pub fn zoom_to_slider(z: f64) -> f64 {
    ((z.clamp(3.13, 64000.0)).ln() - 3.13f64.ln()) / (64000f64.ln() - 3.13f64.ln())
}
pub fn slider_to_zoom(t: f64) -> f64 {
    (3.13f64.ln() + t.clamp(0.0, 1.0) * (64000f64.ln() - 3.13f64.ln())).exp()
}

pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
    let t = Tokens::get(ui.ctx());
    let Some(st) = app.session.active() else {
        super::empty_state(ui, "map", tl!("No document"), tl!("Open a document to navigate it."));
        return;
    };
    let artboard_only = app.ui.navigator_panel.view_artboard_contents_only;
    let doc = st.doc.clone();
    let (uid, revision) = (st.uid, st.revision);
    // A drag in progress: every pointer move makes a new revision. The image waits for its end.
    let busy = st.interaction.is_some() || app.session.tool_busy();
    let region = CACHE.with(|c| c.borrow_mut().region(&doc, uid, revision, artboard_only, busy));
    let w = ui.available_width();
    let h = (w * 0.72).clamp(100.0, 200.0);
    let (thumb, resp) = ui.allocate_exact_size(vec2(w, h), Sense::click_and_drag());
    ui.painter().rect_filled(thumb, 0.0, t.pasteboard);
    let ppp = ui.ctx().pixels_per_point();
    let (pw, ph) = ((w * ppp) as u32, (h * ppp) as u32);
    let key = ImageKey { uid, revision, w: pw, h: ph, artboard_only, solid_dashes: app.ui.navigator_panel.solid_dashes };
    let (tex, tex_region) = CACHE.with(|c| {
        let mut c = c.borrow_mut();
        c.update(ui.ctx(), &doc, key, region, busy);
        // Never another document's image.
        match c.shown {
            Some((k, r)) if k.uid == uid => (c.tex.clone(), r),
            _ => (None, region),
        }
    });
    // Artboards (white) under the art thumbnail, like the canvas.
    for a in &doc.artboards {
        let r = Rect::from_min_max(doc_to_thumb(region, thumb, a.rect.origin()), doc_to_thumb(region, thumb, Point::new(a.rect.x1, a.rect.y1)));
        ui.painter().rect_filled(r, 0.0, Color32::WHITE);
    }
    if let Some(tex) = tex {
        let tl = doc_to_thumb(region, thumb, tex_region.origin());
        let br = doc_to_thumb(region, thumb, Point::new(tex_region.x1, tex_region.y1));
        ui.painter().image(tex.id(), Rect::from_min_max(tl, br), Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
    }
    // The view box: the canvas's corners in the document, turned with the view, cut to the thumbnail.
    let view = app.view().copied();
    if let (Some(v), Some(cr)) = (view, app.canvas_rect) {
        let box_points = view_box(region, thumb, cr, &v, app.canvas_ui_zoom);
        if box_points.len() > 2 {
            let color = app.ui.navigator_panel.view_box();
            ui.painter().with_clip_rect(thumb).add(egui::Shape::closed_line(box_points, Stroke::new(1.5, color)));
        }
        if (resp.dragged() || resp.clicked())
            && let Some(p) = resp.interact_pointer_pos()
        {
            let c = thumb_to_doc(region, thumb, p);
            app.run("view.setZoom", json!({"zoom": v.zoom * 100.0, "center": [c.x, c.y]})).ok();
        }
    }
    ui.painter().rect_stroke(thumb, 0.0, Stroke::new(1.0, t.border), StrokeKind::Outside);
    // Zoom controls.
    let zoom = view.map(|v| v.zoom * 100.0).unwrap_or(100.0);
    ui.horizontal(|ui| {
        ui.spacing_mut().item_spacing.x = 4.0;
        let mut z = None;
        if let Some(v) = widgets::plain_field(ui, "nav-zoom", zoom, "%", 2, 64.0) {
            z = Some(v);
        }
        if widgets::icon_button(ui, "dc-zoom-small", tl!("Zoom Out"), false, 22.0).clicked() {
            app.run("view.zoomOut", json!({})).ok();
        }
        let sw = (ui.available_width() - 30.0).max(40.0);
        let track = |_: f32| t.input_border;
        let shown = widgets::SliderValue::field(zoom, 3.13..=64000.0, "%", 2);
        if let (Some(v), phase) = widgets::color_slider_valued(ui, "nav-slider", tl!("Zoom"), Some(shown), zoom_to_slider(zoom) as f32, sw, &track)
            && phase != widgets::Live::Idle
        {
            z = Some(slider_to_zoom(v as f64));
        }
        if widgets::icon_button(ui, "dc-zoom-large", tl!("Zoom In"), false, 22.0).clicked() {
            app.run("view.zoomIn", json!({})).ok();
        }
        if let Some(z) = z {
            app.run("view.setZoom", json!({"zoom": z.clamp(3.13, 64000.0)})).ok();
        }
    });
}

pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
    let ab = app.ui.navigator_panel.view_artboard_contents_only;
    if menu_item(ui, tl!("View Artboard Contents Only"), true, ab) {
        app.run("ui.navigatorPanelOptions", json!({"viewArtboardContentsOnly": !ab})).ok();
    }
    if menu_item(ui, tl!("Panel Options…"), true, false) {
        app.run("ui.navigatorPanelOptions", json!({})).ok();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Screen readers get the Zoom slider's percent as the zoom field beside it shows it, on the
    /// slider's 3.13% to 64000% range.
    #[test]
    fn screen_readers_get_the_zoom_the_field_shows() {
        use crate::widgets::accessibility_tests::{slider_value, tree};
        let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
        app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
        app.run("view.setZoom", json!({"zoom": 250})).unwrap();
        let shown = tree(|ui| show(&mut app, ui));
        assert_eq!(slider_value(&shown, "Zoom"), (Some(250.0), Some(3.13), Some(64000.0), Some("250%".to_string())));
    }

    #[test]
    fn thumb_mapping_roundtrip() {
        let region = DRect::new(0.0, 0.0, 200.0, 100.0);
        let thumb = Rect::from_min_size(pos2(10.0, 10.0), vec2(100.0, 100.0));
        let p = doc_to_thumb(region, thumb, Point::new(100.0, 50.0));
        assert!((p.x - 60.0).abs() < 1e-3 && (p.y - 60.0).abs() < 1e-3);
        let q = thumb_to_doc(region, thumb, p);
        assert!((q.x - 100.0).abs() < 1e-6 && (q.y - 50.0).abs() < 1e-6);
    }

    /// In a view turned 90° the red view box outlines what the canvas shows: the canvas's corners
    /// mapped into the document and then into the thumbnail.
    #[test]
    fn the_view_box_turns_with_the_view() {
        use vectorcraft_engine::Session;
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
        let canvas = Rect::from_min_size(pos2(0.0, 0.0), vec2(600.0, 400.0));
        app.canvas_rect = Some(canvas);
        app.run("view.setZoom", json!({"zoom": 400, "center": [200, 150]})).unwrap();
        app.run("view.rotate", json!({"angle": 90})).unwrap();
        let ctx = egui::Context::default();
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(&mut app, ui));
        out.textures_delta.clear();
        // The thumbnail is the rectangle filled with the pasteboard color.
        let pasteboard = Tokens::get(&ctx).pasteboard;
        let thumb = out
            .shapes
            .iter()
            .find_map(|c| match &c.shape {
                egui::Shape::Rect(r) if r.fill == pasteboard => Some(r.rect),
                _ => None,
            })
            .expect("the thumbnail");
        let red = egui::epaint::ColorMode::Solid(Color32::from_rgb(0xff, 0x30, 0x30));
        let drawn: Vec<Vec<egui::Pos2>> = out
            .shapes
            .into_iter()
            .filter_map(|c| match c.shape {
                egui::Shape::Path(p) if p.closed && p.stroke.color == red => Some(p.points),
                _ => None,
            })
            .collect();
        let [points] = &drawn[..] else { panic!("one view box: {drawn:?}") };
        let region = CACHE.with(|c| c.borrow().region.unwrap().1);
        let xf = crate::canvas::Xf::new(canvas, app.view().unwrap());
        let want =
            [canvas.left_top(), canvas.right_top(), canvas.right_bottom(), canvas.left_bottom()].map(|c| doc_to_thumb(region, thumb, xf.to_doc(c)));
        assert_eq!(points.len(), 4, "{points:?}");
        assert!(points.iter().zip(&want).all(|(a, b)| a.distance(*b) < 0.5), "{points:?} is not {want:?}");
    }

    /// With the interface zoomed by Preferences › User Interface › UI Scaling, the view box still
    /// outlines what the canvas shows: at 400% and UI Scaling 1.5×, a canvas 600 interface points
    /// wide shows 600 × 1.5 / 4 document points.
    #[test]
    fn the_view_box_outlines_the_canvas_at_every_ui_scaling() {
        use vectorcraft_engine::Session;
        for ui_zoom in [0.75_f32, 1.5, 2.0] {
            let mut app = VectorcraftApp::new(Session::new(), Default::default());
            app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
            let canvas = Rect::from_min_size(pos2(0.0, 0.0), vec2(600.0, 400.0));
            app.canvas_rect = Some(canvas);
            app.canvas_ui_zoom = ui_zoom;
            app.run("view.setZoom", json!({"zoom": 400, "center": [200, 150]})).unwrap();
            let ctx = egui::Context::default();
            let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(&mut app, ui));
            out.textures_delta.clear();
            let pasteboard = Tokens::get(&ctx).pasteboard;
            let thumb = out
                .shapes
                .iter()
                .find_map(|c| match &c.shape {
                    egui::Shape::Rect(r) if r.fill == pasteboard => Some(r.rect),
                    _ => None,
                })
                .expect("the thumbnail");
            let red = egui::epaint::ColorMode::Solid(Color32::from_rgb(0xff, 0x30, 0x30));
            let drawn: Vec<Vec<egui::Pos2>> = out
                .shapes
                .into_iter()
                .filter_map(|c| match c.shape {
                    egui::Shape::Path(p) if p.closed && p.stroke.color == red => Some(p.points),
                    _ => None,
                })
                .collect();
            let [points] = &drawn[..] else { panic!("UI Scaling {ui_zoom}×: one view box: {drawn:?}") };
            let region = CACHE.with(|c| c.borrow().region.unwrap().1);
            // Each canvas corner is (its offset from the canvas's center) × UI Scaling / zoom
            // document points from the view's center.
            let k = f64::from(ui_zoom) / 4.0;
            let want = [canvas.left_top(), canvas.right_top(), canvas.right_bottom(), canvas.left_bottom()].map(|c| {
                let d = c - canvas.center();
                doc_to_thumb(region, thumb, Point::new(200.0 + f64::from(d.x) * k, 150.0 + f64::from(d.y) * k))
            });
            assert_eq!(points.len(), 4, "UI Scaling {ui_zoom}×: {points:?}");
            assert!(points.iter().zip(&want).all(|(a, b)| a.distance(*b) < 0.5), "UI Scaling {ui_zoom}×: {points:?} is not {want:?}");
        }
    }

    /// The panel menu's Panel Options… is enabled, and a click opens the Navigator Panel Options.
    #[test]
    fn panel_options_opens_from_the_panel_menu() {
        use crate::widgets::accessibility_tests::{named, tree};
        let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
        // Menu rows pad their label where a check mark would go.
        assert!(!named(&tree(|ui| menu(&mut app, ui)), "   Panel Options…").is_disabled());
        let ctx = egui::Context::default();
        let screen = Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(240.0, 400.0)));
        let raw = |events| egui::RawInput { events, screen_rect: screen, ..Default::default() };
        let mut out = ctx.run_ui(raw(vec![]), |ui| menu(&mut app, ui));
        out.textures_delta.clear();
        let item = out
            .shapes
            .iter()
            .find_map(|c| match &c.shape {
                egui::Shape::Text(t) if t.galley.text().trim_start() == "Panel Options…" => Some(t.visual_bounding_rect().center()),
                _ => None,
            })
            .expect("the menu item");
        let button = |pressed| egui::Event::PointerButton { pos: item, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
        ctx.run_ui(raw(vec![egui::Event::PointerMoved(item), button(true), button(false)]), |ui| menu(&mut app, ui)).textures_delta.clear();
        assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some(crate::dialogs::navigator_panel_options::KIND));
    }

    /// `ui.navigatorPanelOptions` sets the view box color and Draw Dashed Lines as Solid Lines: the
    /// view box is drawn in that color (Light Red by default), and the thumbnail is made again with
    /// the dashes left out. A value that is not a color, or not true or false, is refused and
    /// changes nothing.
    #[test]
    fn panel_options_set_the_view_box_color_and_solid_dashes() {
        use vectorcraft_engine::Session;
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
        app.canvas_rect = Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(600.0, 400.0)));
        app.run("view.setZoom", json!({"zoom": 400, "center": [200, 150]})).unwrap();
        let ctx = egui::Context::default();
        // The view boxes drawn in `color` in one frame.
        let boxes = |app: &mut VectorcraftApp, color: Color32| {
            let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(app, ui));
            out.textures_delta.clear();
            let want = egui::epaint::ColorMode::Solid(color);
            out.shapes.into_iter().filter(|c| matches!(&c.shape, egui::Shape::Path(p) if p.closed && p.stroke.color == want)).count()
        };
        let (red, blue) = (Color32::from_rgb(0xff, 0x30, 0x30), Color32::from_rgb(0x4f, 0x80, 0xff));
        assert_eq!((boxes(&mut app, red), boxes(&mut app, blue)), (1, 0), "Light Red by default");
        let shown = || CACHE.with(|c| c.borrow().shown.map(|(k, _)| k.solid_dashes));
        assert_eq!(shown(), Some(false));
        let set = app.run("ui.navigatorPanelOptions", json!({"color": "light blue", "solidDashes": true})).unwrap();
        assert_eq!(set, json!({"color": "#4f80ff", "solidDashes": true, "viewArtboardContentsOnly": true}));
        assert_eq!((boxes(&mut app, red), boxes(&mut app, blue)), (0, 1));
        for _ in 0..12 {
            boxes(&mut app, blue);
        }
        assert_eq!(shown(), Some(true), "the thumbnail is made again with solid dashes");
        let bad = [
            json!({"color": 12}),
            json!({"color": "#zz0000"}),
            json!({"color": "#ff000080"}),
            json!({"color": "#f00"}),
            json!({"color": null}),
            json!({"color": "Red", "solidDashes": "yes"}),
            json!({"color": "Red", "viewArtboardContentsOnly": "no"}),
            json!({"solidDashes": false, "viewArtboardContentsOnly": 0}),
            json!({"viewArtboardContentsOnly": null}),
        ];
        for bad in bad {
            assert!(app.run("ui.navigatorPanelOptions", bad.clone()).is_err(), "{bad}");
        }
        let want = json!({"color": "#4f80ff", "solidDashes": true, "viewArtboardContentsOnly": true});
        assert_eq!(serde_json::to_value(&app.ui.navigator_panel).unwrap(), want, "nothing changed");
        app.run("ui.navigatorPanelOptions", json!({"color": "#123456"})).unwrap();
        assert!(app.ui.navigator_panel.solid_dashes, "an option left out keeps its value");
    }

    /// The thumbnail's render options drop a dashed stroke's dashes with Draw Dashed Lines as Solid
    /// Lines, so it draws as the same stroke without dashes; without the option the dashes stay.
    #[test]
    fn the_thumbnail_draws_dashed_lines_as_solid_lines_with_the_option() {
        use vectorcraft_color::Paint;
        use vectorcraft_doc::{Appearance, Document, Node};
        let line = |dash: bool| {
            let mut d = Document::new(100.0, 100.0);
            let mut bp = vectorcraft_geom::BezPath::new();
            bp.move_to((10.0, 50.0));
            bp.line_to((90.0, 50.0));
            let id = d.alloc_id();
            let mut n =
                Node::path(id, vectorcraft_geom::PathData::from_bezpath(&bp), Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 6.0));
            n.appearance.stroke_mut().unwrap().dash = dash.then(|| vectorcraft_doc::Dash { pattern: vec![8.0, 8.0], ..Default::default() });
            let l = d.layers[0].id;
            d.insert(Some(l), 0, n).unwrap();
            d
        };
        let key = ImageKey { uid: 1, revision: 1, w: 100, h: 100, artboard_only: false, solid_dashes: false };
        let draw = |d: &Document, key: ImageKey| {
            vectorcraft_render::Renderer::new().render_region_with(d, DRect::new(0.0, 0.0, 100.0, 100.0), 1.0, &image_options(key)).pixels
        };
        let solid = draw(&line(false), key);
        assert_ne!(draw(&line(true), key), solid, "the dashes show without the option");
        let on = ImageKey { solid_dashes: true, ..key };
        assert!(image_options(on).solid_dashes && !image_options(key).solid_dashes);
        assert_eq!(draw(&line(true), on), solid, "the dashes go with the option");
        assert_eq!(draw(&line(false), on), solid, "a solid stroke draws as it did");
    }

    /// View Artboard Contents Only is on by default, and on for UI state saved without it, whether
    /// the panel's options are missing or saved before the option existed; a saved off stays off.
    #[test]
    fn view_artboard_contents_only_is_on_by_default_and_a_saved_off_stays_off() {
        assert!(PanelOptions::default().view_artboard_contents_only);
        let none: crate::UiState = serde_json::from_value(json!({})).unwrap();
        assert!(none.navigator_panel.view_artboard_contents_only, "UI state saved without the panel's options");
        let older: crate::UiState = serde_json::from_value(json!({"navigator_panel": {"color": "#123456", "solidDashes": true}})).unwrap();
        let o = &older.navigator_panel;
        assert!(o.view_artboard_contents_only && o.solid_dashes && o.color == "#123456", "options saved before the option existed: {o:?}");
        let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
        assert!(app.ui.navigator_panel.view_artboard_contents_only);
        let set = app.run("ui.navigatorPanelOptions", json!({"viewArtboardContentsOnly": false})).unwrap();
        assert_eq!(set, json!({"color": "#ff3030", "solidDashes": false, "viewArtboardContentsOnly": false}));
        let back: crate::UiState = serde_json::from_value(serde_json::to_value(&app.ui).unwrap()).unwrap();
        assert!(!back.navigator_panel.view_artboard_contents_only, "a saved off stays off");
    }

    /// The panel menu's View Artboard Contents Only starts checked, and a click turns it off (the
    /// same option `ui.navigatorPanelOptions` sets), then on again.
    #[test]
    fn view_artboard_contents_only_is_checked_in_the_panel_menu_and_a_click_toggles_it() {
        use crate::widgets::accessibility_tests::{named, tree};
        let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
        named(&tree(|ui| menu(&mut app, ui)), "✓ View Artboard Contents Only");
        let ctx = egui::Context::default();
        let screen = Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(240.0, 400.0)));
        let raw = |events| egui::RawInput { events, screen_rect: screen, ..Default::default() };
        let click = |app: &mut VectorcraftApp| {
            let mut out = ctx.run_ui(raw(vec![]), |ui| menu(app, ui));
            out.textures_delta.clear();
            let item = out
                .shapes
                .iter()
                .find_map(|c| match &c.shape {
                    egui::Shape::Text(t) if t.galley.text().ends_with("View Artboard Contents Only") => Some(t.visual_bounding_rect().center()),
                    _ => None,
                })
                .expect("the menu item");
            let button =
                |pressed| egui::Event::PointerButton { pos: item, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
            ctx.run_ui(raw(vec![egui::Event::PointerMoved(item), button(true), button(false)]), |ui| menu(app, ui)).textures_delta.clear();
        };
        click(&mut app);
        assert!(!app.ui.navigator_panel.view_artboard_contents_only, "a click turns it off");
        named(&tree(|ui| menu(&mut app, ui)), "   View Artboard Contents Only");
        click(&mut app);
        assert!(app.ui.navigator_panel.view_artboard_contents_only, "a second click turns it on");
    }

    /// With View Artboard Contents Only the thumbnail draws only the art inside the artboards: a
    /// shape in the gap between two artboards is left out and one that crosses an artboard's edge
    /// is cut there. Without it both draw.
    #[test]
    fn the_thumbnail_draws_only_the_art_inside_the_artboards_with_the_option() {
        use vectorcraft_color::Paint;
        use vectorcraft_doc::{Appearance, Document, Node};
        let mut d = Document::new(100.0, 100.0);
        let mut second = d.artboards[0].clone();
        second.id = d.artboards.iter().map(|a| a.id).max().unwrap_or(0) + 1;
        second.rect = DRect::new(150.0, 0.0, 250.0, 100.0);
        d.artboards.push(second);
        let l = d.layers[0].id;
        for (x0, y0, x1, y1) in [(110.0, 20.0, 140.0, 80.0), (20.0, 80.0, 60.0, 130.0)] {
            let mut bp = vectorcraft_geom::BezPath::new();
            bp.move_to((x0, y0));
            bp.line_to((x1, y0));
            bp.line_to((x1, y1));
            bp.line_to((x0, y1));
            bp.close_path();
            let id = d.alloc_id();
            let n = Node::path(id, vectorcraft_geom::PathData::from_bezpath(&bp), Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0));
            d.insert(Some(l), 0, n).unwrap();
        }
        let alpha = |artboard_only: bool, x: u32, y: u32| {
            let key = ImageKey { uid: 1, revision: 1, w: 260, h: 140, artboard_only, solid_dashes: false };
            vectorcraft_render::Renderer::new().render_region_with(&d, DRect::new(0.0, 0.0, 260.0, 140.0), 1.0, &image_options(key)).pixel(x, y)[3]
        };
        assert_eq!([alpha(true, 125, 50), alpha(true, 40, 90), alpha(true, 40, 120)], [0, 255, 0], "the gap and below the artboard are empty");
        assert_eq!([alpha(false, 125, 50), alpha(false, 40, 90), alpha(false, 40, 120)], [255, 255, 255], "all the art draws");
    }

    /// Turning View Artboard Contents Only off makes the thumbnail again, fitted to the art on the
    /// pasteboard as well as the artboards; turning it on fits it to the artboards again.
    #[test]
    fn the_thumbnail_is_made_again_when_view_artboard_contents_only_changes() {
        use vectorcraft_engine::Session;
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
        app.session.execute("shape.rectangle", &json!({"x": 400, "y": 50, "width": 50, "height": 50})).unwrap();
        let ctx = egui::Context::default();
        let frames = |app: &mut VectorcraftApp| {
            for _ in 0..12 {
                let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(app, ui));
                out.textures_delta.clear();
            }
            CACHE.with(|c| c.borrow().shown.map(|(k, r)| (k.artboard_only, r.x1)))
        };
        let Some((true, right)) = frames(&mut app) else { panic!("an image of the artboards only") };
        assert!(right < 300.0, "fitted to the artboard: {right}");
        app.run("ui.navigatorPanelOptions", json!({"viewArtboardContentsOnly": false})).unwrap();
        let Some((false, right)) = frames(&mut app) else { panic!("an image with the pasteboard's art") };
        assert!(right > 450.0, "fitted to the art on the pasteboard too: {right}");
        app.run("ui.navigatorPanelOptions", json!({"viewArtboardContentsOnly": true})).unwrap();
        assert!(matches!(frames(&mut app), Some((true, r)) if r < 300.0));
    }

    #[test]
    fn zoom_slider_roundtrip() {
        assert!((zoom_to_slider(3.13)).abs() < 1e-9);
        assert!((zoom_to_slider(64000.0) - 1.0).abs() < 1e-9);
        assert!((slider_to_zoom(zoom_to_slider(100.0)) - 100.0).abs() < 1e-6);
    }

    /// The navigator images made on this thread so far.
    fn renders() -> u64 {
        CACHE.with(|c| c.borrow().renders)
    }

    #[test]
    fn no_image_is_rendered_during_a_drag_and_one_is_after_it() {
        use vectorcraft_engine::Session;
        let mut app = VectorcraftApp::new(Session::new(), Default::default());
        app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
        app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
        // One context, so its clock runs on (1/60 s a pass) and the document can settle.
        let ctx = egui::Context::default();
        let frame = |app: &mut VectorcraftApp| {
            let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(app, ui));
            out.textures_delta.clear();
        };
        frame(&mut app);
        let first = renders();
        assert!(first >= 1, "the first image renders at once");
        // A move drag: every preview is a new revision.
        app.session.begin_interaction("Move").unwrap();
        for i in 1..=20 {
            app.session.preview("object.transform", &json!({"matrix": [1, 0, 0, 1, i, 0]})).unwrap();
            frame(&mut app);
        }
        assert_eq!(renders(), first, "nothing renders while the drag goes on");
        app.session.commit_interaction().unwrap();
        frame(&mut app);
        assert_eq!(renders(), first, "the document settles first");
        for _ in 0..12 {
            frame(&mut app);
        }
        assert_eq!(renders(), first + 1, "one image after the drag");
        let shown = CACHE.with(|c| c.borrow().shown.map(|(k, _)| k.revision));
        assert_eq!(shown, Some(app.session.active().unwrap().revision));
    }
}