diff options
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r-- | src/QuickMedia.cpp | 257 |
1 files changed, 140 insertions, 117 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 046ba9d..c9950a0 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -40,7 +40,7 @@ static const sf::Color back_color(21, 25, 30); static const std::string fourchan_google_captcha_api_key = "6Ldp2bsSAAAAAAJ5uyx_lx34lJeEpTLVkP5k04qc"; -static const float tab_text_size = 18.0f; +static const float tab_text_size = 16.0f; static const float tab_height = tab_text_size + 10.0f; static const sf::Color tab_selected_color(55, 60, 68); static const float tab_margin_x = 10.0f; @@ -188,6 +188,11 @@ namespace QuickMedia { Page *search_page; }; + template <typename T> + static bool is_future_ready(const std::future<T> &future) { + return future.valid() && future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; + } + static Path get_recommended_filepath(const char *plugin_name) { Path video_history_dir = get_storage_dir().join("recommended"); if(create_directory_recursive(video_history_dir) != 0) { @@ -555,7 +560,6 @@ namespace QuickMedia { if(strcmp(plugin_name, "manganelo") == 0) { auto search_body = create_body(); - search_body->draw_thumbnails = true; tabs.push_back(Tab{std::move(search_body), std::make_unique<ManganeloSearchPage>(this), create_search_bar("Search...", 200)}); auto history_body = create_body(); @@ -563,7 +567,6 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(history_body), std::make_unique<HistoryPage>(this, tabs.front().page.get()), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "mangatown") == 0) { auto search_body = create_body(); - search_body->draw_thumbnails = true; tabs.push_back(Tab{std::move(search_body), std::make_unique<MangatownSearchPage>(this), create_search_bar("Search...", 200)}); auto history_body = create_body(); @@ -571,7 +574,6 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(history_body), std::make_unique<HistoryPage>(this, tabs.front().page.get()), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "mangadex") == 0) { auto search_body = create_body(); - search_body->draw_thumbnails = true; tabs.push_back(Tab{std::move(search_body), std::make_unique<MangadexSearchPage>(this), create_search_bar("Search...", 300)}); auto history_body = create_body(); @@ -598,21 +600,17 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(file_manager_body), std::move(file_manager_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "youtube") == 0) { auto search_body = create_body(); - search_body->draw_thumbnails = true; tabs.push_back(Tab{std::move(search_body), std::make_unique<YoutubeSearchPage>(this), create_search_bar("Search...", 350)}); auto history_body = create_body(); - history_body->draw_thumbnails = true; youtube_get_watch_history(history_body->items); tabs.push_back(Tab{std::move(history_body), std::make_unique<HistoryPage>(this, tabs.front().page.get()), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); auto recommended_body = create_body(); - recommended_body->draw_thumbnails = true; fill_recommended_items_from_json(plugin_name, load_recommended_json(), recommended_body->items); tabs.push_back(Tab{std::move(recommended_body), std::make_unique<RecommendedPage>(this, tabs.front().page.get()), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "pornhub") == 0) { auto search_body = create_body(); - search_body->draw_thumbnails = true; tabs.push_back(Tab{std::move(search_body), std::make_unique<PornhubSearchPage>(this), create_search_bar("Search...", 500)}); } @@ -928,6 +926,7 @@ namespace QuickMedia { for(Tab &tab : tabs) { tab.body->thumbnail_max_size = tab.page->get_thumbnail_max_size(); + tab.page->on_navigate_to_page(); } const Json::Value *json_chapters = &Json::Value::nullSingleton(); @@ -937,15 +936,27 @@ namespace QuickMedia { json_chapters = &chapters_json; } + enum class FetchType { + SEARCH, + LAZY + }; + + struct FetchResult { + BodyItems body_items; + PluginResult result; + }; + struct TabAssociatedData { std::string update_search_text; bool search_text_updated = false; - bool search_running = false; + FetchStatus fetch_status = FetchStatus::NONE; + bool lazy_fetch_finished = false; + FetchType fetch_type; bool typing = false; bool fetching_next_page_running = false; int fetched_page = 0; sf::Text search_result_text; - std::future<BodyItems> search_future; + std::future<FetchResult> fetch_future; std::future<BodyItems> next_page_future; }; @@ -969,7 +980,7 @@ namespace QuickMedia { bool loop_running = true; bool redraw = true; - auto submit_handler = [this, &tabs, &selected_tab, &loop_running, &redraw]() { + auto submit_handler = [this, &json_chapters, &tabs, &selected_tab, &loop_running, &redraw]() { BodyItem *selected_item = tabs[selected_tab].body->get_selected(); if(!selected_item) return; @@ -1039,6 +1050,10 @@ namespace QuickMedia { redraw = true; } else { page_loop(std::move(new_tabs)); + tabs[selected_tab].page->on_navigate_to_page(); + const Json::Value &chapters_json = content_storage_json["chapters"]; + if(chapters_json.isObject()) + json_chapters = &chapters_json; } } else { // TODO: Show the exact cause of error (get error message from curl). @@ -1093,6 +1108,8 @@ namespace QuickMedia { while (window.isOpen() && loop_running) { sf::Int32 frame_time_ms = frame_timer.restart().asMilliseconds(); + Tab ¤t_tab = tabs[selected_tab]; + TabAssociatedData ¤t_tab_associated_data = tab_associated_data[selected_tab]; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { @@ -1104,10 +1121,10 @@ namespace QuickMedia { window.setView(sf::View(visible_area)); } - if(tabs[selected_tab].search_bar) { + if(current_tab.search_bar) { if(event.type == sf::Event::TextEntered) - tabs[selected_tab].search_bar->onTextEntered(event.text.unicode); - tabs[selected_tab].search_bar->on_event(event); + current_tab.search_bar->onTextEntered(event.text.unicode); + current_tab.search_bar->on_event(event); } if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) @@ -1117,26 +1134,26 @@ namespace QuickMedia { bool hit_bottom = false; switch(event.key.code) { case sf::Keyboard::Down: - hit_bottom = !tabs[selected_tab].body->select_next_item(); + hit_bottom = !current_tab.body->select_next_item(); break; case sf::Keyboard::PageDown: - hit_bottom = !tabs[selected_tab].body->select_next_page(); + hit_bottom = !current_tab.body->select_next_page(); break; case sf::Keyboard::End: - tabs[selected_tab].body->select_last_item(); + current_tab.body->select_last_item(); hit_bottom = true; break; default: hit_bottom = false; break; } - if(hit_bottom && !tab_associated_data[selected_tab].search_running && !tab_associated_data[selected_tab].fetching_next_page_running && tabs[selected_tab].page) { + if(hit_bottom && current_tab_associated_data.fetch_status == FetchStatus::NONE && !current_tab_associated_data.fetching_next_page_running && current_tab.page) { gradient_inc = 0.0; - tab_associated_data[selected_tab].fetching_next_page_running = true; - int next_page = tab_associated_data[selected_tab].fetched_page + 1; - Page *page = tabs[selected_tab].page.get(); - std::string update_search_text = tab_associated_data[selected_tab].update_search_text; - tab_associated_data[selected_tab].next_page_future = std::async(std::launch::async, [update_search_text, next_page, page]() { + current_tab_associated_data.fetching_next_page_running = true; + int next_page = current_tab_associated_data.fetched_page + 1; + Page *page = current_tab.page.get(); + std::string update_search_text = current_tab_associated_data.update_search_text; + current_tab_associated_data.next_page_future = std::async(std::launch::async, [update_search_text, next_page, page]() { BodyItems result_items; if(page->get_page(update_search_text, next_page, result_items) != PluginResult::OK) fprintf(stderr, "Failed to get next page (page %d)\n", next_page); @@ -1144,33 +1161,33 @@ namespace QuickMedia { }); } } else if(event.key.code == sf::Keyboard::Up) { - tabs[selected_tab].body->select_previous_item(); + current_tab.body->select_previous_item(); } else if(event.key.code == sf::Keyboard::PageUp) { - tabs[selected_tab].body->select_previous_page(); + current_tab.body->select_previous_page(); } else if(event.key.code == sf::Keyboard::Home) { - tabs[selected_tab].body->select_first_item(); + current_tab.body->select_first_item(); } else if(event.key.code == sf::Keyboard::Escape) { goto page_end; } else if(event.key.code == sf::Keyboard::Left) { if(selected_tab > 0) { - tabs[selected_tab].body->clear_cache(); + current_tab.body->clear_cache(); --selected_tab; redraw = true; } } else if(event.key.code == sf::Keyboard::Right) { if(selected_tab < (int)tabs.size() - 1) { - tabs[selected_tab].body->clear_cache(); + current_tab.body->clear_cache(); ++selected_tab; redraw = true; } } else if(event.key.code == sf::Keyboard::Tab) { - if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->set_to_autocomplete(); + if(current_tab.search_bar) current_tab.search_bar->set_to_autocomplete(); } else if(event.key.code == sf::Keyboard::Enter) { - if(!tabs[selected_tab].search_bar) submit_handler(); + if(!current_tab.search_bar) submit_handler(); } else if(event.key.code == sf::Keyboard::T && event.key.control) { - BodyItem *selected_item = tabs[selected_tab].body->get_selected(); - if(selected_item && tabs[selected_tab].page && tabs[selected_tab].page->is_trackable()) { - TrackablePage *trackable_page = static_cast<TrackablePage*>(tabs[selected_tab].page.get()); + BodyItem *selected_item = current_tab.body->get_selected(); + if(selected_item && current_tab.page && current_tab.page->is_trackable()) { + TrackablePage *trackable_page = static_cast<TrackablePage*>(current_tab.page.get()); TrackResult track_result = trackable_page->track(selected_item->get_title()); // TODO: Show proper error message when this fails. For example if we are already tracking the manga if(track_result == TrackResult::OK) { @@ -1185,9 +1202,9 @@ namespace QuickMedia { if(redraw) { redraw = false; - if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->onWindowResize(window_size); + if(current_tab.search_bar) current_tab.search_bar->onWindowResize(window_size); // TODO: Dont show tabs if there is only one tab - get_body_dimensions(window_size, tabs[selected_tab].search_bar.get(), body_pos, body_size, true); + get_body_dimensions(window_size, current_tab.search_bar.get(), body_pos, body_size, true); gradient_points[0].position.x = 0.0f; gradient_points[0].position.y = window_size.y - gradient_height; @@ -1202,12 +1219,24 @@ namespace QuickMedia { gradient_points[3].position.y = window_size.y; } - if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->update(); + if(current_tab.search_bar) current_tab.search_bar->update(); + + if(current_tab.page->is_lazy_fetch_page() && current_tab_associated_data.fetch_status == FetchStatus::NONE && !current_tab_associated_data.lazy_fetch_finished) { + current_tab_associated_data.fetch_status = FetchStatus::LOADING; + current_tab_associated_data.fetch_type = FetchType::LAZY; + current_tab_associated_data.search_result_text.setString("Fetching page..."); + LazyFetchPage *lazy_fetch_page = static_cast<LazyFetchPage*>(current_tab.page.get()); + current_tab_associated_data.fetch_future = std::async(std::launch::async, [lazy_fetch_page]() { + FetchResult fetch_result; + fetch_result.result = lazy_fetch_page->lazy_fetch(fetch_result.body_items); + return fetch_result; + }); + } for(size_t i = 0; i < tabs.size(); ++i) { TabAssociatedData &associated_data = tab_associated_data[i]; - if(associated_data.fetching_next_page_running && associated_data.next_page_future.valid() && associated_data.next_page_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(associated_data.fetching_next_page_running && is_future_ready(associated_data.next_page_future)) { BodyItems new_body_items = associated_data.next_page_future.get(); fprintf(stderr, "Finished fetching page %d, num new messages: %zu\n", associated_data.fetched_page + 1, new_body_items.size()); size_t num_new_messages = new_body_items.size(); @@ -1218,65 +1247,66 @@ namespace QuickMedia { associated_data.fetching_next_page_running = false; } - if(associated_data.search_text_updated && !associated_data.search_running && !associated_data.fetching_next_page_running) { - Page *page = tabs[i].page.get(); + if(associated_data.search_text_updated && associated_data.fetch_status == FetchStatus::NONE && !associated_data.fetching_next_page_running) { std::string update_search_text = associated_data.update_search_text; - associated_data.search_future = std::async(std::launch::async, [update_search_text, page]() { - BodyItems result_items; - if(page->search(update_search_text, result_items) != SearchResult::OK) { - show_notification("QuickMedia", "Search failed!", Urgency::CRITICAL); - } - return result_items; - }); - update_search_text.clear(); + associated_data.update_search_text.clear(); associated_data.search_text_updated = false; - associated_data.search_running = true; + associated_data.fetch_status = FetchStatus::LOADING; + associated_data.fetch_type = FetchType::SEARCH; associated_data.search_result_text.setString("Searching..."); + Page *page = tabs[i].page.get(); + associated_data.fetch_future = std::async(std::launch::async, [update_search_text, page]() { + FetchResult fetch_result; + fetch_result.result = search_result_to_plugin_result(page->search(update_search_text, fetch_result.body_items)); + return fetch_result; + }); } - if(associated_data.search_running && associated_data.search_future.valid() && associated_data.search_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(associated_data.fetch_status == FetchStatus::LOADING && associated_data.fetch_type == FetchType::SEARCH && is_future_ready(associated_data.fetch_future)) { if(!associated_data.search_text_updated) { - BodyItems result_items = associated_data.search_future.get(); - tabs[i].body->items = std::move(result_items); + FetchResult fetch_result = associated_data.fetch_future.get(); + tabs[i].body->items = std::move(fetch_result.body_items); tabs[i].body->select_first_item(); associated_data.fetched_page = 0; - if(tabs[i].body->items.empty()) + if(fetch_result.result != PluginResult::OK) + associated_data.search_result_text.setString("Search failed!"); + else if(tabs[i].body->items.empty()) associated_data.search_result_text.setString("No results found"); else associated_data.search_result_text.setString(""); } else { - associated_data.search_future.get(); + associated_data.fetch_future.get(); } - associated_data.search_running = false; + associated_data.fetch_status = FetchStatus::NONE; } - } - - // 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) { - // if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->set_autocomplete_text(autocomplete_future.get()); - // autocomplete_running = false; - // } + if(associated_data.fetch_status == FetchStatus::LOADING && associated_data.fetch_type == FetchType::LAZY && is_future_ready(associated_data.fetch_future)) { + associated_data.lazy_fetch_finished = true; + FetchResult fetch_result = associated_data.fetch_future.get(); + tabs[i].body->items = std::move(fetch_result.body_items); + if(fetch_result.result != PluginResult::OK) + associated_data.search_result_text.setString("Failed to fetch page!"); + else if(tabs[i].body->items.empty()) + associated_data.search_result_text.setString("No results found"); + else + associated_data.search_result_text.setString(""); + associated_data.fetch_status = FetchStatus::NONE; + } + } window.clear(back_color); - if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->draw(window, false); + if(current_tab.search_bar) current_tab.search_bar->draw(window, false); { float shade_extra_height = 0.0f; - if(!tabs[selected_tab].search_bar) + if(!current_tab.search_bar) shade_extra_height = 10.0f; const float width_per_tab = window_size.x / tabs.size(); tab_background.setSize(sf::Vector2f(std::floor(width_per_tab - tab_margin_x * 2.0f), tab_height)); - float tab_vertical_offset = tabs[selected_tab].search_bar ? tabs[selected_tab].search_bar->getBottomWithoutShadow() : 0.0f; - tabs[selected_tab].body->draw(window, body_pos, body_size, *json_chapters); + float tab_vertical_offset = current_tab.search_bar ? current_tab.search_bar->getBottomWithoutShadow() : 0.0f; + current_tab.body->draw(window, body_pos, body_size, *json_chapters); const float tab_y = tab_spacer_height + std::floor(tab_vertical_offset + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f) + shade_extra_height; tab_shade.setPosition(0.0f, tab_spacer_height + std::floor(tab_vertical_offset)); @@ -1299,7 +1329,7 @@ namespace QuickMedia { } } - if(tab_associated_data[selected_tab].fetching_next_page_running) { + if(current_tab_associated_data.fetching_next_page_running) { double progress = 0.5 + std::sin(std::fmod(gradient_inc, 360.0) * 0.017453292519943295 - 1.5707963267948966*0.5) * 0.5; gradient_inc += (frame_time_ms * 0.5); sf::Color bottom_color = interpolate_colors(back_color, sf::Color(175, 180, 188), progress); @@ -1311,12 +1341,12 @@ namespace QuickMedia { window.draw(gradient_points, 4, sf::Quads); // Note: sf::Quads doesn't work with egl } - if(!tab_associated_data[selected_tab].search_result_text.getString().isEmpty()) { - auto search_result_text_bounds = tab_associated_data[selected_tab].search_result_text.getLocalBounds(); - tab_associated_data[selected_tab].search_result_text.setPosition( + if(!current_tab_associated_data.search_result_text.getString().isEmpty()) { + auto search_result_text_bounds = current_tab_associated_data.search_result_text.getLocalBounds(); + current_tab_associated_data.search_result_text.setPosition( std::floor(body_pos.x + body_size.x * 0.5f - search_result_text_bounds.width * 0.5f), std::floor(body_pos.y + body_size.y * 0.5f - search_result_text_bounds.height * 0.5f)); - window.draw(tab_associated_data[selected_tab].search_result_text); + window.draw(current_tab_associated_data.search_result_text); } window.display(); @@ -1328,8 +1358,8 @@ namespace QuickMedia { for(TabAssociatedData &associated_data : tab_associated_data) { if(associated_data.next_page_future.valid()) associated_data.next_page_future.get(); - if(associated_data.search_future.valid()) - associated_data.search_future.get(); + if(associated_data.fetch_future.valid()) + associated_data.fetch_future.get(); } } @@ -1491,7 +1521,6 @@ namespace QuickMedia { const float related_videos_text_height = related_videos_text.getCharacterSize(); auto related_media_body = create_body(); - related_media_body->draw_thumbnails = true; sf::WindowHandle video_player_window = None; auto on_window_create = [this, &video_player_window](sf::WindowHandle _video_player_window) mutable { @@ -2440,12 +2469,14 @@ namespace QuickMedia { comment_input_shade.setFillColor(sf::Color(33, 38, 44)); sf::Sprite logo_sprite(plugin_logo); + logo_sprite.setScale(0.8f, 0.8f); + sf::Vector2f logo_size(plugin_logo.getSize().x * logo_sprite.getScale().x, plugin_logo.getSize().y * logo_sprite.getScale().y); float prev_chat_height = comment_input.get_height(); float chat_input_height_full = 0.0f; const float logo_padding_x = 15.0f; - const float chat_input_padding_x = 15.0f; - const float chat_input_padding_y = 15.0f; + const float chat_input_padding_x = 10.0f; + const float chat_input_padding_y = 10.0f; sf::Vector2f body_pos; sf::Vector2f body_size; @@ -2641,8 +2672,8 @@ namespace QuickMedia { redraw = false; comment_input.set_max_width(window_size.x); - comment_input.set_max_width(window_size.x - (logo_padding_x + plugin_logo.getSize().x + chat_input_padding_x * 2.0f)); - comment_input.set_position(sf::Vector2f(logo_padding_x + plugin_logo.getSize().x + chat_input_padding_x, chat_input_padding_y)); + comment_input.set_max_width(window_size.x - (logo_padding_x + logo_size.x + chat_input_padding_x + logo_padding_x)); + comment_input.set_position(sf::Vector2f(std::floor(logo_padding_x + logo_size.x + chat_input_padding_x), chat_input_padding_y)); float body_padding_horizontal = 25.0f; float body_padding_vertical = 5.0f; @@ -2658,7 +2689,7 @@ namespace QuickMedia { body_pos = sf::Vector2f(body_padding_horizontal, comment_input_shade.getSize().y + body_padding_vertical); body_size = sf::Vector2f(body_width, window_size.y - comment_input_shade.getSize().y - body_padding_vertical); - logo_sprite.setPosition(logo_padding_x, comment_input_shade.getSize().y * 0.5f - plugin_logo.getSize().y * 0.5f); + logo_sprite.setPosition(logo_padding_x, std::floor(comment_input_shade.getSize().y * 0.5f - logo_size.y * 0.5f)); } //comment_input.update(); @@ -2702,7 +2733,7 @@ namespace QuickMedia { } else if(navigation_stage == NavigationStage::VIEWING_ATTACHED_IMAGE) { // TODO: Use image instead of data with string. texture->loadFromMemory creates a temporary image anyways that parses the string. std::string image_data; - if(downloading_image && load_image_future.valid() && load_image_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(downloading_image && is_future_ready(load_image_future)) { downloading_image = false; image_data = load_image_future.get(); @@ -2932,16 +2963,9 @@ namespace QuickMedia { return result_items; } - enum class PinnedEventStatus { - NONE, - LOADING, - FINISHED_LOADING, - FAILED_TO_LOAD - }; - struct PinnedEventData { std::string event_id; - PinnedEventStatus status = PinnedEventStatus::NONE; + FetchStatus status = FetchStatus::NONE; }; void Program::chat_page() { @@ -2953,7 +2977,6 @@ namespace QuickMedia { ChatTab pinned_tab; pinned_tab.body = std::make_unique<Body>(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); - pinned_tab.body->draw_thumbnails = true; pinned_tab.body->thumbnail_max_size = CHAT_MESSAGE_THUMBNAIL_MAX_SIZE; pinned_tab.body->thumbnail_mask_shader = &circle_mask_shader; //pinned_tab.body->line_separator_color = sf::Color::Transparent; @@ -2962,7 +2985,6 @@ namespace QuickMedia { ChatTab messages_tab; messages_tab.body = std::make_unique<Body>(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); - messages_tab.body->draw_thumbnails = true; messages_tab.body->thumbnail_max_size = CHAT_MESSAGE_THUMBNAIL_MAX_SIZE; messages_tab.body->thumbnail_mask_shader = &circle_mask_shader; //messages_tab.body->line_separator_color = sf::Color::Transparent; @@ -2971,7 +2993,6 @@ namespace QuickMedia { ChatTab rooms_tab; rooms_tab.body = std::make_unique<Body>(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); - rooms_tab.body->draw_thumbnails = true; //rooms_tab.body->line_separator_color = sf::Color::Transparent; rooms_tab.body->thumbnail_mask_shader = &circle_mask_shader; rooms_tab.text = sf::Text("Rooms", *font, tab_text_size); @@ -3106,6 +3127,8 @@ namespace QuickMedia { sf::Text replying_to_text("Replying to:", *font, 18); sf::Sprite logo_sprite(plugin_logo); + logo_sprite.setScale(0.8f, 0.8f); + sf::Vector2f logo_size(plugin_logo.getSize().x * logo_sprite.getScale().x, plugin_logo.getSize().y * logo_sprite.getScale().y); sf::Text room_name_text("", *bold_font, 18); const float room_name_text_height = 20.0f; @@ -3141,7 +3164,7 @@ namespace QuickMedia { auto set_body_as_deleted = [](Message *message, BodyItem *body_item) { body_item->embedded_item = nullptr; - body_item->embedded_item_status = EmbeddedItemStatus::NONE; + body_item->embedded_item_status = FetchStatus::NONE; body_item->thumbnail_url = message->user->avatar_url; body_item->thumbnail_mask_type = ThumbnailMaskType::CIRCLE; body_item->set_description_color(sf::Color::White); @@ -3196,7 +3219,7 @@ namespace QuickMedia { body->set_description("Loading message..."); PinnedEventData *event_data = new PinnedEventData(); event_data->event_id = event; - event_data->status = PinnedEventStatus::NONE; + event_data->status = FetchStatus::NONE; body->userdata = event_data; tabs[PINNED_TAB_INDEX].body->items.push_back(std::move(body)); } @@ -3346,14 +3369,14 @@ namespace QuickMedia { return; PinnedEventData *event_data = static_cast<PinnedEventData*>(body_item->userdata); - if(!event_data || event_data->status != PinnedEventStatus::NONE) + if(!event_data || event_data->status != FetchStatus::NONE) return; // Check if we already have the referenced message as a body item in the messages list, so we dont create a new one auto related_body_item = find_body_item_by_event_id(tabs[MESSAGES_TAB_INDEX].body->items.data(), tabs[MESSAGES_TAB_INDEX].body->items.size(), event_data->event_id); if(related_body_item) { *body_item = *related_body_item; - event_data->status = PinnedEventStatus::FINISHED_LOADING; + event_data->status = FetchStatus::FINISHED_LOADING; body_item->userdata = event_data; return; } @@ -3362,7 +3385,7 @@ namespace QuickMedia { std::string message_event_id = event_data->event_id; fetch_future_room = current_room; fetch_body_item = body_item; - body_item->embedded_item_status = EmbeddedItemStatus::LOADING; + body_item->embedded_item_status = FetchStatus::LOADING; fetch_message_tab = PINNED_TAB_INDEX; // TODO: Check if the message is already cached before calling async? is this needed? is async creation expensive? fetch_message_future = std::async(std::launch::async, [this, &fetch_future_room, message_event_id]() { @@ -3378,14 +3401,14 @@ namespace QuickMedia { Message *message = static_cast<Message*>(body_item->userdata); assert(message); - if(message->related_event_id.empty() || body_item->embedded_item_status != EmbeddedItemStatus::NONE) + if(message->related_event_id.empty() || body_item->embedded_item_status != FetchStatus::NONE) return; // Check if we already have the referenced message as a body item, so we dont create a new one auto related_body_item = find_body_item_by_event_id(tabs[MESSAGES_TAB_INDEX].body->items.data(), tabs[MESSAGES_TAB_INDEX].body->items.size(), message->related_event_id); if(related_body_item) { body_item->embedded_item = related_body_item; - body_item->embedded_item_status = EmbeddedItemStatus::FINISHED_LOADING; + body_item->embedded_item_status = FetchStatus::FINISHED_LOADING; return; } @@ -3393,7 +3416,7 @@ namespace QuickMedia { std::string message_event_id = message->related_event_id; fetch_future_room = current_room; fetch_body_item = body_item; - body_item->embedded_item_status = EmbeddedItemStatus::LOADING; + body_item->embedded_item_status = FetchStatus::LOADING; fetch_message_tab = MESSAGES_TAB_INDEX; // TODO: Check if the message is already cached before calling async? is this needed? is async creation expensive? fetch_message_future = std::async(std::launch::async, [this, &fetch_future_room, message_event_id]() { @@ -3442,8 +3465,8 @@ namespace QuickMedia { float prev_chat_height = chat_input.get_height(); float chat_input_height_full = 0.0f; const float logo_padding_x = 15.0f; - const float chat_input_padding_x = 15.0f; - const float chat_input_padding_y = 15.0f; + const float chat_input_padding_x = 10.0f; + const float chat_input_padding_y = 10.0f; Body url_selection_body(this, font.get(), bold_font.get(), cjk_font.get(), loading_icon); @@ -3902,8 +3925,8 @@ namespace QuickMedia { tab_shade_height = tab_spacer_height + std::floor(tab_vertical_offset) + tab_height + room_name_padding_y + padding_bottom; - chat_input.set_max_width(window_size.x - (logo_padding_x + plugin_logo.getSize().x + chat_input_padding_x * 2.0f)); - chat_input.set_position(sf::Vector2f(logo_padding_x + plugin_logo.getSize().x + chat_input_padding_x, window_size.y - chat_height - chat_input_padding_y)); + chat_input.set_max_width(window_size.x - (logo_padding_x + logo_size.x + chat_input_padding_x + logo_padding_x)); + chat_input.set_position(sf::Vector2f(std::floor(logo_padding_x + logo_size.x + chat_input_padding_x), window_size.y - chat_height - chat_input_padding_y)); float body_padding_horizontal = 25.0f; float body_padding_vertical = 5.0f; @@ -3922,7 +3945,7 @@ namespace QuickMedia { more_messages_below_rect.setSize(sf::Vector2f(window_size.x, gradient_height)); more_messages_below_rect.setPosition(0.0f, std::floor(window_size.y - chat_input_shade.getSize().y - gradient_height)); - logo_sprite.setPosition(logo_padding_x, window_size.y - chat_input_shade.getSize().y * 0.5f - plugin_logo.getSize().y * 0.5f); + logo_sprite.setPosition(logo_padding_x, std::floor(window_size.y - chat_input_shade.getSize().y * 0.5f - logo_size.y * 0.5f)); } room_search_bar.update(); @@ -3945,7 +3968,7 @@ namespace QuickMedia { }); } - if(sync_future.valid() && sync_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(is_future_ready(sync_future)) { SyncFutureResult sync_result = sync_future.get(); add_new_rooms(sync_result.rooms); @@ -3962,13 +3985,13 @@ namespace QuickMedia { synced = true; } - if(set_read_marker_future.valid() && set_read_marker_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(is_future_ready(set_read_marker_future)) { set_read_marker_future.get(); read_marker_timer.restart(); setting_read_marker = false; } - if(fetching_previous_messages_running && previous_messages_future.valid() && previous_messages_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(fetching_previous_messages_running && is_future_ready(previous_messages_future)) { Messages new_messages = previous_messages_future.get(); fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_messages.size()); // Ignore finished fetch of messages if it happened in another room. When we navigate back to the room we will get the messages again @@ -3989,7 +4012,7 @@ namespace QuickMedia { fetching_previous_messages_running = false; } - if(fetching_message_running && fetch_message_future.valid() && fetch_message_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + if(fetching_message_running && is_future_ready(fetch_message_future)) { std::shared_ptr<Message> message = fetch_message_future.get(); fprintf(stderr, "Finished fetching message: %s\n", message ? message->event_id.c_str() : "(null)"); // Ignore finished fetch of messages if it happened in another room. When we navigate back to the room we will get the messages again @@ -3998,18 +4021,18 @@ namespace QuickMedia { PinnedEventData *event_data = static_cast<PinnedEventData*>(fetch_body_item->userdata); if(message) { *fetch_body_item = *message_to_body_item(message.get(), matrix->get_me(current_room).get()); - event_data->status = PinnedEventStatus::FINISHED_LOADING; + event_data->status = FetchStatus::FINISHED_LOADING; fetch_body_item->userdata = event_data; } else { fetch_body_item->set_description("Failed to load message!"); - event_data->status = PinnedEventStatus::FAILED_TO_LOAD; + event_data->status = FetchStatus::FAILED_TO_LOAD; } } else if(fetch_message_tab == MESSAGES_TAB_INDEX) { if(message) { fetch_body_item->embedded_item = message_to_body_item(message.get(), matrix->get_me(current_room).get()); - fetch_body_item->embedded_item_status = EmbeddedItemStatus::FINISHED_LOADING; + fetch_body_item->embedded_item_status = FetchStatus::FINISHED_LOADING; } else { - fetch_body_item->embedded_item_status = EmbeddedItemStatus::FAILED_TO_LOAD; + fetch_body_item->embedded_item_status = FetchStatus::FAILED_TO_LOAD; } } } |