ladybird/Userland/Libraries/LibGUI/UndoStack.h
Andreas Kling 74a4571f02 LibGUI: Reverse internal direction of GUI::UndoStack
The undo stack was very difficult to understand as it grew by adding
new undo commands to the front of the internal vector. This meant we
had to keep updating indices as the stack grew and shrank.

This patch makes the internal vector grow by appending instead.
2021-05-08 13:49:34 +02:00

47 lines
750 B
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <LibGUI/Forward.h>
namespace GUI {
class UndoStack {
public:
UndoStack();
~UndoStack();
void push(NonnullOwnPtr<Command>&&);
bool can_undo() const;
bool can_redo() const;
void undo();
void redo();
void finalize_current_combo();
void set_current_unmodified();
bool is_current_modified() const;
void clear();
private:
struct Combo {
NonnullOwnPtrVector<Command> commands;
};
void pop();
NonnullOwnPtrVector<Combo> m_stack;
size_t m_stack_index { 0 };
Optional<size_t> m_clean_index;
};
}