[refactor] update recent connections panel rendering for high-DPI scaling support

This commit is contained in:
dijunkun
2025-12-04 00:14:09 +08:00
parent 49ed0200e7
commit 1e48b645ca
3 changed files with 91 additions and 87 deletions

View File

@@ -1,3 +1,4 @@
#include "layout_relative.h"
#include "localization.h" #include "localization.h"
#include "rd_log.h" #include "rd_log.h"
#include "render.h" #include "render.h"
@@ -5,25 +6,25 @@
namespace crossdesk { namespace crossdesk {
int Render::RecentConnectionsWindow() { int Render::RecentConnectionsWindow() {
ImGui::SetNextWindowPos( ImGuiIO& io = ImGui::GetIO();
ImVec2(0, title_bar_height_ + local_window_height_ - 1.0f), float recent_connection_window_width = io.DisplaySize.x;
float recent_connection_window_height =
io.DisplaySize.y * (0.46f - STATUS_BAR_HEIGHT);
ImGui::SetNextWindowPos(ImVec2(0, io.DisplaySize.y * 0.55f),
ImGuiCond_Always); ImGuiCond_Always);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::BeginChild( ImGui::BeginChild(
"RecentConnectionsWindow", "RecentConnectionsWindow",
ImVec2(main_window_width_, main_window_height_ - title_bar_height_ - ImVec2(recent_connection_window_width, recent_connection_window_height),
local_window_height_ - status_bar_height_ +
1.0f),
ImGuiChildFlags_Border, ImGuiChildFlags_Border,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoBringToFrontOnFocus); ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGui::PopStyleVar(); ImGui::PopStyleVar();
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + main_window_text_y_padding_); ImGui::SetCursorPos(
ImGui::Indent(main_child_window_x_padding_); ImVec2(io.DisplaySize.x * 0.045f, io.DisplaySize.y * 0.02f));
ImGui::TextColored( ImGui::TextColored(
ImVec4(0.0f, 0.0f, 0.0f, 0.5f), "%s", ImVec4(0.0f, 0.0f, 0.0f, 0.5f), "%s",
@@ -37,35 +38,40 @@ int Render::RecentConnectionsWindow() {
} }
int Render::ShowRecentConnections() { int Render::ShowRecentConnections() {
float recent_connection_window_padding = 25.0f * dpi_scale_; ImGuiIO& io = ImGui::GetIO();
float recent_connection_window_width = float recent_connection_panel_width = io.DisplaySize.x * 0.912f;
main_window_width_ - 2 * recent_connection_window_padding; float recent_connection_panel_height = io.DisplaySize.y * 0.29f;
ImGui::SetCursorPosX(recent_connection_window_padding); float recent_connection_image_height = recent_connection_panel_height * 0.6f;
ImVec2 sub_window_pos = ImGui::GetCursorPos(); float recent_connection_image_width = recent_connection_image_height * 16 / 9;
std::map<std::string, ImVec2> sub_containers_pos;
float recent_connection_sub_container_width = float recent_connection_sub_container_width =
recent_connection_image_width_ + 16.0f * dpi_scale_; recent_connection_image_width * 1.2f;
float recent_connection_sub_container_height = float recent_connection_sub_container_height =
recent_connection_image_height_ + 36.0f * dpi_scale_; recent_connection_image_height * 1.4f;
float recent_connection_button_width = recent_connection_image_width * 0.15f;
float recent_connection_button_height =
recent_connection_image_height * 0.25f;
float recent_connection_dummy_button_width =
recent_connection_image_width - 2 * recent_connection_button_width;
ImGui::SetCursorPos(
ImVec2(io.DisplaySize.x * 0.045f, io.DisplaySize.y * 0.1f));
std::map<std::string, ImVec2> sub_containers_pos;
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::PushStyleColor(ImGuiCol_ChildBg,
ImVec4(239.0f / 255, 240.0f / 255, 242.0f / 255, 1.0f)); ImVec4(239.0f / 255, 240.0f / 255, 242.0f / 255, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f); ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f);
ImGui::BeginChild("RecentConnectionsContainer", ImGui::BeginChild(
ImVec2(recent_connection_window_width, "RecentConnectionsContainer",
recent_connection_sub_container_height * 1.1f), ImVec2(recent_connection_panel_width, recent_connection_panel_height),
ImGuiChildFlags_Border, ImGuiChildFlags_Border,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse); ImGuiWindowFlags_NoScrollWithMouse);
ImGui::PopStyleVar(); ImGui::PopStyleVar();
ImGui::PopStyleColor(); ImGui::PopStyleColor();
size_t recent_connections_count = recent_connections_.size(); size_t recent_connections_count = recent_connections_.size();
int count = 0; int count = 0;
float button_width = 22 * dpi_scale_;
float button_height = 22 * dpi_scale_;
for (auto& it : recent_connections_) { for (auto& it : recent_connections_) {
sub_containers_pos[it.first] = ImGui::GetCursorPos(); sub_containers_pos[it.first] = ImGui::GetCursorPos();
std::string recent_connection_sub_window_name = std::string recent_connection_sub_window_name =
@@ -75,11 +81,8 @@ int Render::ShowRecentConnections() {
ImVec2(recent_connection_sub_container_width, ImVec2(recent_connection_sub_container_width,
recent_connection_sub_container_height), recent_connection_sub_container_height),
ImGuiChildFlags_None, ImGuiChildFlags_None,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoScrollbar);
std::string connection_info = it.first; std::string connection_info = it.first;
// remote id length is 9 // remote id length is 9
@@ -118,15 +121,16 @@ int Render::ShowRecentConnections() {
it.second.remote_host_name = "unknown"; it.second.remote_host_name = "unknown";
} }
ImVec2 image_screen_pos = ImVec2 image_screen_pos = ImVec2(
ImVec2(ImGui::GetCursorScreenPos().x + 5.0f * dpi_scale_, ImGui::GetCursorScreenPos().x + recent_connection_image_width * 0.04f,
ImGui::GetCursorScreenPos().y + 5.0f * dpi_scale_); ImGui::GetCursorScreenPos().y + recent_connection_image_height * 0.08f);
ImVec2 image_pos = ImVec2(ImGui::GetCursorPosX() + 5.0f * dpi_scale_, ImVec2 image_pos =
ImGui::GetCursorPosY() + 5.0f * dpi_scale_); ImVec2(ImGui::GetCursorPosX() + recent_connection_image_width * 0.05f,
ImGui::GetCursorPosY() + recent_connection_image_height * 0.08f);
ImGui::SetCursorPos(image_pos); ImGui::SetCursorPos(image_pos);
ImGui::Image((ImTextureID)(intptr_t)it.second.texture, ImGui::Image(
ImVec2((float)recent_connection_image_width_, (ImTextureID)(intptr_t)it.second.texture,
(float)recent_connection_image_height_)); ImVec2(recent_connection_image_width, recent_connection_image_height));
// remote id display button // remote id display button
{ {
@@ -135,16 +139,17 @@ int Render::ShowRecentConnections() {
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f));
ImVec2 dummy_button_pos = ImVec2 dummy_button_pos =
ImVec2(image_pos.x, image_pos.y + recent_connection_image_height_); ImVec2(image_pos.x, image_pos.y + recent_connection_image_height);
std::string dummy_button_name = "##DummyButton" + it.second.remote_id; std::string dummy_button_name = "##DummyButton" + it.second.remote_id;
ImGui::SetCursorPos(dummy_button_pos); ImGui::SetCursorPos(dummy_button_pos);
ImGui::SetWindowFontScale(0.6f); ImGui::SetWindowFontScale(0.6f);
ImGui::Button(dummy_button_name.c_str(), ImGui::Button(dummy_button_name.c_str(),
ImVec2(recent_connection_image_width_ - 2 * button_width, ImVec2(recent_connection_dummy_button_width,
button_height)); recent_connection_button_height));
ImGui::SetWindowFontScale(1.0f); ImGui::SetWindowFontScale(1.0f);
ImGui::SetCursorPos( ImGui::SetCursorPos(ImVec2(
ImVec2(dummy_button_pos.x + 2.0f, dummy_button_pos.y + 1.0f)); dummy_button_pos.x + recent_connection_dummy_button_width * 0.05f,
dummy_button_pos.y + recent_connection_button_height * 0.05f));
ImGui::SetWindowFontScale(0.65f); ImGui::SetWindowFontScale(0.65f);
ImGui::Text("%s", it.second.remote_id.c_str()); ImGui::Text("%s", it.second.remote_id.c_str());
ImGui::SetWindowFontScale(1.0f); ImGui::SetWindowFontScale(1.0f);
@@ -167,16 +172,18 @@ int Render::ShowRecentConnections() {
ImGui::SetWindowFontScale(0.5f); ImGui::SetWindowFontScale(0.5f);
// trash button // trash button
{ {
ImVec2 trash_can_button_pos = ImVec2( ImVec2 trash_can_button_pos =
image_pos.x + recent_connection_image_width_ - 2 * button_width, ImVec2(image_pos.x + recent_connection_image_width -
image_pos.y + recent_connection_image_height_); 2 * recent_connection_button_width,
image_pos.y + recent_connection_image_height);
ImGui::SetCursorPos(trash_can_button_pos); ImGui::SetCursorPos(trash_can_button_pos);
std::string trash_can = ICON_FA_TRASH_CAN; std::string trash_can = ICON_FA_TRASH_CAN;
std::string recent_connection_delete_button_name = std::string recent_connection_delete_button_name =
trash_can + "##RecentConnectionDelete" + trash_can + "##RecentConnectionDelete" +
std::to_string(trash_can_button_pos.x); std::to_string(trash_can_button_pos.x);
if (ImGui::Button(recent_connection_delete_button_name.c_str(), if (ImGui::Button(recent_connection_delete_button_name.c_str(),
ImVec2(button_width, button_height))) { ImVec2(recent_connection_button_width,
recent_connection_button_height))) {
show_confirm_delete_connection_ = true; show_confirm_delete_connection_ = true;
delete_connection_name_ = it.first; delete_connection_name_ = it.first;
} }
@@ -192,14 +199,16 @@ int Render::ShowRecentConnections() {
// connect button // connect button
{ {
ImVec2 connect_button_pos = ImVec2 connect_button_pos =
ImVec2(image_pos.x + recent_connection_image_width_ - button_width, ImVec2(image_pos.x + recent_connection_image_width -
image_pos.y + recent_connection_image_height_); recent_connection_button_width,
image_pos.y + recent_connection_image_height);
ImGui::SetCursorPos(connect_button_pos); ImGui::SetCursorPos(connect_button_pos);
std::string connect = ICON_FA_ARROW_RIGHT_LONG; std::string connect = ICON_FA_ARROW_RIGHT_LONG;
std::string connect_to_this_connection_button_name = std::string connect_to_this_connection_button_name =
connect + "##ConnectionTo" + it.first; connect + "##ConnectionTo" + it.first;
if (ImGui::Button(connect_to_this_connection_button_name.c_str(), if (ImGui::Button(connect_to_this_connection_button_name.c_str(),
ImVec2(button_width, button_height))) { ImVec2(recent_connection_button_width,
recent_connection_button_height))) {
ConnectTo(it.second.remote_id, it.second.password.c_str(), ConnectTo(it.second.remote_id, it.second.password.c_str(),
it.second.remember_password); it.second.remember_password);
} }
@@ -211,20 +220,20 @@ int Render::ShowRecentConnections() {
if (count != recent_connections_count - 1) { if (count != recent_connections_count - 1) {
ImVec2 line_start = ImVec2 line_start =
ImVec2(image_screen_pos.x + recent_connection_image_width_ + ImVec2(image_screen_pos.x + recent_connection_image_width * 1.19f,
20.0f * dpi_scale_,
image_screen_pos.y); image_screen_pos.y);
ImVec2 line_end = ImVec2( ImVec2 line_end =
image_screen_pos.x + recent_connection_image_width_ + ImVec2(image_screen_pos.x + recent_connection_image_width * 1.19f,
20.0f * dpi_scale_, image_screen_pos.y + recent_connection_image_height +
image_screen_pos.y + recent_connection_image_height_ + button_height); recent_connection_button_height);
ImGui::GetWindowDrawList()->AddLine(line_start, line_end, ImGui::GetWindowDrawList()->AddLine(line_start, line_end,
IM_COL32(0, 0, 0, 122), 1.0f); IM_COL32(0, 0, 0, 122), 1.0f);
} }
count++; count++;
ImGui::SameLine( ImGui::SameLine(0, count != recent_connections_count
0, count != recent_connections_count ? (25.0f * dpi_scale_) : 0.0f); ? (recent_connection_image_width * 0.165f)
: 0.0f);
} }
ImGui::EndChild(); ImGui::EndChild();
@@ -237,32 +246,30 @@ int Render::ShowRecentConnections() {
} }
int Render::ConfirmDeleteConnection() { int Render::ConfirmDeleteConnection() {
const ImGuiViewport* viewport = ImGui::GetMainViewport(); ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowPos(ImVec2((viewport->WorkSize.x - viewport->WorkPos.x - ImGui::SetNextWindowPos(
connection_status_window_width_) / ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
2, ImGui::SetNextWindowSize(
(viewport->WorkSize.y - viewport->WorkPos.y - ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
connection_status_window_height_) /
2));
ImGui::SetNextWindowSize(ImVec2(connection_status_window_width_,
connection_status_window_height_));
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0)); ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
ImGui::Begin("ConfirmDeleteConnectionWindow", nullptr, ImGui::Begin("ConfirmDeleteConnectionWindow", nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoSavedSettings); ImGuiWindowFlags_NoSavedSettings);
ImGui::PopStyleVar(2); ImGui::PopStyleVar(2);
ImGui::PopStyleColor(); ImGui::PopStyleColor();
auto connection_status_window_width = ImGui::GetWindowSize().x;
auto connection_status_window_height = ImGui::GetWindowSize().y;
std::string text = std::string text =
localization::confirm_delete_connection[localization_language_index_]; localization::confirm_delete_connection[localization_language_index_];
ImGui::SetCursorPosX(connection_status_window_width_ * 6 / 19); ImGui::SetCursorPosX(connection_status_window_width * 0.33f);
ImGui::SetCursorPosY(connection_status_window_height_ * 2 / 3); ImGui::SetCursorPosY(connection_status_window_height * 0.67f);
// ok // ok
ImGui::SetWindowFontScale(0.5f); ImGui::SetWindowFontScale(0.5f);
@@ -281,12 +288,9 @@ int Render::ConfirmDeleteConnection() {
show_confirm_delete_connection_ = false; show_confirm_delete_connection_ = false;
} }
auto window_width = ImGui::GetWindowSize().x;
auto window_height = ImGui::GetWindowSize().y;
auto text_width = ImGui::CalcTextSize(text.c_str()).x; auto text_width = ImGui::CalcTextSize(text.c_str()).x;
ImGui::SetCursorPosX((window_width - text_width) * 0.5f); ImGui::SetCursorPosX((connection_status_window_width - text_width) * 0.5f);
ImGui::SetCursorPosY(window_height * 0.2f); ImGui::SetCursorPosY(connection_status_window_height * 0.2f);
ImGui::Text("%s", text.c_str()); ImGui::Text("%s", text.c_str());
ImGui::SetWindowFontScale(1.0f); ImGui::SetWindowFontScale(1.0f);

View File

@@ -10,7 +10,7 @@ namespace crossdesk {
int Render::TitleBar(bool main_window) { int Render::TitleBar(bool main_window) {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
float title_bar_width = io.DisplaySize.x; float title_bar_width = io.DisplaySize.x;
float title_bar_height = io.DisplaySize.y * TITLE_BAR_HEIGHT; float title_bar_height = io.DisplaySize.y * (TITLE_BAR_HEIGHT + 0.01f);
float title_bar_button_width = io.DisplaySize.x * TITLE_BAR_BUTTON_WIDTH; float title_bar_button_width = io.DisplaySize.x * TITLE_BAR_BUTTON_WIDTH;
float title_bar_button_height = io.DisplaySize.y * TITLE_BAR_BUTTON_HEIGHT; float title_bar_button_height = io.DisplaySize.y * TITLE_BAR_BUTTON_HEIGHT;
@@ -165,7 +165,7 @@ int Render::TitleBar(bool main_window) {
float minimize_pos_x = title_bar_width - float minimize_pos_x = title_bar_width -
title_bar_button_width * (main_window ? 2 : 3) + title_bar_button_width * (main_window ? 2 : 3) +
title_bar_button_width * 0.33f; title_bar_button_width * 0.33f;
float minimize_pos_y = title_bar_height * 0.5f; float minimize_pos_y = title_bar_button_height * 0.5f;
std::string window_minimize_button = "##minimize"; // ICON_FA_MINUS; std::string window_minimize_button = "##minimize"; // ICON_FA_MINUS;
if (ImGui::Button(window_minimize_button.c_str(), if (ImGui::Button(window_minimize_button.c_str(),
ImVec2(title_bar_button_width, title_bar_button_height))) { ImVec2(title_bar_button_width, title_bar_button_height))) {
@@ -249,7 +249,7 @@ int Render::TitleBar(bool main_window) {
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0, 0, 0.5f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0, 0, 0.5f));
float xmark_pos_x = xmark_button_pos_x + title_bar_button_width * 0.5f; float xmark_pos_x = xmark_button_pos_x + title_bar_button_width * 0.5f;
float xmark_pos_y = title_bar_height * 0.5f; float xmark_pos_y = title_bar_button_height * 0.5f;
float xmark_size = title_bar_button_width * 0.33f; float xmark_size = title_bar_button_width * 0.33f;
std::string close_button = "##xmark"; // ICON_FA_XMARK; std::string close_button = "##xmark"; // ICON_FA_XMARK;
if (ImGui::Button(close_button.c_str(), if (ImGui::Button(close_button.c_str(),

View File

@@ -9,9 +9,9 @@ int Render::MainWindow() {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
float local_remote_window_width = io.DisplaySize.x; float local_remote_window_width = io.DisplaySize.x;
float local_remote_window_height = float local_remote_window_height =
io.DisplaySize.y * (1 - TITLE_BAR_HEIGHT - STATUS_BAR_HEIGHT); io.DisplaySize.y * (0.56f - TITLE_BAR_HEIGHT);
ImGui::SetNextWindowPos(ImVec2(0.0f, io.DisplaySize.y * TITLE_BAR_HEIGHT), ImGui::SetNextWindowPos(ImVec2(0.0f, io.DisplaySize.y * (TITLE_BAR_HEIGHT)),
ImGuiCond_Always); ImGuiCond_Always);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
@@ -34,7 +34,7 @@ int Render::MainWindow() {
RemoteWindow(); RemoteWindow();
ImGui::EndChild(); ImGui::EndChild();
// RecentConnectionsWindow(); RecentConnectionsWindow();
StatusBar(); StatusBar();
if (show_connection_status_window_) { if (show_connection_status_window_) {