[feat] delete recent connection supported

This commit is contained in:
dijunkun
2024-11-08 17:52:19 +08:00
parent edcf5d408c
commit b6671bdbe7
5 changed files with 69 additions and 19 deletions

View File

@@ -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<std::string, ImVec2> 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;

View File

@@ -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");
}

View File

@@ -226,8 +226,8 @@ class Render {
// recent connections
std::map<std::string, SDL_Texture *> 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

View File

@@ -175,6 +175,7 @@ std::vector<std::filesystem::path> Thumbnail::FindThumbnailPath(
int Thumbnail::LoadThumbnail(SDL_Renderer* renderer,
std::map<std::string, SDL_Texture*>& textures,
int* width, int* height) {
textures.clear();
std::vector<std::filesystem::path> 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;
}
}

View File

@@ -26,6 +26,8 @@ class Thumbnail {
std::map<std::string, SDL_Texture*>& textures, int* width,
int* height);
int DeleteThumbnail(const std::string& file_path);
private:
std::vector<std::filesystem::path> FindThumbnailPath(
const std::filesystem::path& directory);