UndoStack.h 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtrVector.h>
  8. #include <LibGUI/Forward.h>
  9. namespace GUI {
  10. class UndoStack {
  11. public:
  12. UndoStack();
  13. ~UndoStack();
  14. void push(NonnullOwnPtr<Command>&&);
  15. bool can_undo() const { return m_stack_index < m_stack.size() && !m_stack.is_empty(); }
  16. bool can_redo() const { return m_stack_index > 0 && !m_stack.is_empty() && m_stack[m_stack_index - 1].commands.size() > 0; }
  17. void undo();
  18. void redo();
  19. void finalize_current_combo();
  20. void set_current_unmodified();
  21. bool is_current_modified() const;
  22. void clear();
  23. private:
  24. size_t non_empty_stack_index() const;
  25. struct Combo {
  26. NonnullOwnPtrVector<Command> commands;
  27. };
  28. NonnullOwnPtrVector<Combo> m_stack;
  29. size_t m_stack_index { 0 };
  30. Optional<size_t> m_clean_index;
  31. };
  32. }