From adfab363c150e7ab4b9247a156a99efc08abff20 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Sun, 1 Mar 2026 16:29:11 +0800 Subject: [PATCH] [feat] add online presence check before connecting and show offline warning dialog --- src/gui/assets/localization/localization.h | 2 + src/gui/panels/recent_connections_panel.cpp | 56 +++++++++++++++++++-- src/gui/render.h | 5 +- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/gui/assets/localization/localization.h b/src/gui/assets/localization/localization.h index e105eab..b741429 100644 --- a/src/gui/assets/localization/localization.h +++ b/src/gui/assets/localization/localization.h @@ -220,6 +220,8 @@ static std::vector online = { reinterpret_cast(u8"在线"), "Online"}; static std::vector offline = { reinterpret_cast(u8"离线"), "Offline"}; +static std::vector device_offline = { + reinterpret_cast(u8"设备离线"), "Device Offline"}; #if _WIN32 static std::vector exit_program = {L"退出", L"Exit"}; diff --git a/src/gui/panels/recent_connections_panel.cpp b/src/gui/panels/recent_connections_panel.cpp index 47befa8..156d2b7 100644 --- a/src/gui/panels/recent_connections_panel.cpp +++ b/src/gui/panels/recent_connections_panel.cpp @@ -238,8 +238,14 @@ int Render::ShowRecentConnections() { if (ImGui::Button(connect_to_this_connection_button_name.c_str(), ImVec2(recent_connection_button_width, recent_connection_button_height))) { - ConnectTo(it.second.remote_id, it.second.password.c_str(), - it.second.remember_password); + if (online) { + ConnectTo(it.second.remote_id, it.second.password.c_str(), + it.second.remember_password); + } else { + show_offline_warning_window_ = true; + offline_warning_text_ = + localization::device_offline[localization_language_index_]; + } } } ImGui::SetWindowFontScale(1.0f); @@ -270,6 +276,9 @@ int Render::ShowRecentConnections() { if (show_confirm_delete_connection_) { ConfirmDeleteConnection(); } + if (show_offline_warning_window_) { + OfflineWarningWindow(); + } return 0; } @@ -282,7 +291,7 @@ int Render::ConfirmDeleteConnection() { ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f)); 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.0f, 1.0f, 1.0f, 1.0f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f); @@ -327,4 +336,45 @@ int Render::ConfirmDeleteConnection() { ImGui::PopStyleVar(); return 0; } + +int Render::OfflineWarningWindow() { + ImGuiIO& io = ImGui::GetIO(); + ImGui::SetNextWindowPos( + ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f)); + ImGui::SetNextWindowSize( + ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f)); + + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f); + + ImGui::Begin("OfflineWarningWindow", nullptr, + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoSavedSettings); + ImGui::PopStyleVar(2); + ImGui::PopStyleColor(); + + auto window_width = ImGui::GetWindowSize().x; + auto window_height = ImGui::GetWindowSize().y; + + ImGui::SetCursorPosX(window_width * 0.43f); + ImGui::SetCursorPosY(window_height * 0.67f); + ImGui::SetWindowFontScale(0.5f); + if (ImGui::Button(localization::ok[localization_language_index_].c_str()) || + ImGui::IsKeyPressed(ImGuiKey_Enter) || + ImGui::IsKeyPressed(ImGuiKey_Escape)) { + show_offline_warning_window_ = false; + } + + auto text_width = ImGui::CalcTextSize(offline_warning_text_.c_str()).x; + ImGui::SetCursorPosX((window_width - text_width) * 0.5f); + ImGui::SetCursorPosY(window_height * 0.2f); + ImGui::Text("%s", offline_warning_text_.c_str()); + ImGui::SetWindowFontScale(1.0f); + + ImGui::End(); + ImGui::PopStyleVar(); + return 0; +} } // namespace crossdesk \ No newline at end of file diff --git a/src/gui/render.h b/src/gui/render.h index fab332d..3c1806c 100644 --- a/src/gui/render.h +++ b/src/gui/render.h @@ -257,6 +257,7 @@ class Render { int DrawStreamWindow(); int DrawServerWindow(); int ConfirmDeleteConnection(); + int OfflineWarningWindow(); int NetTrafficStats(std::shared_ptr& props); void DrawConnectionStatusText( std::shared_ptr& props); @@ -577,9 +578,11 @@ class Render { bool is_server_mode_ = false; bool reload_recent_connections_ = true; bool show_confirm_delete_connection_ = false; + bool show_offline_warning_window_ = false; bool delete_connection_ = false; bool is_tab_bar_hovered_ = false; std::string delete_connection_name_ = ""; + std::string offline_warning_text_ = ""; bool re_enter_remote_id_ = false; double copy_start_time_ = 0; SignalStatus signal_status_ = SignalStatus::SignalClosed; @@ -685,4 +688,4 @@ class Render { FileTransferState file_transfer_; }; } // namespace crossdesk -#endif \ No newline at end of file +#endif