UndoStack.h 900 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. struct Combo {
  25. NonnullOwnPtrVector<Command> commands;
  26. };
  27. NonnullOwnPtrVector<Combo> m_stack;
  28. size_t m_stack_index { 0 };
  29. Optional<size_t> m_clean_index;
  30. };
  31. }