[feat] use receiver ack to calculate file transfer progress

This commit is contained in:
dijunkun
2025-12-25 18:13:18 +08:00
parent b322181853
commit eea37424c9
9 changed files with 227 additions and 127 deletions

View File

@@ -38,8 +38,8 @@ int Render::FileTransferWindow(
ImGuiIO& io = ImGui::GetIO();
// Position window at bottom-left of stream window
float window_width = 400.0f;
float window_height = 120.0f;
float window_width = main_window_width_ * 0.35f;
float window_height = main_window_height_ * 0.25f;
float pos_x = 10.0f;
float pos_y = 10.0f;
@@ -56,20 +56,22 @@ int Render::FileTransferWindow(
ImGui::SetNextWindowSize(ImVec2(window_width, window_height),
ImGuiCond_Always);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 3.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.2f, 0.2f, 0.2f, 0.95f));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_TitleBg, ImVec4(0.3f, 0.3f, 0.3f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 0.8f));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_TitleBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_TitleBgActive, ImVec4(0.4f, 0.4f, 0.4f, 1.0f));
std::string window_title = "File Transfer";
bool window_opened = true;
// ImGui::SetWindowFontScale(0.5f);
if (ImGui::Begin("FileTransferWindow", &window_opened,
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoSavedSettings)) {
ImGui::SetWindowFontScale(0.5f);
ImGui::PopStyleColor(4);
ImGui::PopStyleVar(2);
@@ -81,14 +83,14 @@ int Render::FileTransferWindow(
return 0;
}
ImGui::SetWindowFontScale(0.6f);
bool is_sending = props->file_sending_.load();
uint64_t total = props->file_total_bytes_.load();
bool has_transfer = total > 0; // Check if there's an active transfer
if (props->file_transfer_completed_ && !is_sending) {
// Show completion message
ImGui::SetCursorPos(ImVec2(10, 30));
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f),
ImGui::TextColored(ImVec4(0.0f, 0.0f, 1.0f, 1.0f),
"%s File transfer completed!", ICON_FA_CHECK);
std::string file_name;
@@ -106,13 +108,14 @@ int Render::FileTransferWindow(
props->file_transfer_completed_ = false;
props->file_transfer_window_visible_ = false;
}
} else if (is_sending) {
// Show transfer progress
} else if (has_transfer && !props->file_transfer_completed_) {
// Show transfer progress (either sending or waiting for ACK)
uint64_t sent = props->file_sent_bytes_.load();
uint64_t total = props->file_total_bytes_.load();
float progress =
total > 0 ? static_cast<float>(sent) / static_cast<float>(total)
: 0.0f;
// Re-read total in case it was updated
uint64_t current_total = props->file_total_bytes_.load();
float progress = current_total > 0 ? static_cast<float>(sent) /
static_cast<float>(current_total)
: 0.0f;
progress = (std::max)(0.0f, (std::min)(1.0f, progress));
// File name
@@ -144,24 +147,25 @@ int Render::FileTransferWindow(
ImGui::SameLine(0, 20);
// Format size display
char size_str[64];
if (total < 1024) {
if (current_total < 1024) {
snprintf(size_str, sizeof(size_str), "%llu B",
(unsigned long long)total);
} else if (total < 1024 * 1024) {
snprintf(size_str, sizeof(size_str), "%.2f KB", total / 1024.0f);
(unsigned long long)current_total);
} else if (current_total < 1024 * 1024) {
snprintf(size_str, sizeof(size_str), "%.2f KB",
current_total / 1024.0f);
} else {
snprintf(size_str, sizeof(size_str), "%.2f MB",
total / (1024.0f * 1024.0f));
current_total / (1024.0f * 1024.0f));
}
ImGui::Text("Size: %s", size_str);
}
ImGui::SetWindowFontScale(1.0f);
ImGui::End();
} else {
ImGui::PopStyleColor(4);
ImGui::PopStyleVar(2);
}
ImGui::SetWindowFontScale(1.0f);
return 0;
}