summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-05-08 13:14:52 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-08 13:49:34 +0200
commit0cb610392823b9591479da4492e9ba964df91141 (patch)
treef88f131506b071f0249b5827ec9ecd5afad7c9d4 /Userland/Libraries/LibGUI
parent74a4571f02edd8b175db990e7bcfe2ce65d93253 (diff)
downloadserenity-0cb610392823b9591479da4492e9ba964df91141.zip
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.
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r--Userland/Libraries/LibGUI/UndoStack.cpp23
-rw-r--r--Userland/Libraries/LibGUI/UndoStack.h2
2 files changed, 25 insertions, 0 deletions
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<ssize_t>(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>&& 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<Combo>());
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<void()> on_state_change;
+
private:
struct Combo {
NonnullOwnPtrVector<Command> commands;