Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

77.1 KB
Raw Blame
//! Live operation previews. While a feature dialog is open, its commands are evaluated on a
//! scratch copy of the session ([`solidcraft_engine::Session::preview`]), off the UI thread
//! where threads exist, and the result is drawn in place of the bodies it changes: new surface
//! tinted (unless Graphics' Tint new surface in a preview is off, when a changed body is drawn
//! as itself), removed material as a translucent ghost. The last good preview stays up while
//! the next one computes; once the current inputs make the feature fail, nothing is previewed
//! and the error shows in the dialog (#3254).
//! Nothing here touches the document or the undo history.
//!
//! A feature that adds or removes a tool body (join, cut, intersect) shows only its tool. That
//! is all its preview builds: its boolean, which can take seconds (a text joined onto a part),
//! runs once, on OK, and an error it finds shows then, the dialog staying open.

use solidcraft_engine::geom::prelude::*;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::sync::mpsc::{Receiver, TryRecvError};

use serde_json::Value;
use solidcore_record::Vec3;
use solidcraft_engine::Session;
use solidcraft_engine::doc::ModelState;
use solidcraft_engine::doc::progress::Progress;
use solidcraft_engine::geom::{Aabb3, Mesh, Ring};
use solidcraft_engine::kernel::history::Change;

use crate::SolveApp;
use crate::gpu::{GpuScene, SceneSlot};
use crate::theme::Tokens;

/// Colours a preview is drawn with (straight sRGBA).
#[derive(Clone, Copy, Debug)]
pub struct Colors {
    pub body: [u8; 4],
    /// The tool a join, cut or intersect adds, a datum plane a preview creates, and, where the
    /// preference asks for it (`tint_new`), the surface a feature adds to a body it changes.
    pub added: [u8; 4],
    /// A body the preview creates (New Body), so it doesn't look like a join.
    pub new_body: [u8; 4],
    pub removed: [u8; 4],
    pub edge: [u8; 4],
    /// Whether a moved component's old place is drawn as a ghost (Drive Joints' Show ghost).
    pub ghost: bool,
    /// Whether the surface a feature adds to a body it changes is drawn in the added colour
    /// (Graphics' Tint new surface in a preview). Off: that body is drawn as itself, and which
    /// of its faces are new is never worked out.
    pub tint_new: bool,
    /// Whether removed material is filled in the cut colour (Graphics' Show removed material in
    /// red): the see-through cut and the ghost of the shape a feature replaces. Off: those fills
    /// are left out altogether, so they are fully transparent and only outlines are drawn.
    pub cut_tint: bool,
}

impl Colors {
    pub fn from_tokens(t: &Tokens) -> Colors {
        let c = |c: egui::Color32| c.to_srgba_unmultiplied();
        Colors {
            body: c(t.body),
            added: c(t.preview_add),
            new_body: c(t.preview_new),
            removed: c(t.preview_cut),
            edge: c(t.body_edge),
            ghost: true,
            tint_new: true,
            cut_tint: true,
        }
    }

    fn for_tool(t: &Tokens, tab: &str, active: bool) -> Colors {
        let mut colors = Self::from_tokens(t);
        let accent = crate::theme::TabAccent::for_tab(t, tab);
        if active && accent.color != t.accent {
            colors.added = accent.color.to_srgba_unmultiplied();
            colors.new_body = t.body.lerp_to_gamma(accent.color, 0.45).to_srgba_unmultiplied();
        }
        // Removed material retains its warning colour and opacity.
        colors
    }
}

pub struct SketchPreview {
    pub sketch: std::sync::Arc<solidcraft_engine::doc::SolvedSketch>,
    pub curves: Vec<usize>,
    pub points: Vec<usize>,
}

/// A computed preview: the scene to draw and the bodies it stands in for.
pub struct Built {
    pub scene: GpuScene,
    pub sketches: Vec<SketchPreview>,
    pub replaced: Vec<String>,
    /// Why the feature failed, when the scene only shows its tool body.
    pub error: Option<String>,
    /// Extent of what the preview shows (the view's depth range must cover it).
    pub bounds: Aabb3,
    /// The smallest and largest radius a fillet used when it reduced one too large for its edges
    /// (`fit: reduce`).
    pub applied: Option<(f64, f64)>,
    /// The radius spline of each edge a fillet tapered (`fit: taper`), with a point on the edge.
    pub tapered: Vec<(Vec3, solidcraft_engine::kernel::Taper)>,
    /// The reply of a previewed drive that stopped short of the asked values (the dialog says
    /// where and why, in the UI's language).
    pub stopped: Option<serde_json::Value>,
}

type Job = Result<Built, String>;

/// How many obsolete preview jobs may still run, left behind for newer inputs (#7167). Beyond
/// them, a new job waits for the running one as before, so a quick series of picks keeps at
/// most this many kernel computations going beside the current one.
const DETACHED: usize = 2;

#[derive(Default)]
pub struct PreviewState {
    /// Key of the commands whose result (or error) is shown.
    shown_key: Option<u64>,
    running: Option<(u64, Receiver<Job>)>,
    /// Asks the running job to stop between features once it is obsolete.
    running_progress: Option<Arc<Progress>>,
    /// Jobs left behind for inputs that changed to another setup (#7167): asked to stop, their
    /// results are dropped when they come. With [`DETACHED`] of them running, the next obsolete
    /// job is waited for.
    detached: Vec<Receiver<Job>>,
    /// The running job builds a tool alone (for a join, cut or intersect).
    running_tool: bool,
    /// Inputs of a joint or distance preview, excluding only its driven values.
    running_stream_key: Option<u64>,
    /// Bodies the preview stands in for (hidden in the model scene).
    pub replaced: Vec<String>,
    /// The preview scene for the GPU and its key (changes whenever the scene does).
    pub slot: SceneSlot,
    pub key: u64,
    /// Is a preview scene up?
    pub active: bool,
    pub sketches: Vec<SketchPreview>,
    /// Why the current inputs don't make a feature (nothing is previewed then).
    pub error: Option<String>,
    /// A preview is being computed.
    pub busy: bool,
    /// Extent of the preview scene.
    pub bounds: Aabb3,
    /// Time the last preview took (ms), for `ui.inspect`.
    pub ms: f64,
    started: f64,
    /// The smallest and largest radius the previewed fillet used when it reduced the one asked for.
    pub applied: Option<(f64, f64)>,
    /// The reply of a previewed drive that stopped short (see [`Built::stopped`]).
    pub stopped: Option<serde_json::Value>,
    /// The radius splines the previewed fillet tapered its edges with.
    pub tapered: Vec<(Vec3, solidcraft_engine::kernel::Taper)>,
    straight: crate::extrude_preview::StraightPreview,
    moving: crate::move_preview::MovePreview,
    /// PreviewReselect: Primary held - hide the preview and restore replaced bodies for picks.
    pub rolled_back: bool,
}

impl PreviewState {
    /// Bodies the model scene should hide for the preview (empty while rolled back).
    pub fn effective_replaced(&self) -> &[String] {
        if self.rolled_back { &[] } else { &self.replaced }
    }

    /// Stop drawing the preview scene and give the bodies it stood in for back to the model.
    fn hide_scene(&mut self) {
        if self.active || !self.replaced.is_empty() {
            self.active = false;
            self.bounds = Aabb3::EMPTY;
            self.replaced.clear();
            self.set_scene(GpuScene::default());
        }
    }

    fn clear(&mut self) {
        self.straight.clear();
        self.moving.clear();
        self.detach();
        self.running_tool = false;
        self.running_stream_key = None;
        self.shown_key = None;
        self.error = None;
        self.applied = None;
        self.stopped = None;
        self.tapered.clear();
        self.sketches.clear();
        self.busy = false;
        self.rolled_back = false;
        self.hide_scene();
    }

    /// Drop the preview of a design that leaves the window. Its scene goes too: the viewport
    /// keeps drawing the last scene it took until the slot it reads hands it another, so a fresh
    /// state (another slot, empty) would leave the old preview drawn over the next design.
    pub fn reset(&mut self) {
        self.detach();
        let (slot, key, detached) = (std::mem::take(&mut self.slot), self.key, std::mem::take(&mut self.detached));
        *self = PreviewState { slot, key, detached, ..PreviewState::default() };
        self.set_scene(GpuScene::default());
    }

    fn set_scene(&mut self, sc: GpuScene) {
        if let Ok(mut slot) = self.slot.lock() {
            *slot = Some(sc);
        }
        self.key = self.key.wrapping_add(1);
    }

    fn apply(&mut self, key: u64, job: Job) {
        self.shown_key = Some(key);
        self.ms = crate::now_ms() - self.started;
        match job {
            Ok(b) => {
                self.sketches = b.sketches;
                self.replaced = b.replaced;
                self.bounds = b.bounds;
                self.active = true;
                self.error = b.error;
                self.applied = b.applied;
                self.stopped = b.stopped;
                self.tapered = b.tapered;
                self.set_scene(b.scene);
            }
            // The current inputs make no feature: the last good geometry would contradict
            // them (a 1 mm extrusion still drawn at 0 mm), so nothing is drawn.
            Err(e) => {
                self.hide_scene();
                self.sketches.clear();
                self.error = Some(e);
                self.applied = None;
                self.stopped = None;
                self.tapered.clear();
            }
        }
    }

    /// Errors validate only the current inputs. Keep the last good geometry while
    /// computing new inputs, but never let an obsolete result block their Apply.
    fn poll(&mut self, key: u64, stream_key: Option<u64>) {
        if self.shown_key != Some(key) {
            self.sketches.clear();
            self.error = None;
        }
        if let Some((k, rx)) = &self.running {
            let k = *k;
            let job = match rx.try_recv() {
                Ok(job) => Some(job),
                Err(TryRecvError::Empty) => None,
                Err(TryRecvError::Disconnected) => Some(Err("the preview failed".into())),
            };
            if let Some(job) = job {
                self.running = None;
                let good = matches!(&job, Ok(b) if b.error.is_none());
                let intermediate = good && (self.running_tool || stream_key.is_some() && self.running_stream_key == stream_key);
                self.running_stream_key = None;
                self.running_tool = false;
                // A held slider, a dragged arrow or an animation may advance faster than the
                // worker. Show completed joint/distance results from the same setup, and tools built for
                // inputs since changed, then compute the latest. Errors still require an exact
                // key.
                if k == key || intermediate {
                    self.apply(k, job);
                }
            }
        }
        // A detached job's result is never shown: drop it once it is done.
        self.detached.retain(|rx| matches!(rx.try_recv(), Err(TryRecvError::Empty)));
        // The inputs moved to another setup (another selection, for the features whose distance,
        // radius or pose streams) while a job runs: its result could only be dropped, so stop
        // waiting for it and let the current inputs start now (#7167). A tool, or a value
        // streaming within one setup, still shows each finished result; inputs that don't stream
        // wait as before. At most `DETACHED` jobs are left behind; while they run, the next
        // obsolete one is waited for as before.
        let other_setup = !self.running_tool && stream_key.is_some() && self.running_stream_key.is_some() && self.running_stream_key != stream_key;
        if self.running.as_ref().is_some_and(|(k, _)| *k != key) && other_setup && self.detached.len() < DETACHED {
            self.detach();
        }
    }

    /// Stop waiting for the running job: ask it to stop between features, and count it among the
    /// detached jobs until its thread ends (also when a closed dialog or another design leaves it
    /// behind, so `DETACHED` bounds what runs).
    fn detach(&mut self) {
        if let Some(progress) = self.running_progress.take() {
            progress.cancel();
        }
        if let Some((_, rx)) = self.running.take() {
            self.detached.push(rx);
        }
        self.running_tool = false;
        self.running_stream_key = None;
    }
}

/// Joint motion can stream successful intermediate poses while values change. Include
/// every other input, the document revision and colours so a different joint, changed
/// limits, feature dialog or document cannot reuse an obsolete pose.
fn motion_key(cmds: &[(String, Value)], revision: u64, colors: Colors) -> Option<u64> {
    if !cmds.iter().any(|(c, p)| c == "joint.drive" || c == "joint.create" && p.get("values").is_some())
        || !cmds.iter().all(|(c, _)| {
            matches!(
                c.as_str(),
                "joint.drive"
                    | "joint.create"
                    | "joint.as_built"
                    | "joint.edit"
                    | "joint.limits"
                    | "joint.gear"
                    | "joint.rack_pinion"
                    | "joint.screw"
                    | "joint.cam"
            )
        })
    {
        return None;
    }
    let mut setup = cmds.to_vec();
    for (command, params) in &mut setup {
        if matches!(command.as_str(), "joint.drive" | "joint.create")
            && let Some(p) = params.as_object_mut()
        {
            p.remove("values");
            if command == "joint.drive" {
                p.remove("value");
            }
        }
    }
    let mut h = std::collections::hash_map::DefaultHasher::new();
    serde_json::to_string(&setup).ok()?.hash(&mut h);
    revision.hash(&mut h);
    format!("{colors:?}").hash(&mut h);
    Some(h.finish())
}

/// Tool previews already stream in `poll`. New-body extrusions, face offsets, fillets and
/// chamfers instead build their full result; allow successful intermediate distances (a
/// fillet's radii, a chamfer's distances and angle) only within the same setup. Preserve selections, feature identity, extent mode, revision and colours. The
/// exact job key still ensures the latest distance is computed and errors never go stale.
fn stream_key(cmds: &[(String, Value)], revision: u64, colors: Colors) -> Option<u64> {
    if let Some(key) = motion_key(cmds, revision, colors) {
        return Some(key);
    }
    if cmds.is_empty() || as_new_body(cmds).is_some() {
        return None;
    }
    let mut setup = cmds.to_vec();
    for (command, params) in &mut setup {
        let (command, params) = if command == "timeline.redefine" {
            let command = params.get("command")?.as_str()?.to_string();
            (command, params.get_mut("params")?)
        } else {
            (command.clone(), params)
        };
        let fields: &[&str] = match command.as_str() {
            "solid.extrude" => &["distance", "distance2", "to_offset"],
            "solid.offset_face" => &["distance"],
            // A radius dragged or typed: each result shows while the next computes, rather than
            // the preview holding still until the input stops changing.
            "solid.fillet" => &["radius", "radius2", "mid"],
            "solid.chamfer" => &["distance", "distance2", "angle"],
            _ => return None,
        };
        let params = params.as_object_mut()?;
        for field in fields {
            // Keep presence: changing extent mode must not reuse the old result.
            if let Some(value) = params.get_mut(*field) {
                *value = Value::Null;
            }
        }
    }
    Some(job_key(&setup, revision, colors))
}

/// Keep the preview in step with the open dialog: start a computation when its inputs changed,
/// take finished results. Called once per frame.
pub fn update(app: &mut SolveApp, ctx: &egui::Context) {
    let cmds = match app.dialog.as_ref().filter(|d| d.previews() && d.preview_enabled).map(|d| crate::dialogs::preview_commands(app, d)) {
        Some(Ok(c)) if !c.is_empty() => c,
        _ if crate::sketch_attachment_ui::commands(app).is_some() => crate::sketch_attachment_ui::commands(app).unwrap_or_default(),
        // A parameter being edited in Change Parameters.
        _ if app.params.as_ref().and_then(|p| p.preview_commands()).is_some() => {
            app.params.as_ref().and_then(|p| p.preview_commands()).unwrap_or_default()
        }
        // No dialog, or inputs still missing: nothing to preview.
        _ => {
            app.preview.clear();
            return;
        }
    };
    let mut colors = Colors::for_tool(&Tokens::get(), &app.ui.tab, app.tool.is_some() || app.dialog.is_some());
    if app.dialog.as_ref().is_some_and(|d| matches!(d.kind, crate::dialogs::Kind::Assembly(crate::dialogs_assembly::Asm::Drive { .. }))) {
        colors.ghost = app.preferences.drive_joints_ghost;
    }
    colors.tint_new = app.preferences.preview_tint_new_faces;
    colors.cut_tint = app.preferences.cut_preview_tint;
    let key = job_key(&cmds, app.session.revision, colors);
    let stream_key = stream_key(&cmds, app.session.revision, colors);
    let tool = as_new_body(&cmds).is_some();
    let pv = &mut app.preview;
    pv.poll(key, stream_key);
    if pv.running.is_none() && pv.shown_key != Some(key) {
        pv.started = crate::now_ms();
        pv.running_stream_key = stream_key;
        let mut straight = std::mem::take(&mut app.preview.straight);
        let mut job = straight.compute(app, &cmds, colors);
        app.preview.straight = straight;
        if job.is_none() {
            let mut moving = std::mem::take(&mut app.preview.moving);
            job = moving.compute(app, &cmds, colors);
            app.preview.moving = moving;
        }
        if let Some(job) = job {
            app.preview.apply(key, job);
            app.preview.busy = false;
            return;
        }
        let scratch = app.session.scratch();
        spawn(&mut app.preview, key, scratch, cmds, colors, tool, Arc::new(Progress::default()));
    }
    app.preview.busy = app.preview.running.is_some();
    if app.preview.busy {
        ctx.request_repaint_after(std::time::Duration::from_millis(16));
    }
}

/// What tells one preview computation from another: its commands, the document revision they
/// run on and the colours.
fn job_key(cmds: &[(String, Value)], revision: u64, colors: Colors) -> u64 {
    let mut h = std::collections::hash_map::DefaultHasher::new();
    serde_json::to_string(cmds).unwrap_or_default().hash(&mut h);
    revision.hash(&mut h);
    format!("{colors:?}").hash(&mut h);
    h.finish()
}

/// Start computing the preview of `cmds`: for a tool feature (`tool`), only its tool.
#[cfg(not(target_arch = "wasm32"))]
fn spawn(pv: &mut PreviewState, key: u64, scratch: Session, cmds: Vec<(String, Value)>, colors: Colors, tool: bool, progress: Arc<Progress>) {
    let (tx, rx) = std::sync::mpsc::channel();
    // Watched by `progress`, so a job left behind for other inputs skips its remaining features.
    let watched = progress.clone();
    let spawned = std::thread::Builder::new().name("preview".into()).spawn(move || {
        let job = scratch.stop_token().within(|| {
            solidcraft_engine::doc::progress::watch(watched, || {
                if tool { compute_tool(&scratch, &cmds, colors) } else { compute(&scratch, &cmds, colors) }
            })
        });
        let _ = tx.send(job);
    });
    match spawned {
        Ok(_) => {
            pv.running = Some((key, rx));
            pv.running_progress = Some(progress);
            pv.running_tool = tool;
        }
        Err(e) => pv.apply(key, Err(e.to_string())),
    }
}

/// No threads on the web: compute right away.
#[cfg(target_arch = "wasm32")]
fn spawn(pv: &mut PreviewState, key: u64, scratch: Session, cmds: Vec<(String, Value)>, colors: Colors, tool: bool, _progress: Arc<Progress>) {
    pv.apply(key, if tool { compute_tool(&scratch, &cmds, colors) } else { compute(&scratch, &cmds, colors) });
}

/// Evaluate the commands on the scratch session and build the preview scene.
/// Features that add or remove a tool body (extrude, revolve, primitives with join, cut or
/// intersect) show the tool itself: tinted for join, see-through red for cut. Other features
/// show their result in place of the bodies they change. When the feature fails (a boolean the
/// kernel can't do yet), the tool body is still shown, with the error.
pub fn compute(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job {
    if cmds.iter().any(|(command, params)| {
        matches!(
            command.as_str(),
            "sketch.redefine"
                | "sketch.mirror"
                | "sketch.pattern.rectangular"
                | "sketch.pattern.circular"
                | "sketch.offset"
                | "sketch.point.divide"
                | "sketch.fillet"
                | "sketch.chamfer.equal_distance"
                | "sketch.chamfer.two_distance"
                | "sketch.chamfer.distance_angle"
                | "sketch.insert_svg"
                | "sketch.insert_dxf"
        ) || command == "construct.plane.along_path"
            || params.get("command").and_then(Value::as_str) == Some("construct.plane.along_path")
    }) {
        let preview = s.preview_visible_world(cmds).map_err(|error| error.to_string())?;
        return std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let mut built = build(&preview.before, &preview.after, colors);
            built.bounds = state_bounds(&preview.after);
            built
        }))
        .map_err(|_| "the preview failed".to_string());
    }
    // Joints and drives move occurrences: shown on the placed model.
    if let Some(job) = crate::dialogs_assembly::world_preview(s, cmds, colors) {
        return job;
    }
    match as_new_body(cmds) {
        Some(_) => {
            let mut b = compute_tool(s, cmds, colors)?;
            b.error = s.preview(cmds).err().map(|e| e.to_string());
            Ok(b)
        }
        None => {
            let p = s.preview(cmds).map_err(|e| e.to_string())?;
            let guard =
                |f: &dyn Fn() -> Built| std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).map_err(|_| "the preview failed".to_string());
            let mut b = guard(&|| build(&p.before, &p.after, colors))?;
            b.bounds = state_bounds(&p.after);
            // The radius a previewed fillet used when it reduced the one asked for (a new
            // fillet or an edited one: the feature the replies name).
            let features = || p.replies.iter().filter_map(|r| r.get("feature").and_then(Value::as_u64));
            b.applied = features().find_map(|id| p.after.fitted_range(id));
            b.tapered = features().find_map(|id| p.after.tapered(id)).map(<[_]>::to_vec).unwrap_or_default();
            Ok(b)
        }
    }
}

/// The tool body of a feature that adds or removes one, built on its own (as a new body): what
/// its live preview shows, without the boolean. When even the tool fails, the feature's own
/// error. Other features: their whole preview ([`compute`]).
fn compute_tool(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job {
    let Some((alt, op)) = as_new_body(cmds) else { return compute(s, cmds, colors) };
    // Where the view shows it: in a component placed away from its own frame (an imported
    // part stood upright) the model's frame would draw the tool at right angles to the face.
    let tool = match s.preview_world(&alt) {
        Ok(t) => t,
        Err(e) => return Err(s.preview(cmds).err().map_or_else(|| e.to_string(), |r| r.to_string())),
    };
    let mut b = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| build_tool(&tool.before, &tool.after, colors, &op)))
        .map_err(|_| "the preview failed".to_string())?;
    b.bounds = state_bounds(&tool.after);
    Ok(b)
}

fn extrusion_params<'a>(id: &str, params: &'a Value) -> Option<&'a Value> {
    match id {
        "solid.extrude" => Some(params),
        "timeline.redefine" if params.get("command").and_then(Value::as_str) == Some("solid.extrude") => params.get("params"),
        _ => None,
    }
}

/// Check references without intersecting the tool with the target's geometry.
pub(crate) fn extrusion_target_error(s: &Session, cmds: &[(String, Value)], before: &ModelState) -> Option<String> {
    for (id, params) in cmds {
        let params = extrusion_params(id, params)?;
        let op = params.get("operation").and_then(Value::as_str).unwrap_or("new");
        if op == "new" {
            continue;
        }
        let targets = params
            .get("targets")
            .and_then(Value::as_array)
            .filter(|targets| !targets.is_empty())
            .or_else(|| params.get("participants").and_then(Value::as_array));
        let mut found = false;
        if let Some(targets) = targets.filter(|targets| !targets.is_empty()) {
            for target in targets.iter().filter_map(Value::as_str) {
                if target == "*" {
                    found |= !before.bodies.is_empty();
                } else if before.body(target).is_some() {
                    found = true;
                } else {
                    return Some(format!("unknown body `{target}`"));
                }
            }
        } else {
            // A cut or intersect from a component with no body of its own takes part in every
            // body: the command decides that (#1234), and the preview asks it the same (#3085).
            found = params.get("faces").and_then(|_| params.get("face_body")).and_then(Value::as_str).map_or_else(
                || {
                    before.bodies.iter().any(|body| s.doc.model_body_component(body) == s.active_component)
                        || (matches!(op, "cut" | "intersect") && s.cut_takes_every_body(before))
                },
                |name| crate::open_meshing::session_world(s).body(name).is_some(),
            );
        }
        if !found && matches!(op, "cut" | "intersect") {
            return Some("there is no body to cut or intersect".into());
        }
    }
    None
}

fn state_bounds(st: &ModelState) -> Aabb3 {
    st.bodies.iter().fold(Aabb3::EMPTY, |b, x| b.union(&x.mesh().bounds()))
}

/// The tool bodies a join/cut/intersect adds: opaque and tinted for join, see-through for cut
/// (red) and intersect.
fn build_tool(before: &ModelState, after: &ModelState, c: Colors, op: &str) -> Built {
    let mut sc = GpuScene::default();
    let removed = sc.color(c.removed);
    let intersect = sc.color([c.added[0], c.added[1], c.added[2], c.removed[3]]);
    let added = sc.color(c.added);
    let edge = if op == "cut" { sc.color([c.removed[0], c.removed[1], c.removed[2], 255]) } else { sc.color(c.edge) };
    for a in after.bodies.iter().filter(|a| before.body(&a.name).is_none()) {
        let m = a.mesh();
        for t in &m.triangles {
            for k in t {
                let i = *k as usize;
                if let (Some(p), Some(n)) = (m.positions.get(i), m.normals.get(i)) {
                    match op {
                        "cut" if !c.cut_tint => {}
                        "cut" => sc.xray_tri(p.to_f32(), n.to_f32(), removed),
                        "intersect" => sc.xray_tri(p.to_f32(), n.to_f32(), intersect),
                        _ => sc.tri(p.to_f32(), n.to_f32(), added),
                    }
                }
            }
        }
        for (ei, e) in m.edges.iter().enumerate() {
            if m.shaded_edge_visible(ei) {
                for w in e.windows(2) {
                    sc.line(w[0].to_f32(), w[1].to_f32(), edge, 1.3, op != "join");
                }
            }
        }
    }
    Built {
        scene: sc,
        sketches: Vec::new(),
        replaced: Vec::new(),
        error: None,
        bounds: Aabb3::EMPTY,
        applied: None,
        tapered: Vec::new(),
        stopped: None,
    }
}

/// The same commands making a new body instead of joining, cutting or intersecting, and the
/// operation they had.
fn as_new_body(cmds: &[(String, Value)]) -> Option<(Vec<(String, Value)>, String)> {
    let mut out = cmds.to_vec();
    let mut changed: Option<String> = None;
    for (command, p) in &mut out {
        let inner = p.get("command").and_then(Value::as_str).map(str::to_string);
        // A combine's operation is between bodies that already exist: it has no new-body form
        // (and no tool body of its own to show).
        if inner.as_deref().unwrap_or(command.as_str()) == "solid.combine" {
            continue;
        }
        let p = if p.get("command").is_some() { p.get_mut("params")? } else { p };
        if let Some(op) = p.get_mut("operation")
            && let Some(o) = op.as_str().filter(|o| *o != "new").map(str::to_string)
        {
            *op = Value::from("new");
            changed = Some(o);
        }
    }
    changed.map(|op| (out, op))
}

fn same(a: &Mesh, b: &Mesh) -> bool {
    a.triangles.len() == b.triangles.len() && a.positions == b.positions
}

/// The preview scene: changed and new bodies (new surface tinted) and, as a ghost, the old
/// version of changed or removed bodies.
pub fn build(before: &ModelState, after: &ModelState, c: Colors) -> Built {
    let mut sc = GpuScene::default();
    let added_color = sc.color(c.added);
    let added_plane = sc.color([c.added[0], c.added[1], c.added[2], 64]);
    let body_color = sc.color(c.body);
    let new_body = sc.color(c.new_body);
    let removed = sc.color(c.removed);
    let edge = sc.color(c.edge);
    let mut replaced = Vec::new();
    for construct in &after.construct {
        if before.construct.iter().any(|old| old == construct) {
            continue;
        }
        if let solidcraft_engine::doc::construct::ConstructGeom::Plane(plane) = construct.geom {
            let (o, x, y) = (plane.origin, plane.x * 10.0, plane.y * 10.0);
            let quad = [o - x - y, o + x - y, o + x + y, o - x + y];
            for triangle in [[quad[0], quad[1], quad[2]], [quad[0], quad[2], quad[3]]] {
                for p in triangle {
                    sc.xray_tri(p.to_f32(), plane.normal().to_f32(), added_plane);
                }
            }
            for (a, b) in Ring::from(quad).edges() {
                sc.line(a.to_f32(), b.to_f32(), added_color, 1.3, true);
            }
        }
    }
    for a in &after.bodies {
        let am = a.mesh();
        let old = before.body(&a.name).map(|b| b.mesh());
        if old.as_ref().is_some_and(|bm| same(bm, &am)) {
            continue;
        }
        // Material removed from a body (a hole, a pocket): its new faces also show through the
        // body in the cut colour, so the cut is seen from any side.
        let cut = c.cut_tint && old.as_ref().is_some_and(|bm| am.measure().volume < bm.measure().volume - 1e-6);
        // Which of the body's faces the feature made, asked only when something is drawn from
        // the answer: the cut above, and the tint the preference offers. A changed body is
        // otherwise drawn as itself, so a preview looks like the model and nothing has to be
        // worked out face by face.
        let new_face: Vec<bool> = match &old {
            Some(bm) if cut || c.tint_new => {
                let mut new = new_faces(&am, am.tri_face.iter().max().map_or(0, |m| *m as usize + 1), bm);
                // The kernel recorded which faces the operation that made this body generated,
                // i.e. has no face of an input under it. The two answers are taken together
                // because each misses something on its own: geometry cannot see a round small
                // beside the part, which lies within its tolerance of the faces it is tangent
                // to all along both of its sides, while the record may be of a later operation
                // than the one previewed (a parameter change re-evaluates the design, and a
                // body keeps the history of whatever last made it), so the record only ever
                // adds faces to the answer.
                if let Some(history) = a.body.history() {
                    for (is_new, face) in new.iter_mut().zip(&history.faces) {
                        *is_new |= face.change == Change::Generated;
                    }
                }
                new
            }
            _ => Vec::new(),
        };
        let kept = if old.is_some() { body_color } else { new_body };
        for (t, f) in am.triangles.iter().zip(&am.tri_face) {
            let new = new_face.get(*f as usize).copied().unwrap_or(false);
            let col = if new && c.tint_new { added_color } else { kept };
            let through = cut && new;
            for k in t {
                let i = *k as usize;
                if let (Some(p), Some(n)) = (am.positions.get(i), am.normals.get(i)) {
                    sc.tri(p.to_f32(), n.to_f32(), col);
                    if through {
                        sc.xray_tri(p.to_f32(), n.to_f32(), removed);
                    }
                }
            }
        }
        for (ei, e) in am.edges.iter().enumerate() {
            if !am.shaded_edge_visible(ei) {
                continue;
            }
            for w in e.windows(2) {
                sc.line(w[0].to_f32(), w[1].to_f32(), edge, 1.3, false);
            }
        }
        if old.is_some() {
            replaced.push(a.name.clone());
        }
    }
    for b in &before.bodies {
        let gone = after.body(&b.name).is_none();
        if !gone && !replaced.contains(&b.name) {
            continue;
        }
        let m = b.mesh();
        if c.cut_tint {
            // A changed body's surface it keeps is not removed material: drawn as ghost there it
            // lies on the new body's face, and where that face is curved and meshed anew (its
            // boundary moved) the two triangulations cross by up to their chord error, far past the
            // ghost's depth bias, so the ghost showed through the new face in slivers.
            let kept = if gone { None } else { after.body(&b.name).and_then(|a| SurfaceIndex::new(&a.mesh())) };
            for t in &m.triangles {
                let Some(corners) = m.tri(t) else { continue };
                if kept.as_ref().is_some_and(|k| on_surface(k, corners)) {
                    continue;
                }
                for k in t {
                    let i = *k as usize;
                    if let (Some(p), Some(n)) = (m.positions.get(i), m.normals.get(i)) {
                        sc.ghost_tri(p.to_f32(), n.to_f32(), removed);
                    }
                }
            }
        }
        if gone {
            replaced.push(b.name.clone());
        }
    }
    let sketches = after.sketches.iter().filter_map(|sketch| sketch_changes(before.sketch(sketch.feature), sketch)).collect();
    Built { scene: sc, sketches, replaced, error: None, bounds: Aabb3::EMPTY, applied: None, tapered: Vec::new(), stopped: None }
}

/// Does the triangle lie on the surface `index` holds: its corners, the middles of its sides
/// and its centroid all on it?
fn on_surface(index: &SurfaceIndex, [a, b, c]: [Vec3; 3]) -> bool {
    [a, b, c, (a + b) / 2.0, (b + c) / 2.0, (c + a) / 2.0, (a + b + c) / 3.0].into_iter().all(|p| index.contains(p))
}

/// Points of one face the vote in [`new_faces`] asks about: every centroid of a face with no
/// more than this many triangles, so its answer doesn't ride on where that face's rows of
/// facets happen to fall, and a spread sample of a larger one. The lookups per face stay
/// bounded, so a modeled thread's thousands of faces still cost little.
const FACE_SAMPLES: usize = 24;

/// What of `sketch` differs from `old`, the same sketch before a change: the curves whose shape
/// or points moved and the points that moved (`None`: nothing did).
pub(crate) fn sketch_changes(
    old: Option<&solidcraft_engine::doc::SolvedSketch>,
    sketch: &std::sync::Arc<solidcraft_engine::doc::SolvedSketch>,
) -> Option<SketchPreview> {
    let old_curves = old.into_iter().flat_map(|s| s.sketch.curves.iter().map(|c| (c.id.as_str(), c))).collect::<std::collections::HashMap<_, _>>();
    let old_points =
        old.into_iter().flat_map(|s| s.sketch.points.iter().map(|p| (p.id.as_str(), p.pos))).collect::<std::collections::HashMap<_, _>>();
    let indices = sketch
        .sketch
        .curves
        .iter()
        .enumerate()
        .filter_map(|(index, curve)| {
            let unchanged = old_curves.get(curve.id.as_str()).is_some_and(|previous| {
                previous.kind == curve.kind && curve.kind.point_ids().iter().all(|i| old.and_then(|s| s.sketch.point(*i)) == sketch.sketch.point(*i))
            });
            if unchanged { None } else { Some(index) }
        })
        .collect::<Vec<_>>();
    let points = sketch
        .sketch
        .points
        .iter()
        .enumerate()
        .filter_map(|(i, p)| if old_points.get(p.id.as_str()) == Some(&p.pos) { None } else { Some(i) })
        .collect::<Vec<_>>();
    if indices.is_empty() && points.is_empty() { None } else { Some(SketchPreview { sketch: sketch.clone(), curves: indices, points }) }
}

/// Which of the `nf` faces of `m` are new surface, i.e. not on the old mesh? Each is decided by
/// a vote of points on it (triangle centroids spread over the face). One pass over `m`'s
/// triangles gathers the centroids, and the old mesh is indexed once, so the cost stays
/// near-linear however many faces there are (a modeled thread has thousands).
fn new_faces(m: &Mesh, nf: usize, old: &Mesh) -> Vec<bool> {
    let mut cents: Vec<Vec<Vec3>> = vec![Vec::new(); nf];
    for (t, f) in m.triangles.iter().zip(&m.tri_face) {
        if let (Some(face), Some([a, b, c])) = (cents.get_mut(*f as usize), m.tri(t)) {
            face.push((a + b + c) / 3.0);
        }
    }
    let index = SurfaceIndex::new(old);
    cents
        .iter()
        .map(|cents| {
            let Some(index) = &index else { return true };
            if cents.is_empty() {
                return true;
            }
            let samples: Vec<&Vec3> = if cents.len() <= FACE_SAMPLES {
                cents.iter().collect()
            } else {
                // Spread by the golden ratio rather than at a fixed step: a face's triangles
                // come in rows across its curve, and a step that divides the row length would
                // ask about the same place in every row (a blend's tangent sides, say), so two
                // alike faces could be told apart by nothing but how long they are.
                let len = cents.len() as f64;
                (0..FACE_SAMPLES).filter_map(|i| cents.get(((i as f64 * 0.618_033_988_75).fract() * len) as usize)).collect()
            };
            let off = samples.iter().filter(|p| !index.contains(***p)).count();
            // More than an eighth of the face off the old surface is new surface. A blend meets
            // the faces it rounds tangentially, so the rows of facets along both of its sides
            // lie within the tolerance of the old surface and only the band through its middle
            // is clearly off: a small round on a large part never reaches the third the vote
            // asked for before, and was drawn as if it had always been there.
            off * 8 > samples.len()
        })
        .collect()
}

#[cfg(test)]
thread_local! {
    /// The triangles [`SurfaceIndex::contains`] measured a point against, on this thread: the
    /// work classifying each face against every triangle made quadratic, counted for the test that
    /// checks it stays near linear, whatever else the machine is doing (#9901).
    static TRIANGLES_MEASURED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

/// The triangles of a mesh in a uniform grid, to ask whether a point lies on the surface (within
/// its tessellation tolerance) by looking only at the triangles near it.
struct SurfaceIndex {
    tris: Vec<[Vec3; 3]>,
    tol: f64,
    lo: [f64; 3],
    cell: [f64; 3],
    res: usize,
    cells: Vec<Vec<u32>>,
}

impl SurfaceIndex {
    /// Most triangle-in-cell entries the grid may hold; a coarser grid is used beyond it.
    const BUDGET: usize = 4_000_000;

    /// None for a mesh with no surface.
    fn new(m: &Mesh) -> Option<SurfaceIndex> {
        let bb = m.bounds();
        if bb.is_empty() {
            return None;
        }
        let tol = (bb.diagonal() * 2e-3).max(1e-4);
        let tris: Vec<[Vec3; 3]> = m.triangles.iter().filter_map(|t| m.tri(t)).collect();
        let lo = [bb.min.x - tol, bb.min.y - tol, bb.min.z - tol];
        let ext = [bb.max.x - bb.min.x + 2.0 * tol, bb.max.y - bb.min.y + 2.0 * tol, bb.max.z - bb.min.z + 2.0 * tol];
        let mut res = ((tris.len() as f64).cbrt().round() as usize).clamp(1, 48);
        loop {
            let cell = [ext[0] / res as f64, ext[1] / res as f64, ext[2] / res as f64];
            let mut index = SurfaceIndex { tris: Vec::new(), tol, lo, cell, res, cells: vec![Vec::new(); res * res * res] };
            let mut entries = 0usize;
            'fill: for (i, t) in tris.iter().enumerate() {
                let (a, b) = index.range(t);
                for x in a[0]..=b[0] {
                    for y in a[1]..=b[1] {
                        for z in a[2]..=b[2] {
                            entries += 1;
                            if entries > Self::BUDGET && res > 1 {
                                break 'fill;
                            }
                            if let Some(c) = index.cells.get_mut((x * res + y) * res + z) {
                                c.push(i as u32);
                            }
                        }
                    }
                }
            }
            if entries <= Self::BUDGET || res == 1 {
                index.tris = tris;
                return Some(index);
            }
            res /= 2;
        }
    }

    fn cell_of(&self, v: f64, axis: usize) -> usize {
        let (lo, cell) = (self.lo.get(axis).copied().unwrap_or(0.0), self.cell.get(axis).copied().unwrap_or(1.0));
        let k = ((v - lo) / cell).floor();
        if k.is_finite() { (k.max(0.0) as usize).min(self.res - 1) } else { 0 }
    }

    /// The cells (inclusive corners) a triangle, grown by the tolerance, touches.
    fn range(&self, t: &[Vec3; 3]) -> ([usize; 3], [usize; 3]) {
        let (mut a, mut b) = ([0; 3], [0; 3]);
        for axis in 0..3 {
            let c = t.map(|p| [p.x, p.y, p.z][axis]);
            let (lo, hi) = (c[0].min(c[1]).min(c[2]) - self.tol, c[0].max(c[1]).max(c[2]) + self.tol);
            a[axis] = self.cell_of(lo, axis);
            b[axis] = self.cell_of(hi, axis);
        }
        (a, b)
    }

    /// Does `p` lie on the surface?
    fn contains(&self, p: Vec3) -> bool {
        let (x, y, z) = (self.cell_of(p.x, 0), self.cell_of(p.y, 1), self.cell_of(p.z, 2));
        let Some(cell) = self.cells.get((x * self.res + y) * self.res + z) else { return false };
        cell.iter().filter_map(|i| self.tris.get(*i as usize)).any(|[a, b, c]| {
            #[cfg(test)]
            TRIANGLES_MEASURED.with(|n| n.set(n.get() + 1));
            point_triangle_dist(p, *a, *b, *c) < self.tol
        })
    }
}

pub fn point_triangle_dist(p: Vec3, a: Vec3, b: Vec3, c: Vec3) -> f64 {
    let n = (b - a).cross(c - a);
    let Some(nn) = n.normalized() else { return f64::INFINITY };
    let h = (p - a).dot(nn);
    let q = p - nn * h;
    let inside = [(a, b), (b, c), (c, a)].iter().all(|(u, v)| (*v - *u).cross(q - *u).dot(n) >= -1e-12);
    if inside { h.abs() } else { [(a, b), (b, c), (c, a)].iter().map(|(u, v)| p.dist_to_segment(*u, *v)).fold(f64::INFINITY, f64::min) }
}

/// The same computed sketch preview is drawn over both native GPU and CPU viewports.
pub fn paint_sketches(app: &SolveApp, painter: &egui::Painter, proj: &crate::viewport::Proj) {
    if app.preview.rolled_back {
        return;
    }
    if !app
        .dialog
        .as_ref()
        .is_some_and(|d| matches!(d.kind, crate::dialogs::Kind::SketchModify(_) | crate::dialogs::Kind::InsertDrawing(_)) && d.previews())
    {
        return;
    }
    let mut budget = solidcraft_engine::render::SamplingBudget::new(32_768, 65_536);
    let curves: usize = app.preview.sketches.iter().map(|preview| paint_sketch(preview, painter, proj, &mut budget)).sum();
    crate::scenario::publish_count("sketch_preview_curves", curves as f64);
}

/// Draw what a change moves in a sketch, its curves and points, in the preview colour; returns
/// how many curves it drew.
pub(crate) fn paint_sketch(
    preview: &SketchPreview,
    painter: &egui::Painter,
    proj: &crate::viewport::Proj,
    budget: &mut solidcraft_engine::render::SamplingBudget,
) -> usize {
    let stroke = egui::Stroke::new(2.5, Tokens::get().preview_sketch);
    let sketch = &preview.sketch;
    for index in &preview.curves {
        crate::tools::paint_curve(&sketch.sketch, *index, &sketch.plane, painter, proj, stroke, budget);
    }
    for index in &preview.points {
        if let Some(point) = sketch.sketch.point(*index).and_then(|p| proj.to_screen(sketch.plane.to_world(p))) {
            painter.circle_filled(point, 3.0, stroke.color);
        }
    }
    preview.curves.len()
}

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

    #[test]
    fn tool_preview_follows_tab_without_recolouring_removal_or_idle_previews() {
        for dark in [false, true] {
            for vision in crate::theme::ColorVision::ALL {
                let t = Tokens::themed(dark, vision);
                let base = Colors::from_tokens(&t);
                for tab in ["MODEL", "SKETCH", "SURFACE", "ASSEMBLE", "INSERT", "INSPECT", "MANAGE", "VIEW", "SHEET METAL", "RENDER"] {
                    for active in [false, true] {
                        let colors = Colors::for_tool(&t, tab, active);
                        assert_eq!(colors.removed, base.removed);
                        assert_eq!(colors.body, base.body);
                        assert_eq!(colors.edge, base.edge);
                        if !active || crate::theme::TabAccent::for_tab(&t, tab).color == t.accent {
                            assert_eq!(colors.added, base.added);
                            assert_eq!(colors.new_body, base.new_body);
                        } else {
                            let accent = crate::theme::TabAccent::for_tab(&t, tab);
                            assert_eq!(colors.added, accent.color.to_srgba_unmultiplied());
                            assert_ne!(colors.new_body, colors.added);
                        }
                    }
                }
            }
        }
    }

    fn colors() -> Colors {
        Colors {
            body: [1, 1, 1, 255],
            added: [2, 2, 2, 255],
            new_body: [4, 4, 4, 255],
            removed: [3, 3, 3, 100],
            edge: [0, 0, 0, 255],
            ghost: true,
            tint_new: true,
            cut_tint: true,
        }
    }

    #[test]
    fn correcting_inputs_clears_old_errors_and_discards_late_old_jobs() {
        let mut pv = PreviewState {
            shown_key: Some(1),
            error: Some("unknown_angle".into()),
            active: true,
            replaced: vec!["Turn".into()],
            key: 9,
            ..PreviewState::default()
        };
        let (tx, rx) = std::sync::mpsc::channel();
        pv.running = Some((1, rx));
        pv.poll(2, None);
        assert!(pv.error.is_none(), "the corrected inputs can Apply while their preview computes");
        assert!(pv.running.is_some());
        assert!(pv.active);
        assert_eq!(pv.replaced, ["Turn"]);
        assert_eq!(pv.key, 9, "the last good geometry stays visible");
        tx.send(Err("unknown_angle".into())).unwrap();
        pv.poll(2, None);
        assert!(pv.error.is_none(), "a late obsolete result cannot disable Apply again");
        assert!(pv.running.is_none());
        assert_eq!(pv.shown_key, Some(1));
        assert_eq!(pv.key, 9);

        let (tx, rx) = std::sync::mpsc::channel();
        pv.running = Some((2, rx));
        tx.send(Err("current invalid expression".into())).unwrap();
        pv.poll(2, None);
        assert_eq!(pv.error.as_deref(), Some("current invalid expression"));
        assert_eq!(pv.shown_key, Some(2));
        assert!(!pv.active && pv.replaced.is_empty(), "the current inputs failed: nothing is previewed (#3254)");
        assert_ne!(pv.key, 9, "the viewport takes the empty scene");
        pv.poll(2, None);
        assert_eq!(pv.error.as_deref(), Some("current invalid expression"), "current errors still prevent invalid Apply");
    }

    fn pose() -> Built {
        Built {
            scene: GpuScene::default(),
            sketches: Vec::new(),
            replaced: vec!["Lid".into()],
            error: None,
            bounds: Aabb3::EMPTY,
            applied: None,
            tapered: Vec::new(),
            stopped: None,
        }
    }

    /// The worker completes while the slider is still held or Play keeps advancing.
    /// Intermediate successful poses must reach the viewport, and the final value
    /// must still be computed rather than treating that intermediate pose as current.
    #[test]
    fn joint_motion_shows_completed_poses_while_inputs_keep_advancing() {
        let mut pv = PreviewState::default();
        for (running, wanted) in [(1, 2), (2, 3), (3, 4)] {
            let (tx, rx) = std::sync::mpsc::channel();
            pv.running = Some((running, rx));
            pv.running_stream_key = Some(42);
            pv.poll(wanted, Some(42));
            assert!(pv.running.is_some());
            tx.send(Ok(pose())).unwrap();
            pv.poll(wanted, Some(42));
            assert!(pv.active, "motion should be visible before input stops");
            assert_eq!(pv.replaced, ["Lid"]);
            assert_eq!(pv.key, running, "every completed pose reaches the scene");
            assert_eq!(pv.shown_key, Some(running));
            assert_ne!(pv.shown_key, Some(wanted), "the latest pose still needs a job");
        }
        let (tx, rx) = std::sync::mpsc::channel();
        pv.running = Some((4, rx));
        pv.running_stream_key = Some(42);
        tx.send(Ok(pose())).unwrap();
        pv.poll(4, Some(42));
        assert_eq!(pv.shown_key, Some(4), "release catches up to the exact final pose");
        assert!(pv.running.is_none());
    }

    #[test]
    fn intermediate_motion_rejects_other_setups_and_obsolete_errors() {
        for (context, job) in [
            (None, Ok(pose())),
            (Some(43), Ok(pose())),
            (Some(42), Err("old invalid value".into())),
            (Some(42), Ok(Built { error: Some("old tool preview error".into()), ..pose() })),
        ] {
            let mut pv = PreviewState { running_stream_key: Some(42), ..PreviewState::default() };
            let (tx, rx) = std::sync::mpsc::channel();
            pv.running = Some((1, rx));
            tx.send(job).unwrap();
            pv.poll(2, context);
            assert!(!pv.active);
            assert!(pv.error.is_none(), "obsolete errors must not block Apply");
            assert!(pv.running.is_none());
        }
    }

    #[test]
    fn motion_context_changes_only_when_the_joint_setup_changes() {
        let commands = |angle| {
            vec![
                ("joint.edit".into(), json!({"joint": 1, "flip": true})),
                ("joint.limits".into(), json!({"joint": 1, "min": "-135 deg", "max": "0 deg"})),
                ("joint.drive".into(), json!({"joint": 1, "values": [angle]})),
            ]
        };
        let key = motion_key(&commands("-30 deg"), 7, colors());
        assert!(key.is_some());
        assert_eq!(key, motion_key(&commands("-60 deg"), 7, colors()));
        assert_ne!(key, motion_key(&commands("-60 deg"), 8, colors()));
        let mut different = commands("-60 deg");
        different[2].1["joint"] = json!(2);
        assert_ne!(key, motion_key(&different, 7, colors()));
        different = commands("-60 deg");
        different[1].1["min"] = json!("-90 deg");
        assert_ne!(key, motion_key(&different, 7, colors()));
        different = commands("-60 deg");
        different[0].1["flip"] = json!(false);
        assert_ne!(key, motion_key(&different, 7, colors()));
        let mut other_colors = colors();
        other_colors.body = [9, 9, 9, 255];
        assert_ne!(key, motion_key(&commands("-60 deg"), 7, other_colors));
        assert!(motion_key(&[("solid.extrude".into(), json!({"distance": 10}))], 7, colors()).is_none());
        for command in ["joint.create", "joint.drive"] {
            assert_eq!(
                motion_key(&[(command.into(), json!({"joint": 1, "values": [0]}))], 7, colors()),
                motion_key(&[(command.into(), json!({"joint": 1, "values": [90]}))], 7, colors()),
            );
        }
        // A gear dialog playing its driving joint streams its poses too.
        let gear = |angle: &str| {
            vec![
                ("joint.gear".to_string(), json!({"a": 1, "b": 2, "ra": 10, "rb": 20})),
                ("joint.drive".to_string(), json!({"joint": 1, "values": [angle]})),
            ]
        };
        assert!(motion_key(&gear("0 deg"), 7, colors()).is_some());
        assert_eq!(motion_key(&gear("0 deg"), 7, colors()), motion_key(&gear("90 deg"), 7, colors()));
        // So does a cam dialog turning its cam.
        let cam = |angle: &str| {
            vec![
                ("joint.cam".to_string(), json!({"a": 1, "b": 2, "profile": {"sketch": 3}})),
                ("joint.drive".to_string(), json!({"joint": 1, "values": [angle]})),
            ]
        };
        assert!(motion_key(&cam("0 deg"), 7, colors()).is_some());
        assert_eq!(motion_key(&cam("0 deg"), 7, colors()), motion_key(&cam("90 deg"), 7, colors()));
    }

    #[test]
    fn edited_pattern_preview_includes_moved_instances_only() -> Result<(), Box<dyn std::error::Error>> {
        for (command, initial, edits) in [
            (
                "sketch.pattern.rectangular",
                json!({"entities":["seed"],"count":3,"spacing":10,"count2":2,"spacing2":15}),
                vec![(json!({"spacing":14}), 4), (json!({"spacing2":19}), 3)],
            ),
            (
                "sketch.pattern.circular",
                json!({"entities":["seed"],"center":"origin","count":5}),
                vec![(json!({"count":20}), 19), (json!({"count":2}), 1)],
            ),
        ] {
            let mut s = Session::default();
            s.execute("sketch.create", &json!({"plane":"XY"}))?;
            s.execute("sketch.circle.center", &json!({"center":[-50,0],"radius":2,"id":"seed"}))?;
            let pattern = s.execute(command, &initial)?;
            // An identical result must not draw the seed or any existing copies.
            let unchanged = compute(&s, &[(command.into(), json!({"pattern":pattern["pattern"]}))], colors())?;
            assert!(unchanged.sketches.is_empty());
            for (mut edit, expected) in edits {
                edit["pattern"] = pattern["pattern"].clone();
                let built = compute(&s, &[(command.into(), edit)], colors())?;
                assert_eq!(built.sketches.len(), 1, "{command}");
                for preview in &built.sketches {
                    assert_eq!(preview.curves.len(), expected, "{command}");
                    assert_eq!(preview.points.len(), expected, "{command}");
                    assert!(preview.curves.iter().all(|i| preview.sketch.sketch.curves.get(*i).is_some_and(|c| c.id != "seed")));
                }
            }
        }
        Ok(())
    }

    #[test]
    fn sketch_preview_includes_changed_curve_shapes() -> Result<(), Box<dyn std::error::Error>> {
        let mut s = Session::default();
        s.execute("sketch.create", &json!({"plane":"XY"}))?;
        s.execute("sketch.circle.center", &json!({"center":[0,0],"radius":2,"id":"circle"}))?;
        s.execute("sketch.line", &json!({"points":[[10,0],[20,0]],"ids":["line"]}))?;
        let before = s.model.state();
        let mut after = before.as_ref().clone();
        for sketch in &mut after.sketches {
            let sketch = &mut std::sync::Arc::make_mut(sketch).sketch;
            for curve in &mut sketch.curves {
                match &mut curve.kind {
                    solidcraft_engine::sketch::CurveKind::Circle { r, .. } => *r = 3.0,
                    solidcraft_engine::sketch::CurveKind::Line { a, b } => std::mem::swap(a, b),
                    _ => {}
                }
            }
        }
        let built = build(&before, &after, colors());
        assert_eq!(built.sketches.len(), 1);
        for preview in &built.sketches {
            assert_eq!(preview.curves, vec![0, 1]);
            assert!(preview.points.is_empty());
        }
        Ok(())
    }

    /// Combine previews its result (cut, join or intersect between existing bodies) without an
    /// error, so its dialog's OK is enabled. Before, the preview also tried the command as a new
    /// body, which a combine has no form of, and always reported "operation must be join, cut or
    /// intersect".
    #[test]
    fn combine_previews_its_result_without_an_error() {
        let mut s = solidcraft_engine::Session::default();
        s.execute("solid.box", &json!({"length": 20, "width": 20, "height": 20, "body_name": "Cube"})).unwrap();
        s.execute("solid.cylinder", &json!({"base": [10, 10, -5], "diameter": 6, "height": 30, "body_name": "Pin"})).unwrap();
        for op in ["cut", "join", "intersect"] {
            let cmd = ("solid.combine".to_string(), json!({"target": "Cube", "tools": ["Pin"], "operation": op, "keep_tools": false}));
            let built = compute(&s, &[cmd], colors()).unwrap_or_else(|e| panic!("{op}: {e}"));
            assert!(built.error.is_none(), "{op}: {:?}", built.error);
        }
    }

    /// #7520: a Hole's preview leaves a hidden body out, drawn nor drilled, as OK does; before,
    /// it replaced the hidden body with a drilled one while the dialog was open.
    #[test]
    fn a_hole_preview_leaves_hidden_bodies_out() {
        let mut s = Session::default();
        s.execute("solid.box", &json!({"length": 40, "width": 40, "height": 5, "body_name": "Plate"})).unwrap();
        s.execute("solid.box", &json!({"corner": [0, 0, 5], "length": 40, "width": 40, "height": 20, "operation": "new", "body_name": "Block"}))
            .unwrap();
        s.execute("browser.visibility", &json!({"items": ["Block"], "visible": false})).unwrap();
        let cmd = ("solid.hole".to_string(), json!({"position": [20, 20, 0], "diameter": 5, "depth": 10}));
        let built = compute(&s, &[cmd], colors()).unwrap();
        assert!(built.error.is_none(), "{:?}", built.error);
        assert_eq!(built.replaced, vec!["Plate".to_string()]);
    }

    /// A plate in its own component, stood up by its placement (an imported part set upright)
    /// and edited with that component active: the extrusion's tool is drawn where the plate is
    /// shown, on its top face, not at right angles to it in the component's own frame.
    #[test]
    fn a_tool_on_a_turned_component_previews_where_it_is_shown() {
        let mut s = Session::default();
        let made = s.execute("component.create", &json!({"name": "Plate", "grounded": false})).unwrap();
        s.execute("solid.box", &json!({"length": 20, "width": 8, "height": 30, "corner": [0, 0, -30], "body_name": "Plate"})).unwrap();
        s.execute("occurrence.move", &json!({"occurrence": made["occurrence"], "axis": [1, 0, 0], "angle": "90 deg"})).unwrap();
        s.execute("component.activate", &json!({"component": made["component"], "occurrence": made["occurrence"], "path": [made["occurrence"]]}))
            .unwrap();
        // Shown lying flat: 20 × 30, 8 high. Its top face pulled up 10.
        let cmd = ("solid.extrude".to_string(), json!({"face": [10, 15, 8], "face_body": "Plate", "distance": "10 mm", "operation": "join"}));
        let b = compute_tool(&s, &[cmd], colors()).unwrap().bounds;
        assert!((b.max.z - 18.0).abs() < 1e-6 && (b.max.y - 30.0).abs() < 1e-6, "on top, not at right angles: {b:?}");
    }

    fn count(scene: &GpuScene, bytes: &[u8], stride: usize, col: [u8; 4]) -> usize {
        bytes
            .chunks(stride)
            .filter(|v| {
                let Some(i) = v.get(24..28).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes) else { return false };
                scene.palette.get(crate::gpu::PaletteIndex(i)) == Some(col)
            })
            .count()
    }

    #[test]
    fn a_hole_shows_its_cut_through_the_body() {
        let mut s = solidcraft_engine::Session::default();
        s.execute("solid.box", &json!({"length": 40, "width": 30, "height": 20})).unwrap();
        let p = s.preview(&[("solid.hole".to_string(), json!({"position": [20, 15, 20], "diameter": 5, "depth": 10}))]).unwrap();
        let b = build(&p.before, &p.after, colors());
        // The hole's walls are drawn see-through in the cut colour (seen from any side).
        assert!(count(&b.scene, &b.scene.xray, 28, colors().removed) > 0);
        // A feature that only adds material shows no cut.
        let p = s.preview(&[("solid.box".to_string(), json!({"length": 10, "width": 10, "height": 30, "operation": "join"}))]).unwrap();
        let b = build(&p.before, &p.after, colors());
        assert_eq!(count(&b.scene, &b.scene.xray, 28, colors().removed), 0);
    }

    /// Graphics' Show removed material in red, off: the cut and the ghost of the shape the
    /// feature replaces are left out, and the body itself is still drawn.
    #[test]
    fn removed_material_can_be_left_fully_transparent() {
        let mut s = solidcraft_engine::Session::default();
        s.execute("solid.box", &json!({"length": 40, "width": 30, "height": 20})).unwrap();
        let cmds = [("solid.hole".to_string(), json!({"position": [20, 15, 20], "diameter": 5, "depth": 10}))];
        let p = s.preview(&cmds).unwrap();
        let plain = colors();
        let b = build(&p.before, &p.after, plain);
        assert!(count(&b.scene, &b.scene.ghost, 28, plain.removed) > 0, "the replaced shape is ghosted while the tint is on");
        let quiet = Colors { cut_tint: false, ..plain };
        let b = build(&p.before, &p.after, quiet);
        assert_eq!(count(&b.scene, &b.scene.xray, 28, quiet.removed), 0, "no see-through cut");
        assert_eq!(count(&b.scene, &b.scene.ghost, 28, quiet.removed), 0, "no ghost of the old shape");
        assert!(count(&b.scene, &b.scene.tris, 28, quiet.body) > 0, "the body is still drawn");
        // The tool of a cut is left out too, its outline alone showing where it goes.
        let cmds = [("solid.box".to_string(), json!({"length": 10, "width": 10, "height": 30, "operation": "new"}))];
        let p = s.preview(&cmds).unwrap();
        let shown = build_tool(&p.before, &p.after, plain, "cut");
        assert!(count(&shown.scene, &shown.scene.xray, 28, plain.removed) > 0, "the tool is see-through red while the tint is on");
        let b = build_tool(&p.before, &p.after, quiet, "cut");
        assert_eq!(count(&b.scene, &b.scene.xray, 28, quiet.removed), 0, "no see-through tool");
        assert!(!b.scene.overlay.is_empty(), "the tool's outline still shows where the cut goes");
    }

    fn plate() -> Session {
        let mut s = Session::default();
        for (id, p) in [
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 30]})),
            ("sketch.finish", json!({})),
            ("solid.extrude", json!({"distance": 20})),
        ] {
            s.execute(id, &p).unwrap();
        }
        s
    }

    /// The surface test the preview used to run per face against every triangle.
    fn brute_force_on_surface(m: &Mesh, p: Vec3) -> bool {
        let bb = m.bounds();
        let tol = (bb.diagonal() * 2e-3).max(1e-4);
        m.triangles.iter().filter_map(|t| m.tri(t)).any(|[a, b, c]| point_triangle_dist(p, a, b, c) < tol)
    }

    /// A flat sheet of `n × n` squares, two triangles and one face each, lifted by `z`.
    fn sheet(n: usize, z: f64) -> Mesh {
        let mut m = Mesh::default();
        for i in 0..=n {
            for j in 0..=n {
                m.positions.push(Vec3::new(i as f64, j as f64, z));
                m.normals.push(Vec3::Z);
            }
        }
        for i in 0..n {
            for j in 0..n {
                let a = (i * (n + 1) + j) as u32;
                let (b, c, d) = (a + 1, a + n as u32 + 1, a + n as u32 + 2);
                m.triangles.extend([[a, c, d], [a, d, b]]);
                m.tri_face.extend([(i * n + j) as u32; 2]);
            }
        }
        m
    }

    /// The grid answers as a scan of every triangle does, inside, beside and far from the mesh.
    #[test]
    fn surface_index_matches_a_scan() {
        let m = sheet(12, 0.0);
        let index = SurfaceIndex::new(&m).unwrap();
        for k in 0..2000 {
            let f = k as f64;
            let p = Vec3::new((f * 0.37).rem_euclid(15.0) - 1.5, (f * 0.61).rem_euclid(15.0) - 1.5, ((f * 0.13).rem_euclid(1.0) - 0.5) * 0.05);
            assert_eq!(index.contains(p), brute_force_on_surface(&m, p), "{p:?}");
        }
        assert!(SurfaceIndex::new(&Mesh::default()).is_none());
    }

    /// How many triangles classifying the faces of a sheet of `n × n` squares, against an old
    /// sheet of the same size, measured points against ([`TRIANGLES_MEASURED`]): the work that
    /// used to freeze the app.
    fn classify_sheet(n: usize) -> usize {
        let (old, same, lifted) = (sheet(n, 0.0), sheet(n, 0.0), sheet(n, 5.0));
        let nf = n * n;
        let before = TRIANGLES_MEASURED.with(std::cell::Cell::get);
        let on = new_faces(&same, nf, &old);
        let off = new_faces(&lifted, nf, &old);
        let measured = TRIANGLES_MEASURED.with(std::cell::Cell::get) - before;
        assert!(on.iter().all(|new| !new), "faces on the old surface are not new");
        assert!(off.iter().all(|new| *new), "faces off the old surface are new");
        measured
    }

    /// Thousands of faces (a modeled thread's) are classified at once, not face by face against
    /// every triangle: filleting beside a thread froze the app for minutes.
    ///
    /// The triangles measured are counted, not the time taken, so a busy machine can't fail it
    /// (#9901). 900 faces are classified, then 3 600, four times as many: linear is 4x, the
    /// face-against-triangle cost 16x or more; at most eight.
    #[test]
    fn many_faces_are_classified_quickly() {
        let (small, big) = (classify_sheet(30), classify_sheet(60));
        assert!(small > 0 && big <= 8 * small, "900 faces measured {small} triangles, 3 600 measured {big}");
    }

    /// A fillet preview stands in for the body: the round face is new, the rest is kept, and the
    /// old body is the ghost. The session is unchanged.
    #[test]
    fn fillet_preview_scene() {
        let s = plate();
        let (rev, undo) = (s.revision, s.undo.len());
        let b = compute(&s, &[("solid.fillet".into(), json!({"edges": [[0, 0, 10]], "radius": 3}))], colors()).unwrap();
        assert_eq!(b.replaced, vec!["Body1".to_string()]);
        let added = count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, colors().added);
        let kept = count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, colors().body);
        assert!(added > 0 && kept > 0, "added {added} kept {kept}");
        assert!(!b.scene.ghost.is_empty());
        assert_eq!(s.revision, rev);
        assert_eq!(s.undo.len(), undo);
    }

    /// A fillet running into a boss: the boss's cylinder face keeps its surface but is meshed
    /// anew (the fillet's end moves its boundary), so the ghost leaves it out. Drawn as ghost it
    /// crossed the new triangulation and showed through the face in slivers.
    #[test]
    fn a_fillet_preview_ghosts_only_the_removed_material() {
        let s = session_of(&[
            ("solid.box", json!({"length": 60, "width": 40, "height": 30, "center": [0, 0, 15]})),
            ("solid.cylinder", json!({"radius": 12, "height": 50, "base": [10, -20, 0], "operation": "join"})),
        ]);
        let b = compute(&s, &[("solid.fillet".into(), json!({"edges": [[-16, -20, 30]], "radius": 4}))], colors()).unwrap();
        assert_eq!(b.replaced, vec!["Body1".to_string()]);
        // Ghost triangles on the boss's side, well away from the fillet.
        let on_boss = b.scene.ghost.as_chunks::<28>().0.as_chunks::<3>().0.iter().filter(|t| {
            t.iter().all(|v| {
                let f = |k: usize| f32::from_le_bytes([v[4 * k], v[4 * k + 1], v[4 * k + 2], v[4 * k + 3]]) as f64;
                let (x, y, z) = (f(0), f(1), f(2));
                (((x - 10.0).powi(2) + (y + 20.0).powi(2)).sqrt() - 12.0).abs() < 1e-3 && z > 36.0
            })
        });
        assert_eq!(on_boss.count(), 0, "the boss keeps its surface: no ghost on it");
        assert!(!b.scene.ghost.is_empty(), "the edge the fillet removes is still shown as ghost");
    }

    /// Graphics' Tint new surface in a preview, off: the changed body is drawn as itself, and
    /// no face of it is worked out.
    #[test]
    fn the_tint_on_new_surface_can_be_turned_off() {
        let s = plate();
        let cmds = [("solid.fillet".to_string(), json!({"edges": [[0, 0, 10]], "radius": 3}))];
        let plain = Colors { tint_new: false, ..colors() };
        let b = compute(&s, &cmds, plain).unwrap();
        assert_eq!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, plain.added), 0, "nothing is tinted");
        assert!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, plain.body) > 0, "the body is drawn as itself");
    }

    /// With the tint on, a round far smaller than the part is new surface too. Every facet of
    /// this one lies within the preview's tolerance (half a millimetre here) of the faces it is
    /// tangent to, so no point on it is off the old body and only the kernel's record of what
    /// the fillet generated can tell.
    #[test]
    fn a_small_round_on_a_large_part_is_tinted_as_new() {
        let mut s = Session::default();
        for (id, p) in [
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [200, 150]})),
            ("sketch.finish", json!({})),
            ("solid.extrude", json!({"distance": 10})),
        ] {
            s.execute(id, &p).unwrap();
        }
        // The plate's diagonal is 250 mm, so a point counts as on the old surface within half a
        // millimetre: a 0.5 mm round never leaves it, bulging 0.15 mm at the most.
        let tinted = Colors { tint_new: true, ..colors() };
        let b = compute(&s, &[("solid.fillet".into(), json!({"edges": [[100, 0, 10]], "radius": 0.5}))], tinted).unwrap();
        let added = count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, tinted.added);
        assert!(added > 0, "the round is drawn as new surface, not as part of the old body");
        assert!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, tinted.body) > 0, "the rest of the plate is kept");
    }

    /// A cut shows its tool see-through and replaces nothing.
    #[test]
    fn cut_preview_scene() {
        let mut s = plate();
        s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
        s.execute("sketch.circle.center", &json!({"center": [20, 15], "radius": 5})).unwrap();
        s.execute("sketch.finish", &json!({})).unwrap();
        let b = compute(&s, &[("solid.extrude".into(), json!({"distance": 20, "operation": "cut"}))], colors()).unwrap();
        assert!(b.replaced.is_empty());
        assert!(b.scene.tris.is_empty() && !b.scene.xray.is_empty());
        assert!(b.error.is_none());
    }

    /// A new body is all new surface and replaces nothing; a bad value is an error.
    #[test]
    fn extrude_preview_scene() {
        let mut s = Session::default();
        s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
        s.execute("sketch.circle.center", &json!({"center": [0, 0], "radius": 5})).unwrap();
        s.execute("sketch.finish", &json!({})).unwrap();
        let b = compute(&s, &[("solid.extrude".into(), json!({"distance": "15 mm"}))], colors()).unwrap();
        assert!(b.replaced.is_empty());
        assert_eq!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, colors().body), 0);
        // #1805: a new body has its own colour, not a join's.
        assert!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, colors().new_body) > 0);
        assert_eq!(count(&b.scene, &b.scene.tris, crate::gpu::TRI_SIZE, colors().added), 0);
        assert!(b.scene.ghost.is_empty());
        assert!(compute(&s, &[("solid.extrude".into(), json!({"distance": "1 +"}))], colors()).is_err());
        assert!(s.model.state().bodies.is_empty());
    }

    fn session_of(cmds: &[(&str, Value)]) -> Session {
        let mut s = Session::default();
        for (id, p) in cmds {
            s.execute(id, p).unwrap();
        }
        s
    }

    /// A block and a two-letter text to cut into it.
    fn text_on_block() -> (Session, Vec<(String, Value)>) {
        let s = session_of(&[
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [60, 20]})),
            ("sketch.finish", json!({})),
            ("solid.extrude", json!({"distance": 10})),
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.text", json!({"text": "AB", "at": [5, 5], "height": 10})),
            ("sketch.finish", json!({})),
        ]);
        (s, vec![("solid.extrude".to_string(), json!({"distance": 2, "operation": "cut"}))])
    }

    /// A tool feature's live preview builds its tool without the boolean: a cut into nothing
    /// shows its tool with no error (OK will report it), while the whole preview has the error.
    #[test]
    fn a_tool_preview_builds_the_tool_without_the_boolean() {
        let s = session_of(&[
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [10, 10]})),
            ("sketch.finish", json!({})),
        ]);
        let cmds = vec![("solid.extrude".to_string(), json!({"distance": 2, "operation": "cut"}))];
        let tool = compute_tool(&s, &cmds, colors()).unwrap();
        assert!(tool.error.is_none() && !tool.scene.xray.is_empty(), "{:?}", tool.error);
        let whole = compute(&s, &cmds, colors()).unwrap();
        assert!(whole.error.as_deref().is_some_and(|e| e.contains("no body to cut")), "{:?}", whole.error);
        assert_eq!(whole.scene.xray.len(), tool.scene.xray.len());
    }

    /// #3085: a cut sketched in the root, where every body lives in a component, cuts every body
    /// it meets (#1234). The preview said "there is no body to cut or intersect" all the same,
    /// while the cut itself went through.
    #[test]
    fn a_cut_from_an_empty_root_is_not_refused_by_its_preview() {
        let mut s = session_of(&[
            ("component.create", json!({"name": "Part"})),
            ("solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Block"})),
            ("component.activate", json!({"component": "root"})),
            ("sketch.create", json!({"plane": "XY"})),
            ("sketch.rectangle.two_point", json!({"p0": [3, 3], "p1": [7, 7]})),
            ("sketch.finish", json!({})),
        ]);
        let params = json!({"distance": 20, "operation": "cut"});
        let cmds = vec![("solid.extrude".to_string(), params.clone())];
        assert_eq!(extrusion_target_error(&s, &cmds, &s.model.state()), None);
        s.execute("solid.extrude", &params).unwrap();
        let volume = s.model.state().body("Block").map(|b| solidcraft_engine::kernel::measure(&b.body).unwrap().volume).unwrap_or(0.0);
        assert!((volume - (1000.0 - 160.0)).abs() < 1e-6, "{volume}");
    }

    /// While an arrow is dragged the inputs change faster than a tool is built: a tool built
    /// for inputs since changed is shown anyway (then the latest computed), an error only for
    /// the inputs it belongs to.
    #[test]
    fn a_tool_for_inputs_since_changed_still_shows() {
        let (s, cmds) = text_on_block();
        let tool = compute_tool(&s, &cmds, colors()).unwrap();
        let mut pv = PreviewState::default();
        let (tx, rx) = std::sync::mpsc::channel();
        pv.running = Some((1, rx));
        pv.running_tool = true;
        tx.send(Ok(tool)).unwrap();
        pv.poll(2, None);
        assert!(pv.running.is_none() && pv.active && pv.shown_key == Some(1), "shown, and key 2 still to compute");
        let (tx, rx) = std::sync::mpsc::channel();
        pv.running = Some((3, rx));
        pv.running_tool = true;
        tx.send(Err("too far".into())).unwrap();
        pv.poll(4, None);
        assert!(pv.error.is_none() && pv.shown_key == Some(1), "an old error is dropped");
    }

    /// A design leaving the window (another tab) takes its preview off the viewport: an empty
    /// scene in the slot the viewport reads, under a new key. A fresh slot left the fillet
    /// preview of the old design drawn over the new tab's empty design.
    #[test]
    fn reset_hands_the_viewport_an_empty_scene() {
        let mut pv = PreviewState::default();
        let mut sc = GpuScene::default();
        let white = sc.color([255; 4]);
        sc.xray_tri([0.0; 3], [0.0, 0.0, 1.0], white);
        pv.set_scene(sc);
        pv.active = true;
        pv.replaced = vec!["Body1".into()];
        let slot = pv.slot.clone();
        let key = pv.key;
        // The viewport took the scene.
        assert!(slot.lock().unwrap().take().is_some_and(|s| !s.xray.is_empty()));
        pv.reset();
        assert!(std::sync::Arc::ptr_eq(&slot, &pv.slot), "the slot the viewport reads");
        assert_ne!(pv.key, key, "a new key: the viewport takes the slot again");
        assert!(slot.lock().unwrap().as_ref().is_some_and(|s| s.xray.is_empty() && s.blocks.is_empty()));
        assert!(!pv.active && pv.replaced.is_empty());
    }
}

#[cfg(test)]
#[path = "preview_drag_tests.rs"]
mod drag_tests;