Phase 27: strengthen undo transactions and improve editor workflow

This commit is contained in:
Johannes Findeisen 2026-10-27 16:49:30 +01:00
commit c4daef7c42
21 changed files with 460 additions and 27 deletions

View file

@ -6,6 +6,7 @@
struct xtc_history {
xtc_project *states[XTC_HISTORY_EDITS + 1];
size_t count, cursor;
xtc_project *reserved;
uint64_t next_track, next_clip, next_note;
size_t next_node;
};
@ -40,6 +41,7 @@ void xtc_history_destroy(xtc_history *h)
return;
for (i = 0; i < h->count; ++i)
xtc_project_destroy(h->states[i]);
xtc_project_destroy(h->reserved);
free(h);
}
@ -73,6 +75,41 @@ xtc_result xtc_history_create(const xtc_project *p, xtc_history **out)
return XTC_OK;
}
xtc_result xtc_history_reserve(xtc_history *h)
{
if (!h) return XTC_ERR_INVALID_ARGUMENT;
if (h->reserved) return XTC_OK;
return xtc_project_create(&h->reserved);
}
xtc_result xtc_history_counts(const xtc_history *h, size_t *undo_count, size_t *redo_count)
{
if (!h || !undo_count || !redo_count || undo_count == redo_count) return XTC_ERR_INVALID_ARGUMENT;
*undo_count = h->cursor;
*redo_count = h->count-h->cursor-1;
return XTC_OK;
}
/* Copy into the preallocated project and 64-node/4096-edge graph. No allocation
* can fail after an accepted model command or successful engine publication. */
static xtc_result capture(xtc_history *h, const xtc_project *p, xtc_project **out)
{
xtc_project *copy = h->reserved;
xtc_graph *graph;
xtc_node *nodes;
xtc_edge *edges;
if (!copy) return xtc_project_clone(p,out);
if (p->graph->node_count > 64 || p->graph->edge_count > 4096) return XTC_ERR_CAPACITY;
graph = copy->graph; nodes = graph->nodes; edges = graph->edges;
*copy = *p;
copy->graph = graph;
*graph = *p->graph;
graph->max_nodes = 64; graph->nodes = nodes; graph->edges = edges;
memcpy(nodes,p->graph->nodes,p->graph->node_count*sizeof(*nodes));
memcpy(edges,p->graph->edges,p->graph->edge_count*sizeof(*edges));
h->reserved = NULL;
*out = copy;
return XTC_OK;
}
/**
* @brief Appends a project snapshot, discarding redo states and evicting the oldest state if full.
*
@ -87,7 +124,7 @@ xtc_result xtc_history_record(xtc_history *h, const xtc_project *p)
xtc_result r;
if (!h || !p)
return XTC_ERR_INVALID_ARGUMENT;
r = xtc_project_clone(p, &copy);
r = capture(h,p,&copy);
if (r != XTC_OK)
return r;
remember_ids(h, p);