|
| 1 | +#include <fmt/format.h> |
| 2 | +#include <imgui.h> |
| 3 | +#include <facade/engine/editor/browse_file.hpp> |
| 4 | +#include <facade/util/env.hpp> |
| 5 | +#include <filesystem> |
| 6 | + |
| 7 | +namespace facade::editor { |
| 8 | +namespace fs = std::filesystem; |
| 9 | + |
| 10 | +namespace { |
| 11 | +constexpr bool drop_leading_dir(std::string_view& out) { |
| 12 | + if (!out.empty() && out[0] == '/') { out = out.substr(1); } |
| 13 | + if (auto const i = out.find_first_of('/'); i != std::string_view::npos) { |
| 14 | + out = out.substr(i + 1); |
| 15 | + return true; |
| 16 | + } |
| 17 | + return false; |
| 18 | +} |
| 19 | + |
| 20 | +std::string truncate(std::string_view in, std::size_t limit) { |
| 21 | + if (in.size() <= limit) { return std::string{in}; } |
| 22 | + while (in.size() + 4 > limit && drop_leading_dir(in)) |
| 23 | + ; |
| 24 | + return fmt::format(".../{}", in); |
| 25 | +} |
| 26 | +} // namespace |
| 27 | + |
| 28 | +std::string BrowseFile::operator()(NotClosed<Popup> popup, std::string& out_path) { |
| 29 | + auto const fs_path = [&] { |
| 30 | + auto ret = fs::absolute(out_path); |
| 31 | + if (!fs::is_directory(ret)) { |
| 32 | + ret = fs::current_path(); |
| 33 | + out_path = ret.generic_string(); |
| 34 | + } |
| 35 | + return ret; |
| 36 | + }(); |
| 37 | + auto label = out_path; |
| 38 | + auto const text_size = ImGui::CalcTextSize(label.c_str()); |
| 39 | + ImGui::PushItemWidth(-50.0f); |
| 40 | + if (auto const ratio = (ImGui::GetWindowWidth() - 100.0f) / text_size.x; ratio > 0.0f) { |
| 41 | + auto const limit = static_cast<std::size_t>(ratio * static_cast<float>(label.size())); |
| 42 | + label = truncate(label, limit); |
| 43 | + } |
| 44 | + if (ImGui::BeginCombo("Path", label.c_str())) { |
| 45 | + auto item_width = 0.0f; |
| 46 | + for (auto p = fs_path; !p.empty(); p = p.parent_path()) { |
| 47 | + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + item_width); |
| 48 | + item_width += parent_indent; |
| 49 | + auto name = p.filename().generic_string(); |
| 50 | + name += "/"; |
| 51 | + if (ImGui::Selectable(name.c_str(), false)) { |
| 52 | + out_path = p.generic_string(); |
| 53 | + break; |
| 54 | + } |
| 55 | + if (p.parent_path() == p) { break; } |
| 56 | + } |
| 57 | + ImGui::EndCombo(); |
| 58 | + } |
| 59 | + if (fs_path.has_parent_path() && ImGui::Button("Up")) { out_path = fs_path.parent_path().generic_string(); } |
| 60 | + if (auto documents = env::documents_path(); !documents.empty()) { |
| 61 | + ImGui::SameLine(); |
| 62 | + if (ImGui::Button("Documents")) { out_path = std::move(documents); } |
| 63 | + } |
| 64 | + if (auto downloads = env::downloads_path(); !downloads.empty()) { |
| 65 | + ImGui::SameLine(); |
| 66 | + if (ImGui::Button("Downloads")) { out_path = std::move(downloads); } |
| 67 | + } |
| 68 | + |
| 69 | + ImGui::Separator(); |
| 70 | + if (auto window = editor::Window{popup, "File Tree"}) { |
| 71 | + env::GetDirEntries{.extensions = extensions}(out_entries, out_path.c_str()); |
| 72 | + if (fs_path.has_parent_path() && ImGui::Selectable("..", false, ImGuiSelectableFlags_DontClosePopups)) { |
| 73 | + out_path = fs_path.parent_path().generic_string(); |
| 74 | + } |
| 75 | + for (auto const& dir : out_entries.dirs) { |
| 76 | + auto filename = env::to_filename(dir); |
| 77 | + filename += "/"; |
| 78 | + if (ImGui::Selectable(filename.c_str(), false, ImGuiSelectableFlags_DontClosePopups)) { out_path = dir; } |
| 79 | + } |
| 80 | + for (auto const& file : out_entries.files) { |
| 81 | + if (ImGui::Selectable(env::to_filename(file).c_str(), false, ImGuiSelectableFlags_DontClosePopups)) { return file; } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return {}; |
| 86 | +} |
| 87 | +} // namespace facade::editor |
0 commit comments