From a50b832de4019ce8b5d72e8541d64d68ed3a615a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 10 Jul 2020 03:52:44 +0200 Subject: Make autocomplete asynchronous --- src/QuickMedia.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index e30d453..2ccb9d6 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -350,6 +350,9 @@ namespace QuickMedia { bool search_running = false; bool typing = false; + std::string autocomplete_text; + bool autocomplete_running = false; + Body history_body(this, font, bold_font); const float tab_text_size = 18.0f; const float tab_height = tab_text_size + 10.0f; @@ -410,8 +413,9 @@ namespace QuickMedia { }; search_bar->autocomplete_search_delay = current_plugin->get_autocomplete_delay(); - search_bar->onAutocompleteRequestCallback = [this](const sf::String &text) { - return current_plugin->autocomplete_search(text); + search_bar->onAutocompleteRequestCallback = [this, &tabs, &selected_tab, &autocomplete_text](const sf::String &text) { + if(tabs[selected_tab].body == body && !current_plugin->search_is_filter()) + autocomplete_text = text; }; search_bar->onTextUpdateCallback = [&update_search_text, this, &tabs, &selected_tab, &typing](const std::string &text) { @@ -559,6 +563,19 @@ namespace QuickMedia { search_running = false; } + if(!autocomplete_text.empty() && !autocomplete_running) { + autocomplete_future = std::async(std::launch::async, [this, autocomplete_text]() { + return current_plugin->autocomplete_search(autocomplete_text); + }); + autocomplete_text.clear(); + autocomplete_running = true; + } + + if(autocomplete_running && autocomplete_future.valid() && autocomplete_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + search_bar->set_autocomplete_text(autocomplete_future.get()); + autocomplete_running = false; + } + window.clear(back_color); { tab_spacing_rect.setPosition(0.0f, search_bar->getBottomWithoutShadow()); -- cgit v1.2.3