//! Pictures, shapes and text boxes on the canvas: click one to select it, drag it to move it, //! drag a handle of its frame to resize it, drag the rotation handle to turn it (Shift: 15° steps), //! arrow keys nudge a floating one. The frame is a `TransformBox` ([`show`]); a move is an //! [`ObjectGesture`] of the page tool. Changes go through `arrange.bounds` / `arrange.rotation` / //! `arrange.nudge`, so they're undoable and scriptable like everything else. //! //! While dragging, nothing is laid out: the preview is the object drawn alone, once, when the //! drag starts (#1010: a cut of the page would carry along whatever is behind it). The page it //! was on is drawn again without it (see [`dragged_off_page`]), so it is not left behind where it //! was. The drop is one command, one relayout. //! //! Dragging an In Line with Text object moves it within the text, as in Word: a bold insertion //! cursor follows the pointer through the text (#2233, #4195) and the drop puts it there, still inline //! (`arrange.bounds` / `arrange.copyTo` with `to`, #2234). A floating one goes where it's dropped. //! //! Moving a floating one snaps it to alignment guides ([`snap`]): the page's edges and centre, the //! margins and the other floating objects on the page. Alt moves freely; File › Options › Advanced //! turns them off (`ui.alignmentGuides`). //! //! A selected connector shows a handle at each end instead of a frame: dragging one onto a shape //! glues it to the nearest connection site there (`shape.connect`), elsewhere leaves it free. //! //! Inside a Drawing Canvas, a press on a member picks that member (`select.member`, #2637): it //! gets the frame and handles, inside the canvas's frame, and is dragged, resized and turned on //! its own; a press in a text box member's text puts the caret there. The canvas itself is //! picked where no member is. use std::sync::Arc; use craft::behavior::geometry::{BoxAllow, BoxRules, BoxState, ScaleRule, SnapKind, SnapOptions, SnapSuspend, SnapTarget}; use craft::behavior::kurbo::{BezPath, Point, Rect as DocRect, Vec2}; use craft::behavior::{Axis, CanvasView, Cursor, CursorIcon, GesturePhase, Input, Metrics, Sense}; use craft::draw::ui::transform::{LineEnd, LineEnds, LineLook, RotateZone, TransformBox}; use craft::keymap::Mods; use craft::pages::ui::{PageBody, PageSnap, PlacedPage}; use craft::ui::canvas::{ToolContext, ToolPointer}; use craft::ui::canvas_controls::SelectionOutline; use craft::ui::contract::{Bind, Common, Component, Cx, MeasureCx, PaintUi, Props, Role, Show as _}; use craft::ui::primitives::{Draw, PaintShape, StrokeWidth, TextureCache}; use craft::ui::runtime::env::Label; use craft::ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx}; use egui::{Color32, CornerRadius, Pos2, Rect, Ui}; use serde_json::json; use wordcraft_doc::para::{InlineObject, OBJ, Wrap}; use wordcraft_doc::{PartKind, Pos, StoryRef}; use wordcraft_layout::DocLayout; use wordcraft_layout::hit::ObjectHit; use crate::WordApp; /// The band along a text box's border that grabs the box rather than its text, screen points. const EDGE: f32 = 5.0; /// How opaque the drag preview of an object's pixels is (0–1). const PREVIEW_SHARE: f32 = 170.0 / 255.0; /// Arrow-key nudge, points (Ctrl/Alt: fine). const NUDGE: f32 = 6.0; const NUDGE_FINE: f32 = 1.0; /// Where an In Line with Text object dragged through the text would land. #[derive(Clone)] struct Drop { /// The text position under the pointer. pos: Pos, /// The colour of the bold insertion cursor drawn there. color: Color32, /// The page the cursor is on and its box there, points. page: usize, caret: wordcraft_geom::Rect, } /// The object drawn alone over its rotated bounds, made once a move starts. struct Preview { /// The bits of the pixels-per-page-point it was drawn at, which with the object names its texture. scale_bits: u32, image: Arc, } /// The object the frame's turn handle is turning: its pixels, drawn once, turned live by the box's angle. pub(crate) struct Turning { /// The object turned. pos: Pos, preview: Preview, /// The turn so far beyond the object's own angle, degrees. delta: f32, } /// An object being moved by the page tool. pub(crate) struct ObjectGesture { /// The object pressed; `None` for a press that was taken as a click (a link followed, an /// object added to the selection) and has nothing to drag. object: Option, /// The press, screen. press: Pos2, /// The grabbed point within the frame, points. grab: (f32, f32), /// The page and rect the object started on, points. page: usize, start: wordcraft_geom::Rect, /// Where it would land: page and rect, points. to_page: usize, rect: wordcraft_geom::Rect, /// Past the drag threshold (a press that never gets there is a click). moved: bool, /// A selected Behind Text picture overlaps text: the press also goes to that text until the /// drag takes the object, so a simple click can still put the caret there. pending_behind: bool, /// Ctrl/⌘ is held: dropping leaves the original and puts a copy at the new place. copy: bool, /// The (Shift, Ctrl/⌘) state at the last move: a change by the release re-places the object (an aligned /// copy's axis lock). Alt is not part of it: letting Alt go just before the release must not snap an /// object that was placed freely while Alt was held. last_mods: (bool, bool), /// The pointer position and Alt state at the last move: releasing Alt with the pointer still /// must not re-snap the object when a modifier change stirs the pass. last_at: (Pos2, bool), /// Moving an In Line with Text object: where it lands. drop_at: Option, preview: Option, } /// The connection sites of the shapes near a dragged connector end: each a screen point, and whether it is the /// one the end would glue to (filled). #[derive(Component, Props)] #[component(canvas_control)] struct GlueSites { #[common] common: Common, sites: Vec<(Pos2, bool)>, } impl GlueSites { /// The `sites` (screen points, and whether each is the glue target), named `label`. fn new(label: impl Into