1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-28 16:42:12 +00:00

LibGUI+TextEditor: Make TextDocument modified state track undo stack

This was quite unreliable before. Changes to the undo stack's modified
state are now reflected in the document's modified state, and the
GUI::TextEditor widget has its undo/redo actions updated automatically.

UndoStack is still a bit hard to understand due to the lazy coalescing
of commands, and that's something we should improve upon (e.g with more
explicit, incremental command merging.) But for now, this is a nice
improvement and undo/redo finally behaves in a way that feels natural.
This commit is contained in:
Andreas Kling 2021-05-08 13:40:33 +02:00
parent ee19f7c0aa
commit 2905e10513
4 changed files with 23 additions and 4 deletions

View file

@ -19,7 +19,7 @@ UndoStack::~UndoStack()
bool UndoStack::can_undo() const
{
return m_stack_index > 0;
return m_stack_index > 0 || (m_stack.size() == 1 && m_stack[0].commands.size() > 0);
}
bool UndoStack::can_redo() const
@ -67,7 +67,7 @@ void UndoStack::pop()
void UndoStack::push(NonnullOwnPtr<Command>&& command)
{
if (m_stack.is_empty())
m_stack.append(make<Combo>());
finalize_current_combo();
// If the stack cursor is behind the top of the stack, nuke everything from here to the top.
if (m_stack_index != m_stack.size() - 1) {
@ -101,6 +101,8 @@ void UndoStack::finalize_current_combo()
void UndoStack::set_current_unmodified()
{
finalize_current_combo();
if (m_clean_index.has_value() && m_clean_index.value() == m_stack_index)
return;
m_clean_index = m_stack_index;
@ -111,7 +113,15 @@ void UndoStack::set_current_unmodified()
bool UndoStack::is_current_modified() const
{
return !(m_clean_index.has_value() && m_clean_index.value() == m_stack_index);
if (!m_clean_index.has_value())
return true;
if (m_stack_index != m_clean_index.value())
return true;
if (m_stack.is_empty())
return false;
if (m_stack_index == m_stack.size() - 1 && !m_stack[m_stack_index].commands.is_empty())
return true;
return false;
}
void UndoStack::clear()