diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp index 746a43eb3c..8437582a63 100644 --- a/Userland/Libraries/LibGUI/UndoStack.cpp +++ b/Userland/Libraries/LibGUI/UndoStack.cpp @@ -38,6 +38,9 @@ void UndoStack::undo() auto& combo = m_stack[--m_stack_index]; for (auto i = static_cast(combo.commands.size()) - 1; i >= 0; i--) combo.commands[i].undo(); + + if (on_state_change) + on_state_change(); } void UndoStack::redo() @@ -48,6 +51,9 @@ void UndoStack::redo() auto& commands = m_stack[m_stack_index++].commands; for (auto& command : commands) command.redo(); + + if (on_state_change) + on_state_change(); } void UndoStack::pop() @@ -72,6 +78,9 @@ void UndoStack::push(NonnullOwnPtr&& command) } m_stack.last().commands.append(move(command)); + + if (on_state_change) + on_state_change(); } void UndoStack::finalize_current_combo() @@ -84,12 +93,20 @@ void UndoStack::finalize_current_combo() if (!m_stack.last().commands.is_empty()) { m_stack.append(make()); m_stack_index = m_stack.size() - 1; + + if (on_state_change) + on_state_change(); } } void UndoStack::set_current_unmodified() { + if (m_clean_index.has_value() && m_clean_index.value() == m_stack_index) + return; m_clean_index = m_stack_index; + + if (on_state_change) + on_state_change(); } bool UndoStack::is_current_modified() const @@ -99,9 +116,15 @@ bool UndoStack::is_current_modified() const void UndoStack::clear() { + if (m_stack.is_empty() && m_stack_index == 0 && !m_clean_index.has_value()) + return; + m_stack.clear(); m_stack_index = 0; m_clean_index.clear(); + + if (on_state_change) + on_state_change(); } } diff --git a/Userland/Libraries/LibGUI/UndoStack.h b/Userland/Libraries/LibGUI/UndoStack.h index 3e5b3a4898..5c2b829219 100644 --- a/Userland/Libraries/LibGUI/UndoStack.h +++ b/Userland/Libraries/LibGUI/UndoStack.h @@ -31,6 +31,8 @@ public: void clear(); + Function on_state_change; + private: struct Combo { NonnullOwnPtrVector commands;