1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:37:46 +00:00

LibGUI: Add fallible try_push() variant to UndoStack

This commit is contained in:
thankyouverycool 2022-07-04 21:36:30 -04:00 committed by Andreas Kling
parent ae333fad98
commit 510551bb4f
2 changed files with 11 additions and 3 deletions

View file

@ -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()