UndoStack.h 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/NonnullOwnPtrVector.h>
  10. #include <AK/Time.h>
  11. #include <LibGUI/Forward.h>
  12. namespace GUI {
  13. class UndoStack {
  14. public:
  15. UndoStack() = default;
  16. ~UndoStack() = default;
  17. void push(NonnullOwnPtr<Command>);
  18. bool can_undo() const;
  19. bool can_redo() const;
  20. void undo();
  21. void redo();
  22. void set_current_unmodified();
  23. bool is_current_modified() const;
  24. Optional<Time> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }
  25. void clear();
  26. Optional<String> undo_action_text() const;
  27. Optional<String> redo_action_text() const;
  28. Function<void()> on_state_change;
  29. private:
  30. NonnullOwnPtrVector<Command> m_stack;
  31. size_t m_stack_index { 0 };
  32. Optional<size_t> m_clean_index;
  33. Optional<Time> m_last_unmodified_timestamp;
  34. };
  35. }