1
0
Fork 0
forked from hanez/xtc

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

@ -445,6 +445,26 @@ void GraphWidget::draw()
* @return 1 when the event was consumed, 0 for unsupported left-button input,
* or the result of Fl_Widget::handle() for unhandled events.
*/
bool GraphWidget::select_at(int left, int top)
{
for (size_t i = count_; i > 0; --i) {
const auto &pos = positions_[i-1];
if (left >= pos.left && left <= pos.left+node_width && top >= pos.top && top <= pos.top+node_height)
return select_node(pos.id);
}
size_t nodes = 0, edges = 0;
xtc_graph_get_counts(graph_,&nodes,&edges);
for (size_t i = 0; i < edges; ++i) {
xtc_connection_info edge{}; int sx, sy, dx, dy;
xtc_graph_get_connection(graph_,i,&edge);
if (!port_position(edge.source_node,1,sx,sy) || !port_position(edge.destination_node,0,dx,dy)) continue;
double vx = dx-sx, vy = dy-sy, length = vx*vx+vy*vy;
double t = length == 0 ? 0 : std::clamp(((left-sx)*vx+(top-sy)*vy)/length,0.0,1.0);
if (std::hypot(left-sx-t*vx,top-sy-t*vy) <= 6) return select_connection(edge.source_node,edge.destination_node);
}
selected_node_ = SIZE_MAX; edge_selected_ = false; describe_selection(); redraw(); return false;
}
int GraphWidget::handle(int event)
{
const int left = Fl::event_x() - x(), top = Fl::event_y() - y();
@ -454,6 +474,7 @@ int GraphWidget::handle(int event)
return 1;
case FL_PUSH: {
if (Fl::event_button() == FL_RIGHT_MOUSE) {
take_focus(); select_at(left,top);
Fl_Menu_Item items[5]{};
items[0].text = "Inspect selection";
items[1].text = "Arrange by signal flow (L)";

View file

@ -109,6 +109,7 @@ class GraphWidget : public Fl_Widget {
* @return True when the node has a cached position, false otherwise.
*/
bool select_node(size_t id);
bool select_at(int left, int top); // Canvas-local coordinates; selection only.
/**
* @brief Selects a wire between two nodes and clears the node selection.

View file

@ -13,6 +13,7 @@
#include <FL/Fl_Group.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Input_.H>
#include <FL/Fl_Tabs.H>
#include <FL/fl_ask.H>
#include <cstdio>
@ -37,6 +38,15 @@ void set_label(Fl_Box *box, const char *text)
if (box->label() == nullptr || std::strcmp(box->label(), text) != 0)
box->copy_label(text);
}
/* Paint progress without dispatching input/reentering edit commands. */
class BusyOperation {
Fl_Window &window_;
public:
BusyOperation(Fl_Window &window, Fl_Box *status, const char *message) : window_(window) {
set_label(status,message); window_.cursor(FL_CURSOR_WAIT); Fl::flush();
}
~BusyOperation() { window_.cursor(FL_CURSOR_DEFAULT); }
};
} // namespace
/**
@ -64,6 +74,7 @@ MainWindow::MainWindow(bool prompt_unsaved)
history_.reset(history);
begin();
auto *menu = new Fl_Menu_Bar(0, 0, 1000, 28);
menu_ = menu;
menu->add("Edit/Undo", FL_CTRL + 'z', history_callback, this);
menu->add("Edit/Redo", FL_CTRL + FL_SHIFT + 'z', history_callback, this);
menu->add("Edit/Redo alternate", FL_CTRL + 'y', history_callback, this);
@ -325,8 +336,11 @@ void MainWindow::report_error(const char *operation, xtc_result result)
char text[384];
const char *hint = std::strstr(operation, "Project") != nullptr
? "Check the project path, file format and write permissions."
: result == XTC_ERR_UNSUPPORTED ? "Rebuild with JACK development support."
: "Check the JACK-compatible server and use Reconnect to retry.";
: result == XTC_ERR_OUT_OF_MEMORY ? "Insufficient memory; the command was not accepted."
: result == XTC_ERR_CAPACITY ? "A project capacity limit was reached."
: result == XTC_ERR_NOT_FOUND || result == XTC_ERR_INVALID_ARGUMENT ? "Check the selected target and entered values."
: result == XTC_ERR_UNSUPPORTED ? "Check optional build support and the requested format or plugin layout."
: "Check the operation details; for audio failures use Reconnect to retry.";
std::snprintf(text, sizeof(text), "%s: %s. %s", operation, xtc_result_string(result), hint);
set_label(error_, text);
}
@ -360,6 +374,7 @@ xtc_result MainWindow::edit_graph(void *context, const xtc_graph_edit &edit, siz
if (result == XTC_OK)
result = xtc_project_set_graph(replacement, candidate);
xtc_graph_destroy(candidate);
if (result == XTC_OK) result = xtc_history_reserve(window->history_.get());
if (result == XTC_OK && window->ready_)
result = xtc_backend_submit(window->backend_, xtc_project_graph(replacement));
if (result != XTC_OK) {
@ -524,8 +539,18 @@ void MainWindow::reconnect_engine()
* error indicators from the latest engine status, then collects backend
* work.
*/
void MainWindow::update_history_controls()
{
size_t undo_count = 0, redo_count = 0;
if (!menu_ || xtc_history_counts(history_.get(),&undo_count,&redo_count) != XTC_OK) return;
for (const char *path : {"Edit/Undo","Edit/Redo","Edit/Redo alternate"}) {
int index = menu_->find_index(path);
if (index >= 0) menu_->mode(index,(!recorder_ && (std::strcmp(path,"Edit/Undo") == 0 ? undo_count : redo_count)) ? 0 : FL_MENU_INACTIVE);
}
}
void MainWindow::refresh_status()
{
update_history_controls();
if (mixer_ && !ready_) { mixer_->peaks(0, 0); mixer_->track_peaks(0, 0); }
bool can_play = false, can_record = false;
if (!ready_) {
@ -652,6 +677,9 @@ bool MainWindow::close_window()
*/
int MainWindow::handle(int event)
{
if (event == FL_SHORTCUT && dynamic_cast<Fl_Input_ *>(Fl::focus()) &&
(Fl::event_key() == ' ' || (Fl::event_state(FL_CTRL) &&
(Fl::event_key() == 'z' || Fl::event_key() == 'y' || Fl::event_key() == 'd')))) return 0;
if (event == FL_SHORTCUT && Fl::event_key() == ' ' && (Fl::event_state() & (FL_CTRL | FL_ALT | FL_META)) == 0) {
if (running_)
stop_engine();
@ -766,6 +794,7 @@ void MainWindow::mark_dirty()
if (piano_)
piano_->set_project(project_.get(), piano_->clip());
dirty_ = true;
update_history_controls();
copy_label("xtc * — unsaved changes");
}
@ -925,6 +954,7 @@ xtc_result MainWindow::install_project(xtc_project *candidate, bool reset_histor
*/
xtc_result MainWindow::open_project(const char *path)
{
BusyOperation busy(*this,status_,"Loading and preparing project…");
if (path == nullptr)
return XTC_ERR_INVALID_ARGUMENT;
project_issue_.clear();
@ -1047,11 +1077,9 @@ void MainWindow::file_callback(Fl_Widget *widget, void *context)
xtc_result MainWindow::before_arrangement_edit(void *context)
{
auto *window = static_cast<MainWindow *>(context);
if (window->recorder_)
return XTC_ERR_INVALID_STATE;
if (!window->ready_)
return XTC_OK;
xtc_result r = xtc_backend_stop(window->backend_);
if (window->recorder_) return XTC_ERR_INVALID_STATE;
xtc_result r = xtc_history_reserve(window->history_.get());
if (r == XTC_OK && window->ready_) r = xtc_backend_stop(window->backend_);
window->refresh_status();
return r;
}
@ -1069,6 +1097,7 @@ xtc_result MainWindow::before_arrangement_edit(void *context)
*/
xtc_result MainWindow::import_audio(const char *path)
{
BusyOperation busy(*this,status_,"Importing audio and preparing playback…");
xtc_result r = before_arrangement_edit(this);
if (r != XTC_OK)
return r;
@ -1131,8 +1160,7 @@ xtc_result MainWindow::record_audio(const char *path)
(void)xtc_backend_set_recorder(backend_, nullptr);
xtc_recorder_destroy(recorder_);
recorder_ = nullptr;
} else
mark_dirty();
}
refresh_status();
return r;
}
@ -1174,6 +1202,7 @@ void MainWindow::finish_recording()
r = xtc_project_add_track(candidate, "Recording", &track);
if (r == XTC_OK)
r = xtc_project_import_audio(candidate, track, recording_path_.c_str(), recording_tick_, &clip);
if (r == XTC_OK) r = xtc_history_reserve(history_.get());
if (r == XTC_OK)
r = xtc_backend_prepare_project(backend_, candidate);
if (r != XTC_OK) {
@ -1264,6 +1293,9 @@ xtc_result MainWindow::open_piano_roll()
auto *bar = static_cast<Fl_Menu_Bar *>(widget);
owner->piano_->command(static_cast<int>(bar->mvalue() - bar->menu()) - 1);
}, this);
menu->add("Edit/Undo",FL_CTRL+'z',history_callback,this);
menu->add("Edit/Redo",FL_CTRL+FL_SHIFT+'z',history_callback,this);
menu->add("Edit/Redo alternate",FL_CTRL+'y',history_callback,this);
auto *scroll = new Fl_Scroll(12, 48, 876, 560);
piano_ = new PianoRollWidget(12, 48, project_.get(), clip.id, before_arrangement_edit, this);
piano_->callback(
@ -1334,6 +1366,12 @@ xtc_result MainWindow::undo(bool redo)
{
if (recorder_)
return XTC_ERR_INVALID_STATE;
const uint64_t track = sequencer_->selected_track(), clip = sequencer_->selected_clip();
const uint64_t mixer_track = mixer_->selected_track();
const size_t node = graph_widget_->selected_node();
const uint64_t piano_clip = piano_ ? piano_->clip() : UINT64_MAX;
const uint64_t note = piano_ ? piano_->selected_note() : UINT64_MAX;
const bool piano_visible = piano_window_ && piano_window_->shown();
xtc_project *candidate = nullptr;
xtc_result r = xtc_history_checkout(history_.get(), redo ? 1 : 0, &candidate);
if (r == XTC_OK)
@ -1342,8 +1380,20 @@ xtc_result MainWindow::undo(bool redo)
xtc_project_destroy(candidate);
return r;
}
xtc_clip_info selected_clip{};
if (xtc_project_find_clip(project_.get(),clip,&selected_clip) == XTC_OK)
sequencer_->select(selected_clip.track_id,clip);
else sequencer_->select(track);
mixer_->select_track(mixer_track);
graph_widget_->select_node(node);
xtc_clip_info info{};
if (piano_ && xtc_project_find_clip(project_.get(),piano_clip,&info) == XTC_OK && info.midi) {
piano_->set_project(project_.get(),piano_clip); piano_->select_note(note);
if (piano_visible) piano_window_->show();
}
r = xtc_history_accept(history_.get(), redo ? 1 : 0);
dirty_ = true;
update_history_controls();
copy_label("xtc * — unsaved changes");
return r;
}
@ -1373,6 +1423,7 @@ xtc_result MainWindow::set_control_map(unsigned int channel, const unsigned int
xtc_project *candidate = nullptr;
xtc_result r = xtc_project_clone(project_.get(),&candidate);
if (r == XTC_OK) r = xtc_project_set_control_map(candidate,channel,controllers);
if (r == XTC_OK) r = xtc_history_reserve(history_.get());
if (r == XTC_OK) r = install_project(candidate,false);
if (r != XTC_OK) xtc_project_destroy(candidate); else mark_dirty();
return r;
@ -1402,6 +1453,7 @@ xtc_result MainWindow::install_plugin_graph(xtc_graph *graph)
xtc_project *candidate = nullptr;
xtc_result r = xtc_project_clone(project_.get(),&candidate);
if (r == XTC_OK) r = xtc_project_set_graph(candidate,graph);
if (r == XTC_OK) r = xtc_history_reserve(history_.get());
if (r == XTC_OK) r = install_project(candidate,false);
if (r != XTC_OK) xtc_project_destroy(candidate);
else mark_dirty();
@ -1409,6 +1461,7 @@ xtc_result MainWindow::install_plugin_graph(xtc_graph *graph)
}
xtc_result MainWindow::add_plugin(const char *path, unsigned int index, size_t *id)
{
BusyOperation busy(*this,status_,"Loading plugin and preparing playback…");
if (recorder_) return XTC_ERR_INVALID_STATE;
double rate = 48000;
xtc_backend_status status{};
@ -1429,6 +1482,7 @@ xtc_result MainWindow::add_plugin(const char *path, unsigned int index, size_t *
}
xtc_result MainWindow::replace_plugin(size_t id, const char *path, unsigned int index)
{
BusyOperation busy(*this,status_,"Replacing plugin and preparing playback…");
if (recorder_) return XTC_ERR_INVALID_STATE;
double rate = 48000; xtc_backend_status status{};
if (ready_ && xtc_backend_get_status(backend_,&status) == XTC_OK) rate = status.sample_rate;

View file

@ -17,6 +17,7 @@ class SequencerWidget;
class Fl_Box;
class Fl_Button;
class Fl_Tabs;
class Fl_Menu_Bar;
class MainWindow : public Fl_Double_Window {
public:
@ -168,6 +169,8 @@ class MainWindow : public Fl_Double_Window {
private:
MixerWidget *mixer_ = nullptr;
Fl_Menu_Bar *menu_ = nullptr;
void update_history_controls();
std::unique_ptr<xtc_history, decltype(&xtc_history_destroy)> history_{nullptr, xtc_history_destroy};
/**
* @brief FLTK menu callback for the Undo and Redo menu items.

View file

@ -7,6 +7,7 @@
#include <FL/Fl_Choice.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Input_.H>
#include <FL/Fl_Value_Slider.H>
#include <FL/fl_ask.H>
#include <FL/fl_draw.H>
@ -459,6 +460,7 @@ void MixerWidget::select(Fl_Widget *, void *c)
*/
int MixerWidget::handle(int e)
{
if (e == FL_KEYDOWN && dynamic_cast<Fl_Input_ *>(Fl::focus())) return Fl_Group::handle(e);
if (e == FL_KEYDOWN && !(Fl::event_state() & (FL_CTRL | FL_ALT | FL_META))) {
int key = Fl::event_key();
xtc_track_info t{};

View file

@ -2,6 +2,7 @@
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Menu_Item.H>
#include <algorithm>
#include <cstdio>
#include <sstream>
@ -46,6 +47,19 @@ PianoRollWidget::PianoRollWidget(int left, int top, xtc_project *project, uint64
* @param project Borrowed project that must outlive the widget.
* @param clip ID of the MIDI clip to display.
*/
bool PianoRollWidget::select_note(uint64_t id)
{
size_t count = 0;
xtc_project_note_count(project_,&count);
for (size_t i = 0; i < count; ++i) {
xtc_note_info note{};
if (xtc_project_get_note(project_,i,&note) == XTC_OK && note.id == id && visible(note)) {
selected_ = id; all_ = false; mode_ = 0; redraw(); return true;
}
}
return false;
}
void PianoRollWidget::set_project(xtc_project *project, uint64_t clip)
{
if (project != project_ || clip != clip_) {
@ -248,8 +262,17 @@ int PianoRollWidget::handle(int event)
}
}
if (selected_ != UINT64_MAX && Fl::event_button() == FL_RIGHT_MOUSE) {
delete_selected();
mode_ = 0;
Fl_Menu_Item items[5]{};
items[0].text = "Velocity and channel (V)";
items[1].text = "Quantize (Q)";
items[2].text = "Repeat (Ctrl+D)";
items[3].text = "Delete (undoable)";
const auto *picked = items->popup(Fl::event_x(),Fl::event_y());
if (picked == items) command(2);
else if (picked == items+1) command(3);
else if (picked == items+2) command(5);
else if (picked == items+3) delete_selected();
} else if (Fl::event_button() == FL_LEFT_MOUSE && selected_ == UINT64_MAX && Fl::event_x() >= x() + key_width &&
Fl::event_y() >= y() + header) {
int pitch = 127 - (Fl::event_y() - y() - header) / row_height;
@ -293,11 +316,11 @@ int PianoRollWidget::handle(int event)
if (event == FL_KEYDOWN) {
int key = Fl::event_key();
if (key == 'a' && (Fl::event_state() & FL_CTRL)) { select_filtered(); return 1; }
if (key == 'q') { command(3); return 1; }
if (key == 'v') { command(2); return 1; }
if (key == 'g') { command(0); return 1; }
if (key == 'f') { command(1); return 1; }
if (key == 't') { command(4); return 1; }
if (!Fl::event_state(FL_CTRL|FL_ALT|FL_META) && key == 'q') { command(3); return 1; }
if (!Fl::event_state(FL_CTRL|FL_ALT|FL_META) && key == 'v') { command(2); return 1; }
if (!Fl::event_state(FL_CTRL|FL_ALT|FL_META) && key == 'g') { command(0); return 1; }
if (!Fl::event_state(FL_CTRL|FL_ALT|FL_META) && key == 'f') { command(1); return 1; }
if (!Fl::event_state(FL_CTRL|FL_ALT|FL_META) && key == 't') { command(4); return 1; }
if (key == 'd' && (Fl::event_state() & FL_CTRL)) { command(5); return 1; }
if (key == FL_Up || key == FL_Down) {
xtc_note_edit edit{};

View file

@ -69,6 +69,8 @@ class PianoRollWidget : public Fl_Widget {
*
* @return XTC_OK on success, the guard's error, or the project error.
*/
uint64_t selected_note() const { return selected_; }
bool select_note(uint64_t id);
xtc_result delete_selected();
/* UI preferences; independent of persisted project state. */
xtc_result set_grid(uint64_t ticks);

View file

@ -31,6 +31,24 @@ SequencerWidget::SequencerWidget(int left, int top, xtc_project *project, Seek s
* @param project Borrowed project; must outlive the widget's use of it.
* @param reset Whether to clear selection, cursor, and drag state.
*/
bool SequencerWidget::select(uint64_t track, uint64_t clip)
{
size_t tracks = 0, clips = 0;
xtc_project_get_counts(project_,&tracks,&clips);
bool found = false;
for (size_t i = 0; i < tracks; ++i) {
xtc_track_info info{};
if (xtc_project_get_track(project_,i,&info) == XTC_OK && info.id == track) found = true;
}
if (!found) return false;
if (clip != UINT64_MAX) {
xtc_clip_info info{};
if (xtc_project_find_clip(project_,clip,&info) != XTC_OK || info.track_id != track) return false;
}
track_ = track; clip_ = clip; mode_ = 0; redraw();
return true;
}
void SequencerWidget::set_project(xtc_project *project, bool reset)
{
project_ = project;

View file

@ -13,6 +13,7 @@ class SequencerWidget : public Fl_Widget {
*
* @return XTC_OK to allow the edit, or an error to cancel it.
*/
bool select(uint64_t track, uint64_t clip = UINT64_MAX);
xtc_result duplicate_selection();
xtc_result split_selection();
xtc_result audio_properties(float gain, uint64_t fade_in, uint64_t fade_out);

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);