From b6671bdbe7547c4a519ca87b2a093951947c5dd0 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Fri, 8 Nov 2024 17:52:19 +0800 Subject: [PATCH] [feat] delete recent connection supported --- src/single_window/main_window.cpp | 57 +++++++++++++++++++++++++------ src/single_window/render.cpp | 1 - src/single_window/render.h | 4 +-- src/single_window/thumbnail.cpp | 24 ++++++++++--- src/single_window/thumbnail.h | 2 ++ 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/single_window/main_window.cpp b/src/single_window/main_window.cpp index 40983b9..02f2ab9 100644 --- a/src/single_window/main_window.cpp +++ b/src/single_window/main_window.cpp @@ -1,3 +1,4 @@ +#include "IconsFontAwesome6.h" #include "localization.h" #include "rd_log.h" #include "render.h" @@ -69,6 +70,13 @@ int Render::MainWindow() { int Render::ShowRecentConnections() { ImGui::SetCursorPosX(25.0f); + ImVec2 sub_window_pos = ImGui::GetCursorPos(); + std::map sub_containers_pos; + int recent_connection_sub_container_width = + recent_connection_image_width_ + 16.0f; + int recent_connection_sub_container_height = + recent_connection_image_height_ + 32.0f; + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); ImGui::BeginChild("RecentConnectionsContainer", ImVec2(main_window_width_default_ - 50.0f, 152.0f), ImGuiChildFlags_Border, @@ -76,39 +84,66 @@ int Render::ShowRecentConnections() { ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_AlwaysHorizontalScrollbar); - + ImGui::PopStyleColor(); int recent_connections_count = recent_connection_textures_.size(); int count = 0; for (auto it = recent_connection_textures_.begin(); it != recent_connection_textures_.end(); ++it) { + sub_containers_pos[it->first] = ImGui::GetCursorPos(); std::string recent_connection_sub_window_name = - "RecentConnectionsSubWindow" + it->first; + "RecentConnectionsSubContainer" + it->first; ImGui::BeginChild(recent_connection_sub_window_name.c_str(), - ImVec2(recent_connection_image_width_ + 16.0f, - recent_connection_image_height_ + 32.0f), + ImVec2(recent_connection_sub_container_width, + recent_connection_sub_container_height), ImGuiChildFlags_Border, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoScrollbar); - ImGui::Image((ImTextureID)(intptr_t)it->second, - ImVec2((float)recent_connection_image_width_, - (float)recent_connection_image_height_)); + size_t pos1 = it->first.find('\\') + 1; + size_t pos2 = it->first.rfind('@'); + std::string host_name = it->first.substr(pos1, pos2 - pos1); ImGui::SetWindowFontScale(0.4f); - ImVec2 window_size = ImGui::GetWindowSize(); - ImVec2 text_size = ImGui::CalcTextSize(it->first.c_str()); + ImVec2 text_size = ImGui::CalcTextSize(host_name.c_str()); ImVec2 pos = ImGui::GetCursorPos(); pos.x = (window_size.x - text_size.x) / 2.0f; ImGui::SetCursorPos(pos); - - ImGui::Text("%s", it->first.c_str()); + ImGui::Text(host_name.c_str()); ImGui::SetWindowFontScale(1.0f); + ImGui::Image((ImTextureID)(intptr_t)it->second, + ImVec2((float)recent_connection_image_width_, + (float)recent_connection_image_height_)); + ImGui::EndChild(); + count++; ImGui::SameLine(0, count != recent_connections_count ? 23.0f : 0.0f); } + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0)); + for (const auto& pos : sub_containers_pos) { + ImVec2 delete_button_pos = + ImVec2(pos.second.x + recent_connection_sub_container_width - 11.0f, + pos.second.y - 7.0f); + ImGui::SetCursorPos(delete_button_pos); + ImGui::SetWindowFontScale(0.5f); + std::string xmake = ICON_FA_CIRCLE_XMARK; + std::string recent_connection_delete_button_name = + xmake + "##RecentConnectionDelete" + + std::to_string(delete_button_pos.x); + if (ImGui::SmallButton(recent_connection_delete_button_name.c_str())) { + if (!thumbnail_.DeleteThumbnail(pos.first)) { + reload_recent_connections_ = true; + } + } + ImGui::SetWindowFontScale(1.0f); + } + ImGui::PopStyleColor(3); + ImGui::EndChild(); return 0; diff --git a/src/single_window/render.cpp b/src/single_window/render.cpp index 6204d17..71a8ebc 100644 --- a/src/single_window/render.cpp +++ b/src/single_window/render.cpp @@ -914,7 +914,6 @@ int Render::Run() { int ret = thumbnail_.LoadThumbnail( main_renderer_, recent_connection_textures_, &recent_connection_image_width_, &recent_connection_image_height_); - LOG_ERROR("load [{} {}]", ret, recent_connection_textures_.size()); if (!ret) { LOG_INFO("Load recent connection thumbnails"); } diff --git a/src/single_window/render.h b/src/single_window/render.h index 9a56356..6463d4a 100644 --- a/src/single_window/render.h +++ b/src/single_window/render.h @@ -226,8 +226,8 @@ class Render { // recent connections std::map recent_connection_textures_; - int recent_connection_image_width_ = 128; - int recent_connection_image_height_ = 72; + int recent_connection_image_width_ = 160; + int recent_connection_image_height_ = 90; uint32_t recent_connection_image_save_time_ = 0; // video window diff --git a/src/single_window/thumbnail.cpp b/src/single_window/thumbnail.cpp index 6f68729..483ea31 100644 --- a/src/single_window/thumbnail.cpp +++ b/src/single_window/thumbnail.cpp @@ -175,6 +175,7 @@ std::vector Thumbnail::FindThumbnailPath( int Thumbnail::LoadThumbnail(SDL_Renderer* renderer, std::map& textures, int* width, int* height) { + textures.clear(); std::vector image_path = FindThumbnailPath(image_path_); @@ -182,14 +183,27 @@ int Thumbnail::LoadThumbnail(SDL_Renderer* renderer, return -1; } else { for (int i = 0; i < image_path.size(); i++) { - size_t pos1 = image_path[i].string().find('\\') + 1; - size_t pos2 = image_path[i].string().rfind('@'); - std::string host_name = image_path[i].string().substr(pos1, pos2 - pos1); - textures[host_name] = nullptr; + // size_t pos1 = image_path[i].string().find('\\') + 1; + // size_t pos2 = image_path[i].string().rfind('@'); + // std::string host_name = image_path[i].string().substr(pos1, pos2 - + // pos1); + std::string image_p = image_path[i].string(); + textures[image_p] = nullptr; LoadTextureFromFile(image_path[i].string().c_str(), renderer, - &(textures[host_name]), width, height); + &(textures[image_p]), width, height); } return 0; } return 0; +} + +int Thumbnail::DeleteThumbnail(const std::string& file_path) { + if (std::filesystem::exists(file_path)) { + std::filesystem::remove(file_path); + LOG_INFO("File [{}] removed successfully", file_path); + return 0; + } else { + LOG_ERROR("File [{}] does not exist", file_path); + return -1; + } } \ No newline at end of file diff --git a/src/single_window/thumbnail.h b/src/single_window/thumbnail.h index 4f6d3dc..48d35b4 100644 --- a/src/single_window/thumbnail.h +++ b/src/single_window/thumbnail.h @@ -26,6 +26,8 @@ class Thumbnail { std::map& textures, int* width, int* height); + int DeleteThumbnail(const std::string& file_path); + private: std::vector FindThumbnailPath( const std::filesystem::path& directory);