Serenity Operating System
at portability 119 lines 3.7 kB view raw
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 27#include <LibGUI/Command.h> 28#include <LibGUI/UndoStack.h> 29 30namespace GUI { 31 32UndoStack::UndoStack() 33{ 34} 35 36UndoStack::~UndoStack() 37{ 38} 39 40void UndoStack::undo() 41{ 42 if (!can_undo()) 43 return; 44 45 auto& undo_container = m_stack[m_stack_index]; 46 auto& undo_vector = undo_container.m_undo_vector; 47 48 //If we try to undo a empty vector, delete it and skip over. 49 if (undo_vector.is_empty()) { 50 m_stack.remove(m_stack_index); 51 undo(); 52 return; 53 } 54 55 for (size_t i = 0; i < undo_vector.size(); i++) { 56 auto& undo_command = undo_vector[i]; 57 undo_command.undo(); 58 } 59 60 m_stack_index++; 61} 62 63void UndoStack::redo() 64{ 65 if (!can_redo()) 66 return; 67 68 auto& undo_container = m_stack[m_stack_index - 1]; 69 auto& redo_vector = undo_container.m_undo_vector; 70 71 for (int i = static_cast<int>(redo_vector.size()) - 1; i >= 0; i--) { 72 auto& undo_command = redo_vector[i]; 73 undo_command.redo(); 74 } 75 76 m_stack_index--; 77} 78 79void UndoStack::push(NonnullOwnPtr<Command>&& command) 80{ 81 if (m_stack.is_empty()) { 82 auto undo_commands_container = make<UndoCommandsContainer>(); 83 m_stack.prepend(move(undo_commands_container)); 84 } 85 86 // Clear the elements of the stack before the m_undo_stack_index (Excluding our new element) 87 for (size_t i = 1; i < m_stack_index; i++) 88 m_stack.remove(1); 89 90 if (m_stack_index > 0 && !m_stack.is_empty()) 91 m_stack[0].m_undo_vector.clear(); 92 93 m_stack_index = 0; 94 95 m_stack[0].m_undo_vector.prepend(move(command)); 96} 97 98void UndoStack::finalize_current_combo() 99{ 100 if (m_stack.is_empty()) 101 return; 102 103 auto& undo_vector = m_stack[0].m_undo_vector; 104 105 if (undo_vector.size() == m_last_updated_undo_vector_size && !undo_vector.is_empty()) { 106 auto undo_commands_container = make<UndoCommandsContainer>(); 107 m_stack.prepend(move(undo_commands_container)); 108 // Note: Remove dbg() if we're 100% sure there are no bugs left. 109 dbg() << "Undo stack increased to " << m_stack.size(); 110 111 // Shift the index to the left since we're adding an empty container. 112 if (m_stack_index > 0) 113 m_stack_index++; 114 } 115 116 m_last_updated_undo_vector_size = undo_vector.size(); 117} 118 119}