From 510551bb4f7c7fd325b753b5654174d4baaa5464 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Mon, 4 Jul 2022 21:36:30 -0400 Subject: [PATCH] LibGUI: Add fallible try_push() variant to UndoStack --- Userland/Libraries/LibGUI/UndoStack.cpp | 13 ++++++++++--- Userland/Libraries/LibGUI/UndoStack.h | 1 + 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) +ErrorOr UndoStack::try_push(NonnullOwnPtr 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) 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) +{ + 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); + ErrorOr try_push(NonnullOwnPtr); bool can_undo() const; bool can_redo() const;