UndoStack.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Command.h>
  27. #include <LibGUI/UndoStack.h>
  28. namespace GUI {
  29. UndoStack::UndoStack()
  30. {
  31. }
  32. UndoStack::~UndoStack()
  33. {
  34. }
  35. void UndoStack::undo()
  36. {
  37. if (!can_undo())
  38. return;
  39. auto pop_container_and_undo = [this]() {
  40. for (;;) {
  41. if (m_stack_index >= m_stack.size())
  42. break;
  43. auto& container = m_stack[m_stack_index++];
  44. if (container.m_undo_vector.size() == 0)
  45. continue;
  46. for (auto& command : container.m_undo_vector)
  47. command.undo();
  48. break;
  49. }
  50. };
  51. // If this is the first undo, finish off our current combo
  52. if (m_stack_index == 0)
  53. finalize_current_combo();
  54. pop_container_and_undo();
  55. }
  56. void UndoStack::redo()
  57. {
  58. if (!can_redo())
  59. return;
  60. m_stack_index -= 1;
  61. auto& vector = m_stack[m_stack_index].m_undo_vector;
  62. for (int i = vector.size() - 1; i >= 0; i--)
  63. vector[i].redo();
  64. }
  65. void UndoStack::push(NonnullOwnPtr<Command>&& command)
  66. {
  67. if (m_stack.is_empty())
  68. finalize_current_combo();
  69. if (m_stack_index > 0) {
  70. for (size_t i = 0; i < m_stack_index; i++)
  71. m_stack.remove(0);
  72. m_stack_index = 0;
  73. finalize_current_combo();
  74. }
  75. auto& current_vector = m_stack.first().m_undo_vector;
  76. current_vector.prepend(move(command));
  77. }
  78. void UndoStack::finalize_current_combo()
  79. {
  80. if (m_stack_index > 0)
  81. return;
  82. if (m_stack.size() != 0 && m_stack.first().m_undo_vector.size() == 0)
  83. return;
  84. auto undo_commands_container = make<UndoCommandsContainer>();
  85. m_stack.prepend(move(undo_commands_container));
  86. }
  87. }