mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-05 02:33:03 +00:00
Since the `redo` action never goes back to `index: 0`, we have to mark the clean index as being the current non-empty index for the undo/redo navigation to work properly. The problem is that if we never `undo`, the stack index stays at zero, which is the empty container waiting for commands. In that situation, if we save the document, it registers the clean index as being 1 (the non-empty index) but because the stack index has never left zero, the document was being reported as modified, being out of sync with the window modified state.
46 lines
943 B
C++
46 lines
943 B
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
#include <LibGUI/Forward.h>
|
|
|
|
namespace GUI {
|
|
|
|
class UndoStack {
|
|
public:
|
|
UndoStack();
|
|
~UndoStack();
|
|
|
|
void push(NonnullOwnPtr<Command>&&);
|
|
|
|
bool can_undo() const { return m_stack_index < m_stack.size() && !m_stack.is_empty(); }
|
|
bool can_redo() const { return m_stack_index > 0 && !m_stack.is_empty() && m_stack[m_stack_index - 1].commands.size() > 0; }
|
|
|
|
void undo();
|
|
void redo();
|
|
|
|
void finalize_current_combo();
|
|
|
|
void set_current_unmodified();
|
|
bool is_current_modified() const;
|
|
|
|
void clear();
|
|
|
|
private:
|
|
size_t non_empty_stack_index() const;
|
|
|
|
struct Combo {
|
|
NonnullOwnPtrVector<Command> commands;
|
|
};
|
|
|
|
NonnullOwnPtrVector<Combo> m_stack;
|
|
size_t m_stack_index { 0 };
|
|
Optional<size_t> m_clean_index;
|
|
};
|
|
|
|
}
|