From 86ea41970df46ecb0de291b90b418b482aa692bd Mon Sep 17 00:00:00 2001 From: thislooksfun Date: Wed, 27 Oct 2021 23:39:55 -0500 Subject: [PATCH] LibGUI: Hide autocomplete on any event other than typing Moving the cursor to a different location, by any means, should dismiss the autocomplete popup. This is the behavior of virtually every editor/IDE out there, and it is really annoying (and confusing) when our autocomplete doesn't behave like that. --- Userland/Libraries/LibGUI/TextEditor.cpp | 22 ++++++++++++++++------ Userland/Libraries/LibGUI/TextEditor.h | 3 ++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 7832c34e2b..cffe6fa8f3 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -738,7 +738,6 @@ void TextEditor::select_all() void TextEditor::keydown_event(KeyEvent& event) { - TemporaryChange change { m_should_keep_autocomplete_box, true }; if (m_autocomplete_box && m_autocomplete_box->is_visible() && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Tab)) { m_autocomplete_box->apply_suggestion(); m_autocomplete_box->close(); @@ -882,6 +881,7 @@ void TextEditor::keydown_event(KeyEvent& event) } if (!event.ctrl() && !event.alt() && event.code_point() != 0) { + TemporaryChange change { m_should_keep_autocomplete_box, true }; add_code_point(event.code_point()); return; } @@ -1469,6 +1469,15 @@ void TextEditor::try_show_autocomplete(UserRequestedAutocomplete user_requested_ } } +void TextEditor::hide_autocomplete_if_needed() +{ + if (m_autocomplete_box && !m_should_keep_autocomplete_box) { + m_autocomplete_box->close(); + if (m_autocomplete_timer) + m_autocomplete_timer->stop(); + } +} + void TextEditor::enter_event(Core::Event&) { m_automatic_selection_scroll_timer->stop(); @@ -1484,11 +1493,7 @@ void TextEditor::did_change(AllowCallback allow_callback) { update_content_size(); recompute_all_visual_lines(); - if (m_autocomplete_box && !m_should_keep_autocomplete_box) { - m_autocomplete_box->close(); - if (m_autocomplete_timer) - m_autocomplete_timer->stop(); - } + hide_autocomplete_if_needed(); m_needs_rehighlight = true; if (!m_has_pending_change_notification) { m_has_pending_change_notification = true; @@ -1827,6 +1832,11 @@ void TextEditor::document_did_set_cursor(TextPosition const& position) set_cursor(position); } +void TextEditor::cursor_did_change() +{ + hide_autocomplete_if_needed(); +} + void TextEditor::clipboard_content_did_change(String const& mime_type) { m_paste_action->set_enabled(is_editable() && mime_type.starts_with("text/") && !Clipboard::the().data().is_empty()); diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 7d2253b3fc..9a1cfb8683 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -229,7 +229,7 @@ protected: virtual void context_menu_event(ContextMenuEvent&) override; virtual void resize_event(ResizeEvent&) override; virtual void theme_change_event(ThemeChangeEvent&) override; - virtual void cursor_did_change() { } + virtual void cursor_did_change(); Gfx::IntRect ruler_content_rect(size_t line) const; Gfx::IntRect gutter_content_rect(size_t line) const; @@ -279,6 +279,7 @@ private: Yes }; void try_show_autocomplete(UserRequestedAutocomplete); + void hide_autocomplete_if_needed(); int icon_size() const { return 16; } int icon_padding() const { return 2; }