mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
The live shape fields in the Transform and Properties panels and the Contextual Task Bar (a rectangle's, polygon's or ellipse's Angle, a polygon's Sides, Radius and Side Length, a star's Points and Ratio, an ellipse's Pie Start and End Angle, and a line's Line Length and Line Angle) kept a typed but unfinished value when another shape was selected. If the new shape showed the same value, the field went on showing the old text, and the next Enter applied it to the new shape: "3+" typed for one rectangle, then "7" and Enter after selecting another, turned the second one 10 degrees. Each field now passes NumberField::edit_target the active document, its File > Revert count, its Undo and Redo restore count, its rollback count and the selected objects, which object.setLiveShape changes. A change of any of them drops the unfinished value and the field shows the new shape's value. An edit that leaves the selection as it is keeps the value typed so far. The target is hashed once per document revision and kept in CanvasCache::shape_edit_target, as the shared angle beside it is. DocState::rollbacks counts the other ways an older document snapshot and its node ID counter come back, after which a new shape can take the node ID of the shape a value was typed for: - a canceled undo group, or a canceled undo scope that changed the document; - canceling an interaction whose preview changed the document; - Exit Symbol Editing Mode with nothing changed, which takes back the entering step; - a plug-in run made away from the session that ends without applying after its calls changed the document; - a preview that discards an earlier preview that allocated node IDs, or that fails after discarding an earlier preview's change. The count goes to the document whose preview was discarded, also when the preview activates another document; - a command run during a tool interaction that sets aside a preview that allocated node IDs. history_restores still advances only on Undo and Redo.
302 lines
15 KiB
Rust
302 lines
15 KiB
Rust
use super::{Arc, BatchSteps, Document, Session, UndoGroup, UndoScope, Value, cmd, deselect_hidden};
|
|
|
|
impl Session {
|
|
// ---------- undo groups (scrubbed numeric fields) ----------
|
|
|
|
/// Open an undo group in the active document: until [`Session::end_undo_group`], the edits
|
|
/// made there (commands, committed interactions) are one undo step. A scrubbed numeric field
|
|
/// applies each value it passes as its own command, as a typed value is applied; the drag is
|
|
/// one step.
|
|
pub fn begin_undo_group(&mut self) {
|
|
self.begin_named_undo_group(None);
|
|
}
|
|
|
|
/// [`Session::begin_undo_group`] with the undo step named `label` (else after its first edit).
|
|
pub fn begin_named_undo_group(&mut self, label: Option<String>) {
|
|
let paint = self.paint.clone();
|
|
if let Some(st) = self.active_mut()
|
|
&& st.undo_group.is_none()
|
|
{
|
|
let isolation = st.isolation;
|
|
st.undo_group = Some(UndoGroup { first: None, journal: vec![], steps: vec![], label, paint, isolation });
|
|
}
|
|
}
|
|
|
|
/// Between `edit.undoGroup.begin` and `edit.undoGroup.end`, open an undo group with the begun
|
|
/// group's name in each document that the top-level command just run opened or created (its
|
|
/// [`DocState::uid`] is not in `open`). The group takes the edits after that command: the
|
|
/// command's own edits there stay its undo step, and a canceled group keeps them, as the journal
|
|
/// entry that made the document stays.
|
|
pub(super) fn open_undo_groups_in_new_documents(&mut self, open: &[u64]) {
|
|
let Some(label) = &self.explicit_undo_group else { return };
|
|
let paint = self.paint.clone();
|
|
for st in &mut self.docs {
|
|
if !open.contains(&st.uid) && st.undo_group.is_none() {
|
|
let isolation = st.isolation;
|
|
st.undo_group =
|
|
Some(UndoGroup { first: None, journal: vec![], steps: vec![], label: label.clone(), paint: paint.clone(), isolation });
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The documents with an undo group open ([`DocState::uid`]), each with the document it holds.
|
|
pub(super) fn undo_group_docs(&self) -> Vec<(u64, *const Document)> {
|
|
self.docs.iter().filter(|st| st.undo_group.is_some()).map(|st| (st.uid, Arc::as_ptr(&st.doc))).collect()
|
|
}
|
|
|
|
/// Note the journal entries from number `from` on in the undo groups they belong to: the group of
|
|
/// each document in `grouped` ([`Session::undo_group_docs`] before the entries) that they changed,
|
|
/// each group they opened and edited, and the group of the document with [`DocState::uid`] `doc`
|
|
/// when they started in it and it is still the active one. A cancel drops them. The last entry of a command that changed the
|
|
/// active document keeps the index of the document it left active. A `command.batch` entry also
|
|
/// keeps, in each group, the steps that belong to it ([`Session::batch_steps`]).
|
|
pub(super) fn journal_in_undo_groups(&mut self, doc: Option<u64>, grouped: &[(u64, *const Document)], from: usize) {
|
|
let to = self.journal.len();
|
|
let active = self.active().map(|d| d.uid);
|
|
let switched = if active == doc { None } else { self.active };
|
|
let batch = self.batch_steps.take().filter(|_| to > from && self.journal.get(to - 1).is_some_and(|(id, _)| id == "command.batch"));
|
|
for st in &mut self.docs {
|
|
let Some(g) = &mut st.undo_group else { continue };
|
|
let started = doc == Some(st.uid) && active == doc;
|
|
// A group the entries opened has an edit of theirs once it has a first edit.
|
|
let edited = match grouped.iter().find(|&&(uid, _)| uid == st.uid) {
|
|
Some(&(_, d)) => !std::ptr::eq(d, Arc::as_ptr(&st.doc)),
|
|
None => g.first.is_some(),
|
|
};
|
|
if started || edited {
|
|
// A committed interaction inside the command noted its entry already.
|
|
let noted = g.journal.partition_point(|&(n, _)| n < from);
|
|
g.journal.truncate(noted);
|
|
g.journal.extend((from..to).map(|n| (n, switched.filter(|_| n + 1 == to))));
|
|
g.steps.retain(|&(n, _)| n < from);
|
|
if let Some(steps) = &batch {
|
|
let own = steps.iter().enumerate().filter(|(_, (uids, _))| uids.contains(&st.uid));
|
|
g.steps.push((to - 1, own.map(|(i, &(_, switched))| (i, switched)).collect()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Close the open undo groups: their edits stay one undo step or, `cancel`led (Escape), are
|
|
/// undone and dropped from the journal. A group begun with `edit.undoGroup.begin` ends too, so
|
|
/// documents opened after this open no undo group.
|
|
pub fn end_undo_group(&mut self, cancel: bool) {
|
|
self.explicit_undo_group = None;
|
|
self.close_undo_groups(cancel, None);
|
|
}
|
|
|
|
/// [`Session::end_undo_group`] without ending a group begun with `edit.undoGroup.begin`, for the
|
|
/// document with [`DocState::uid`] `only` alone when given. The groups that stay open follow the
|
|
/// journal entries and batch steps of theirs that a cancel renumbers.
|
|
fn close_undo_groups(&mut self, cancel: bool, only: Option<u64>) {
|
|
let mut journal: Vec<(usize, Option<usize>)> = vec![];
|
|
let mut steps: Vec<BatchSteps> = vec![];
|
|
for st in &mut self.docs {
|
|
if only.is_some_and(|uid| uid != st.uid) {
|
|
continue;
|
|
}
|
|
let Some(g) = st.undo_group.take() else { continue };
|
|
// Redo of a group that changed document color mode restores the new-art colors the
|
|
// whole group left, not the ones right after the conversion.
|
|
if !cancel
|
|
&& let Some(first) = &g.first
|
|
&& let Some(e) = st.history.undo.last_mut().filter(|e| Arc::ptr_eq(&e.doc, first))
|
|
&& let Some((_, after)) = &mut e.color_mode_paints
|
|
{
|
|
after.clone_from(&self.paint);
|
|
e.recost_paints();
|
|
st.history.trim();
|
|
}
|
|
// Only while the group's step is still the newest one.
|
|
if cancel
|
|
&& let Some(first) = g.first
|
|
&& st.history.undo.last().is_some_and(|e| Arc::ptr_eq(&e.doc, &first))
|
|
&& let Some(e) = st.history.undo.pop()
|
|
{
|
|
if let Some((before, _)) = e.color_mode_paints {
|
|
self.paint = before;
|
|
}
|
|
let undone = std::mem::replace(&mut st.doc, e.doc);
|
|
st.selection = Arc::unwrap_or_clone(e.selection);
|
|
deselect_hidden(&self.menu, &st.doc, &mut st.selection);
|
|
st.restore_isolation(g.isolation, &undone);
|
|
st.sync_new_art();
|
|
cmd::globaledit::follow_history(st);
|
|
st.drop_stale_variables_highlight();
|
|
st.revision += 1;
|
|
st.rollbacks += 1;
|
|
// The journal drops only the canceled groups' own entries and keeps the ones made in other documents meanwhile.
|
|
journal.extend(g.journal);
|
|
steps.extend(g.steps);
|
|
}
|
|
}
|
|
// A batch that edited two of the documents is in both groups.
|
|
journal.sort_unstable();
|
|
journal.dedup_by_key(|&mut (n, _)| n);
|
|
// A batch with steps outside the canceled groups keeps those steps.
|
|
let mut rewritten: Vec<(usize, Vec<Option<usize>>)> = vec![];
|
|
journal.retain(|&(n, _)| match self.keep_batch_steps(n, &steps) {
|
|
Some(kept) => {
|
|
rewritten.push((n, kept));
|
|
false
|
|
}
|
|
None => true,
|
|
});
|
|
// An entry that changed the active document becomes that change, so the entries after it run
|
|
// in the same document when the journal is replayed.
|
|
for &(n, index) in &journal {
|
|
if let Some(index) = index {
|
|
self.journal.replace(n, ("document.activate".to_string(), serde_json::json!({ "index": index })));
|
|
}
|
|
}
|
|
let gone: Vec<usize> = journal.into_iter().filter(|(_, index)| index.is_none()).map(|(n, _)| n).collect();
|
|
self.journal.remove(&gone);
|
|
let below = |n: usize| gone.partition_point(|&m| m < n);
|
|
for g in self.docs.iter_mut().filter_map(|st| st.undo_group.as_mut()) {
|
|
g.journal.retain(|(n, _)| gone.binary_search(n).is_err());
|
|
for (n, _) in &mut g.journal {
|
|
*n -= below(*n);
|
|
}
|
|
g.steps.retain(|(n, _)| gone.binary_search(n).is_err());
|
|
for (n, own) in &mut g.steps {
|
|
if let Some((_, kept)) = rewritten.iter().find(|(r, _)| r == n) {
|
|
own.retain_mut(|(i, _)| kept.get(*i).copied().flatten().map(|k| *i = k).is_some());
|
|
}
|
|
*n -= below(*n);
|
|
}
|
|
}
|
|
// The shared budget, which waits while an undo group is open, applies again once none is.
|
|
if self.depth == 0 {
|
|
self.trim_histories();
|
|
}
|
|
// A canceled group's Assign Profile is undone with it.
|
|
if cancel {
|
|
self.sync_front_profiles();
|
|
}
|
|
}
|
|
|
|
/// When journal entry `n` is a `command.batch` with `steps` noted in canceled undo groups and
|
|
/// other steps that are not `document.activate`: rewrite its `commands` without the noted steps
|
|
/// (a noted step that changed the active document becomes that change) → each step's number in
|
|
/// the rewritten `commands`, `None` for a noted step. `None` leaves the entry to be dropped as a
|
|
/// whole.
|
|
fn keep_batch_steps(&mut self, n: usize, steps: &[BatchSteps]) -> Option<Vec<Option<usize>>> {
|
|
let noted: Vec<(usize, Option<usize>)> = steps.iter().filter(|(e, _)| *e == n).flat_map(|(_, s)| s.iter().copied()).collect();
|
|
let (id, entry) = self.journal.get(n).filter(|(id, _)| id == "command.batch" && steps.iter().any(|(e, _)| *e == n))?;
|
|
let commands = entry.get("commands").and_then(Value::as_array)?;
|
|
let kept = |i: usize| !noted.iter().any(|&(s, _)| s == i);
|
|
let activate = |c: &Value| c.get("command").and_then(Value::as_str) == Some("document.activate");
|
|
if commands.iter().enumerate().all(|(i, c)| !kept(i) || activate(c)) {
|
|
return None;
|
|
}
|
|
let (mut rewritten, mut numbers) = (Vec::with_capacity(commands.len()), Vec::with_capacity(commands.len()));
|
|
for (i, c) in commands.iter().enumerate() {
|
|
if kept(i) {
|
|
numbers.push(Some(rewritten.len()));
|
|
rewritten.push(c.clone());
|
|
} else {
|
|
numbers.push(None);
|
|
if let Some(index) = noted.iter().find(|&&(s, _)| s == i).and_then(|&(_, index)| index) {
|
|
rewritten.push(serde_json::json!({ "command": "document.activate", "params": { "index": index } }));
|
|
}
|
|
}
|
|
}
|
|
let (id, mut entry) = (id.clone(), entry.clone());
|
|
if let Some(o) = entry.as_object_mut() {
|
|
o.insert("commands".into(), Value::Array(rewritten));
|
|
}
|
|
self.journal.replace(n, (id, entry));
|
|
Some(numbers)
|
|
}
|
|
|
|
/// [`Session::end_undo_group`] for the open document with [`DocState::uid`] `uid` only (it may
|
|
/// have closed since): its open undo group closes, and undo groups open in other documents stay
|
|
/// open.
|
|
fn end_document_undo_group(&mut self, uid: u64, cancel: bool) {
|
|
self.close_undo_groups(cancel, Some(uid));
|
|
}
|
|
|
|
/// Make the edits that follow in the active document one undo step, until
|
|
/// [`Session::end_undo_scope`]: open an undo group there or, when one is open already, join it,
|
|
/// so that the edits are part of its step. `None` without a document.
|
|
pub fn begin_undo_scope(&mut self) -> Option<UndoScope> {
|
|
// An edit the tool computes on another thread is a step before the scope, so the scope's
|
|
// snapshot holds it. A failure to apply it was the edit's own, and the scope begins all the
|
|
// same.
|
|
let _ = self.finish_tool_edit_later();
|
|
let journal = self.journal.len();
|
|
let st = self.active()?;
|
|
let uid = st.uid;
|
|
let color_mode_paints = st
|
|
.undo_group
|
|
.as_ref()
|
|
.and_then(|g| g.first.as_ref())
|
|
.and_then(|first| st.history.undo.last().filter(|e| Arc::ptr_eq(&e.doc, first)).and_then(|e| e.color_mode_paints.clone()));
|
|
let paint = self.paint.clone();
|
|
let joined = st.undo_group.as_ref().map(|g| (st.doc.clone(), st.selection.clone(), g.first.clone()));
|
|
let isolation = st.isolation;
|
|
if joined.is_none() {
|
|
self.begin_undo_group();
|
|
}
|
|
Some(UndoScope { uid, journal, joined, isolation, paint, color_mode_paints })
|
|
}
|
|
|
|
/// End `scope` in its own document, whichever document is active now; undo groups open in other
|
|
/// documents stay open. The undo group the scope opened closes as [`Session::end_undo_group`]
|
|
/// closes it. A scope that joined an open group leaves the group open; with `cancel`, it puts the
|
|
/// document and its selection back as they were when the scope began and cuts the journal back to
|
|
/// where the scope began (only while the group's step is still the newest one), and an undo step
|
|
/// that the scope's first edit recorded goes too. The open undo groups drop the cut entries.
|
|
pub fn end_undo_scope(&mut self, scope: UndoScope, cancel: bool) {
|
|
let Some((doc, selection, first)) = scope.joined else {
|
|
self.end_document_undo_group(scope.uid, cancel);
|
|
return;
|
|
};
|
|
let menu = self.menu.clone();
|
|
let Some(st) = self.document_mut(scope.uid) else { return };
|
|
let Some(g) = &mut st.undo_group else { return };
|
|
let newest = g.first.as_ref().is_some_and(|f| st.history.undo.last().is_some_and(|e| Arc::ptr_eq(&e.doc, f)));
|
|
if !cancel || !newest {
|
|
return;
|
|
}
|
|
let colors_changed = st.history.undo.last().and_then(|e| e.color_mode_paints.as_ref()) != scope.color_mode_paints.as_ref();
|
|
match &first {
|
|
// The scope's first edit recorded the group's step.
|
|
None => {
|
|
st.history.undo.pop();
|
|
g.first = None;
|
|
}
|
|
Some(f) if g.first.as_ref().is_some_and(|now| Arc::ptr_eq(now, f)) => {
|
|
if let Some(e) = st.history.undo.last_mut() {
|
|
e.color_mode_paints = scope.color_mode_paints;
|
|
e.recost_paints();
|
|
}
|
|
}
|
|
Some(_) => return,
|
|
}
|
|
let changed = !Arc::ptr_eq(&st.doc, &doc);
|
|
let undone = std::mem::replace(&mut st.doc, doc);
|
|
st.selection = selection;
|
|
st.restore_isolation(scope.isolation, &undone);
|
|
cmd::globaledit::follow_history(st);
|
|
st.drop_stale_variables_highlight();
|
|
deselect_hidden(&menu, &st.doc, &mut st.selection);
|
|
st.history.recost_last(&st.doc);
|
|
st.revision += 1;
|
|
if changed {
|
|
st.rollbacks += 1;
|
|
}
|
|
if colors_changed {
|
|
self.paint = scope.paint;
|
|
}
|
|
self.journal.truncate(scope.journal);
|
|
// The next commands take the cut entries' numbers, so no open undo group keeps them.
|
|
for g in self.docs.iter_mut().filter_map(|st| st.undo_group.as_mut()) {
|
|
g.journal.retain(|&(n, _)| n < scope.journal);
|
|
g.steps.retain(|&(n, _)| n < scope.journal);
|
|
}
|
|
// A canceled scope's Assign Profile is undone with it.
|
|
self.sync_front_profiles();
|
|
}
|
|
}
|