mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
LibGUI: Use UndoStack::on_state_change inside TextDocument/TextEditor
Have TextDocument listen for state changes on the internal undo stack, and forward those to all clients via a new virtual function. This simplifies updating the can_undo / can_redo states of TextEditor.
This commit is contained in:
parent
0cb6103928
commit
ee19f7c0aa
5 changed files with 17 additions and 2 deletions
|
@ -102,6 +102,7 @@ public:
|
|||
virtual void document_did_change() override {};
|
||||
virtual void document_did_set_text() override {};
|
||||
virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
|
||||
virtual void document_did_update_undo_stack() override { }
|
||||
|
||||
virtual bool is_automatic_indentation_enabled() const override { return false; }
|
||||
virtual int soft_tab_width() const override { return 4; }
|
||||
|
|
|
@ -28,6 +28,13 @@ TextDocument::TextDocument(Client* client)
|
|||
append_line(make<TextDocumentLine>(*this));
|
||||
set_unmodified();
|
||||
|
||||
m_undo_stack.on_state_change = [this] {
|
||||
if (m_client_notifications_enabled) {
|
||||
for (auto* client : m_clients)
|
||||
client->document_did_update_undo_stack();
|
||||
}
|
||||
};
|
||||
|
||||
m_undo_timer = Core::Timer::create_single_shot(
|
||||
2000, [this] {
|
||||
update_undo();
|
||||
|
@ -694,6 +701,7 @@ void TextDocument::redo()
|
|||
void TextDocument::add_to_undo_stack(NonnullOwnPtr<TextDocumentUndoCommand> undo_command)
|
||||
{
|
||||
m_undo_stack.push(move(undo_command));
|
||||
notify_did_change();
|
||||
}
|
||||
|
||||
TextDocumentUndoCommand::TextDocumentUndoCommand(TextDocument& document)
|
||||
|
|
|
@ -47,6 +47,7 @@ public:
|
|||
virtual void document_did_change() = 0;
|
||||
virtual void document_did_set_text() = 0;
|
||||
virtual void document_did_set_cursor(const TextPosition&) = 0;
|
||||
virtual void document_did_update_undo_stack() = 0;
|
||||
|
||||
virtual bool is_automatic_indentation_enabled() const = 0;
|
||||
virtual int soft_tab_width() const = 0;
|
||||
|
|
|
@ -1324,8 +1324,6 @@ void TextEditor::did_change()
|
|||
{
|
||||
update_content_size();
|
||||
recompute_all_visual_lines();
|
||||
m_undo_action->set_enabled(can_undo());
|
||||
m_redo_action->set_enabled(can_redo());
|
||||
if (m_autocomplete_box && !m_should_keep_autocomplete_box) {
|
||||
m_autocomplete_box->close();
|
||||
if (m_autocomplete_timer)
|
||||
|
@ -1636,6 +1634,12 @@ void TextEditor::document_did_change()
|
|||
update();
|
||||
}
|
||||
|
||||
void TextEditor::document_did_update_undo_stack()
|
||||
{
|
||||
m_undo_action->set_enabled(can_undo());
|
||||
m_redo_action->set_enabled(can_redo());
|
||||
}
|
||||
|
||||
void TextEditor::document_did_set_text()
|
||||
{
|
||||
m_line_visual_data.clear();
|
||||
|
|
|
@ -223,6 +223,7 @@ private:
|
|||
virtual void document_did_change() override;
|
||||
virtual void document_did_set_text() override;
|
||||
virtual void document_did_set_cursor(const TextPosition&) override;
|
||||
virtual void document_did_update_undo_stack() override;
|
||||
|
||||
// ^Syntax::HighlighterClient
|
||||
virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue