diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-09 16:20:36 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-09 16:20:36 +0200 |
commit | 151b7149e615b6954a31a8524027ea2f4575cff4 (patch) | |
tree | 1c4a790e8d4b75f914266b747d806633fbfb2bf6 /LibGUI | |
parent | 60a819c14ac5a843c521f1c7b947dd578223b5ca (diff) | |
download | serenity-151b7149e615b6954a31a8524027ea2f4575cff4.zip |
GTextEditor: Add GTextEditor::on_change callback for when content changes.
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GTextEditor.cpp | 27 | ||||
-rw-r--r-- | LibGUI/GTextEditor.h | 3 |
2 files changed, 24 insertions, 6 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index f409f06a54..1c17d14b0b 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -33,6 +33,7 @@ void GTextEditor::set_text(const String& text) if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters(), m_lines[0]->characters(), text.length())) return; + m_selection.clear(); m_lines.clear(); int start_of_current_line = 0; @@ -414,8 +415,8 @@ void GTextEditor::do_delete() if (m_cursor.column() < current_line().length()) { // Delete within line current_line().remove(m_cursor.column()); - update_content_size(); update_cursor(); + did_change(); return; } if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) { @@ -424,9 +425,9 @@ void GTextEditor::do_delete() int previous_length = current_line().length(); current_line().append(next_line.characters(), next_line.length()); m_lines.remove(m_cursor.line() + 1); - update_content_size(); update(); set_cursor(m_cursor.line(), previous_length); + did_change(); return; } } @@ -451,18 +452,18 @@ void GTextEditor::insert_at_cursor(char ch) } if (at_tail || at_head) { m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>()); - update_content_size(); update(); set_cursor(m_cursor.line() + 1, 0); + did_change(); return; } auto new_line = make<Line>(); new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column()); current_line().truncate(m_cursor.column()); m_lines.insert(m_cursor.line() + 1, move(new_line)); - update_content_size(); update(); set_cursor(m_cursor.line() + 1, 0); + did_change(); return; } if (ch == '\t') { @@ -471,15 +472,15 @@ void GTextEditor::insert_at_cursor(char ch) for (int i = 0; i < spaces_to_insert; ++i) { current_line().insert(m_cursor.column(), ' '); } - update_content_size(); set_cursor(m_cursor.line(), next_soft_tab_stop); update_cursor(); + did_change(); return; } current_line().insert(m_cursor.column(), ch); - update_content_size(); set_cursor(m_cursor.line(), m_cursor.column() + 1); update_cursor(); + did_change(); } Rect GTextEditor::cursor_content_rect() const @@ -772,6 +773,7 @@ void GTextEditor::delete_selection() m_selection.clear(); set_cursor(selection.start()); update(); + did_change(); } void GTextEditor::insert_at_cursor_or_replace_selection(const String& text) @@ -814,3 +816,16 @@ void GTextEditor::leave_event(GEvent&) ASSERT(window()); window()->set_override_cursor(GStandardCursor::None); } + +void GTextEditor::did_change() +{ + update_content_size(); + if (!m_have_pending_change_notification) { + m_have_pending_change_notification = true; + deferred_invoke([this] (auto&) { + if (on_change) + on_change(); + m_have_pending_change_notification = false; + }); + } +} diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 0e2313c9b5..42a5df16e7 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -96,6 +96,7 @@ public: void do_delete(); void delete_current_line(); + Function<void()> on_change; Function<void(GTextEditor&)> on_return_pressed; Function<void(GTextEditor&)> on_escape_pressed; @@ -116,6 +117,7 @@ private: void paint_ruler(Painter&); void update_content_size(); + void did_change(); class Line { friend class GTextEditor; @@ -163,6 +165,7 @@ private: bool m_cursor_state { true }; bool m_in_drag_select { false }; bool m_ruler_visible { true }; + bool m_have_pending_change_notification { false }; int m_line_spacing { 4 }; int m_soft_tab_width { 4 }; int m_horizontal_content_padding { 2 }; |