From dd12857ba8d9fc2d46be1fe66d79f3e11f821b02 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 31 Mar 2022 01:38:59 -0500 Subject: [PATCH] [nostalgia/studio] Add UndoCommand::mergeWith --- src/nostalgia/studio/lib/undostack.cpp | 12 ++++++++++-- src/nostalgia/studio/lib/undostack.hpp | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/nostalgia/studio/lib/undostack.cpp b/src/nostalgia/studio/lib/undostack.cpp index b15f7a4fd..dd736ae72 100644 --- a/src/nostalgia/studio/lib/undostack.cpp +++ b/src/nostalgia/studio/lib/undostack.cpp @@ -6,16 +6,24 @@ namespace nostalgia::studio { +bool UndoCommand::mergeWith(const UndoCommand*) noexcept { + return false; +} + void UndoStack::push(UndoCommand *cmd) noexcept { for (const auto i = m_stackIdx; i < m_stack.size();) { oxIgnoreError(m_stack.erase(i)); } - m_stack.emplace_back(cmd); - ++m_stackIdx; auto cmdId = cmd->commandId(); cmd->redo(); redoTriggered.emit(cmdId); changeTriggered.emit(cmdId); + if (!m_stack.size() || !m_stack.back().value->mergeWith(cmd)) { + m_stack.emplace_back(cmd); + ++m_stackIdx; + } else { + delete cmd; + } } void UndoStack::redo() noexcept { diff --git a/src/nostalgia/studio/lib/undostack.hpp b/src/nostalgia/studio/lib/undostack.hpp index 0e52035a2..b46f0e249 100644 --- a/src/nostalgia/studio/lib/undostack.hpp +++ b/src/nostalgia/studio/lib/undostack.hpp @@ -18,6 +18,7 @@ class UndoCommand { virtual void undo() noexcept = 0; [[nodiscard]] virtual int commandId() const noexcept = 0; + virtual bool mergeWith(const UndoCommand *cmd) noexcept; }; class UndoStack {