diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-08 13:40:33 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-08 13:49:34 +0200 |
commit | 2905e10513859de9d0fce4e7f18a99bf1ecb01a7 (patch) | |
tree | 5c3e0a641d69ac3932b282e0dcda287fc6dcf94a /Userland/Libraries | |
parent | ee19f7c0aac48b1ca6527413552fc05a4fe16710 (diff) | |
download | serenity-2905e10513859de9d0fce4e7f18a99bf1ecb01a7.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/UndoStack.cpp | 16 |
3 files changed, 19 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index bd9b9754e9..7e1582562d 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1638,6 +1638,11 @@ void TextEditor::document_did_update_undo_stack() { m_undo_action->set_enabled(can_undo()); m_redo_action->set_enabled(can_redo()); + + // FIXME: This is currently firing more often than it should. + // Ideally we'd only send this out when the undo stack modified state actually changes. + if (on_modified_change) + on_modified_change(document().is_modified()); } void TextEditor::document_did_set_text() diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index d99929718b..7c6f4b8978 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -134,6 +134,7 @@ public: virtual void redo() { document().redo(); } Function<void()> on_change; + Function<void(bool modified)> on_modified_change; Function<void()> on_mousedown; Function<void()> on_return_pressed; Function<void()> on_escape_pressed; diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp index 8437582a63..dec25df49c 100644 --- a/Userland/Libraries/LibGUI/UndoStack.cpp +++ b/Userland/Libraries/LibGUI/UndoStack.cpp @@ -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() |