mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 20:01:07 +01:00
Undo: a command run during a tool drag is its own undo step
A command run from a panel, a menu or an automation client while a tool drag was open ran inside the drag. With Real-time Drawing on, the drag's next move put the document back to where the drag began and dropped the command's edit, and Escape dropped it too. With Real-time Drawing off, the release lost what the drag drew or moved. Such a command now runs on the document without the drag's preview and records its own undo step and journal entry. The drag then continues from the command's result: with Real-time Drawing on, its preview is applied again at once. The release records the drag's step after the command's, Undo takes back the drag first and the command next, and Escape cancels only the drag. The drag acts on the objects it started with, so a selection the command makes does not change what it moves, and the fill and stroke proxies keep showing those objects' paints. Objects the command deletes, hides or locks are left out of the drag. A change the command makes to the current layer or to the new-art paints stays when the drag ends or is canceled. When the command deletes the art being dragged, the drag records no step. A warning or an error from the preview applied again goes to the command's result. Commands that already kept the drag as a step first still do (Undo, Redo, command.batch, swatch.renameGroup, edit.newDocumentFromState and the others that call settle_interaction). Typing in progress is still kept as a step of its own first. A command run during a Quick Pen press, object.transform with copy during an Alt-drag, and a command run during a dialog's or a panel's preview still run inside the interaction. Docs: docs/mcp.md (undo / redo row), docs/target-app-parity.md.
This commit is contained in:
1 parent
db9acab4da
commit
828f43447d
14 files changed
+800
-6
No files matched your search
@@ -695,6 +695,7 @@ fn undo_group_end(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// goes to the running command's result or the tool's status messages, as the tool's own commit
|
||||
/// reports it ([`Session::note_settled_warning`]).
|
||||
pub(crate) fn settle_interaction(s: &mut Session) -> Result<()> {
|
||||
s.put_back_interaction();
|
||||
let typing = typing_in_progress(s);
|
||||
match s.unapplied_interaction_preview().filter(|_| !s.previewing) {
|
||||
Some((cmd, p)) => match s.preview(&cmd, &p) {
|
||||
@@ -894,8 +895,10 @@ fn clear_history(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
fn new_document_from_state(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "edit.newDocumentFromState";
|
||||
let created = date_param(p, "created", C)?;
|
||||
// The row as the panel lists it now, with a drag in progress on show: keeping it below
|
||||
// changes the rows.
|
||||
s.put_back_interaction();
|
||||
let st = s.doc()?;
|
||||
// The row as the panel lists it now: keeping an edit in progress below changes the rows.
|
||||
let chosen = match super::index_opt(p, "index", C)? {
|
||||
None => None,
|
||||
Some(row) => {
|
||||
|
||||
@@ -1252,7 +1252,7 @@ fn swatch_rename_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(C, format!("a swatch or color group named `{name}` already exists")));
|
||||
}
|
||||
// A drag still in progress is a step of its own, so canceling or undoing it leaves the rename.
|
||||
if !s.previewing && s.batch_stash.is_none() && s.in_interaction() {
|
||||
if !s.previewing && s.batch_stash.is_none() && (s.in_interaction() || s.holds_interaction()) {
|
||||
super::settle_interaction(s)?;
|
||||
}
|
||||
s.edit("Color Group Options", |d, _| {
|
||||
|
||||
@@ -8,6 +8,10 @@ pub struct Interaction {
|
||||
/// The selection the previews start from (shared, so a preview frame copies nothing).
|
||||
pub selection: Arc<Selection>,
|
||||
pub preview: Option<(String, Value)>,
|
||||
/// The active tool began it (a drag): a command from outside the tool runs as a step of its
|
||||
/// own under it ([`crate::Session::outside_interaction`]). A dialog or a panel that begins an
|
||||
/// interaction runs its commands inside it.
|
||||
pub(crate) tool: bool,
|
||||
/// A replaced or canceled preview restores these new-art paints from before the interaction.
|
||||
pub(crate) paint: vectorcraft_tools::PaintDefaults,
|
||||
/// An accepted batch preview uses these final paints after changing the document color mode.
|
||||
@@ -35,6 +39,61 @@ pub struct Interaction {
|
||||
pub(crate) move_box: std::sync::OnceLock<Option<vectorcraft_doc::OrientedBox>>,
|
||||
}
|
||||
|
||||
/// An interaction that a command from outside the tool set aside while it runs
|
||||
/// ([`crate::Session::outside_interaction`]).
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct HeldInteraction {
|
||||
/// The [`crate::DocState::uid`] of the interaction's document.
|
||||
pub(crate) uid: u64,
|
||||
pub(crate) interaction: Interaction,
|
||||
/// The document and the selection on show when the command began, when a preview had changed
|
||||
/// them from the interaction's snapshot.
|
||||
pub(crate) shown: Option<(Arc<Document>, Selection)>,
|
||||
/// The document's current layer, highlighted Layers panel rows, highlighted variable and
|
||||
/// isolation when the command began; a cancel puts back the ones it changed as it left them.
|
||||
pub(crate) state: ViewState,
|
||||
/// The new-art paints when the command began: a change the command makes to them stays when
|
||||
/// the interaction's next preview or a cancel puts back the paints it began with.
|
||||
pub(crate) paint: vectorcraft_tools::PaintDefaults,
|
||||
}
|
||||
|
||||
/// The per-document state an interaction puts back when it is canceled, besides the document and
|
||||
/// the selection.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) struct ViewState {
|
||||
pub(crate) active_layer: Option<NodeId>,
|
||||
pub(crate) layer_rows: Vec<NodeId>,
|
||||
pub(crate) variables_highlight: Option<String>,
|
||||
pub(crate) isolation: Option<NodeId>,
|
||||
}
|
||||
|
||||
impl ViewState {
|
||||
pub(crate) fn of(st: &crate::DocState) -> Self {
|
||||
ViewState {
|
||||
active_layer: st.active_layer,
|
||||
layer_rows: st.layer_rows.clone(),
|
||||
variables_highlight: st.variables_highlight.clone(),
|
||||
isolation: st.isolation,
|
||||
}
|
||||
}
|
||||
|
||||
/// Give `it` the parts of `now` that differ from `self`, the state before a command.
|
||||
pub(crate) fn rebase(&self, now: ViewState, it: &mut Interaction) {
|
||||
if now.active_layer != self.active_layer {
|
||||
it.active_layer = now.active_layer;
|
||||
}
|
||||
if now.layer_rows != self.layer_rows {
|
||||
it.layer_rows = now.layer_rows;
|
||||
}
|
||||
if now.variables_highlight != self.variables_highlight {
|
||||
it.variables_highlight = now.variables_highlight;
|
||||
}
|
||||
if now.isolation != self.isolation {
|
||||
it.isolation = now.isolation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What Object → Transform → Transform Again (⌘D) repeats ([`SessionState::last_transform`]).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum LastTransform {
|
||||
|
||||
@@ -320,6 +320,9 @@ pub struct Session {
|
||||
/// The warnings of the previews that settling an interaction applied, while a top-level command
|
||||
/// or the tool's actions collect them ([`Session::collect_settled_warnings`]).
|
||||
settled_warnings: Option<Vec<String>>,
|
||||
/// The interaction a command from outside the tool set aside while it runs
|
||||
/// ([`Session::outside_interaction`]).
|
||||
held_interaction: Option<HeldInteraction>,
|
||||
/// While [`Session::preview`] runs its command: its edits are the interaction's own.
|
||||
previewing: bool,
|
||||
/// The images Remove Background decoded during the interaction's preview steps, emptied when
|
||||
@@ -438,7 +441,7 @@ pub use error::{EngineError, READ_ONLY_LOCATION, Result, SCRIPT_BUSY};
|
||||
#[cfg(test)]
|
||||
use history::history_budget_for;
|
||||
pub use history::{History, HistoryEntry, NamedStep, default_history_budget};
|
||||
pub(crate) use interaction::PreviewCopies;
|
||||
pub(crate) use interaction::{HeldInteraction, PreviewCopies};
|
||||
pub use interaction::{Interaction, LastTransform, UndoGroup, UndoScope};
|
||||
pub use prefs::{DrawMode, KEY_HOLD_DELAY, KEY_HOLD_DELAY_RANGE, PluginBudget, Prefs};
|
||||
pub use sanity::TOO_DEEP;
|
||||
|
||||
@@ -137,6 +137,7 @@ impl Session {
|
||||
recovery: Default::default(),
|
||||
in_tool_actions: false,
|
||||
settled_warnings: None,
|
||||
held_interaction: None,
|
||||
previewing: false,
|
||||
decoded_images: Default::default(),
|
||||
envelope_defaults: None,
|
||||
|
||||
@@ -260,6 +260,163 @@ impl Session {
|
||||
(r, settled)
|
||||
}
|
||||
|
||||
/// Run `f`, the top-level command `id` with `params` from outside the tool (a panel, a menu
|
||||
/// item, an automation client), with the active document's tool drag set aside: the command
|
||||
/// runs on the document the drag started from and records its own undo step and journal entry.
|
||||
/// The drag then starts from what the command left, and its preview is applied again, so the
|
||||
/// next preview, the commit and a cancel keep the command's edit, and Undo takes back the drag's
|
||||
/// step before the command's. While the command runs, [`Session::in_interaction`] is false, so
|
||||
/// it updates Recent Colors and converts or remaps the new-art paints at once, as it does with
|
||||
/// no drag in progress.
|
||||
///
|
||||
/// The interaction stays as it is, and the command runs inside it, for an interaction a dialog
|
||||
/// or a panel began (it builds its preview from commands), for typing in progress
|
||||
/// ([`Session::edit`] keeps it as a step of its own), for the Quick Pen (an edit made outside
|
||||
/// it ends the path it draws), and for a copy made with `object.transform` during a drag that
|
||||
/// copies (it copies the document as the drag's move left it). A command that settles,
|
||||
/// commits or cancels the interaction, or that hands the active tool actions to apply, gets it
|
||||
/// back first ([`Session::put_back_interaction`]), as it found it.
|
||||
pub(crate) fn outside_interaction(&mut self, id: &str, params: &Value, f: impl FnOnce(&mut Self) -> Result<Value>) -> Result<Value> {
|
||||
if cmd::typing_in_progress(self) || self.tool_id() == vectorcraft_tools::quickpen::ID {
|
||||
return f(self);
|
||||
}
|
||||
let Some(st) = self.active_mut() else { return f(self) };
|
||||
let Some(it) = st.interaction.as_ref().filter(|it| it.tool) else { return f(self) };
|
||||
// A drag that copies: its latest preview, applied or held back by Real-time Drawing off, copies.
|
||||
let copying = it.preview.as_ref().is_some_and(|(c, p)| c == "object.transform" && p.get("copy").and_then(Value::as_bool) == Some(true));
|
||||
if copying && id == "object.transform" && params.get("copy").and_then(Value::as_bool) == Some(true) {
|
||||
return f(self);
|
||||
}
|
||||
let Some(interaction) = st.interaction.take() else { return f(self) };
|
||||
// The document and selection on show, when a preview changed them.
|
||||
let previewed = !Arc::ptr_eq(&st.doc, &interaction.doc) || st.selection != *interaction.selection;
|
||||
let shown = previewed.then(|| (st.doc.clone(), st.selection.clone()));
|
||||
if previewed {
|
||||
st.doc = interaction.doc.clone();
|
||||
st.selection.clone_from(&interaction.selection);
|
||||
st.revision += 1;
|
||||
cmd::globaledit::follow_history(st);
|
||||
}
|
||||
let (uid, state) = (st.uid, crate::interaction::ViewState::of(st));
|
||||
let paint = self.paint.clone();
|
||||
self.held_interaction = Some(crate::HeldInteraction { uid, interaction, shown, state, paint });
|
||||
if previewed {
|
||||
self.sync_front_profiles();
|
||||
}
|
||||
let r = f(self);
|
||||
self.put_back_interaction();
|
||||
r
|
||||
}
|
||||
|
||||
/// Put back the interaction [`Session::outside_interaction`] set aside. When the command left
|
||||
/// the document and the selection as it found them, the preview on show before it is on show
|
||||
/// again. Otherwise the interaction starts from the document the command left and from its
|
||||
/// own selection, without the objects the command removed, hid or locked, and the preview that Real-time
|
||||
/// Drawing showed is applied to it again. The warning or the error of that preview goes to
|
||||
/// the command's result ([`Session::note_settled_warning`]); a preview that fails leaves the
|
||||
/// interaction with nothing to commit. An interaction whose document the command closed ends,
|
||||
/// and a document the command left an interaction of its own on keeps that one.
|
||||
pub(crate) fn put_back_interaction(&mut self) {
|
||||
let Some(crate::HeldInteraction { uid, interaction: mut it, shown, state, paint }) = self.held_interaction.take() else { return };
|
||||
// A selection command loaded the paints of the selection it made into the fill and stroke
|
||||
// proxies. The drag goes on with its own selection, so they show its paints again.
|
||||
if self.paint != paint
|
||||
&& let Some(st) = self.active_mut().filter(|d| d.uid == uid && d.interaction.is_none())
|
||||
{
|
||||
let own = drag_selection(&it.selection, &st.doc);
|
||||
if st.selection != own {
|
||||
let made = std::mem::replace(&mut st.selection, own);
|
||||
self.inherit_new_art();
|
||||
self.load_selection_paints();
|
||||
if let Some(st) = self.active_mut() {
|
||||
st.selection = made;
|
||||
}
|
||||
}
|
||||
}
|
||||
// A change the command made to the new-art paints stays when the next preview or a cancel
|
||||
// puts back the paints the interaction began with (the Live Paint Bucket's colors); the
|
||||
// parts it left alone keep the interaction's.
|
||||
let (now, start) = (&self.paint, &mut it.paint);
|
||||
if now.fill != paint.fill {
|
||||
start.fill.clone_from(&now.fill);
|
||||
}
|
||||
if now.stroke != paint.stroke {
|
||||
start.stroke.clone_from(&now.stroke);
|
||||
}
|
||||
if now.stroke_width != paint.stroke_width {
|
||||
start.stroke_width = now.stroke_width;
|
||||
}
|
||||
if now.appearance != paint.appearance {
|
||||
start.appearance.clone_from(&now.appearance);
|
||||
}
|
||||
if now.opacity != paint.opacity {
|
||||
start.opacity = now.opacity;
|
||||
}
|
||||
if now.blend != paint.blend {
|
||||
start.blend = now.blend;
|
||||
}
|
||||
if now.style != paint.style {
|
||||
start.style.clone_from(&now.style);
|
||||
}
|
||||
if now.inherited != paint.inherited {
|
||||
start.inherited = now.inherited;
|
||||
}
|
||||
let active = self.active().is_some_and(|d| d.uid == uid);
|
||||
let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) else { return };
|
||||
if st.interaction.is_some() {
|
||||
return;
|
||||
}
|
||||
// A change the command made to the current layer, the highlighted rows or variable, or
|
||||
// isolation stays when the interaction is canceled.
|
||||
state.rebase(crate::interaction::ViewState::of(st), &mut it);
|
||||
let previewed = shown.is_some();
|
||||
if Arc::ptr_eq(&st.doc, &it.doc) && st.selection == *it.selection {
|
||||
if let Some((doc, selection)) = shown {
|
||||
st.doc = doc;
|
||||
st.selection = selection;
|
||||
st.revision += 1;
|
||||
cmd::globaledit::follow_history(st);
|
||||
}
|
||||
st.interaction = Some(it);
|
||||
if previewed {
|
||||
self.sync_front_profiles();
|
||||
}
|
||||
return;
|
||||
}
|
||||
it.doc = st.doc.clone();
|
||||
// The drag acts on the objects it began with: a selection the command made (a paste, a
|
||||
// duplicate, Select All) does not change what the drag moves.
|
||||
it.selection = Arc::new(drag_selection(&it.selection, &st.doc));
|
||||
it.copies = None;
|
||||
it.move_box = std::sync::OnceLock::new();
|
||||
// With Real-time Drawing off the preview waits for the commit, as it did before the command.
|
||||
let again = it.preview.clone().filter(|_| previewed && active);
|
||||
// Without a preview to apply, the drag's own selection is on show, as a preview would leave
|
||||
// it before it acts.
|
||||
if again.is_none() && st.selection != *it.selection {
|
||||
st.selection.clone_from(&it.selection);
|
||||
st.revision += 1;
|
||||
}
|
||||
st.interaction = Some(it);
|
||||
self.clear_deferred_overlay_cache();
|
||||
if let Some((cmd, p)) = again {
|
||||
match self.preview(&cmd, &p) {
|
||||
Ok(v) => {
|
||||
if let Some(w) = v.get("warning").and_then(Value::as_str) {
|
||||
self.note_settled_warning(w);
|
||||
}
|
||||
}
|
||||
Err(e) => self.note_settled_warning(&e.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// While a command from outside the tool runs: the interaction it set aside
|
||||
/// ([`Session::outside_interaction`]).
|
||||
pub(crate) fn holds_interaction(&self) -> bool {
|
||||
self.held_interaction.is_some()
|
||||
}
|
||||
|
||||
/// [`Session::begin_interaction`] for a dialog or a panel that starts a preview → the warnings
|
||||
/// of the preview that keeping typing in progress applied, for its status bar.
|
||||
pub fn begin_interaction_warned(&mut self, label: &str) -> Result<Vec<String>> {
|
||||
@@ -284,6 +441,8 @@ impl Session {
|
||||
if label != "Typing" && cmd::typing_in_progress(self) {
|
||||
cmd::settle_interaction(self)?;
|
||||
}
|
||||
// A command from outside the tool that begins an interaction during a drag joins the drag's.
|
||||
self.put_back_interaction();
|
||||
// An edit the tool computes on another thread (a Shape Builder merge) is applied first too,
|
||||
// so the snapshot the interaction previews from and goes back to holds it.
|
||||
if !self.in_interaction() {
|
||||
@@ -300,6 +459,7 @@ impl Session {
|
||||
doc: st.doc.clone(),
|
||||
selection: Arc::new(st.selection.clone()),
|
||||
preview: None,
|
||||
tool: false,
|
||||
paint,
|
||||
preview_paint: None,
|
||||
color_mode_paints: None,
|
||||
@@ -318,6 +478,7 @@ impl Session {
|
||||
|
||||
/// Re-apply `cmd` on top of the interaction snapshot (replacing the previous preview).
|
||||
pub fn preview(&mut self, cmd: &str, params: &Value) -> Result<Value> {
|
||||
self.put_back_interaction();
|
||||
if self.script_running() {
|
||||
return Err(EngineError::Busy);
|
||||
}
|
||||
@@ -392,6 +553,7 @@ impl Session {
|
||||
pub fn commit_interaction(&mut self) -> Result<()> {
|
||||
self.clear_deferred_overlay_cache();
|
||||
self.decoded_images.empty();
|
||||
self.put_back_interaction();
|
||||
if let Some(paint) = self.active().and_then(|st| st.interaction.as_ref()).and_then(|it| it.preview_paint.clone()) {
|
||||
self.paint = paint;
|
||||
}
|
||||
@@ -481,6 +643,7 @@ impl Session {
|
||||
}
|
||||
|
||||
pub fn cancel_interaction(&mut self) -> Result<()> {
|
||||
self.put_back_interaction();
|
||||
self.clear_deferred_overlay_cache();
|
||||
self.decoded_images.empty();
|
||||
self.pending_paint = None;
|
||||
@@ -512,3 +675,14 @@ impl Session {
|
||||
self.active().is_some_and(|d| d.interaction.is_some())
|
||||
}
|
||||
}
|
||||
|
||||
/// The selection a drag began with, without the objects that a command run during it removed,
|
||||
/// hid or locked in `doc`.
|
||||
fn drag_selection(began: &Selection, doc: &Document) -> Selection {
|
||||
let mut selection = began.clone();
|
||||
selection.prune(doc);
|
||||
let editable = doc.editable();
|
||||
let held: Vec<_> = selection.objects.iter().copied().filter(|&id| !editable(id)).collect();
|
||||
selection.remove_all(held);
|
||||
selection
|
||||
}
|
||||
@@ -75,10 +75,15 @@ impl Session {
|
||||
self.finish_tool_edit_later()?;
|
||||
}
|
||||
let outer = std::mem::replace(&mut self.reading, !spec.journal || JOURNALED_READS.contains(&id));
|
||||
// A command from outside the tool, while the tool's drag is in progress, is a step of its
|
||||
// own before the drag's ([`Session::outside_interaction`]).
|
||||
let outside = spec.journal && !self.previewing && !self.in_tool_actions;
|
||||
// A preview that a top-level command applied first (a drag with Real-time Drawing off, kept
|
||||
// before Undo) reports its warning in the command's result.
|
||||
let r = if self.depth == 0 {
|
||||
let (r, settled) = self.collect_settled_warnings(|s| s.execute_spec(spec, id, params));
|
||||
let (r, settled) = self.collect_settled_warnings(|s| {
|
||||
if outside { s.outside_interaction(id, params, |s| s.execute_spec(spec, id, params)) } else { s.execute_spec(spec, id, params) }
|
||||
});
|
||||
r.map(|v| with_settled_warnings(v, &settled))
|
||||
} else {
|
||||
self.execute_spec(spec, id, params)
|
||||
|
||||
@@ -87,6 +87,7 @@ mod history_document;
|
||||
mod history_mem;
|
||||
mod ids_without_selection;
|
||||
mod inline;
|
||||
mod interaction_commands;
|
||||
mod interactivity;
|
||||
mod interpolate_handles;
|
||||
mod intertwine;
|
||||
|
||||
@@ -0,0 +1,485 @@
|
||||
//! A command run from outside the tool (a panel, a menu item, an automation client) while a tool's drag is
|
||||
//! in progress is a step of its own, before the drag's: the drag's next preview, its release and
|
||||
//! Escape keep the command's edit, and the release still draws what the drag drew, with Real-time
|
||||
//! Drawing on and off.
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_geom::Rect;
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// The view the gestures run in: page coordinates, with Smart Guides off so nothing snaps.
|
||||
fn view() -> ViewInfo {
|
||||
ViewInfo { smart_guides: false, ..ViewInfo::default() }
|
||||
}
|
||||
|
||||
/// Feed the pointer event `(kind, x, y)` to the active tool.
|
||||
fn pointer(s: &mut Session, kind: PointerKind, x: f64, y: f64) {
|
||||
s.pointer(&PointerEvent::new(kind, x, y), view()).unwrap();
|
||||
}
|
||||
|
||||
/// A new 400 × 400 pt document with Real-time Drawing `real_time`, holding one 20 × 20 pt rectangle
|
||||
/// (returned), nothing selected, and the Rectangle tool chosen.
|
||||
fn session(real_time: bool) -> (Session, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.prefs.real_time_drawing = real_time;
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
let other = s.doc().unwrap().selection.objects[0];
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.select_tool("rectangle", view()).unwrap();
|
||||
(s, other)
|
||||
}
|
||||
|
||||
fn visible(s: &Session, id: NodeId) -> bool {
|
||||
s.doc().unwrap().doc.node(id).expect("the object is in the document").visible
|
||||
}
|
||||
|
||||
/// The objects of the document's first layer.
|
||||
fn objects(s: &Session) -> usize {
|
||||
s.doc().unwrap().doc.layers[0].children().map_or(0, Vec::len)
|
||||
}
|
||||
|
||||
/// The commands journaled from entry `start` on.
|
||||
fn journaled(s: &Session, start: usize) -> Vec<String> {
|
||||
s.journal.iter().skip(start).map(|(id, _)| id.clone()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_command_during_a_drag_is_a_step_of_its_own_before_the_drag() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, other) = session(real_time);
|
||||
let (steps, start) = (s.doc().unwrap().history.undo.len(), s.journal.len());
|
||||
pointer(&mut s, PointerKind::Down, 100.0, 100.0);
|
||||
pointer(&mut s, PointerKind::Drag, 150.0, 150.0);
|
||||
s.execute("object.hide", &json!({"ids": [other.0]})).unwrap();
|
||||
assert!(!visible(&s, other), "real time {real_time}: the command hides the object at once");
|
||||
pointer(&mut s, PointerKind::Drag, 200.0, 180.0);
|
||||
assert!(!visible(&s, other), "real time {real_time}: the drag's next preview keeps the command's edit");
|
||||
pointer(&mut s, PointerKind::Up, 200.0, 180.0);
|
||||
assert!(!visible(&s, other), "real time {real_time}: the release keeps the command's edit");
|
||||
assert_eq!(objects(&s), 2, "real time {real_time}: the release draws the rectangle");
|
||||
let drawn = s.doc().unwrap().selection.objects[0];
|
||||
assert_ne!(drawn, other);
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[drawn], false), Some(Rect::new(100.0, 100.0, 200.0, 180.0)), "real time {real_time}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "real time {real_time}: the command and the drag are a step each");
|
||||
assert_eq!(journaled(&s, start), ["object.hide", "shape.rectangle"], "real time {real_time}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(objects(&s), 1, "real time {real_time}: Undo takes back the drawn rectangle");
|
||||
assert!(!visible(&s, other), "real time {real_time}: and leaves the command's edit");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(visible(&s, other), "real time {real_time}: the next Undo takes back the command's edit");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_after_a_command_during_a_drag_keeps_the_command() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, other) = session(real_time);
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
pointer(&mut s, PointerKind::Down, 100.0, 100.0);
|
||||
pointer(&mut s, PointerKind::Drag, 150.0, 150.0);
|
||||
s.execute("object.hide", &json!({"ids": [other.0]})).unwrap();
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), view()).unwrap();
|
||||
pointer(&mut s, PointerKind::Up, 150.0, 150.0);
|
||||
assert!(!visible(&s, other), "real time {real_time}: Escape takes back only the drag");
|
||||
assert_eq!(objects(&s), 1, "real time {real_time}: nothing is drawn");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "real time {real_time}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(visible(&s, other), "real time {real_time}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Every object of the first layer but `other`, as the document has it.
|
||||
fn others(s: &Session, other: NodeId) -> Vec<vectorcraft_doc::Node> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
d.layers[0].children().map_or_else(Vec::new, |c| c.iter().filter(|n| n.id != other).map(|n| (**n).clone()).collect())
|
||||
}
|
||||
|
||||
/// A new 400 × 400 pt document with Real-time Drawing `real_time`, holding a 40 × 40 pt square at
|
||||
/// (100, 100) and a 20 × 20 pt one at (10, 10) → the session, the larger square (selected) and the
|
||||
/// smaller one.
|
||||
fn two_squares(real_time: bool) -> (Session, NodeId, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.prefs.real_time_drawing = real_time;
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
|
||||
let other = s.doc().unwrap().selection.objects[0];
|
||||
s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 40, "height": 40})).unwrap();
|
||||
let target = s.doc().unwrap().selection.objects[0];
|
||||
(s, target, other)
|
||||
}
|
||||
|
||||
/// A press at the first point of `pts`, drags to the others and a release at the last, with
|
||||
/// `mid` run after the first drag.
|
||||
fn drag_with(s: &mut Session, pts: &[(f64, f64)], mid: impl FnOnce(&mut Session)) {
|
||||
let (&(x0, y0), rest) = pts.split_first().expect("a press");
|
||||
pointer(s, PointerKind::Down, x0, y0);
|
||||
let mut mid = Some(mid);
|
||||
for &(x, y) in rest {
|
||||
pointer(s, PointerKind::Drag, x, y);
|
||||
if let Some(f) = mid.take() {
|
||||
f(s);
|
||||
}
|
||||
}
|
||||
let &(x, y) = pts.last().expect("a release");
|
||||
pointer(s, PointerKind::Up, x, y);
|
||||
}
|
||||
|
||||
/// Is `id` filled red?
|
||||
fn red(s: &Session, id: NodeId) -> bool {
|
||||
s.doc().unwrap().doc.node(id).and_then(|n| n.appearance.fill_paint().color()).is_some_and(|c| c.to_hex() == "#ff0000")
|
||||
}
|
||||
|
||||
/// The Selection tool's move, the Scale and Rotate tools and the Pen, each with a command that
|
||||
/// fills another object red during the drag: the release keeps both, as two undo steps, and Undo
|
||||
/// takes back the drag first.
|
||||
#[test]
|
||||
fn the_move_scale_rotate_and_pen_drags_keep_a_command_run_during_them() {
|
||||
let drags: [(&str, &[(f64, f64)]); 4] = [
|
||||
("selection", &[(120.0, 120.0), (140.0, 130.0), (170.0, 150.0)]),
|
||||
("scale", &[(150.0, 150.0), (170.0, 170.0), (190.0, 190.0)]),
|
||||
("rotate", &[(160.0, 120.0), (150.0, 150.0), (120.0, 160.0)]),
|
||||
("pen", &[(300.0, 300.0), (320.0, 300.0), (350.0, 320.0)]),
|
||||
];
|
||||
for (tool, pts) in drags {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("{tool}, real time {real_time}");
|
||||
let (mut s, _, other) = two_squares(real_time);
|
||||
if tool == "pen" {
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
}
|
||||
s.select_tool(tool, view()).unwrap();
|
||||
let (before, steps) = (others(&s, other), s.doc().unwrap().history.undo.len());
|
||||
drag_with(&mut s, pts, |s| {
|
||||
s.execute("paint.setFill", &json!({"ids": [other.0], "color": "#ff0000"})).unwrap();
|
||||
});
|
||||
assert!(!s.in_interaction(), "{at}");
|
||||
assert!(red(&s, other), "{at}: the release keeps the command's edit");
|
||||
assert_ne!(others(&s, other), before, "{at}: the release keeps the drag's edit");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "{at}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(others(&s, other), before, "{at}: Undo takes back the drag");
|
||||
assert!(red(&s, other), "{at}: and leaves the command's edit");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!red(&s, other), "{at}: the next Undo takes back the command's edit");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that changes the object being moved: the move applies to the changed object.
|
||||
#[test]
|
||||
fn a_move_drag_moves_the_object_as_a_command_during_it_changed_it() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let fill = |s: &Session| s.doc().unwrap().doc.node(target).unwrap().appearance.fill_paint().color().map(|c| c.to_hex());
|
||||
let was = fill(&s);
|
||||
drag_with(&mut s, &[(120.0, 120.0), (140.0, 130.0), (170.0, 150.0)], |s| {
|
||||
s.execute("paint.setFill", &json!({"ids": [target.0], "color": "#ff0000"})).unwrap();
|
||||
});
|
||||
let moved = s.doc().unwrap().doc.bounds_of(&[target], false);
|
||||
assert_eq!(moved, Some(Rect::new(150.0, 130.0, 190.0, 170.0)), "real time {real_time}");
|
||||
assert_eq!(fill(&s).as_deref(), Some("#ff0000"), "real time {real_time}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(100.0, 100.0, 140.0, 140.0)), "real time {real_time}");
|
||||
assert_eq!(fill(&s).as_deref(), Some("#ff0000"), "real time {real_time}: the fill is a step of its own");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(fill(&s), was, "real time {real_time}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that deletes the object being moved: the drag goes on and ends without an edit of
|
||||
/// its own, on release and on Escape, and the deletion stays.
|
||||
#[test]
|
||||
fn a_move_drag_of_an_object_a_command_deleted_ends_without_an_edit() {
|
||||
for (real_time, escape) in [(true, false), (false, false), (true, true), (false, true)] {
|
||||
let at = format!("real time {real_time}, Escape {escape}");
|
||||
let (mut s, target, other) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
pointer(&mut s, PointerKind::Down, 120.0, 120.0);
|
||||
pointer(&mut s, PointerKind::Drag, 140.0, 130.0);
|
||||
s.execute("edit.clear", &json!({"ids": [target.0]})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.node(target).is_none(), "{at}");
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
if escape {
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), view()).unwrap();
|
||||
}
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
assert!(!s.in_interaction(), "{at}");
|
||||
assert!(s.doc().unwrap().doc.node(target).is_none(), "{at}: the deletion stays");
|
||||
assert_eq!(others(&s, other), [], "{at}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "{at}: only the deletion is a step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(100.0, 100.0, 140.0, 140.0)), "{at}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that hides or locks the object being moved: the object stays where it was and
|
||||
/// unselected, and the drag ends without an edit of its own.
|
||||
#[test]
|
||||
fn a_move_drag_of_an_object_a_command_hid_or_locked_leaves_it_alone() {
|
||||
for command in ["object.hide", "object.lock"] {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("{command}, real time {real_time}");
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
drag_with(&mut s, &[(120.0, 120.0), (140.0, 130.0), (170.0, 150.0)], |s| {
|
||||
s.execute(command, &json!({"ids": [target.0]})).unwrap();
|
||||
});
|
||||
assert!(!s.in_interaction(), "{at}");
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(100.0, 100.0, 140.0, 140.0)), "{at}: not moved");
|
||||
assert!(!s.doc().unwrap().selection.objects.contains(&target), "{at}: not selected");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "{at}: only the command is a step");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The commands that end a drag first still do: Undo during a drag keeps the drag as a step and
|
||||
/// takes it back, and `command.batch` and `swatch.renameGroup` keep it as a step before their own.
|
||||
#[test]
|
||||
fn undo_and_a_batch_during_a_drag_still_keep_the_drag_first() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, target, other) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
pointer(&mut s, PointerKind::Down, 120.0, 120.0);
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!s.in_interaction(), "real time {real_time}");
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(100.0, 100.0, 140.0, 140.0)), "real time {real_time}");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(150.0, 130.0, 190.0, 170.0)), "real time {real_time}");
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
|
||||
let (mut s, target, other2) = two_squares(real_time);
|
||||
assert_eq!(other, other2);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
pointer(&mut s, PointerKind::Down, 120.0, 120.0);
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
s.execute("command.batch", &json!({"commands": [{"command": "object.hide", "params": {"ids": [other.0]}}]})).unwrap();
|
||||
assert!(!s.in_interaction(), "real time {real_time}: the batch keeps the drag first");
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "real time {real_time}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(visible(&s, other), "real time {real_time}: Undo takes back the batch first");
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(150.0, 130.0, 190.0, 170.0)), "real time {real_time}");
|
||||
|
||||
// A color group renamed during the drag keeps the drag as a step before the rename.
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
pointer(&mut s, PointerKind::Down, 120.0, 120.0);
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
s.execute("swatch.renameGroup", &json!({"group": "Brights", "name": "Vivid"})).unwrap();
|
||||
assert!(!s.in_interaction(), "real time {real_time}: the rename keeps the drag first");
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "real time {real_time}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.swatch_groups.iter().any(|g| g.name == "Brights"), "real time {real_time}: Undo takes back the rename");
|
||||
assert_eq!(s.doc().unwrap().doc.bounds_of(&[target], false), Some(Rect::new(150.0, 130.0, 190.0, 170.0)), "real time {real_time}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A copy made with `object.transform` during an Alt-drag runs inside the drag, with Real-time
|
||||
/// Drawing on and off: the release records one step that holds both.
|
||||
#[test]
|
||||
fn a_copy_during_an_alt_drag_is_part_of_the_drag() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
let alt = Mods { alt: true, ..Mods::default() };
|
||||
for (kind, x, y) in [(PointerKind::Down, 120.0, 120.0), (PointerKind::Drag, 170.0, 150.0)] {
|
||||
s.pointer(&PointerEvent::new(kind, x, y).with_mods(alt), view()).unwrap();
|
||||
}
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, 0, 60], "copy": true, "ids": [target.0]})).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 170.0, 150.0).with_mods(alt), view()).unwrap();
|
||||
assert!(!s.in_interaction(), "real time {real_time}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "real time {real_time}: one step for the drag and the copy");
|
||||
|
||||
// With Alt let go during the drag, the drag moves again, and the copy is a step of its own.
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 120.0, 120.0).with_mods(alt), view()).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, 150.0, 140.0).with_mods(alt), view()).unwrap();
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, 0, 60], "copy": true, "ids": [target.0]})).unwrap();
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "real time {real_time}: the copy and the move");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that changes only the current layer during a drag keeps its change through Escape.
|
||||
#[test]
|
||||
fn escape_keeps_a_current_layer_set_during_a_drag() {
|
||||
for real_time in [true, false] {
|
||||
let (mut s, _) = session(real_time);
|
||||
let first = s.doc().unwrap().active_layer;
|
||||
let second = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
let first = first.expect("a current layer");
|
||||
s.execute("layer.setCurrent", &json!({"id": first.0})).unwrap();
|
||||
pointer(&mut s, PointerKind::Down, 100.0, 100.0);
|
||||
pointer(&mut s, PointerKind::Drag, 150.0, 150.0);
|
||||
s.execute("layer.setCurrent", &json!({"id": second.0})).unwrap();
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), view()).unwrap();
|
||||
pointer(&mut s, PointerKind::Up, 150.0, 150.0);
|
||||
assert_eq!(s.doc().unwrap().active_layer, Some(second), "real time {real_time}");
|
||||
}
|
||||
}
|
||||
|
||||
/// With Real-time Drawing on, the drag's preview is on show again right after the command.
|
||||
#[test]
|
||||
fn the_drag_preview_shows_again_after_the_command() {
|
||||
let (mut s, other) = session(true);
|
||||
pointer(&mut s, PointerKind::Down, 100.0, 100.0);
|
||||
pointer(&mut s, PointerKind::Drag, 150.0, 150.0);
|
||||
assert_eq!(objects(&s), 2, "the preview draws the rectangle");
|
||||
s.execute("object.hide", &json!({"ids": [other.0]})).unwrap();
|
||||
assert_eq!(objects(&s), 2, "the preview is on show after the command");
|
||||
s.execute("object.showAll", &json!({})).unwrap();
|
||||
assert_eq!(objects(&s), 2, "and after a second one");
|
||||
assert!(visible(&s, other));
|
||||
assert!(s.in_interaction());
|
||||
pointer(&mut s, PointerKind::Up, 150.0, 150.0);
|
||||
assert_eq!(objects(&s), 2);
|
||||
assert!(visible(&s, other));
|
||||
}
|
||||
|
||||
/// A command that changes the selection during a move drag (Select All, Deselect, a duplicate)
|
||||
/// leaves the drag moving the object it began with.
|
||||
#[test]
|
||||
fn a_selection_made_during_a_move_drag_leaves_the_drag_moving_its_object() {
|
||||
for command in ["select.all", "select.none", "edit.duplicate"] {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("{command}, real time {real_time}");
|
||||
let (mut s, target, other) = two_squares(real_time);
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
drag_with(&mut s, &[(120.0, 120.0), (140.0, 130.0), (170.0, 150.0)], |s| {
|
||||
s.execute(command, &json!({})).unwrap();
|
||||
});
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(d.bounds_of(&[target], false), Some(Rect::new(150.0, 130.0, 190.0, 170.0)), "{at}: the drag moves its object");
|
||||
assert_eq!(d.bounds_of(&[other], false), Some(Rect::new(10.0, 10.0, 30.0, 30.0)), "{at}: and nothing else");
|
||||
if command == "edit.duplicate" {
|
||||
let copies = others(&s, other).into_iter().filter(|n| n.id != target).map(|n| n.id).collect::<Vec<_>>();
|
||||
assert_eq!(copies.len(), 1, "{at}");
|
||||
assert_eq!(d.bounds_of(&copies, false), Some(Rect::new(100.0, 100.0, 140.0, 140.0)), "{at}: the copy stays where it was made");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A command that selects another object during a move drag: the drag's object stays selected,
|
||||
/// and the fill and stroke proxies keep showing its paints.
|
||||
#[test]
|
||||
fn a_selection_made_during_a_move_drag_leaves_the_paints_of_the_drag_object() {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("real time {real_time}");
|
||||
let (mut s, target, other) = two_squares(real_time);
|
||||
s.execute("paint.setFill", &json!({"ids": [target.0], "color": "#ff0000"})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"ids": [other.0], "color": "#0000ff"})).unwrap();
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [target.0]})).unwrap();
|
||||
s.select_tool("selection", view()).unwrap();
|
||||
let fill = |s: &Session| s.paint.fill.color().map(|c| c.to_hex());
|
||||
assert_eq!(fill(&s).as_deref(), Some("#ff0000"), "{at}");
|
||||
pointer(&mut s, PointerKind::Down, 120.0, 120.0);
|
||||
pointer(&mut s, PointerKind::Drag, 140.0, 130.0);
|
||||
s.execute("select.set", &json!({"ids": [other.0]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [target], "{at}");
|
||||
assert_eq!(fill(&s).as_deref(), Some("#ff0000"), "{at}: after the command");
|
||||
pointer(&mut s, PointerKind::Drag, 170.0, 150.0);
|
||||
pointer(&mut s, PointerKind::Up, 170.0, 150.0);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [target], "{at}");
|
||||
assert_eq!(fill(&s).as_deref(), Some("#ff0000"), "{at}: after the release");
|
||||
}
|
||||
}
|
||||
|
||||
/// The Scale and Rotate tools and the Pen adding to a path, with a command that deletes the
|
||||
/// object they act on during the drag: the drag ends without an edit of its own and the deletion
|
||||
/// stays.
|
||||
#[test]
|
||||
fn a_scale_rotate_or_pen_drag_of_an_object_a_command_deleted_ends_without_an_edit() {
|
||||
let drags: [(&str, &[(f64, f64)]); 3] = [
|
||||
("scale", &[(150.0, 150.0), (170.0, 170.0), (190.0, 190.0)]),
|
||||
("rotate", &[(160.0, 120.0), (150.0, 150.0), (120.0, 160.0)]),
|
||||
("pen", &[(350.0, 350.0), (370.0, 360.0), (380.0, 380.0)]),
|
||||
];
|
||||
for (tool, pts) in drags {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("{tool}, real time {real_time}");
|
||||
let (mut s, target, _) = two_squares(real_time);
|
||||
s.select_tool(tool, view()).unwrap();
|
||||
let target = if tool == "pen" {
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
for (x, y) in [(300.0, 300.0), (350.0, 300.0)] {
|
||||
pointer(&mut s, PointerKind::Down, x, y);
|
||||
pointer(&mut s, PointerKind::Up, x, y);
|
||||
}
|
||||
s.doc().unwrap().selection.objects[0]
|
||||
} else {
|
||||
target
|
||||
};
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
drag_with(&mut s, pts, |s| {
|
||||
s.execute("edit.clear", &json!({"ids": [target.0]})).unwrap();
|
||||
});
|
||||
assert!(!s.in_interaction(), "{at}");
|
||||
assert!(s.doc().unwrap().doc.node(target).is_none(), "{at}: the deletion stays");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "{at}: only the deletion is a step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(*s.doc().unwrap().doc, *before, "{at}: Undo brings the object back as it was");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// With the Live Paint Bucket's Selection tool (Cmd+drag), a fill chosen during the drag stays the
|
||||
/// new-art fill after the release and after Escape, though the drag puts back the bucket's colors
|
||||
/// it began with.
|
||||
#[test]
|
||||
fn a_fill_chosen_during_a_bucket_drag_stays() {
|
||||
for (real_time, escape) in [(true, false), (false, false), (true, true), (false, true)] {
|
||||
let at = format!("real time {real_time}, Escape {escape}");
|
||||
let (mut s, _, _) = two_squares(real_time);
|
||||
s.select_tool("livePaintBucket", view()).unwrap();
|
||||
let cmd = Mods { cmd: true, ..Mods::default() };
|
||||
for (kind, x, y) in [(PointerKind::Down, 120.0, 120.0), (PointerKind::Drag, 140.0, 130.0)] {
|
||||
s.pointer(&PointerEvent::new(kind, x, y).with_mods(cmd), view()).unwrap();
|
||||
}
|
||||
assert!(s.in_interaction(), "{at}: the lent Selection tool drags the square");
|
||||
s.execute("paint.setFill", &json!({"color": "#00ff00", "ids": []})).unwrap();
|
||||
let green = s.paint.fill.clone();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, 170.0, 150.0).with_mods(cmd), view()).unwrap();
|
||||
if escape {
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), view()).unwrap();
|
||||
}
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 170.0, 150.0).with_mods(cmd), view()).unwrap();
|
||||
assert_eq!(s.paint.fill, green, "{at}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command run during a Quick Pen press still runs inside the press, so it records no undo
|
||||
/// step of its own while the press is held.
|
||||
#[test]
|
||||
fn a_command_during_a_quick_pen_press_runs_inside_it() {
|
||||
for real_time in [true, false] {
|
||||
let at = format!("real time {real_time}");
|
||||
let (mut s, other) = session(real_time);
|
||||
s.select_tool(vectorcraft_tools::quickpen::ID, view()).unwrap();
|
||||
pointer(&mut s, PointerKind::Down, 50.0, 50.0);
|
||||
pointer(&mut s, PointerKind::Up, 50.0, 50.0);
|
||||
pointer(&mut s, PointerKind::Down, 100.0, 100.0);
|
||||
pointer(&mut s, PointerKind::Drag, 150.0, 120.0);
|
||||
assert!(s.in_interaction(), "{at}: the press drags a piece");
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
s.execute("object.hide", &json!({"ids": [other.0]})).unwrap();
|
||||
assert!(s.in_interaction(), "{at}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps, "{at}: no step of its own during the press");
|
||||
}
|
||||
}
|
||||
@@ -1611,6 +1611,10 @@ impl Session {
|
||||
|
||||
/// Apply tool actions. Errors from previews are reported but keep the interaction alive.
|
||||
pub fn apply_actions(&mut self, acts: Vec<Action>) -> Result<Vec<UiRequest>> {
|
||||
// The tool acts on its own interaction, which a command from outside it may have set aside.
|
||||
if !acts.is_empty() {
|
||||
self.put_back_interaction();
|
||||
}
|
||||
let outer = std::mem::replace(&mut self.in_tool_actions, true);
|
||||
let outer_bucket = self.back_to_bucket;
|
||||
// The Eyedropper's sample into a stop of the Live Paint Bucket's gradient, which switches back to the bucket.
|
||||
@@ -1636,7 +1640,16 @@ impl Session {
|
||||
let mut ui = vec![];
|
||||
for a in acts {
|
||||
match a {
|
||||
Action::Begin(label) => self.begin_interaction(&label)?,
|
||||
Action::Begin(label) => {
|
||||
// The snapshot of the interaction open before, which the tool's drag then joins.
|
||||
let open = self.active().and_then(|st| st.interaction.as_ref()).map(|it| std::sync::Arc::as_ptr(&it.doc));
|
||||
self.begin_interaction(&label)?;
|
||||
if let Some(it) = self.active_mut().and_then(|st| st.interaction.as_mut())
|
||||
&& open != Some(std::sync::Arc::as_ptr(&it.doc))
|
||||
{
|
||||
it.tool = true;
|
||||
}
|
||||
}
|
||||
Action::Preview(cmd, p) => {
|
||||
// Performance › Real-time Drawing and Editing (#394): off, remember the latest
|
||||
// params and apply them once on Commit instead of every move. A ruler guide
|
||||
|
||||
@@ -8,6 +8,7 @@ mod cut;
|
||||
mod distortkeys;
|
||||
mod docsetup;
|
||||
mod document_drag;
|
||||
mod drag_commands;
|
||||
pub(super) mod folder;
|
||||
mod font_menu;
|
||||
mod fonts;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//! A command sent through the control channel (`engine.execute`, which MCP's `run_command` sends)
|
||||
//! or run as a menu item while a tool's drag is in progress is a step of its own, before the drag's.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
use crate::VectorcraftApp;
|
||||
|
||||
/// Send a control request → its reply.
|
||||
fn control(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, params: Value) -> Value {
|
||||
let (request, _) = crate::control::ControlRequest::new(method, params);
|
||||
let crate::control::Outcome::Done(reply) = crate::control::handle(app, ctx, &request) else { panic!("{method}: not done") };
|
||||
reply
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_command_from_the_control_channel_or_a_menu_during_a_drag_is_a_step_of_its_own() {
|
||||
for through_control in [true, false] {
|
||||
let at = format!("control channel {through_control}");
|
||||
let mut app = VectorcraftApp::new(Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||||
let other = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("select.none", json!({})).unwrap();
|
||||
app.select_tool("rectangle");
|
||||
let ctx = egui::Context::default();
|
||||
let objects = |app: &VectorcraftApp| app.session.doc().unwrap().doc.layers[0].children().map_or(0, Vec::len);
|
||||
let hidden = |app: &VectorcraftApp| !app.session.doc().unwrap().doc.node(vectorcraft_doc::NodeId(other)).unwrap().visible;
|
||||
let steps = app.session.doc().unwrap().history.undo.len();
|
||||
let down = json!({"events": [{"kind": "down", "x": 100, "y": 100}, {"kind": "drag", "x": 150, "y": 150}]});
|
||||
assert_eq!(control(&mut app, &ctx, "ui.pointer", down)["ok"], true, "{at}");
|
||||
if through_control {
|
||||
let r = control(&mut app, &ctx, "engine.execute", json!({"command": "object.hide", "params": {"ids": [other]}}));
|
||||
assert_eq!(r["ok"], true, "{at}: {r}");
|
||||
} else {
|
||||
app.run("object.hide", json!({"ids": [other]})).unwrap();
|
||||
}
|
||||
let up = json!({"events": [{"kind": "drag", "x": 200, "y": 180}, {"kind": "up", "x": 200, "y": 180}]});
|
||||
assert_eq!(control(&mut app, &ctx, "ui.pointer", up)["ok"], true, "{at}");
|
||||
assert!(hidden(&app), "{at}: the drag keeps the command's edit");
|
||||
assert_eq!(objects(&app), 2, "{at}: the drag draws its rectangle");
|
||||
assert_eq!(app.session.doc().unwrap().history.undo.len(), steps + 2, "{at}: a step each");
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(objects(&app), 1, "{at}: Undo takes back the drag first");
|
||||
assert!(hidden(&app), "{at}");
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(!hidden(&app), "{at}: then the command");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -758,7 +758,7 @@ ends the wait, so the sampler can be shown again. The BSDs and headless sessions
|
||||
| `transform` | `{ids?, dx?, dy?, rotate?, scale?, scaleX?, scaleY?, reflect?, shear?, origin?, copy?}` | Runs move, rotate, scale, reflect, shear in that order. `rotate` is in degrees counter-clockwise; a positive `shear` moves the top to the right. With `copy`, the first step duplicates. A `scale`, `reflect` or `shear` that its command would refuse returns that error before any step runs; nothing changes and no undo step is recorded. The same holds for a step that is neither a number nor null, a `reflect` that is not a string, a `copy` that is not true or false, and an `origin` that is not two numbers. A null `rotate`, `scale`, `scaleX`, `scaleY`, `shear`, `reflect`, `copy` or `origin` is left out; a null `dx` or `dy` counts as 0, so it still runs the move. |
|
||||
| `create_graph` | `{type?, x, y, width, height, csv? \| series?, categories?, rows?}` | The nine Illustrator graph types: column, stacked column, bar, stacked bar, line, area, scatter, pie and radar. An empty CSV cell or a `null` in `rows` is a blank value; a number in straight quotes is a label. Edit later with `graph.setData` / `graph.setType` via `run_command`. `graph.column {design, columnType: vertical\|uniform\|repeating\|sliding, each, fractions: chop\|scale}` draws the columns or bars of the series picked with Group Selection (or `seriesIndexes`, or all of them) with a graph design saved by `graph.design`; with no `design` it returns the series' current design and options. Type in the design that holds a `%` followed by two digits shows each column's value, with at least the first digit's places before the decimal point and the second digit's places after it. |
|
||||
| `text_wrap` | `{ids?, offset?, invert?, release?}` | Area type below the objects (same layer) flows around them. `offset` is in points, from -1000 to 1000 (default 6). An offset outside that range or not a number, or an `invert` or `release` that is not true or false, is an error and changes nothing; `object.textWrap.make` and `object.textWrap.options` refuse the same values. |
|
||||
| `undo` / `redo` | `{}` | Many calls that change nothing record no undo step, for example `object.align` with nothing to move, `object.move` by 0 without `copy` and `object.showAll` with nothing hidden. `undo` (`edit.undo`) after such a call undoes the step before it. Settings that change without an undo step stay as they are when `undo` or `redo` brings back a step that did not change them: the perspective grid's view options and active plane, the grid preferences (`gridlineEvery`, `gridSubdivisions`), the export settings (`assets.settings.set`), and the current brush and symbol. A step that changed one of them, such as a perspective grid preset that shows the grid, takes that change back. The point Automatic Plane Positioning holds comes back with the step, and is let go when the active plane kept is another one. `inspect_document` lists the undo steps as `history`. `run_command` with `edit.clearHistory` removes every undo and redo step of the active document and returns `{cleared}`, the number of steps removed; the document stays as it is, and the command records no undo step. `run_command` with `edit.newDocumentFromState` and `{index?, created?}` makes a new document holding the art as it was at that state and makes it the active document, and returns `{index, title}`. `index` is a row of the History panel, with 0 the oldest state kept, and an omitted or null `index` uses the current state. An `index` that names no state fails with `no history state N`, one that is not a whole number with `` `index` must be a whole number ``, and either changes nothing. `created` is File Info's created date in Unix seconds or null, and defaults to now. The new document takes the next `Untitled-N` name, is marked unsaved, has nothing selected and has no steps to undo or redo. It is out of symbol, opacity-mask and pattern editing, and the art drawn in a pattern's tile is stored in the pattern, as `object.pattern.done` stores it. The source document and its steps stay as they are, except that typing or a drag in progress is first kept as a step. When a command keeps a drag whose preview Real-time Drawing off had not applied yet (`edit.undo`, `edit.redo`, `edit.clearHistory`, `edit.newDocumentFromState` and the other commands that end a drag first), and applying that preview gives a warning, the command's result carries it as `previewWarning`, and as `warning` too when the command gives no warning of its own; the app's status bar shows `warning`. While the Type tool edits type, `undo` and `redo` put its caret, with no characters selected, at the end of the text the step changed: after the text it puts back or retypes, or where the text it takes out was. A step that changes no characters, such as a formatting change, leaves the caret and the selection where they are. |
|
||||
| `undo` / `redo` | `{}` | Many calls that change nothing record no undo step, for example `object.align` with nothing to move, `object.move` by 0 without `copy` and `object.showAll` with nothing hidden. `undo` (`edit.undo`) after such a call undoes the step before it. Settings that change without an undo step stay as they are when `undo` or `redo` brings back a step that did not change them: the perspective grid's view options and active plane, the grid preferences (`gridlineEvery`, `gridSubdivisions`), the export settings (`assets.settings.set`), and the current brush and symbol. A step that changed one of them, such as a perspective grid preset that shows the grid, takes that change back. The point Automatic Plane Positioning holds comes back with the step, and is let go when the active plane kept is another one. `inspect_document` lists the undo steps as `history`. `run_command` with `edit.clearHistory` removes every undo and redo step of the active document and returns `{cleared}`, the number of steps removed; the document stays as it is, and the command records no undo step. `run_command` with `edit.newDocumentFromState` and `{index?, created?}` makes a new document holding the art as it was at that state and makes it the active document, and returns `{index, title}`. `index` is a row of the History panel, with 0 the oldest state kept, and an omitted or null `index` uses the current state. An `index` that names no state fails with `no history state N`, one that is not a whole number with `` `index` must be a whole number ``, and either changes nothing. `created` is File Info's created date in Unix seconds or null, and defaults to now. The new document takes the next `Untitled-N` name, is marked unsaved, has nothing selected and has no steps to undo or redo. It is out of symbol, opacity-mask and pattern editing, and the art drawn in a pattern's tile is stored in the pattern, as `object.pattern.done` stores it. The source document and its steps stay as they are, except that typing or a drag in progress is first kept as a step. When a command keeps a drag whose preview Real-time Drawing off had not applied yet (`edit.undo`, `edit.redo`, `edit.clearHistory`, `edit.newDocumentFromState` and the other commands that end a drag first), and applying that preview gives a warning, the command's result carries it as `previewWarning`, and as `warning` too when the command gives no warning of its own; the app's status bar shows `warning`. Any other command that changes the document during a tool's drag, from `run_command`, a panel or a menu, records its own undo step before the drag's. The command acts on the document without the drag's preview, and the drag then continues from the command's result: with Real-time Drawing on, the drag's preview is applied again at once, and with it off, the release applies it as before. The release records the drag's step after the command's, `undo` takes back the drag first and the command next, and Escape cancels only the drag. The drag acts on the objects it started with, so a selection the command makes (`select.all`, `edit.duplicate`) does not change what the drag moves, and the drag's release and Escape set the selection as they do without the command (with Real-time Drawing on, so does each move of the drag); a command that records no undo step, such as `select.none`, adds no step. When the command deletes or changes the art being dragged, the drag continues with the art the command left, and a drag left with nothing to change records no step. A warning or an error from the drag's preview applied again after the command is in the command's result as `previewWarning`. Typing in progress is kept as a step of its own before the command. Two cases run the command inside the drag, which then keeps the command's edit as part of its own step: a press of the Quick Pen (an edit to its path ends the path), and `object.transform` with `copy` during an Alt-drag, which copies the art as the drag's move left it. A command run while a dialog's or a panel's preview is open runs inside that preview. While the Type tool edits type, `undo` and `redo` put its caret, with no characters selected, at the end of the text the step changed: after the text it puts back or retypes, or where the text it takes out was. A step that changes no characters, such as a formatting change, leaves the caret and the selection where they are. |
|
||||
|
||||
For example, create editable Japanese area type with the engine's vertical writing mode:
|
||||
|
||||
|
||||
@@ -763,6 +763,7 @@ since 2026-10-01.
|
||||
- **Mirrored handles in a Direct Selection drag (#7842):** Cmd/Ctrl added after a handle drag begins puts the anchor's other handle opposite the dragged one at the same length, on paths, type on a path and blend spines, and the anchor keeps its kind; Shift constrains the dragged handle first, and Alt keeps the handles independent. At an open end, where no segment leaves the anchor on the other side, the handle moves as it does without Cmd/Ctrl. When Cmd/Ctrl lends Direct Selection for the gesture while another tool, such as the Pen, is chosen, that press does not mirror; letting it go and pressing it again during the drag does. This is VectorCraft's own behavior. Regression tests: `crates/engine/tests/direct_handle_mirror.rs`.
|
||||
- **Cut of picked anchors (#6361):** Edit › Cut removes direct-selected anchors from their path and from a compound path's member, with the segments attached to them, as Clear does. Picked segments still lose only those segments. When a path also has a picked segment, Cut removes and copies only its picked segments and ignores its picked anchors, on every contour, as Clear does; an anchor left without a surviving segment is removed with them. Regression tests: `cut_*_anchors_*` and `cut_of_an_anchor_with_a_segment_on_another_contour_leaves_the_source_as_clear_does` in `crates/engine/tests/it/copy_direct_selection.rs`.
|
||||
- **Escape in Selection tool drags:** Escape during a Selection tool move or Alt-drag copy, a bounding-box scale or rotation, a marquee, or a corner widget, pie widget or bracket drag cancels it: the art returns to its state at the press, no copy is left, no undo step is added, the release does nothing, and the tool stays chosen. This is VectorCraft's own behavior. Regression tests: `escape_cancels_*` in `select/tests.rs` and `pie.rs`, and `escape_cancels_a_selection_tool_move_copy_scale_or_rotation` in `tests/selectescape.rs`.
|
||||
- **Commands run during a tool drag:** by VectorCraft's own rule, a command that changes the document while a tool drag is in progress (from a panel, a menu or `run_command`) records its own undo step before the drag's. The drag goes on with the art the command left, Escape cancels only the drag, and Undo takes back the drag first and the command next, with Real-time Drawing on or off. A selection the command makes does not change which objects the drag moves. Commands that keep the drag as a step first (Undo, Redo, `command.batch` and others), a Quick Pen press, and a copy made with `object.transform` during an Alt-drag work as before. Regression tests: `crates/engine/src/tests/interaction_commands.rs` and `tests::drag_commands` in `crates/ui-egui`.
|
||||
- **Large Shift-marquee selections and Select › Inverse:** `select.toggle` and `select.add` with many ids (a Shift-drag marquee sends them together) go through the selection once instead of once per id, and Select › Inverse looks each object up in a set of the selected ones. The resulting selection, its order and the reply are the same as before. With 50,000 objects the selection step of toggling every object off took 528 to 655 ms before and 2.5 to 4.6 ms after in 7 runs, wall clock on a machine that was running other builds (`cargo run --release -p vectorcraft-engine --example selection_bench`). Regression tests: `add_all_does_what_adding_each_does` and `toggle_all_does_what_toggling_each_does` in `crates/doc/src/selection/tests.rs`, and `toggle_and_add_handle_each_listed_id_in_order` in `crates/engine/tests/it/selection_commands.rs`.
|
||||
- **Shift-click in the Layers panel's selection column on a large layer:** `layer.selectAll {id, add: true}` checks whether the row's art is all selected with a set of the selected objects, and removes or adds that art in one pass instead of once per object. The resulting selection, its order, the reply and the highlighted rows are the same as before. On a layer of 50,000 objects the whole command took a median of 733 ms before and 6.9 ms after when all of the layer's art was selected, and 369 ms before and 24 ms after when every other object was, in 7 runs each, wall clock on Linux while other builds were running (`cargo run --release -p vectorcraft-engine --example layer_select_bench`). Regression test: `a_shift_click_in_the_selection_column_matches_adding_or_removing_each_id` in `crates/engine/src/tests/layers.rs`.
|
||||
- **Ctrl/Cmd-click and Shift-click on Layers panel target circles (#8639):** the target circles follow the same modifier rules as the row names, as the help page describes for selecting objects in the Layers panel: Shift for adjacent objects, Ctrl or Command for non-adjacent ones ([Move objects to different layers](https://helpx.adobe.com/illustrator/desktop/manage-layers/create-and-organize-layers/move-objects-to-different-layers.html)). Ctrl/Cmd-click adds the item (a layer: its art) to the selection or takes it out, which Shift-click did before (#913); Shift-click selects the art of every row from the last circle clicked (else the targeted row) to this one, and Shift+Ctrl/Cmd-click adds it (`layer.target {range, add?}`). Hidden or locked art and art outside the isolated group in the range is left out, and nothing stays targeted. Regression tests: `clicking_a_target_circle_targets_the_layer` and `shift_clicking_a_target_circle_selects_a_range_of_rows` in `crates/ui-egui/src/panels/layers/tests.rs`.
|
||||
|
||||
Reference in new issue
Block a user