1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibGUI: Add UndoStack::on_state_change hook

This will allow clients to react to the undo stack changing state.
It's invoked when the stack or clean index are changed.
This commit is contained in:
Andreas Kling 2021-05-08 13:14:52 +02:00
parent 74a4571f02
commit 0cb6103928
2 changed files with 25 additions and 0 deletions

View file

@ -38,6 +38,9 @@ void UndoStack::undo()
auto& combo = m_stack[--m_stack_index]; auto& combo = m_stack[--m_stack_index];
for (auto i = static_cast<ssize_t>(combo.commands.size()) - 1; i >= 0; i--) for (auto i = static_cast<ssize_t>(combo.commands.size()) - 1; i >= 0; i--)
combo.commands[i].undo(); combo.commands[i].undo();
if (on_state_change)
on_state_change();
} }
void UndoStack::redo() void UndoStack::redo()
@ -48,6 +51,9 @@ void UndoStack::redo()
auto& commands = m_stack[m_stack_index++].commands; auto& commands = m_stack[m_stack_index++].commands;
for (auto& command : commands) for (auto& command : commands)
command.redo(); command.redo();
if (on_state_change)
on_state_change();
} }
void UndoStack::pop() void UndoStack::pop()
@ -72,6 +78,9 @@ void UndoStack::push(NonnullOwnPtr<Command>&& command)
} }
m_stack.last().commands.append(move(command)); m_stack.last().commands.append(move(command));
if (on_state_change)
on_state_change();
} }
void UndoStack::finalize_current_combo() void UndoStack::finalize_current_combo()
@ -84,12 +93,20 @@ void UndoStack::finalize_current_combo()
if (!m_stack.last().commands.is_empty()) { if (!m_stack.last().commands.is_empty()) {
m_stack.append(make<Combo>()); m_stack.append(make<Combo>());
m_stack_index = m_stack.size() - 1; m_stack_index = m_stack.size() - 1;
if (on_state_change)
on_state_change();
} }
} }
void UndoStack::set_current_unmodified() 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; m_clean_index = m_stack_index;
if (on_state_change)
on_state_change();
} }
bool UndoStack::is_current_modified() const bool UndoStack::is_current_modified() const
@ -99,9 +116,15 @@ bool UndoStack::is_current_modified() const
void UndoStack::clear() void UndoStack::clear()
{ {
if (m_stack.is_empty() && m_stack_index == 0 && !m_clean_index.has_value())
return;
m_stack.clear(); m_stack.clear();
m_stack_index = 0; m_stack_index = 0;
m_clean_index.clear(); m_clean_index.clear();
if (on_state_change)
on_state_change();
} }
} }

View file

@ -31,6 +31,8 @@ public:
void clear(); void clear();
Function<void()> on_state_change;
private: private:
struct Combo { struct Combo {
NonnullOwnPtrVector<Command> commands; NonnullOwnPtrVector<Command> commands;