GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Shape tools: Rectangle, Rounded Rectangle, Ellipse, Polygon, Star, Line Segment.
//!
//! Drag draws (Shift constrains to square/circle/45°, Alt draws from the centre, Space held moves
//! the shape being drawn); a click without a drag asks the UI for the size dialog (like
//! Illustrator). ↑/↓ during a polygon/star drag change the side/point count; during a star drag,
//! Ctrl/Cmd keeps the inner radius where it is while the outer one follows the pointer, and Alt
//! keeps the star's shoulders straight (#812). The start point and
//! the dragged corner snap to Smart Guides ([`DrawSnap`]), hovering before the press too. With Snap
//! to Tangent on, a line pressed on one convex curved path and dragged near another lands on a
//! common tangent of the two, both its ends moving to the touch points ([`TangentLine`]).
//! Escape cancels the drag and puts back the options its keys and Ctrl/Cmd changed.
//! Backtick (`` ` ``, the grave accent) held while dragging leaves a copy of the shape being drawn
//! every [`COPY_SPACING`] screen points the pointer travels, up to [`MAX_COPIES`] in one drag; the
//! release keeps the copies and the last shape as one undo step
//! (<https://help.adobe.com/archive/en_US/illustrator/cs3/illustrator_cs3_help.pdf#page=469>).
//!
//! With the Ellipse tool, three Shift-clicks draw the circle through the three points clicked
//! (`shape.circleThroughPoints`). Once a point is placed, a Shift-click on the selection's handles
//! or widgets places a point too. Escape, a click without Shift, a drag, a press without Shift on the
//! selection or a shape widget, and an edit, Undo or Redo made outside the tool drop the points
//! placed so far.
//! While the perspective grid shows with an active plane, a Shift-click opens the size dialog, as a
//! click does, and the shape goes on the plane.
//!
//! The selection keeps its bounding box and its Live Shape widgets on show with the Rectangle,
//! Rounded Rectangle, Ellipse, Polygon and Star tools, so a shape just drawn can be adjusted at
//! once: a drag on a live rectangle's or polygon's corner widget rounds its corners and a drag on
//! a live ellipse's pie widget sets its pie angle and a drag on a live polygon's side widget sets
//! its number of sides, as with the Selection tool; a drag on one of the
//! box's eight handles scales the selection, a press on the selection's centre point (Show Center)
//! moves it, and one just outside a corner of its box turns it about that centre, as the Selection
//! tool's handles, centre point and rotate zone do; a press anywhere else draws a new shape (#5477).
//!
//! With Smart Guides and Snap to Tangent on, a circle drawn from its center with the Ellipse tool (Alt and Shift)
//! takes the radius at which it touches a nearby path whenever its radius comes within the
//! snapping tolerance of it ([`DrawSnap::circle_radius`]).

use serde_json::{Value, json};
use vectorcraft_doc::OrientedBox;
use vectorcraft_geom::{Affine, Point, Rect, Vec2};

use crate::bbox::{Handle, move_delta, rotate_for_drag, scale_for_drag};
use crate::corners::{self, CornerDrag};
use crate::guides::{DrawSnap, Leave, PointSnap, TangentLine, Targets, square};
use crate::pie::{self, PieDrag};
use crate::select::{MoveSnap, handle_cursor, hit_center, is_area_type, matrix_json, rotate_zone, shown_box};
use crate::sidewidget::{self, SideDrag};
use crate::{Action, Cursor, Mods, Overlay, PointerEvent, PointerKind, Tool, ToolContext, ToolKey};

/// The largest corner radius the Rounded Rectangle tool draws, points: the range of General ›
/// Corner Radius, which the tool's radius is (its `prefs.list` spec), so ↑ and a Tool Options
/// value stop there too.
pub const MAX_CORNER_RADIUS: f64 = 1296.0;

/// How far (screen points) the pointer travels with Backtick held between two copies a shape drag
/// leaves: VectorCraft's own spacing.
pub const COPY_SPACING: f64 = 8.0;

/// The most copies one shape drag with Backtick held leaves, besides the shape it ends with:
/// VectorCraft's own limit.
pub const MAX_COPIES: usize = 128;

/// How far (screen points, along each axis) from a bounding-box handle's centre a shape tool's
/// press scales the selection: half the drawn handle square, which is 6 points wide at the default
/// Selection & Anchor Display › Size 3 and a point wider or narrower per step. The Selection tool
/// takes 5 points.
fn handle_reach(cx: &ToolContext) -> f64 {
    (6.0 + f64::from(cx.anchor_size.clamp(1, 7)) - 3.0) / 2.0
}

/// A document offset `d` in screen points: across and down the window.
fn screen_offset(cx: &ToolContext, d: Vec2) -> (f64, f64) {
    match cx.screen {
        Some(s) if s.right.hypot2() > 0.0 && s.down.hypot2() > 0.0 => (d.dot(s.right) / s.right.hypot2(), d.dot(s.down) / s.down.hypot2()),
        _ => (d.x * cx.zoom, d.y * cx.zoom),
    }
}

/// Does tool `id` keep the selection's bounding box and Live Shape widgets on show and let its
/// corner and pie widgets be dragged (#5477)? The shape tools that draw a box: not Line Segment.
pub fn shows_shape_widgets(id: &str) -> bool {
    matches!(id, "rectangle" | "roundedRectangle" | "ellipse" | "polygon" | "star")
}

/// A drag on a Live Shape widget of the selection, taken by a shape tool.
enum WidgetDrag {
    Corner(CornerDrag),
    Pie(PieDrag),
    Sides(SideDrag),
}

/// A drag of the selection itself, taken by a shape tool: a handle of its box scales it, the centre
/// point moves it and the zone just outside a corner of its box turns it, as the Selection tool's
/// drags do (#5477).
enum SelectionDrag {
    /// Scaling the selection by `handle` of its box `bx`, as it was when the press came. `areas`:
    /// area type is selected, and its frame resizes while its type keeps its size. `snap` holds
    /// what the moved handle snaps to. `last`: where the last drag was, and whether Backtick was
    /// down then.
    Scale { handle: Handle, bx: OrientedBox, areas: bool, snap: Box<PointSnap>, last: Option<(Point, bool)> },
    /// Moving the selection by the centre point `hold`: `start` is where the press was, and the
    /// move has begun once the pointer left the drag threshold.
    Move { start: Point, hold: Point, began: bool, snap: Box<MoveSnap> },
    /// Turning the selection about `center` from where the press was.
    Rotate { center: Point, start: Point },
}

pub struct ShapeTool {
    id: &'static str,
    start: Option<Point>,
    last: Point,
    mods: Mods,
    began: bool,
    pub sides: u32,
    pub points: u32,
    /// The anchors an ellipse has (the Ellipse dialog opens on it); 4 is a live ellipse.
    pub anchors: u32,
    pub corner_radius: f64,
    /// Star inner radius as a fraction of the outer radius.
    pub star_ratio: f64,
    /// The star's inner radius held while Ctrl/Cmd is down in a drag.
    inner: Option<f64>,
    /// Where the last drag put the dragged corner of a circle drawn from its center, from the
    /// center, when Snap to Tangent sized it ([`Self::tangent_circle`]); none when the pointer
    /// sized it. Space moving the circle moves the corner it was drawn with there and clears it.
    tangent_corner: Option<Vec2>,
    /// Line Segment Tool Options: the length and angle (degrees) a click's dialog opens with, and
    /// Fill Line (new lines take the current fill).
    pub line_length: f64,
    pub line_angle: f64,
    pub fill_line: bool,
    snap: DrawSnap,
    /// Snap to Tangent for a line from one curve to another: both ends on a common tangent.
    tangent_line: TangentLine,
    /// The options a drag's keys and Ctrl/Cmd change, as they were when the drag began: Escape
    /// puts them back.
    before_drag: Option<DragOptions>,
    /// A widget of the selection is being dragged instead of a shape drawn.
    widget: Option<WidgetDrag>,
    /// The selection is being moved or turned instead of a shape drawn.
    selection: Option<SelectionDrag>,
    /// The Smart Guides a move of the selection shows.
    guides: Vec<Overlay>,
    /// The measurement label of a move or a turn: where it goes and what it reads.
    measure: Option<(Point, String)>,
    /// The move of the selection snapped to a point, a guide or the grid at its last step: the
    /// arrow turns hollow, as the Selection tool's does.
    snapped: bool,
    /// The copies the drag has left with Backtick held, oldest first: each shape's command and
    /// params as the drag showed it when the copy was left.
    copies: Vec<(String, Value)>,
    /// While Backtick is held in a drag, the pointer position the next copy is measured from: where
    /// the last copy was left, or where the drag first had the key down.
    copy_from: Option<Point>,
    /// Ellipse tool: the points placed by Shift-clicks so far (fewer than three); the third
    /// Shift-click draws the circle through them ([`circle_through_points`]).
    through: Vec<Point>,
    /// Where the pointer was last seen while `through` holds points, for the circle's preview.
    hover: Option<Point>,
    /// The press in progress came with Shift held, with the Ellipse tool and no active perspective
    /// plane: a click places a point.
    places_point: bool,
}

/// Whether the perspective grid shows with an active plane: shapes then go on the plane.
fn perspective_plane_shown(cx: &ToolContext) -> bool {
    let g = crate::distort::perspective::PerspectiveGrid::current(cx.doc);
    g.visible && g.plane != crate::distort::perspective::Plane::None
}

/// The command three Shift-clicks with the Ellipse tool run: the circle through their points.
fn circle_through_points(points: &[Point]) -> (String, Value) {
    let pairs: Vec<Value> = points.iter().map(|p| json!([p.x, p.y])).collect();
    ("shape.circleThroughPoints".into(), json!({ "points": pairs }))
}

/// The options that keys and Ctrl/Cmd change while a shape is dragged out.
#[derive(Clone, Copy)]
struct DragOptions {
    sides: u32,
    points: u32,
    anchors: u32,
    corner_radius: f64,
    star_ratio: f64,
}

/// The inner-to-outer radius ratio that keeps a star of `points` points' shoulders straight (each
/// point's sides in line with the next points' sides: a regular star polygon {n/2}); none for
/// fewer than five points, whose shoulders can't line up.
fn straight_ratio(points: u32) -> Option<f64> {
    let n = f64::from(points);
    (points >= 5).then(|| (2.0 * std::f64::consts::PI / n).cos() / (std::f64::consts::PI / n).cos())
}

impl ShapeTool {
    pub fn new(id: &str) -> Self {
        let id: &'static str = match id {
            "roundedRectangle" => "roundedRectangle",
            "ellipse" => "ellipse",
            "polygon" => "polygon",
            "star" => "star",
            "lineSegment" => "lineSegment",
            _ => "rectangle",
        };
        Self {
            id,
            start: None,
            last: Point::ZERO,
            mods: Mods::default(),
            began: false,
            sides: 6,
            points: 5,
            anchors: 4,
            corner_radius: 12.0,
            star_ratio: 0.5,
            inner: None,
            tangent_corner: None,
            snap: DrawSnap::default(),
            tangent_line: TangentLine::default(),
            line_length: 100.0,
            line_angle: 0.0,
            fill_line: false,
            before_drag: None,
            widget: None,
            selection: None,
            guides: vec![],
            measure: None,
            snapped: false,
            through: vec![],
            hover: None,
            places_point: false,
            copies: vec![],
            copy_from: None,
        }
    }

    /// Whether a press with `m` places another point for a circle: Shift with the Ellipse tool once
    /// a point is placed. Such a press skips the selection's handles and widgets.
    fn placing_point(&self, m: Mods) -> bool {
        self.id == "ellipse" && m.shift && !self.through.is_empty()
    }

    /// With Backtick `held` at pointer `raw`, leave a copy of the shape the drag shows (`cmd` and
    /// `params`) once the pointer is [`COPY_SPACING`] screen points from the last copy, or from
    /// where the key was first held. A shape with no width, height or length, one the same as the
    /// last copy, and any past [`MAX_COPIES`] are not left. Without the key, the next copy is
    /// measured from where it goes down again.
    fn leave_copy(&mut self, cx: &ToolContext, raw: Point, held: bool, cmd: &str, params: &Value) {
        if !held {
            self.copy_from = None;
            return;
        }
        let Some(from) = self.copy_from else {
            self.copy_from = Some(raw);
            return;
        };
        let (x, y) = screen_offset(cx, raw - from);
        if x.hypot(y) < COPY_SPACING || self.copies.len() >= MAX_COPIES || !draws_something(cmd, params) {
            return;
        }
        self.copy_from = Some(raw);
        if self.copies.last().is_some_and(|(c, p)| c == cmd && p == params) {
            return;
        }
        self.copies.push((cmd.to_owned(), params.clone()));
    }

    /// The preview of the shape `cmd` with `params`: with copies left, a `command.batch` that draws
    /// them and then the shape, unless it has no size or is the same as the last copy.
    fn preview(&self, cmd: String, params: Value) -> Action {
        if self.copies.is_empty() {
            return Action::Preview(cmd, params);
        }
        let step = |c: &str, p: &Value| json!({ "command": c, "params": p });
        let mut commands: Vec<Value> = self.copies.iter().map(|(c, p)| step(c, p)).collect();
        if draws_something(&cmd, &params) && self.copies.last().is_none_or(|(c, p)| *c != cmd || *p != params) {
            commands.push(step(&cmd, &params));
        }
        Action::Preview("command.batch".into(), json!({ "label": self.label(), "commands": commands }))
    }

    /// Forget the copies of the drag that ended.
    fn clear_copies(&mut self) {
        self.copies.clear();
        self.copy_from = None;
    }

    fn drag_options(&self) -> DragOptions {
        DragOptions { sides: self.sides, points: self.points, anchors: self.anchors, corner_radius: self.corner_radius, star_ratio: self.star_ratio }
    }

    /// Put back the options a canceled drag changed.
    fn restore_drag_options(&mut self, o: DragOptions) {
        self.sides = o.sides;
        self.points = o.points;
        self.anchors = o.anchors;
        self.corner_radius = o.corner_radius;
        self.star_ratio = o.star_ratio;
    }

    /// A press on a corner, pie or side widget of the selection: the drag adjusts it.
    fn press_widget(&mut self, cx: &ToolContext, ev: &PointerEvent) -> bool {
        if !shows_shape_widgets(self.id) {
            return false;
        }
        self.widget = CornerDrag::hit(cx, ev, false)
            .map(WidgetDrag::Corner)
            .or_else(|| PieDrag::hit(cx, ev).map(WidgetDrag::Pie))
            .or_else(|| SideDrag::hit(cx, ev).map(WidgetDrag::Sides));
        self.widget.is_some()
    }

    /// Is `p` over a corner, pie or side widget of the selection that a press would drag?
    fn over_widget(&self, cx: &ToolContext, p: Point) -> bool {
        shows_shape_widgets(self.id) && (corners::over_widget(cx, p, false) || pie::over_widget(cx, p) || sidewidget::over_widget(cx, p))
    }

    /// The selection's box, while this tool shows it (the tools that draw a box, with a selection
    /// and no anchors picked on their own).
    fn box_of(&self, cx: &ToolContext) -> Option<vectorcraft_doc::OrientedBox> {
        shows_shape_widgets(self.id).then(|| shown_box(cx)).flatten()
    }

    /// Is `p` in the zone just outside a corner of the selection's box, where a press turns the
    /// selection about its centre ([`crate::select::rotate_zone`])? On the corner itself the Live
    /// Corners widget rounds it (#5477).
    fn over_rotate(&self, cx: &ToolContext, p: Point) -> Option<Point> {
        rotate_zone(cx, &self.box_of(cx)?, p)
    }

    /// The handle of the selection's box under `p`, with the box: a press there scales the
    /// selection, as with the Selection tool. A shape tool takes only the drawn handle square
    /// ([`handle_reach`]), which stays square to the screen when the box or the view is turned, so
    /// a press just beside a handle still draws a new shape.
    fn over_handle(&self, cx: &ToolContext, p: Point) -> Option<(Handle, OrientedBox)> {
        let bx = self.box_of(cx)?;
        let reach = handle_reach(cx);
        Handle::ALL
            .into_iter()
            .find(|h| {
                let (x, y) = screen_offset(cx, p - bx.to_doc() * h.pos(bx.rect));
                x.abs() <= reach && y.abs() <= reach
            })
            .map(|h| (h, bx))
    }

    /// Is `p` over the centre point of a selected object (Attributes › Show Center), where a press
    /// moves the selection? The point shows whatever View › Show Bounding Box says, so the press
    /// takes it too.
    fn over_center(&self, cx: &ToolContext, p: Point) -> bool {
        shows_shape_widgets(self.id) && hit_center(cx, p, cx.point_tol()).is_some()
    }

    /// A press on a handle of the selection's box, on its centre point or just outside a corner of
    /// its box: the drag scales, moves or turns the selection instead of drawing a new shape
    /// (#5477). The handles come first, as with the Selection tool. A scale or a turn begins with
    /// the press, as the Selection tool's do; a move begins once the pointer leaves the drag
    /// threshold.
    fn press_selection(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Option<Vec<Action>> {
        if !shows_shape_widgets(self.id) {
            return None;
        }
        self.selection = if let Some((handle, bx)) = self.over_handle(cx, ev.pos) {
            let is_area = |id: &vectorcraft_doc::NodeId| cx.doc.node(*id).is_some_and(is_area_type);
            let areas = cx.selection.objects.iter().any(is_area);
            Some(SelectionDrag::Scale { handle, bx, areas, snap: Box::new(PointSnap::scaling(cx, || Targets::for_scale(cx))), last: None })
        } else {
            match hit_center(cx, ev.pos, cx.point_tol()) {
                Some((_, hold)) => Some(SelectionDrag::Move { start: ev.pos, hold, began: false, snap: Box::new(MoveSnap::new(cx)) }),
                None => self.over_rotate(cx, ev.pos).map(|center| SelectionDrag::Rotate { center, start: ev.pos }),
            }
        };
        self.selection.as_ref()?;
        self.snap.clear();
        self.guides.clear();
        self.measure = None;
        Some(match self.selection {
            // Only area type: the step resizes type areas; anything else scales.
            Some(SelectionDrag::Scale { areas, .. }) => {
                let only_areas = areas && cx.selection.objects.iter().all(|id| cx.doc.node(*id).is_some_and(is_area_type));
                vec![Action::Begin(if only_areas { "Resize Type Area" } else { "Scale" }.into())]
            }
            Some(SelectionDrag::Rotate { .. }) => vec![Action::Begin("Rotate".into())],
            _ => vec![],
        })
    }

    /// The pointer and keys while the selection is moved or turned: `None` when it is not.
    fn selection_pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Option<Vec<Action>> {
        let d = self.selection.as_mut()?;
        let out = match (ev.kind, d) {
            (PointerKind::Drag, SelectionDrag::Move { start, hold, began, snap }) => {
                let mut out = vec![];
                if !*began {
                    if ev.pos.distance(*start) < cx.tol(3.0) {
                        return Some(out);
                    }
                    *began = true;
                    out.push(Action::Begin(if ev.mods.alt { "Copy".into() } else { "Move".into() }));
                }
                let shift = ev.mods.shift;
                let unsnapped = move_delta(*start, ev.pos, shift, cx.constrain_angle);
                let (delta, guides) =
                    if shift { snap.snap_along(cx, *hold, unsnapped, ev.mods.alt) } else { snap.snap(cx, *hold, unsnapped, ev.mods.alt) };
                // Shift has already constrained the move: any change is a snap.
                self.snapped = (delta - unsnapped).hypot() > 1e-9;
                self.guides = guides;
                self.measure = cx.measurement_labels.then(|| (ev.pos, cx.offset_label(delta.x, delta.y)));
                out.push(Action::Preview("object.transform".into(), json!({"matrix": matrix_json(Affine::translate(delta)), "copy": ev.mods.alt})));
                out
            }
            (PointerKind::Drag, SelectionDrag::Rotate { center, start }) => {
                let (a, deg) = rotate_for_drag(*center, *start, ev.pos, ev.mods.shift);
                self.measure = cx.transform_tools_guides.then(|| (ev.pos, format!("{:.1}°", -deg)));
                vec![Action::Preview("object.transform".into(), json!({"matrix": matrix_json(a), "copy": false}))]
            }
            (PointerKind::Drag, SelectionDrag::Scale { handle, bx, areas, snap, last }) => {
                // As the Selection tool scales: in the box's own frame, the moved handle snapped,
                // and with Use Preview Bounds by the visual box's size unless the drag flips it.
                *last = Some((ev.pos, ev.backquote));
                let m = ev.mods;
                let a = scale_for_drag(bx.rect, *handle, bx.to_local(ev.pos), m.shift, m.alt);
                let (a, guides) = snap.snap_scale(cx, bx, *handle, a, m.shift, m.alt);
                self.guides = guides;
                let nr = a.transform_rect_bbox(bx.rect);
                self.measure = cx.transform_tools_guides.then(|| (ev.pos, cx.size_label(nr.width(), nr.height())));
                let [sx, _, _, sy, _, _] = a.as_coeffs();
                let (cmd, mut params) = if !ev.backquote && cx.preview_bounds && sx > 0.0 && sy > 0.0 {
                    let reference = if m.alt { 4 } else { handle.opposite().reference() };
                    ("object.setBounds", json!({ "width": nr.width(), "height": nr.height(), "reference": reference, "copy": false }))
                } else {
                    ("object.transform", json!({ "matrix": matrix_json(bx.conjugate(a)), "copy": false }))
                };
                if *areas {
                    params["typeAreas"] = json!(true);
                }
                if ev.backquote {
                    params["objects"] = json!(false);
                    params["patterns"] = json!(true);
                }
                vec![Action::Preview(cmd.into(), params)]
            }
            (PointerKind::Up, SelectionDrag::Move { began, .. }) => {
                let began = *began;
                self.selection = None;
                self.guides.clear();
                self.measure = None;
                self.snapped = false;
                if began { vec![Action::Commit] } else { vec![] }
            }
            (PointerKind::Up, SelectionDrag::Scale { last, .. }) => {
                // Backtick pressed or let go after the last drag: the scale is previewed again at
                // that drag's position with the key as it is at the release, as the Selection tool
                // does.
                let again = last.filter(|(_, backquote)| *backquote != ev.backquote).map(|(pos, _)| pos);
                let mut out =
                    again.and_then(|pos| self.selection_pointer(cx, &PointerEvent { kind: PointerKind::Drag, pos, ..*ev })).unwrap_or_default();
                self.selection = None;
                self.guides.clear();
                self.measure = None;
                out.push(Action::Commit);
                out
            }
            (PointerKind::Up, SelectionDrag::Rotate { .. }) => {
                self.selection = None;
                self.guides.clear();
                self.measure = None;
                vec![Action::Commit]
            }
            _ => vec![],
        };
        Some(out)
    }

    /// Escape during a drag of the selection turns it back, as the Selection tool's does.
    fn cancel_selection(&mut self) -> Vec<Action> {
        self.selection = None;
        self.guides.clear();
        self.measure = None;
        self.snapped = false;
        vec![Action::Cancel]
    }

    /// The pointer and keys while a widget is dragged: `None` when none is.
    fn widget_pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Option<Vec<Action>> {
        let w = self.widget.as_mut()?;
        Some(match ev.kind {
            PointerKind::Drag => match w {
                WidgetDrag::Corner(c) => c.drag(cx, ev.pos),
                WidgetDrag::Pie(d) => d.drag(cx, ev.pos),
                WidgetDrag::Sides(d) => d.drag(cx, ev.pos),
            },
            PointerKind::Up => match self.widget.take() {
                Some(WidgetDrag::Corner(c)) => c.finish(),
                Some(WidgetDrag::Pie(d)) => d.finish(),
                Some(WidgetDrag::Sides(mut d)) => {
                    let mut out = d.drag(cx, ev.pos);
                    out.extend(d.finish());
                    out
                }
                None => vec![],
            },
            _ => vec![],
        })
    }

    /// Ctrl/Cmd in a star drag from `start` to `p`, where the last drag showed a star with the inner
    /// radius `shown`: once the star has begun, the inner radius stays as the star showed it when
    /// the key went down, so the ratio follows the outer radius (and stays so once the key is let
    /// go). When the key goes down before the star has begun, the inner radius stays as the first
    /// star the drag shows has it.
    fn hold_inner(&mut self, shown: f64, start: Point, p: Point, m: Mods) {
        let outer = (p - start).hypot();
        match (m.cmd, self.inner) {
            (true, None) if self.began => self.inner = Some(shown),
            (false, Some(_)) => self.inner = None,
            _ => {}
        }
        if let Some(inner) = self.inner
            && outer > 0.0
            && (inner / outer).is_finite()
        {
            self.star_ratio = (inner / outer).clamp(0.01, 1.0);
        }
    }

    fn label(&self) -> &'static str {
        match self.id {
            "roundedRectangle" => "Rounded Rectangle",
            "ellipse" => "Ellipse",
            "polygon" => "Polygon",
            "star" => "Star",
            "lineSegment" => "Line",
            _ => "Rectangle",
        }
    }

    /// How the dragged point keeps to the start: a polygon's or star's radius goes anywhere, a
    /// line is a segment (Shift: 45° steps), the rest are boxes (Shift: a square).
    fn leave(&self, cx: &ToolContext, start: Point, m: Mods) -> Option<Leave> {
        match self.id {
            "polygon" | "star" => None,
            "lineSegment" => Some(Leave::segment(cx, start, m.shift)),
            // With a Constrain Angle, Shift's square is taken along the rotated axes
            // ([`Self::command_at`]), not the page's diagonal.
            _ => Some(Leave::diagonal(start, m.shift && vectorcraft_geom::constrain_axes(cx.constrain_angle).is_none())),
        }
    }

    /// Snap to Tangent for a circle drawn from its center `s` (the Ellipse tool with Alt and
    /// Shift): the dragged corner `p` moved toward or away from `s` so that the circle touches a
    /// nearby path, when its radius is within the snapping tolerance of a touching one
    /// ([`DrawSnap::circle_radius`]); `raw` is the pointer before snapping. The radius is measured
    /// along the Constrain Angle's axes, as the drawn box is ([`turned_box`]). None for any other
    /// shape or modifiers, or with `p` on `s`.
    fn tangent_circle(&mut self, cx: &ToolContext, s: Point, raw: Point, p: Point, m: Mods) -> Option<Point> {
        if self.id != "ellipse" || !(m.alt && m.shift) || m.space {
            return None;
        }
        let axes = vectorcraft_geom::constrain_axes(cx.constrain_angle).unwrap_or(Affine::IDENTITY);
        let back = axes.inverse();
        let (c, d) = (back * s, back * p - back * s);
        let r = d.x.abs().max(d.y.abs());
        if r.is_nan() || r <= 0.0 {
            return None;
        }
        let radius = self.snap.circle_radius(cx, s, raw, p, r)?;
        Some(axes * (c + d * (radius / r)))
    }

    /// The command for a drag from `start` to `p` (snapped: a line's end already keeps Shift's
    /// angle).
    pub fn command(&self, start: Point, p: Point, m: Mods) -> (String, Value) {
        self.command_at(start, p, m, 0.0)
    }

    /// [`Self::command`] with General › Constrain Angle `base` (degrees, counter-clockwise):
    /// rectangles, rounded rectangles and ellipses are drawn along the x and y axes turned by it,
    /// as the reference app draws new objects along the rotated axes
    /// (<https://helpx.adobe.com/illustrator/desktop/manage-objects/arrange-objects/rotate-objects-by-specific-angles.html>).
    pub fn command_at(&self, start: Point, p: Point, m: Mods, base: f64) -> (String, Value) {
        match self.id {
            "polygon" | "star" => {
                let v = p - start;
                let r = v.hypot().max(0.01);
                // Shift keeps the base orientation: upright, turned by the Constrain Angle.
                let rot = if m.shift { vectorcraft_geom::constrain_rotation(base).unwrap_or(0.0) } else { (v.atan2().to_degrees() + 90.0) % 360.0 };
                if self.id == "polygon" {
                    ("shape.polygon".into(), json!({ "cx": start.x, "cy": start.y, "radius": r, "sides": self.sides, "rotation": rot }))
                } else {
                    let ratio = if m.alt { straight_ratio(self.points).unwrap_or(self.star_ratio) } else { self.star_ratio };
                    (
                        "shape.star".into(),
                        json!({ "cx": start.x, "cy": start.y, "radius1": r, "radius2": r * ratio, "points": self.points, "rotation": rot }),
                    )
                }
            }
            "lineSegment" => {
                let (a, b) = if m.alt { (start - (p - start), p) } else { (start, p) };
                let mut v = json!({ "x1": a.x, "y1": a.y, "x2": b.x, "y2": b.y });
                if self.fill_line {
                    v["fill"] = json!(true);
                }
                ("shape.line".into(), v)
            }
            _ => {
                let cmd = if self.id == "ellipse" { "shape.ellipse" } else { "shape.rectangle" };
                let mut v = box_params(start, p, m, base);
                if self.id == "roundedRectangle" {
                    v["radius"] = json!(self.corner_radius);
                } else if self.id == "ellipse" && self.anchors != 4 {
                    v["anchors"] = json!(self.anchors);
                }
                (cmd.into(), v)
            }
        }
    }
}

/// Does the shape command `cmd` with `params` draw something: a box with a width and a height, a
/// polygon or star with a radius, a line with a length?
fn draws_something(cmd: &str, params: &Value) -> bool {
    let n = |k: &str| params.get(k).and_then(Value::as_f64).unwrap_or(0.0);
    match cmd {
        // The drag gives a polygon or star a radius of at least 0.01 pt, even on its centre.
        "shape.polygon" => n("radius") > 0.01,
        "shape.star" => n("radius1") > 0.01,
        "shape.line" => (n("x2") - n("x1")).hypot(n("y2") - n("y1")) > 0.0,
        _ => n("width").abs() > 0.0 && n("height").abs() > 0.0,
    }
}

/// The box a drag from `start` to `p` draws (from its centre with Alt, square with Shift),
/// measured along the page axes turned by Constrain Angle `base`: the box in those axes, and the
/// axes (`None` for the page's own: 0° or a whole turn, where the box is on the page exactly).
/// The drawn shape and its size label both come from it.
pub(crate) fn turned_box(start: Point, p: Point, m: Mods, base: f64) -> (Rect, Option<Affine>) {
    match vectorcraft_geom::constrain_axes(base) {
        None => (drag_rect(start, p, m), None),
        Some(axes) => {
            let back = axes.inverse();
            (drag_rect(back * start, back * p, m), Some(axes))
        }
    }
}

/// The `{x, y, width, height, rotation?}` params of the box a drag from `start` to `p` draws along
/// Constrain Angle `base` ([`turned_box`]): the box's page position at 0° or a whole turn, else
/// the turned box's centre out on the page again with the rotation that turns it about it.
pub(crate) fn box_params(start: Point, p: Point, m: Mods, base: f64) -> Value {
    match turned_box(start, p, m, base) {
        (r, None) => json!({ "x": r.x0, "y": r.y0, "width": r.width(), "height": r.height() }),
        (r, Some(axes)) => {
            let c = axes * r.center();
            let rotation = -vectorcraft_geom::normalize_deg(base);
            json!({ "x": c.x - r.width() / 2.0, "y": c.y - r.height() / 2.0, "width": r.width(), "height": r.height(), "rotation": rotation })
        }
    }
}

/// Space held once a shape drag has begun moves the shape being drawn instead of sizing it: `start`
/// moves as far as the pointer did since `last`, which becomes the pointer.
pub(crate) fn space_moves(start: &mut Point, last: &mut Point, ev: &PointerEvent, began: bool) {
    if ev.mods.space && began {
        *start += ev.pos - *last;
    }
    *last = ev.pos;
}

/// The rectangle a drag from `start` to `p` draws: Shift makes it a square, Alt draws it from its
/// centre.
pub(crate) fn drag_rect(start: Point, p: Point, m: Mods) -> Rect {
    let d = if m.shift { square(p - start) } else { p - start };
    if m.alt { Rect::from_points(start - d, start + d) } else { Rect::from_points(start, start + d) }
}

impl Tool for ShapeTool {
    fn id(&self) -> &'static str {
        self.id
    }
    /// A shape being drawn, a Live Shape widget dragged, or the selection moved or turned is a
    /// gesture in progress.
    fn busy(&self) -> bool {
        self.start.is_some() || self.widget.is_some() || self.selection.is_some()
    }
    /// A widget or selection drag takes the pointer; otherwise a press draws, unless it lands on a
    /// handle of the selection's box, its centre point or the rotate zone just outside its box's
    /// corner.
    fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {
        if let Some(out) = self.widget_pointer(cx, ev) {
            return out;
        }
        if let Some(out) = self.selection_pointer(cx, ev) {
            return out;
        }
        match ev.kind {
            PointerKind::Move => {
                // Over a widget a press would drag, no Smart Guide for a new shape's start.
                if !self.placing_point(ev.mods)
                    && (self.over_widget(cx, ev.pos)
                        || self.over_handle(cx, ev.pos).is_some()
                        || self.over_center(cx, ev.pos)
                        || self.over_rotate(cx, ev.pos).is_some())
                {
                    self.snap.clear();
                    // A press there drags the widget or the selection, which drops the points.
                    self.hover = None;
                } else {
                    // The snapped point: the third Shift-click lands where the press snaps.
                    let p = self.snap.hover(cx, ev.pos, &[], None);
                    self.hover = (!self.through.is_empty()).then_some(p);
                }
                vec![]
            }
            PointerKind::Down => {
                if !self.placing_point(ev.mods) {
                    if self.press_widget(cx, ev) {
                        self.snap.clear();
                        self.through.clear();
                        return vec![];
                    }
                    if let Some(out) = self.press_selection(cx, ev) {
                        self.through.clear();
                        return out;
                    }
                }
                let p = self.snap.press(cx, ev.pos, &[], None);
                if self.id == "lineSegment" {
                    // The pressed spot finds the curve; a press that snapped to an anchor, center
                    // or crossing off that curve keeps the line's start there.
                    let held = self.snap.snapped_to_point(cx, ev.pos, p).then_some(p);
                    self.tangent_line.press(cx, ev.pos, held);
                }
                self.start = Some(p);
                self.last = ev.pos;
                self.began = false;
                self.places_point = self.id == "ellipse" && ev.mods.shift && !perspective_plane_shown(cx);
                self.inner = None;
                self.before_drag = None;
                self.clear_copies();
                // Backtick already held at the press: the copies are measured from there.
                if ev.backquote {
                    self.copy_from = Some(ev.pos);
                }
                vec![]
            }
            PointerKind::Drag => {
                let Some(mut s) = self.start else { return vec![] };
                let raw = ev.pos;
                let pos = self.snap.drag(cx, ev.pos, self.leave(cx, s, ev.mods).as_ref());
                let ev = &PointerEvent { pos, ..*ev };
                // The inner radius of the star the last drag showed, with the ratio it drew with:
                // the canvas sends a drag only once the pointer moves, so a key pressed in between
                // first arrives with the next position.
                let ratio = if self.mods.alt { straight_ratio(self.points).unwrap_or(self.star_ratio) } else { self.star_ratio };
                let shown = (self.last - s).hypot() * ratio;
                // Space moves a circle Snap to Tangent sized from the corner it was drawn with, so
                // it keeps that size while it moves and after Space is let go.
                if ev.mods.space
                    && self.began
                    && let Some(d) = self.tangent_corner.take()
                {
                    self.last = s + d;
                }
                space_moves(&mut s, &mut self.last, ev, self.began);
                if ev.mods.space && self.began {
                    // The start no longer lies on the pressed path.
                    self.tangent_line.clear();
                }
                self.start = Some(s);
                self.mods = ev.mods;
                if self.id == "star" {
                    self.hold_inner(shown, s, ev.pos, ev.mods);
                }
                // A circle from its center takes a radius that touches a nearby path. Whether the
                // drag has begun is judged before that snap.
                let dragged = ev.pos;
                self.tangent_corner = self.tangent_circle(cx, s, raw, ev.pos, ev.mods).map(|q| q - s);
                let ev = &PointerEvent { pos: self.tangent_corner.map_or(ev.pos, |d| s + d), ..*ev };
                let mut out = vec![];
                if !self.began {
                    if dragged.distance(s) < cx.tol(2.0) {
                        return out;
                    }
                    self.began = true;
                    self.before_drag = Some(self.drag_options());
                    out.push(Action::Begin(self.label().into()));
                }
                // Snap to Tangent: a line pressed on one curve and ending near another moves both
                // its ends onto a common tangent of the two, unless Shift, Alt or Space is held or
                // an anchor or crossing nearer the pointer pulls it.
                let tangent = if self.id == "lineSegment" && !(ev.mods.shift || ev.mods.alt || ev.mods.space) {
                    let rival = self.snap.point_within_reach(cx, raw);
                    self.tangent_line.snap(cx, raw, rival)
                } else {
                    self.tangent_line.drop_line();
                    None
                };
                let (a, b) = tangent.unwrap_or((s, ev.pos));
                let (c, v) = self.command_at(a, b, ev.mods, cx.constrain_angle);
                self.leave_copy(cx, raw, ev.backquote, &c, &v);
                out.push(self.preview(c, v));
                out
            }
            PointerKind::Up => {
                let Some(s) = self.start.take() else { return vec![] };
                self.snap.clear();
                self.tangent_line.clear();
                self.inner = None;
                self.before_drag = None;
                self.clear_copies();
                if self.began {
                    self.began = false;
                    self.through.clear();
                    vec![Action::Commit]
                } else if self.places_point {
                    // A Shift-click places a point; the third draws the circle through the three.
                    self.through.push(s);
                    self.hover = None;
                    if self.through.len() < 3 {
                        return vec![];
                    }
                    let (c, v) = circle_through_points(&self.through);
                    self.through.clear();
                    vec![Action::Exec(c, v)]
                } else {
                    self.through.clear();
                    // Alt (draw from centre) is passed on, so the size dialog can centre the shape on the click.
                    let args = if ev.mods.alt { json!({ "x": s.x, "y": s.y, "alt": true }) } else { json!({ "x": s.x, "y": s.y }) };
                    vec![Action::Dialog(self.id.into(), args)]
                }
            }
            // A double-click on a corner widget of the selected live shape shows the Transform
            // panel; the tool and the selection stay as they are.
            PointerKind::DoubleClick if shows_shape_widgets(self.id) && corners::over_widget(cx, ev.pos, false) => {
                vec![Action::Panel("transform".into())]
            }
            _ => vec![],
        }
    }
    /// Switching tools during a drag of a corner, pie or side widget cancels it, as Escape does:
    /// the shape goes back.
    fn deactivate(&mut self, cx: &ToolContext) -> Vec<Action> {
        if self.widget.is_some() { self.key(cx, ToolKey::Escape, Mods::default()) } else { vec![] }
    }

    /// ↑/↓ change a polygon's sides, a star's points or the rounded rectangle's radius while it is
    /// drawn; Escape turns a widget or selection drag back.
    fn key(&mut self, cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec<Action> {
        // A drag of the selection: Escape turns it back, as with the Selection tool.
        if self.selection.is_some() {
            return if key == ToolKey::Escape { self.cancel_selection() } else { vec![] };
        }
        // A widget drag: Up and Down step a corner's kind; Escape cancels it, as with the
        // Selection tool.
        if let Some(w) = &mut self.widget {
            return match (key, w) {
                (ToolKey::Up | ToolKey::Down, WidgetDrag::Corner(c)) => c.step_kind(key == ToolKey::Up),
                (ToolKey::Escape, _) => match self.widget.take() {
                    Some(WidgetDrag::Corner(c)) if c.adjusting() => vec![Action::Cancel],
                    Some(WidgetDrag::Pie(d)) if d.adjusting() => vec![Action::Cancel],
                    Some(WidgetDrag::Sides(d)) => d.cancel(),
                    _ => vec![],
                },
                _ => vec![],
            };
        }
        // Escape drops the points Shift-clicks placed for a circle; the document stays as it is. A
        // press that has not begun a drag is dropped too, so its release places no point.
        if key == ToolKey::Escape && !self.began && !self.through.is_empty() {
            self.through.clear();
            self.hover = None;
            if self.start.take().is_some() {
                self.places_point = false;
                self.snap.clear();
            }
            return vec![];
        }
        let Some(s) = self.start.filter(|_| self.began) else { return vec![] };
        let changed = match (self.id, key) {
            ("polygon", ToolKey::Up) => {
                self.sides = (self.sides + 1).min(1000);
                true
            }
            ("polygon", ToolKey::Down) => {
                self.sides = self.sides.saturating_sub(1).max(3);
                true
            }
            ("star", ToolKey::Up) => {
                self.points = (self.points + 1).min(1000);
                true
            }
            ("star", ToolKey::Down) => {
                self.points = self.points.saturating_sub(1).max(3);
                true
            }
            ("ellipse", ToolKey::Up) => {
                self.anchors = self.anchors.saturating_add(1).min(1000);
                true
            }
            ("ellipse", ToolKey::Down) => {
                self.anchors = self.anchors.saturating_sub(1).max(2);
                true
            }
            ("roundedRectangle", ToolKey::Up) => {
                self.corner_radius = (self.corner_radius + 1.0).min(MAX_CORNER_RADIUS);
                true
            }
            ("roundedRectangle", ToolKey::Down) => {
                self.corner_radius = (self.corner_radius - 1.0).max(0.0);
                true
            }
            _ => false,
        };
        if changed {
            // A count-only key keeps the corner a tangent-snapped circle already showed.
            let p = self.tangent_corner.map_or(self.last, |d| s + d);
            let (c, v) = self.command_at(s, p, self.mods, cx.constrain_angle);
            vec![self.preview(c, v)]
        } else if key == ToolKey::Escape {
            self.start = None;
            self.began = false;
            self.through.clear();
            self.inner = None;
            self.clear_copies();
            if let Some(o) = self.before_drag.take() {
                self.restore_drag_options(o);
            }
            self.snap.clear();
            self.tangent_line.clear();
            vec![Action::Cancel]
        } else {
            vec![]
        }
    }
    /// A drag of a handle of the selection's box: the canvas keeps drawing the box at the changing
    /// bounds, as for the Selection tool.
    fn resizing(&self) -> bool {
        matches!(self.selection, Some(SelectionDrag::Scale { .. }))
    }
    /// A move of the selection by its center point, as the Selection tool's move.
    fn moving(&self) -> bool {
        matches!(self.selection, Some(SelectionDrag::Move { began: true, .. }))
    }
    /// Drawing a shape with a tool that shows the selection's bounding box. The Line Segment tool
    /// shows no box, so it reports none.
    fn drawing_boxed(&self) -> bool {
        shows_shape_widgets(self.id) && self.began
    }
    /// Adjusting a widget (the canvas hides the bounding box meanwhile, as for the Selection tool).
    fn transforming(&self) -> bool {
        match &self.widget {
            Some(WidgetDrag::Corner(c)) => c.adjusting(),
            Some(WidgetDrag::Pie(d)) => d.adjusting(),
            Some(WidgetDrag::Sides(d)) => d.adjusting(),
            None => match &self.selection {
                Some(SelectionDrag::Move { began, .. }) => *began,
                Some(SelectionDrag::Rotate { .. } | SelectionDrag::Scale { .. }) => true,
                None => false,
            },
        }
    }
    /// ↑/↓ step a corner's kind while its widget is dragged, and Escape ends a drag of the
    /// selection.
    fn claims_key(&self, _cx: &ToolContext, key: ToolKey) -> bool {
        match (&self.widget, &self.selection) {
            (Some(WidgetDrag::Corner(_)), _) => matches!(key, ToolKey::Up | ToolKey::Down),
            (_, Some(_)) => key == ToolKey::Escape,
            _ => key == ToolKey::Escape && !self.through.is_empty(),
        }
    }
    /// Points placed by Shift-clicks for a circle: Escape drops them.
    fn ends_on_escape(&self, _cx: &ToolContext) -> bool {
        !self.through.is_empty()
    }
    /// The pointer left the canvas: the circle's preview through it goes.
    fn hover_ended(&mut self, _cx: &ToolContext) {
        self.hover = None;
    }
    /// An edit, Undo or Redo made outside the tool, or another document, drops the points placed
    /// for a circle.
    fn history_moved(&mut self) {
        self.through.clear();
        self.hover = None;
    }
    /// The widget being dragged, the Smart Guides of the shape being drawn or the selection being
    /// moved, and its size, angle or offset label.
    fn overlays(&self, cx: &ToolContext) -> Vec<Overlay> {
        match &self.widget {
            Some(WidgetDrag::Corner(c)) => return c.overlays(cx),
            Some(WidgetDrag::Pie(d)) => return d.overlays(cx),
            Some(WidgetDrag::Sides(d)) => return d.overlays(cx),
            None => {}
        }
        // A line on a common tangent shows that snap's guides, and its length and angle.
        let tangent = self.tangent_line.line().filter(|_| self.start.is_some());
        let mut o = if tangent.is_some() { self.tangent_line.guides(cx) } else { self.snap.guides().to_vec() };
        o.extend(self.guides.iter().cloned());
        // The points Shift-clicks placed, and after two of them the circle through them and the
        // pointer, dashed.
        for p in &self.through {
            o.push(Overlay::Anchor { p: *p, color: crate::draw2::FEEDBACK, filled: true, size: 6.0 });
        }
        if let ([a, b], Some(h)) = (self.through.as_slice(), self.hover)
            && let Some((c, r)) = vectorcraft_geom::shapes::circle_through(*a, *b, h)
        {
            let path = vectorcraft_geom::shapes::ellipse(Rect::new(c.x - r, c.y - r, c.x + r, c.y + r)).to_bezpath();
            o.push(Overlay::Path { path, color: crate::draw2::FEEDBACK, width: 1.0, dashed: true });
        }
        if let Some((p, text)) = &self.measure {
            o.push(Overlay::Measure { p: *p, text: text.clone() });
        }
        if let Some(s) = self.start.filter(|_| self.began && cx.measurement_labels) {
            let p = self.last + Vec2::new(cx.tol(12.0), cx.tol(12.0));
            let text = if self.id == "lineSegment" {
                let (a, b) = tangent.unwrap_or((s, self.last));
                cx.segment_measure_label(a, b)
            } else {
                // A box's size as drawn (from its centre with Alt, square with Shift).
                match self.id {
                    "star" => {
                        let outer = (self.last - s).hypot().max(0.01);
                        // The same ratio `command_at` draws with, so the label matches the preview
                        // (Ctrl/Cmd's held radius is already in `star_ratio`, clamped; Alt overrides it).
                        let ratio = if self.mods.alt { straight_ratio(self.points).unwrap_or(self.star_ratio) } else { self.star_ratio };
                        cx.star_radii_label(outer, outer * ratio)
                    }
                    _ => {
                        let (w, h) = match self.id {
                            "polygon" => ((self.last - s).x.abs(), (self.last - s).y.abs()),
                            _ => {
                                // The corner Snap to Tangent moved a circle's to, as the preview draws it.
                                let corner = self.tangent_corner.map_or(self.last, |d| s + d);
                                let (r, _) = turned_box(s, corner, self.mods, cx.constrain_angle);
                                (r.width(), r.height())
                            }
                        };
                        cx.size_label(w, h)
                    }
                }
            };
            o.push(Overlay::Measure { p, text });
        }
        o
    }
    /// The Selection tool's pointer over a Live Shape widget, a handle of the selection's box, its
    /// centre point or the rotate zone just outside its box's corner; the crosshair everywhere
    /// else, where a press draws.
    fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
        // Over a widget a press would drag (or dragging one), the Selection tool's pointer for it.
        let idle = self.start.is_none();
        match &self.widget {
            Some(WidgetDrag::Corner(_)) => return Cursor::CornerRadius,
            Some(WidgetDrag::Pie(_) | WidgetDrag::Sides(_)) => return Cursor::ArrowHollow,
            None => {}
        }
        // Alt-dragging the selection leaves a copy behind; otherwise the arrow, hollow while the
        // move snaps, as the Selection tool's.
        match &self.selection {
            Some(SelectionDrag::Rotate { center, .. }) => return Cursor::rotate_about_in_view(*center, p, cx.view_angle()),
            Some(SelectionDrag::Scale { handle, bx, .. }) => return handle_cursor(*handle, bx.angle - cx.view_angle()),
            Some(SelectionDrag::Move { began: true, .. }) if m.alt => return Cursor::SelectionCopy,
            Some(SelectionDrag::Move { began: true, .. }) => return if self.snapped { Cursor::ArrowHollow } else { Cursor::Arrow },
            Some(_) => return Cursor::Arrow,
            None => {}
        }
        // A press takes the corner and pie widgets first, then the box's handles, then the centre
        // point, then the zone just outside a corner; everywhere else it draws a new shape. A
        // Shift-press that places another point for a circle skips them all.
        if idle && !self.placing_point(m) {
            if shows_shape_widgets(self.id) && corners::over_widget(cx, p, false) {
                return Cursor::CornerRadius;
            }
            if self.over_widget(cx, p) {
                return Cursor::ArrowHollow;
            }
            if let Some((handle, bx)) = self.over_handle(cx, p) {
                return handle_cursor(handle, bx.angle - cx.view_angle());
            }
            if self.over_center(cx, p) {
                return Cursor::Move;
            }
            if let Some(center) = self.over_rotate(cx, p) {
                return Cursor::rotate_about_in_view(center, p, cx.view_angle());
            }
        }
        Cursor::Crosshair
    }
    fn options(&self) -> Value {
        json!({
            "sides": self.sides,
            "points": self.points,
            "anchors": self.anchors,
            "cornerRadius": self.corner_radius,
            "starRatio": self.star_ratio,
            "length": self.line_length,
            "angle": self.line_angle,
            "fillLine": self.fill_line,
        })
    }
    fn set_option(&mut self, key: &str, v: &Value) {
        let finite = v.as_f64().filter(|n| n.is_finite());
        match key {
            "sides" => self.sides = v.as_u64().unwrap_or(6).clamp(3, 1000) as u32,
            "points" => self.points = v.as_u64().unwrap_or(5).clamp(3, 1000) as u32,
            "anchors" => self.anchors = v.as_u64().unwrap_or(4).clamp(2, 1000) as u32,
            "cornerRadius" => self.corner_radius = finite.unwrap_or(12.0).clamp(0.0, MAX_CORNER_RADIUS),
            "starRatio" => self.star_ratio = v.as_f64().unwrap_or(0.5).clamp(0.01, 1.0),
            "length" => self.line_length = finite.map_or(self.line_length, |n| n.abs().min(1e6)),
            "angle" => self.line_angle = finite.map_or(self.line_angle, |n| n.rem_euclid(360.0)),
            "fillLine" => self.fill_line = v.as_bool().unwrap_or(self.fill_line),
            _ => {}
        }
        // An option set while a shape is dragged out stays when Escape cancels the drag.
        if let Some(b) = self.before_drag.as_mut() {
            match key {
                "sides" => b.sides = self.sides,
                "points" => b.points = self.points,
                "anchors" => b.anchors = self.anchors,
                "cornerRadius" => b.corner_radius = self.corner_radius,
                "starRatio" => b.star_ratio = self.star_ratio,
                _ => {}
            }
        }
    }
    /// The options every shape tool keeps ([`Self::options`]). Any finite angle is taken: it is
    /// kept as the same direction from 0 to 360°.
    fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
        use crate::option_check as c;
        match key {
            "sides" | "points" => c::whole(key, v, 3, 1000),
            "anchors" => c::whole(key, v, 2, 1000),
            "cornerRadius" => c::number(key, v, 0.0, MAX_CORNER_RADIUS),
            "starRatio" => c::number(key, v, 0.01, 1.0),
            "length" => c::number(key, v, 0.0, 1e6),
            "angle" => c::finite(key, v),
            "fillLine" => c::boolean(key, v),
            _ => Err(c::unknown(key)),
        }
    }
}

#[cfg(test)]
mod tests;