UndoStack.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ErrorOr<void> try_push(NonnullOwnPtr<Command>);
  19. bool can_undo() const;
  20. bool can_redo() const;
  21. void undo();
  22. void redo();
  23. void set_current_unmodified();
  24. bool is_current_modified() const;
  25. Optional<Time> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }
  26. void clear();
  27. Optional<String> undo_action_text() const;
  28. Optional<String> redo_action_text() const;
  29. Function<void()> on_state_change;
  30. private:
  31. NonnullOwnPtrVector<Command> m_stack;
  32. size_t m_stack_index { 0 };
  33. Optional<size_t> m_clean_index;
  34. Optional<Time> m_last_unmodified_timestamp;
  35. };
  36. }