From b96dce2d670e085067d7b62b43fceac7658c2d5a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 13 Jul 2020 12:04:12 +0200 Subject: Add recommended tab for youtube --- src/QuickMedia.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index db244c8..ed94700 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -355,7 +355,8 @@ namespace QuickMedia { enum class SearchSuggestionTab { ALL, - HISTORY + HISTORY, + RECOMMENDED }; static void fill_history_items_from_json(const Json::Value &history_json, BodyItems &history_items) { @@ -477,10 +478,17 @@ namespace QuickMedia { bool autocomplete_running = false; Body history_body(this, font, bold_font); + std::unique_ptr recommended_body; const float tab_text_size = 18.0f; const float tab_height = tab_text_size + 10.0f; sf::Text all_tab_text("All", font, tab_text_size); sf::Text history_tab_text("History", font, tab_text_size); + sf::Text recommended_tab_text("Recommended", font, tab_text_size); + + if(current_plugin->name == "youtube") { + recommended_body = std::make_unique(this, font, bold_font); + recommended_body->draw_thumbnails = true; + } struct Tab { Body *body; @@ -488,7 +496,10 @@ namespace QuickMedia { sf::Text *text; }; - std::array tabs = { Tab{body, SearchSuggestionTab::ALL, &all_tab_text}, Tab{&history_body, SearchSuggestionTab::HISTORY, &history_tab_text} }; + std::vector tabs = { Tab{body, SearchSuggestionTab::ALL, &all_tab_text}, Tab{&history_body, SearchSuggestionTab::HISTORY, &history_tab_text} }; + if(recommended_body) + tabs.push_back(Tab{recommended_body.get(), SearchSuggestionTab::RECOMMENDED, &recommended_tab_text}); + int selected_tab = 0; plugin_get_watch_history(current_plugin, history_body.items); @@ -505,13 +516,17 @@ namespace QuickMedia { autocomplete_text = text; }; - search_bar->onTextUpdateCallback = [&update_search_text, this, &tabs, &selected_tab, &typing](const std::string &text) { + std::string recommended_filter; + + search_bar->onTextUpdateCallback = [&update_search_text, this, &tabs, &selected_tab, &typing, &recommended_body, &recommended_filter](const std::string &text) { if(tabs[selected_tab].body == body && !current_plugin->search_is_filter()) update_search_text = text; else { tabs[selected_tab].body->filter_search_fuzzy(text); tabs[selected_tab].body->clamp_selection(); } + if(tabs[selected_tab].body == recommended_body.get()) + recommended_filter = text; typing = false; }; @@ -567,8 +582,17 @@ namespace QuickMedia { return true; }; - PluginResult front_page_result = current_plugin->get_front_page(body->items); - body->clamp_selection(); + std::future recommended_future; + if(recommended_body) { + recommended_future = std::async(std::launch::async, [this]() { + BodyItems body_items; + PluginResult front_page_result = current_plugin->get_front_page(body_items); + return body_items; + }); + } else { + PluginResult front_page_result = current_plugin->get_front_page(body->items); + body->clamp_selection(); + } sf::Vector2f body_pos; sf::Vector2f body_size; @@ -663,6 +687,12 @@ namespace QuickMedia { autocomplete_running = false; } + if(recommended_future.valid() && recommended_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + recommended_body->items = recommended_future.get(); + recommended_body->filter_search_fuzzy(recommended_filter); + recommended_body->clamp_selection(); + } + window.clear(back_color); { tab_spacing_rect.setPosition(0.0f, search_bar->getBottomWithoutShadow()); -- cgit v1.2.3