mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 18:24:45 -05:00
LibGUI: Remember modified state on undo/redo actions
This commit is contained in:
parent
67537bfc80
commit
928f16d360
Notes:
sideshowbarker
2024-07-18 18:39:25 +09:00
Author: https://github.com/pakerwreah Commit: https://github.com/SerenityOS/serenity/commit/928f16d3608 Pull-request: https://github.com/SerenityOS/serenity/pull/6868 Reviewed-by: https://github.com/awesomekling
5 changed files with 39 additions and 12 deletions
|
@ -299,7 +299,7 @@ MainWidget::MainWidget()
|
|||
return;
|
||||
}
|
||||
|
||||
editor().document().set_modified(false);
|
||||
editor().document().set_unmodified();
|
||||
// FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
|
||||
window()->set_modified(false);
|
||||
|
||||
|
@ -312,7 +312,7 @@ MainWidget::MainWidget()
|
|||
if (!m_editor->write_to_file(m_path)) {
|
||||
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||
} else {
|
||||
editor().document().set_modified(false);
|
||||
editor().document().set_unmodified();
|
||||
// FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
|
||||
window()->set_modified(false);
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ TextDocument::TextDocument(Client* client)
|
|||
if (client)
|
||||
m_clients.set(client);
|
||||
append_line(make<TextDocumentLine>(*this));
|
||||
set_modified(false);
|
||||
set_unmodified();
|
||||
|
||||
m_undo_timer = Core::Timer::create_single_shot(
|
||||
2000, [this] {
|
||||
|
@ -94,7 +94,7 @@ bool TextDocument::set_text(const StringView& text)
|
|||
clear_text_guard.disarm();
|
||||
|
||||
// FIXME: Should the modified state be cleared on some of the earlier returns as well?
|
||||
set_modified(false);
|
||||
set_unmodified();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -309,8 +309,6 @@ void TextDocument::update_views(Badge<TextDocumentLine>)
|
|||
|
||||
void TextDocument::notify_did_change()
|
||||
{
|
||||
set_modified(true);
|
||||
|
||||
if (m_undo_timer)
|
||||
m_undo_timer->restart();
|
||||
|
||||
|
@ -887,9 +885,9 @@ const TextDocumentSpan* TextDocument::span_at(const TextPosition& position) cons
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void TextDocument::set_modified(bool modified)
|
||||
void TextDocument::set_unmodified()
|
||||
{
|
||||
m_modified = modified;
|
||||
m_undo_stack.set_current_unmodified();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -122,8 +122,8 @@ public:
|
|||
virtual bool is_code_document() const { return false; }
|
||||
|
||||
bool is_empty() const;
|
||||
bool is_modified() const { return m_modified; }
|
||||
void set_modified(bool);
|
||||
bool is_modified() const { return m_undo_stack.is_current_modified(); }
|
||||
void set_unmodified();
|
||||
|
||||
protected:
|
||||
explicit TextDocument(Client* client);
|
||||
|
@ -136,7 +136,6 @@ private:
|
|||
|
||||
HashTable<Client*> m_clients;
|
||||
bool m_client_notifications_enabled { true };
|
||||
bool m_modified { false };
|
||||
|
||||
UndoStack m_undo_stack;
|
||||
RefPtr<Core::Timer> m_undo_timer;
|
||||
|
|
|
@ -61,6 +61,14 @@ void UndoStack::push(NonnullOwnPtr<Command>&& command)
|
|||
if (m_stack_index > 0) {
|
||||
for (size_t i = 0; i < m_stack_index; i++)
|
||||
m_stack.remove(0);
|
||||
|
||||
if (m_clean_index.has_value()) {
|
||||
if (m_clean_index.value() < m_stack_index)
|
||||
m_clean_index.clear();
|
||||
else
|
||||
m_clean_index = m_clean_index.value() - m_stack_index;
|
||||
}
|
||||
|
||||
m_stack_index = 0;
|
||||
finalize_current_combo();
|
||||
}
|
||||
|
@ -78,12 +86,30 @@ void UndoStack::finalize_current_combo()
|
|||
|
||||
auto undo_commands_container = make<UndoCommandsContainer>();
|
||||
m_stack.prepend(move(undo_commands_container));
|
||||
|
||||
if (m_clean_index.has_value())
|
||||
m_clean_index = m_clean_index.value() + 1;
|
||||
}
|
||||
|
||||
void UndoStack::set_current_unmodified()
|
||||
{
|
||||
// Skip empty container
|
||||
if (can_undo() && m_stack[m_stack_index].m_undo_vector.is_empty())
|
||||
m_clean_index = m_stack_index + 1;
|
||||
else
|
||||
m_clean_index = m_stack_index;
|
||||
}
|
||||
|
||||
bool UndoStack::is_current_modified() const
|
||||
{
|
||||
return !m_clean_index.has_value() || m_stack_index != m_clean_index.value();
|
||||
}
|
||||
|
||||
void UndoStack::clear()
|
||||
{
|
||||
m_stack.clear();
|
||||
m_stack_index = 0;
|
||||
m_clean_index.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,13 +19,16 @@ public:
|
|||
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(); }
|
||||
bool can_redo() const { return m_stack_index > 0 && !m_stack.is_empty() && m_stack[m_stack_index - 1].m_undo_vector.size() > 0; }
|
||||
|
||||
void undo();
|
||||
void redo();
|
||||
|
||||
void finalize_current_combo();
|
||||
|
||||
void set_current_unmodified();
|
||||
bool is_current_modified() const;
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
|
@ -35,6 +38,7 @@ private:
|
|||
|
||||
NonnullOwnPtrVector<UndoCommandsContainer> m_stack;
|
||||
size_t m_stack_index { 0 };
|
||||
Optional<size_t> m_clean_index;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue