From 0627ed990089c34e1b98dbe12e19e8bc583e0f77 Mon Sep 17 00:00:00 2001 From: thislooksfun Date: Wed, 27 Oct 2021 23:13:49 -0500 Subject: [PATCH] LibGUI: Always show a box when the user requests autocomplete Previously if there were no suggestions, we simply wouldn't show the autocomplete popup at all. This is functional, but when there are no resultes it does leave the user wondering if it actually worked. Now, if the user requests autocomplete and we do have requests, it behaves exactly as before, but if there' aren't any we now show a box with the message "No suggestions" to show the user that we got the request, there just isn't anything to suggest. --- .../Libraries/LibGUI/AutocompleteProvider.cpp | 22 ++++++++++++++----- .../Libraries/LibGUI/AutocompleteProvider.h | 2 ++ Userland/Libraries/LibGUI/TextEditor.cpp | 8 +++---- Userland/Libraries/LibGUI/TextEditor.h | 6 ++++- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibGUI/AutocompleteProvider.cpp b/Userland/Libraries/LibGUI/AutocompleteProvider.cpp index a6f73bde04..280d19aefc 100644 --- a/Userland/Libraries/LibGUI/AutocompleteProvider.cpp +++ b/Userland/Libraries/LibGUI/AutocompleteProvider.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -86,10 +87,17 @@ AutocompleteBox::AutocompleteBox(TextEditor& editor) { m_popup_window = GUI::Window::construct(m_editor->window()); m_popup_window->set_window_type(GUI::WindowType::Tooltip); - m_popup_window->set_rect(0, 0, 300, 100); + m_popup_window->set_rect(0, 0, 175, 25); - m_suggestion_view = m_popup_window->set_main_widget(); + auto& main_widget = m_popup_window->set_main_widget(); + main_widget.set_fill_with_background_color(true); + main_widget.set_layout(); + + m_suggestion_view = main_widget.add(); m_suggestion_view->set_column_headers_visible(false); + m_suggestion_view->set_visible(false); + + m_no_suggestions_view = main_widget.add("No suggestions"); } void AutocompleteBox::update_suggestions(Vector&& suggestions) @@ -104,15 +112,17 @@ void AutocompleteBox::update_suggestions(Vector&& s model.set_suggestions(move(suggestions)); } else { m_suggestion_view->set_model(adopt_ref(*new AutocompleteSuggestionModel(move(suggestions)))); - m_suggestion_view->update(); if (has_suggestions) m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set); } m_suggestion_view->model()->invalidate(); + + m_suggestion_view->set_visible(has_suggestions); + m_no_suggestions_view->set_visible(!has_suggestions); + m_popup_window->resize(has_suggestions ? Gfx::IntSize(300, 100) : Gfx::IntSize(175, 25)); + m_suggestion_view->update(); - if (!has_suggestions) - close(); } bool AutocompleteBox::is_visible() const @@ -122,7 +132,7 @@ bool AutocompleteBox::is_visible() const void AutocompleteBox::show(Gfx::IntPoint suggestion_box_location) { - if (!m_suggestion_view->model() || m_suggestion_view->model()->row_count() == 0) + if (!m_suggestion_view->model()) return; m_popup_window->move_to(suggestion_box_location); diff --git a/Userland/Libraries/LibGUI/AutocompleteProvider.h b/Userland/Libraries/LibGUI/AutocompleteProvider.h index 832989b37e..1d9c298180 100644 --- a/Userland/Libraries/LibGUI/AutocompleteProvider.h +++ b/Userland/Libraries/LibGUI/AutocompleteProvider.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -92,6 +93,7 @@ private: WeakPtr m_editor; RefPtr m_popup_window; RefPtr m_suggestion_view; + RefPtr m_no_suggestions_view; }; } diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 0f59e511e7..7832c34e2b 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -814,7 +814,7 @@ void TextEditor::keydown_event(KeyEvent& event) if (is_multi_line() && !event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_Space) { if (m_autocomplete_provider) { - try_show_autocomplete(); + try_show_autocomplete(UserRequestedAutocomplete::Yes); update_autocomplete.disarm(); return; } @@ -1456,14 +1456,14 @@ void TextEditor::undefer_reflow() } } -void TextEditor::try_show_autocomplete() +void TextEditor::try_show_autocomplete(UserRequestedAutocomplete user_requested_autocomplete) { if (m_autocomplete_provider) { m_autocomplete_provider->provide_completions([&](auto completions) { auto has_completions = !completions.is_empty(); m_autocomplete_box->update_suggestions(move(completions)); auto position = content_rect_for_position(cursor()).translated(0, -visible_content_rect().y()).bottom_right().translated(screen_relative_rect().top_left().translated(ruler_width(), 0).translated(10, 5)); - if (has_completions) + if (has_completions || user_requested_autocomplete == Yes) m_autocomplete_box->show(position); }); } @@ -1958,7 +1958,7 @@ void TextEditor::set_should_autocomplete_automatically(bool value) if (value) { VERIFY(m_autocomplete_provider); - m_autocomplete_timer = Core::Timer::create_single_shot(m_automatic_autocomplete_delay_ms, [this] { try_show_autocomplete(); }); + m_autocomplete_timer = Core::Timer::create_single_shot(m_automatic_autocomplete_delay_ms, [this] { try_show_autocomplete(UserRequestedAutocomplete::No); }); return; } diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index f783d29b51..7d2253b3fc 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -274,7 +274,11 @@ private: void defer_reflow(); void undefer_reflow(); - void try_show_autocomplete(); + enum UserRequestedAutocomplete { + No, + Yes + }; + void try_show_autocomplete(UserRequestedAutocomplete); int icon_size() const { return 16; } int icon_padding() const { return 2; }