UndoStack.cpp 2.4 KB

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