UndoStack.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <AK/NonnullOwnPtr.h>
  8. #include <LibGUI/Command.h>
  9. #include <LibGUI/UndoStack.h>
  10. namespace GUI {
  11. bool UndoStack::can_undo() const
  12. {
  13. return m_stack_index > 0;
  14. }
  15. bool UndoStack::can_redo() const
  16. {
  17. if (m_stack.is_empty())
  18. return false;
  19. return m_stack_index != m_stack.size();
  20. }
  21. void UndoStack::undo()
  22. {
  23. if (!can_undo())
  24. return;
  25. auto& command = m_stack[--m_stack_index];
  26. command->undo();
  27. if (on_state_change)
  28. on_state_change();
  29. }
  30. void UndoStack::redo()
  31. {
  32. if (!can_redo())
  33. return;
  34. auto& command = m_stack[m_stack_index++];
  35. command->redo();
  36. if (on_state_change)
  37. on_state_change();
  38. }
  39. ErrorOr<void> UndoStack::try_push(NonnullOwnPtr<Command> command)
  40. {
  41. // If the stack cursor is behind the top of the stack, nuke everything from here to the top.
  42. while (m_stack.size() != m_stack_index)
  43. (void)m_stack.take_last();
  44. if (m_clean_index.has_value() && m_clean_index.value() > m_stack.size())
  45. m_clean_index = {};
  46. if (!m_stack.is_empty() && is_current_modified()) {
  47. if (m_stack.last()->merge_with(*command))
  48. return {};
  49. }
  50. TRY(m_stack.try_append(move(command)));
  51. m_stack_index = m_stack.size();
  52. if (on_state_change)
  53. on_state_change();
  54. return {};
  55. }
  56. void UndoStack::push(NonnullOwnPtr<Command> command)
  57. {
  58. MUST(try_push(move(command)));
  59. }
  60. void UndoStack::set_current_unmodified()
  61. {
  62. if (m_clean_index.has_value() && m_clean_index.value() == m_stack_index)
  63. return;
  64. m_clean_index = m_stack_index;
  65. m_last_unmodified_timestamp = Time::now_monotonic();
  66. if (on_state_change)
  67. on_state_change();
  68. }
  69. bool UndoStack::is_current_modified() const
  70. {
  71. if (m_stack.is_empty())
  72. return false;
  73. if (!m_clean_index.has_value())
  74. return true;
  75. if (m_stack_index != m_clean_index.value())
  76. return true;
  77. return false;
  78. }
  79. void UndoStack::clear()
  80. {
  81. if (m_stack.is_empty() && m_stack_index == 0 && !m_clean_index.has_value())
  82. return;
  83. m_stack.clear();
  84. m_stack_index = 0;
  85. m_clean_index.clear();
  86. if (on_state_change)
  87. on_state_change();
  88. }
  89. Optional<DeprecatedString> UndoStack::undo_action_text() const
  90. {
  91. if (!can_undo())
  92. return {};
  93. return m_stack[m_stack_index - 1]->action_text();
  94. }
  95. Optional<DeprecatedString> UndoStack::redo_action_text() const
  96. {
  97. if (!can_redo())
  98. return {};
  99. return m_stack[m_stack_index]->action_text();
  100. }
  101. }