diff --git a/src/gui/render.h b/src/gui/render.h index 5d1765e..fd2a2cb 100644 --- a/src/gui/render.h +++ b/src/gui/render.h @@ -458,6 +458,7 @@ class Render { bool self_hosted_server_config_window_pos_reset_ = true; std::string selected_current_file_path_ = ""; std::string selected_file_ = ""; + bool show_file_browser_ = true; /* ------ main window property end ------ */ /* ------ sub stream window property start ------ */ diff --git a/src/gui/windows/server_settings_window.cpp b/src/gui/windows/server_settings_window.cpp index 7f8df2f..c64e3d3 100644 --- a/src/gui/windows/server_settings_window.cpp +++ b/src/gui/windows/server_settings_window.cpp @@ -28,6 +28,11 @@ std::vector GetRootEntries() { int Render::ShowSimpleFileBrowser() { std::string display_text; + + if (selected_current_file_path_.empty()) { + selected_current_file_path_ = std::filesystem::current_path().string(); + } + if (!selected_file_.empty()) { display_text = std::filesystem::path(selected_file_).filename().string(); } else if (selected_current_file_path_ != "Root") { @@ -43,62 +48,70 @@ int Render::ShowSimpleFileBrowser() { localization::select_a_file[localization_language_index_].c_str(); } - // 设置固定宽度 - // ImGui::SetNextItemWidth(SELF_HOSTED_SERVER_INPUT_WINDOW_WIDTH); + if (show_file_browser_) { + ImGui::PushItemFlag(ImGuiItemFlags_AutoClosePopups, false); + + float fixed_width = 130.0f; + ImGui::SetNextItemWidth(fixed_width); + ImGui::SetNextWindowSizeConstraints(ImVec2(fixed_width, 0), + ImVec2(fixed_width, 100.0f)); + + if (ImGui::BeginCombo("##select_a_file", display_text.c_str(), 0)) { + bool file_selected = false; - ImGui::PushItemFlag(ImGuiItemFlags_AutoClosePopups, false); // 禁用自动关闭 - if (ImGui::BeginCombo("##select_a_file", display_text.c_str())) { - bool file_selected = false; - if (selected_current_file_path_ == "Root" || - !std::filesystem::exists(selected_current_file_path_) || - !std::filesystem::is_directory(selected_current_file_path_)) { auto roots = GetRootEntries(); - for (const auto& root : roots) { - if (ImGui::Selectable(root.c_str())) { - selected_current_file_path_ = root; - selected_file_.clear(); - } - } - } else { - std::filesystem::path p(selected_current_file_path_); - - if (ImGui::Selectable("..")) { - if (p.has_parent_path() && p != p.root_path()) - selected_current_file_path_ = p.parent_path().string(); - else - selected_current_file_path_ = "Root"; - selected_file_.clear(); - } - - try { - for (const auto& entry : - std::filesystem::directory_iterator(selected_current_file_path_)) { - std::string name = entry.path().filename().string(); - if (entry.is_directory()) { - if (ImGui::Selectable(name.c_str())) { - selected_current_file_path_ = entry.path().string(); - selected_file_.clear(); - } - } else { - if (ImGui::Selectable(name.c_str())) { - selected_file_ = entry.path().string(); - file_selected = true; // 记录选中文件 - } + if (selected_current_file_path_ == "Root" || + !std::filesystem::exists(selected_current_file_path_) || + !std::filesystem::is_directory(selected_current_file_path_)) { + for (const auto& root : roots) { + if (ImGui::Selectable(root.c_str())) { + selected_current_file_path_ = root; + selected_file_.clear(); } } - } catch (const std::exception& e) { - ImGui::TextColored(ImVec4(1, 0, 0, 1), "Error: %s", e.what()); - } - } + } else { + std::filesystem::path p(selected_current_file_path_); - // 如果选中了文件,则自动关闭下拉框 - if (file_selected) { - ImGui::EndCombo(); // 关闭下拉框 - } else { - ImGui::EndCombo(); // 保持下拉框开启 + if (ImGui::Selectable("..")) { + if (std::find(roots.begin(), roots.end(), + selected_current_file_path_) != roots.end()) { + selected_current_file_path_ = "Root"; + } else if (p.has_parent_path()) { + selected_current_file_path_ = p.parent_path().string(); + } else { + selected_current_file_path_ = "Root"; + } + selected_file_.clear(); + } + + try { + for (const auto& entry : std::filesystem::directory_iterator( + selected_current_file_path_)) { + std::string name = entry.path().filename().string(); + if (entry.is_directory()) { + if (ImGui::Selectable(name.c_str())) { + selected_current_file_path_ = entry.path().string(); + selected_file_.clear(); + } + } else { + if (ImGui::Selectable(name.c_str())) { + selected_file_ = entry.path().string(); + file_selected = true; + show_file_browser_ = false; + } + } + } + } catch (const std::exception& e) { + ImGui::TextColored(ImVec4(1, 0, 0, 1), "Error: %s", e.what()); + } + } + + ImGui::EndCombo(); } + ImGui::PopItemFlag(); + } else { + show_file_browser_ = true; } - ImGui::PopItemFlag(); // 恢复默认行为 return 0; }