aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-07-10 03:52:44 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-10 03:52:44 +0200
commita50b832de4019ce8b5d72e8541d64d68ed3a615a (patch)
tree7a49d864642e5c8254db9c3bbfd6ce35c890c670
parenta5be59329daa54c8afeab01a8ee530fca07baaf7 (diff)
Make autocomplete asynchronous
-rw-r--r--include/QuickMedia.hpp1
-rw-r--r--include/SearchBar.hpp3
-rw-r--r--src/QuickMedia.cpp21
-rw-r--r--src/SearchBar.cpp6
4 files changed, 27 insertions, 4 deletions
diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp
index 8884c79..584903a 100644
--- a/include/QuickMedia.hpp
+++ b/include/QuickMedia.hpp
@@ -83,6 +83,7 @@ namespace QuickMedia {
Json::Value content_storage_json;
std::unordered_set<std::string> watched_videos;
std::future<BodyItems> search_suggestion_future;
+ std::future<std::string> autocomplete_future;
std::future<void> image_download_future;
std::string downloading_chapter_url;
bool image_download_cancel = false;
diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp
index 0cffff5..6966466 100644
--- a/include/SearchBar.hpp
+++ b/include/SearchBar.hpp
@@ -12,7 +12,7 @@ namespace QuickMedia {
// Return true to consume the search (clear the search field)
using TextSubmitCallback = std::function<bool(const sf::String &text)>;
using TextBeginTypingCallback = std::function<void()>;
- using AutocompleteRequestCallback = std::function<std::string(const sf::String &text)>;
+ using AutocompleteRequestCallback = std::function<void(const sf::String &text)>;
class SearchBar {
public:
@@ -25,6 +25,7 @@ namespace QuickMedia {
void append_text(const std::string &text_to_add);
bool is_cursor_at_start_of_line() const;
void set_to_autocomplete();
+ void set_autocomplete_text(const std::string &text);
float getBottom() const;
float getBottomWithoutShadow() const;
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());
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp
index 5c3c35c..a621d02 100644
--- a/src/SearchBar.cpp
+++ b/src/SearchBar.cpp
@@ -68,7 +68,7 @@ namespace QuickMedia {
} else if(updated_autocomplete && elapsed_time >= autocomplete_search_delay) {
updated_autocomplete = false;
if(!show_placeholder && onAutocompleteRequestCallback)
- autocomplete_text.setString(onAutocompleteRequestCallback(text.getString()));
+ onAutocompleteRequestCallback(text.getString());
}
}
@@ -209,6 +209,10 @@ namespace QuickMedia {
}
}
+ void SearchBar::set_autocomplete_text(const std::string &text) {
+ autocomplete_text.setString(text);
+ }
+
void SearchBar::clear_autocomplete_if_text_not_substring() {
const sf::String &text_str = text.getString();
const sf::String &autocomplete_str = autocomplete_text.getString();