summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2022-07-04 21:36:30 -0400
committerAndreas Kling <kling@serenityos.org>2022-07-06 14:25:30 +0200
commit510551bb4f7c7fd325b753b5654174d4baaa5464 (patch)
treee81d183e232fc2babfc4711388104a67397805c2
parentae333fad98734d9e99ca8cdbf11d99a9bc8a70e4 (diff)
downloadserenity-510551bb4f7c7fd325b753b5654174d4baaa5464.zip
LibGUI: Add fallible try_push() variant to UndoStack
-rw-r--r--Userland/Libraries/LibGUI/UndoStack.cpp13
-rw-r--r--Userland/Libraries/LibGUI/UndoStack.h1
2 files changed, 11 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp
index 6ac2205648..cb273f4d37 100644
--- a/Userland/Libraries/LibGUI/UndoStack.cpp
+++ b/Userland/Libraries/LibGUI/UndoStack.cpp
@@ -46,7 +46,7 @@ void UndoStack::redo()
on_state_change();
}
-void UndoStack::push(NonnullOwnPtr<Command> command)
+ErrorOr<void> UndoStack::try_push(NonnullOwnPtr<Command> command)
{
// If the stack cursor is behind the top of the stack, nuke everything from here to the top.
while (m_stack.size() != m_stack_index)
@@ -57,14 +57,21 @@ void UndoStack::push(NonnullOwnPtr<Command> command)
if (!m_stack.is_empty() && is_current_modified()) {
if (m_stack.last().merge_with(*command))
- return;
+ return {};
}
- m_stack.append(move(command));
+ TRY(m_stack.try_append(move(command)));
m_stack_index = m_stack.size();
if (on_state_change)
on_state_change();
+
+ return {};
+}
+
+void UndoStack::push(NonnullOwnPtr<Command> command)
+{
+ MUST(try_push(move(command)));
}
void UndoStack::set_current_unmodified()
diff --git a/Userland/Libraries/LibGUI/UndoStack.h b/Userland/Libraries/LibGUI/UndoStack.h
index ab58d140e5..4ed787975b 100644
--- a/Userland/Libraries/LibGUI/UndoStack.h
+++ b/Userland/Libraries/LibGUI/UndoStack.h
@@ -20,6 +20,7 @@ public:
~UndoStack() = default;
void push(NonnullOwnPtr<Command>);
+ ErrorOr<void> try_push(NonnullOwnPtr<Command>);
bool can_undo() const;
bool can_redo() const;